Skip to content

Commit 28fafb1

Browse files
committed
Merge branch 'develop'
2 parents 9f88dd5 + 9cf3e48 commit 28fafb1

29 files changed

+668
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ There are five kinds of components (each components README.md contains module st
3737

3838
### 4. Bundled libraries :
3939
* [Simple table (fj-doc-lib-singletable)](fj-doc-lib-simpletable/README.md) - offers a simple API for creating a document made of a table.
40+
* [Doc type validation (fj-doc-val)](fj-doc-val/README.md) - simple utilities for validating file type.
4041

4142
### 5. Tutorial :
4243
* [Samples and Quickstart (fj-doc-sample)](fj-doc-sample/README.md)

docgen/parameters.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"title" : "Venus (Fugerit Document Generation Framework)",
33
"name": "Venus",
4-
"version" : "0.5.9",
5-
"date" : "11/12/2022",
4+
"version" : "0.5.11",
5+
"date" : "19/12/2022",
66
"organization" : {
77
"name" : "Fugerit Org",
88
"url" : "https://www.fugerit.org"

docgen/release-notes.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
0.5.10 (2022-12-12)
1+
0.5.11 (2022-12-19)
2+
------------------
3+
+ Added doc type validation module (fj-doc-val)
4+
5+
0.5.10 (2022-12-12)
26
------------------
37
+ Added support for maximum table width and doc language in simpletable module
48

fj-doc-val/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Fugerit Document Generation Framework (fj-doc)
2+
3+
## Doc type validation utilities (fj-doc-val)
4+
5+
[back to fj-doc index](../README.md)
6+
7+
*Description* :
8+
This module offer validation utilities to check content of files and streams based on type.
9+
10+
*Status* :
11+
Offer basic support for validating jpg, png, tiff and pdf
12+
13+
*[ChangeLog](ChangeLog.md)*
14+
15+
*Quickstart* :
16+
Created the facade and use it :
17+
18+
```
19+
String extension = ...;
20+
InputStream stream = ...;
21+
// add the validators you eant, included custom validators :
22+
DocValidatorFacade facade = DocValidatorFacade.newFacadeStrict(
23+
PdfboxValidator.DEFAULT,
24+
ImageValidator.JPG_VALIDATOR,
25+
ImageValidator.PNG_VALIDATOR
26+
);
27+
boolean isValidType = facade.checkByExtension(extension, stream);
28+
```

fj-doc-val/pom.xml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<artifactId>fj-doc-val</artifactId>
6+
7+
<parent>
8+
<groupId>org.fugerit.java</groupId>
9+
<artifactId>fj-doc</artifactId>
10+
<version>0.5-SNAPSHOT</version>
11+
</parent>
12+
13+
<name>fj-doc-val</name>
14+
<description>Common freemarker functionalities and Renderer for HTML using Freemarker</description>
15+
16+
<licenses>
17+
<license>
18+
<name>Apache License, Version 2.0</name>
19+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
20+
<distribution>repo</distribution>
21+
</license>
22+
</licenses>
23+
24+
<properties>
25+
<pdfbox-version>2.0.27</pdfbox-version>
26+
</properties>
27+
28+
<build>
29+
30+
</build>
31+
32+
<dependencies>
33+
34+
<dependency>
35+
<groupId>org.fugerit.java</groupId>
36+
<artifactId>fj-core</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.apache.pdfbox</groupId>
41+
<artifactId>pdfbox</artifactId>
42+
<version>${pdfbox-version}</version>
43+
</dependency>
44+
45+
</dependencies>
46+
47+
<organization>
48+
<url>https://www.fugerit.org</url>
49+
<name>Fugerit</name>
50+
</organization>
51+
52+
<url>https://www.fugerit.org/perm/venus/</url>
53+
54+
<profiles>
55+
56+
<profile>
57+
<id>full</id>
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-source-plugin</artifactId>
63+
<executions>
64+
<execution>
65+
<id>attach-sources</id>
66+
<goals>
67+
<goal>jar</goal>
68+
</goals>
69+
</execution>
70+
</executions>
71+
</plugin>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-javadoc-plugin</artifactId>
75+
<configuration>
76+
<stylesheetfile>src/main/javadoc/stylesheet.css</stylesheetfile>
77+
</configuration>
78+
<executions>
79+
<execution>
80+
<id>attach-javadocs</id>
81+
<goals>
82+
<goal>jar</goal>
83+
</goals>
84+
</execution>
85+
</executions>
86+
</plugin>
87+
</plugins>
88+
</build>
89+
</profile>
90+
91+
</profiles>
92+
93+
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.fugerit.java.doc.val.core;
2+
import org.fugerit.java.core.util.result.BasicResult;
3+
4+
public class DocTypeValidationResult extends BasicResult {
5+
6+
public DocTypeValidationResult(int resultCode) {
7+
super(resultCode);
8+
}
9+
10+
public static DocTypeValidationResult newOk() {
11+
return new DocTypeValidationResult( RESULT_CODE_OK );
12+
}
13+
14+
public static DocTypeValidationResult newFail() {
15+
return new DocTypeValidationResult( RESULT_CODE_KO );
16+
}
17+
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.fugerit.java.doc.val.core;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.Set;
6+
7+
public interface DocTypeValidator {
8+
9+
Set<String> getSupportedExtensions();
10+
11+
String getMimeType();
12+
13+
boolean isExtensionSupported( String ext );
14+
15+
boolean isMimeTypeSupported( String mime );
16+
17+
boolean check( InputStream is ) throws IOException;
18+
19+
DocTypeValidationResult validate( InputStream is ) throws IOException;
20+
21+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package org.fugerit.java.doc.val.core;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.stream.Collectors;
12+
13+
import org.fugerit.java.core.cfg.ConfigRuntimeException;
14+
import org.slf4j.Logger;
15+
import org.slf4j.LoggerFactory;
16+
17+
public class DocValidatorFacade {
18+
19+
private static final Logger logger = LoggerFactory.getLogger( DocValidatorFacade.class );
20+
21+
private List<DocTypeValidator> validators;
22+
23+
private Map<String, DocTypeValidator> extMapValidator;
24+
25+
private Map<String, DocTypeValidator> mimMapValidator;
26+
27+
private DocValidatorFacade() {
28+
this.validators = new ArrayList<>();
29+
this.extMapValidator = new HashMap<>();
30+
this.mimMapValidator = new HashMap<>();
31+
}
32+
33+
private boolean addValidator( DocTypeValidator validator ) {
34+
boolean ok = true;
35+
this.validators.add(validator);
36+
DocTypeValidator previous = this.mimMapValidator.put( validator.getMimeType(), validator );
37+
if ( previous != null ) {
38+
ok = false;
39+
logger.warn( "Validator {} has been overridden for mimeType {}", previous, validator.getMimeType() );
40+
}
41+
for ( String ext : validator.getSupportedExtensions() ) {
42+
previous = this.extMapValidator.put(ext, validator);
43+
if ( previous != null ) {
44+
ok = false;
45+
logger.warn( "Validator {} has been overridden for extension {}", previous, ext );
46+
}
47+
}
48+
return ok;
49+
}
50+
51+
public List<DocTypeValidator> findAllByMimeType( final String mimeType ) {
52+
return this.validators.stream().filter( v -> v.isMimeTypeSupported( mimeType ) ).collect( Collectors.toList() );
53+
}
54+
55+
public List<DocTypeValidator> findAllExtension( final String extension ) {
56+
return this.validators.stream().filter( v -> v.isExtensionSupported( extension ) ).collect( Collectors.toList() );
57+
}
58+
59+
public DocTypeValidator findByMimeType( String mimeType ) {
60+
return this.mimMapValidator.get(mimeType);
61+
}
62+
63+
public DocTypeValidator findByExtension( String extension ) {
64+
return this.extMapValidator.get( extension );
65+
}
66+
67+
public boolean isMimeTypeSupprted( String mimeType ) {
68+
DocTypeValidator validator = this.findByMimeType(mimeType);
69+
return validator != null && validator.isMimeTypeSupported(mimeType);
70+
}
71+
72+
public boolean isExtensionSupported( String extension ) {
73+
DocTypeValidator validator = this.findByExtension(extension);
74+
return validator != null && validator.isExtensionSupported(extension);
75+
}
76+
77+
public boolean check( File file ) throws IOException {
78+
return this.validate(file).isResultOk();
79+
}
80+
81+
public DocTypeValidationResult validate( File file ) throws IOException {
82+
DocTypeValidationResult result = DocTypeValidationResult.newFail();
83+
try ( InputStream is = new FileInputStream( file ) ) {
84+
result = this.validate( file.getName(), is );
85+
}
86+
return result;
87+
}
88+
89+
public boolean check( String fileName, InputStream is ) throws IOException {
90+
return this.validate(fileName, is).isResultOk();
91+
}
92+
93+
public DocTypeValidationResult validate( String fileName, InputStream is ) throws IOException {
94+
String extension = fileName.substring( fileName.lastIndexOf( '.' )+1 );
95+
return validateByExtension(extension, is);
96+
}
97+
98+
public boolean checkByExtension( String extension, InputStream is ) throws IOException {
99+
return this.validateByExtension(extension, is).isResultOk();
100+
}
101+
102+
public DocTypeValidationResult validateByExtension( String extension, InputStream is ) throws IOException {
103+
return this.evaluate(this.findByExtension(extension.toUpperCase()) , is);
104+
}
105+
106+
public boolean checkByMimeType( String mimeType, InputStream is ) throws IOException {
107+
return this.validateByMimeType(mimeType, is).isResultOk();
108+
}
109+
110+
public DocTypeValidationResult validateByMimeType( String mimeType, InputStream is ) throws IOException {
111+
return this.evaluate(this.findByMimeType(mimeType) , is);
112+
}
113+
114+
private DocTypeValidationResult evaluate( DocTypeValidator validator, InputStream is ) throws IOException {
115+
DocTypeValidationResult result = DocTypeValidationResult.newFail();
116+
if ( validator != null ) {
117+
result = validator.validate( is );
118+
} else {
119+
logger.info( "no validator found!" );
120+
}
121+
return result;
122+
}
123+
124+
public static DocValidatorFacade newFacade( DocTypeValidator... validators ) {
125+
DocValidatorFacade facade = new DocValidatorFacade();
126+
for ( int k=0; k<validators.length; k++ ) {
127+
DocTypeValidator v = validators[k];
128+
logger.info( "add validator {}", v );
129+
facade.addValidator( v );
130+
}
131+
return facade;
132+
}
133+
134+
public static DocValidatorFacade newFacadeStrict( DocTypeValidator... validators ) {
135+
DocValidatorFacade facade = new DocValidatorFacade();
136+
for ( int k=0; k<validators.length; k++ ) {
137+
DocTypeValidator v = validators[k];
138+
logger.info( "add validator {}", v );
139+
boolean ok = facade.addValidator( v );
140+
if ( !ok ) {
141+
throw new ConfigRuntimeException( "Facade creation error for validator : "+v );
142+
}
143+
}
144+
return facade;
145+
}
146+
147+
}

0 commit comments

Comments
 (0)