Skip to content

Commit cdfe609

Browse files
committed
0.7.2 (2023-01-04)
------------------ + Fixed xml validation in DocXmlParser + Added validation in DocJsonParser and DocYamlParser (though conversion to XML) + The xml parsing is no namespace aware (experimental) + More functionalities in playground
1 parent 7fdb9ed commit cdfe609

File tree

10 files changed

+143
-107
lines changed

10 files changed

+143
-107
lines changed

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.7.1",
5-
"date" : "02/01/2023",
4+
"version" : "0.7.2",
5+
"date" : "04/01/2023",
66
"organization" : {
77
"name" : "Fugerit Org",
88
"url" : "https://www.fugerit.org"

docgen/release-notes.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
0.7.1 (2023-01-02)
1+
0.7.2 (2023-01-04)
2+
------------------
3+
+ Fixed xml validation in DocXmlParser
4+
+ Added validation in DocJsonParser and DocYamlParser (though conversion to XML)
5+
+ The xml parsing is no namespace aware (experimental)
6+
+ More functionalities in playground
7+
8+
0.7.1 (2023-01-02)
29
------------------
310
+ Added conversion utility from json/yaml to xml
411
+ Added xml to [json/yaml conversion conventions](https://github.com/fugerit-org/fj-doc/tree/main/fj-doc-base-json/xml_conversion.md)

fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocObjectMapperHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private void handleElement( JsonNode node, DocParserContext context ) {
9595
}
9696

9797
public DocValidationResult validateWorkerResult(Reader reader) throws Exception {
98-
DocValidationResult result = DocValidationResult.newDefaultNotDefiniedResult();
98+
DocValidationResult result = DocValidationResult.newDefaultNotDefinedResult();
9999
DocJsonToXml convert = new DocJsonToXml( this.mapper );
100100
Element root = convert.convertToElement( reader );
101101
try ( ByteArrayOutputStream buffer = new ByteArrayOutputStream() ) {

fj-doc-base/src/main/java/org/fugerit/java/doc/base/facade/DocFacade.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ The Apache Software Foundation (http://www.apache.org/).
3838
import org.fugerit.java.doc.base.model.DocContainer;
3939
import org.fugerit.java.doc.base.model.DocElement;
4040
import org.fugerit.java.doc.base.model.DocHelper;
41+
import org.fugerit.java.doc.base.parser.DocParser;
4142
import org.fugerit.java.doc.base.parser.DocValidationResult;
4243
import org.fugerit.java.doc.base.xml.DocXmlParser;
4344

4445
/**
46+
* Better to avoid this implementations, which is left only for compatibility.
4547
*
48+
* Should be used instande {@link DocFacadeSource} or a {@link DocParser} instance.
4649
*
4750
* @author mfranci
4851
*
@@ -83,7 +86,7 @@ public static void print( PrintStream s, DocBase doc ) {
8386
public static boolean validate( Reader is, Properties params ) throws Exception {
8487
boolean valRes = false;
8588
try {
86-
DocXmlParser parser = new DocXmlParser( DocHelper.DEFAULT, params );
89+
DocXmlParser parser = new DocXmlParser( DocHelper.DEFAULT );
8790
int result = parser.validate( is );
8891
valRes = ( result == DocValidationResult.VALIDATION_OK );
8992
} catch (Exception e) {
@@ -101,7 +104,7 @@ public static DocBase parse( Reader is, DocHelper docHelper ) throws Exception {
101104
public static DocBase parse( Reader is, DocHelper docHelper, Properties params ) throws Exception {
102105
DocBase docBase = null;
103106
try {
104-
DocXmlParser parser = new DocXmlParser( DocHelper.DEFAULT, params );
107+
DocXmlParser parser = new DocXmlParser( DocHelper.DEFAULT );
105108
docBase = parser.parse(is);
106109
} catch (Exception e) {
107110
throw e;

fj-doc-base/src/main/java/org/fugerit/java/doc/base/parser/DocValidationResult.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class DocValidationResult extends BasicResult {
1414

1515
public static final int VALIDATION_NOT_SUPPORTED = VALIDATION_KO-11;
1616

17-
public static final int VALIDATION_NOT_DEFINIED = VALIDATION_KO-12;
17+
public static final int VALIDATION_NOT_DEFINED = VALIDATION_KO-12;
1818

1919
public DocValidationResult( int result ) {
2020
super( result );
@@ -46,8 +46,8 @@ public static DocValidationResult newDefaultNotSupportedResult() {
4646
return new DocValidationResult( VALIDATION_NOT_SUPPORTED );
4747
}
4848

49-
public static DocValidationResult newDefaultNotDefiniedResult() {
50-
return new DocValidationResult( VALIDATION_NOT_DEFINIED );
49+
public static DocValidationResult newDefaultNotDefinedResult() {
50+
return new DocValidationResult( VALIDATION_NOT_DEFINED );
5151
}
5252

5353
public int evaluateResult() {

fj-doc-base/src/main/java/org/fugerit/java/doc/base/xml/DocXmlParser.java

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
package org.fugerit.java.doc.base.xml;
22

3-
import java.io.ByteArrayOutputStream;
43
import java.io.Reader;
5-
import java.util.Properties;
64

75
import javax.xml.parsers.SAXParser;
86

9-
import org.fugerit.java.core.io.StreamIO;
107
import org.fugerit.java.core.xml.sax.SAXParseResult;
118
import org.fugerit.java.core.xml.sax.XMLFactorySAX;
129
import org.fugerit.java.core.xml.sax.dh.DefaultHandlerComp;
13-
import org.fugerit.java.core.xml.sax.er.ByteArrayEntityResolver;
14-
import org.fugerit.java.doc.base.facade.DocFacade;
1510
import org.fugerit.java.doc.base.facade.DocFacadeSource;
1611
import org.fugerit.java.doc.base.model.DocBase;
1712
import org.fugerit.java.doc.base.model.DocHelper;
@@ -25,69 +20,29 @@ public class DocXmlParser extends AbstractDocParser {
2520

2621
private static final Logger logger = LoggerFactory.getLogger( DocXmlParser.class );
2722

28-
private static final String DTD = "/config/doc-1-0.dtd";
29-
private static final String XSD = "/config/doc-"+DocFacade.CURRENT_VERSION+".xsd";
30-
31-
private static final Properties DEFAULT_PARAMS = new Properties();
32-
3323
private DocHelper docHelper;
34-
35-
private Properties params;
3624

37-
public DocXmlParser( DocHelper docHelper, Properties params ) {
25+
public DocXmlParser( DocHelper docHelper ) {
3826
super( DocFacadeSource.SOURCE_TYPE_XML );
3927
this.docHelper = docHelper;
40-
this.params = params;
4128
}
4229

4330
public DocXmlParser() {
44-
this( DocHelper.DEFAULT, DEFAULT_PARAMS );
31+
this( DocHelper.DEFAULT );
4532
}
4633

47-
private static byte[] readData( String res, byte[] alreadRead ) {
48-
byte[] data = alreadRead;
49-
if ( data != null ) {
50-
try {
51-
data = StreamIO.readBytes( DocFacade.class.getResourceAsStream( res ) );
52-
} catch (Exception e) {
53-
logger.warn( "Read error", e );
54-
}
55-
}
56-
return data;
57-
}
58-
59-
private static final byte[] XSD_DATA = readData( XSD, null );
60-
private static final byte[] DTD_DATA = readData( DTD, null );
61-
62-
private static ByteArrayEntityResolver newEntityResolver( Properties params ) {
63-
byte[] data = null;
64-
String validatationMode = params.getProperty( DocFacade.PARAM_DEFINITION_MODE, DocFacade.PARAM_DEFINITION_MODE_DEFAULT ) ;
65-
if ( DocFacade.PARAM_DEFINITION_MODE_DTD.equalsIgnoreCase( validatationMode ) ) {
66-
data = DTD_DATA;
67-
} else {
68-
data = XSD_DATA;
69-
}
70-
logger.debug( DocFacade.PARAM_DEFINITION_MODE+" -> "+validatationMode );
71-
ByteArrayEntityResolver er = new ByteArrayEntityResolver( data, null, DocFacade.SYSTEM_ID );
72-
return er;
73-
}
74-
7534
@Override
7635
protected DocBase parseWorker(Reader reader) throws Exception {
77-
SAXParser parser = XMLFactorySAX.makeSAXParser( false , false );
78-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
79-
StreamIO.pipeStream( DocFacade.class.getResourceAsStream( DTD ), baos, StreamIO.MODE_CLOSE_BOTH );
80-
ByteArrayEntityResolver er = newEntityResolver( this.params );
36+
SAXParser parser = XMLFactorySAX.makeSAXParser( false , true );
8137
DocContentHandler dch = new DocContentHandler( this.docHelper );
8238
DefaultHandlerComp dh = new DefaultHandlerComp( dch );
83-
dh.setWrappedEntityResolver( er );
8439
parser.parse( new InputSource(reader), dh);
8540
return dch.getDocBase();
8641
}
8742

8843
@Override
8944
protected DocValidationResult validateWorker(Reader reader) throws Exception {
90-
DocValidationResult docResult = DocValidationResult.newDefaultNotDefiniedResult();
45+
DocValidationResult docResult = DocValidationResult.newDefaultNotDefinedResult();
9146
SAXParseResult result = DocValidator.validate( reader );
9247
for ( Exception e : result.fatalsAndErrors() ) {
9348
docResult.getErrorList().add( e.toString() );
@@ -96,6 +51,7 @@ protected DocValidationResult validateWorker(Reader reader) throws Exception {
9651
docResult.getInfoList().add( e.toString() );
9752
}
9853
docResult.evaluateResult();
54+
logger.debug( "Validation result {}", docResult );
9955
return docResult;
10056
}
10157

fj-doc-playground-quarkus/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ npm run start
3636

3737
## modules currently supported in playground :
3838
* fj-doc-val (and all its extensions)
39-
* fj-doc-base (and all its extensions)
39+
* fj-doc-base (and all its extensions)
40+
41+
## functions available
42+
* Doc Xml Editor : allow for writing documents in XML/JSON/YAML, validating them and generating some output formats (currently PDF/XLSX/HTML) [covers fj-doc-mod* modules]
43+
* Doc Conversion : allow source conversion from/to XML/JSON/YAML [convers fj-doc-base* modules]
44+
* Doc Type Validator : allow validation of documents in various formats [covers fj-doc-val* modules]

fj-doc-playground-quarkus/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@
7676
<groupId>org.fugerit.java</groupId>
7777
<artifactId>fj-doc-mod-poi</artifactId>
7878
</dependency>
79+
<dependency>
80+
<groupId>org.fugerit.java</groupId>
81+
<artifactId>fj-doc-lib-simpletable</artifactId>
82+
</dependency>
7983
<dependency>
8084
<groupId>io.quarkus</groupId>
8185
<artifactId>quarkus-junit5</artifactId>

fj-doc-playground-quarkus/src/main/java/org/fugerit/java/doc/playground/doc/GenerateRest.java

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44
import java.io.StringReader;
55
import java.util.Base64;
66

7+
import org.fugerit.java.core.lang.helpers.BooleanUtils;
78
import org.fugerit.java.doc.base.config.DocInput;
89
import org.fugerit.java.doc.base.config.DocOutput;
910
import org.fugerit.java.doc.base.config.DocTypeHandler;
1011
import org.fugerit.java.doc.base.facade.DocFacadeSource;
12+
import org.fugerit.java.doc.base.parser.DocParser;
13+
import org.fugerit.java.doc.base.parser.DocValidationResult;
1114
import org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlFragmentTypeHandler;
12-
import org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandler;
15+
import org.fugerit.java.doc.lib.simpletable.SimpleTableDocConfig;
16+
import org.fugerit.java.doc.lib.simpletable.SimpleTableFacade;
17+
import org.fugerit.java.doc.lib.simpletable.model.SimpleRow;
18+
import org.fugerit.java.doc.lib.simpletable.model.SimpleTable;
1319
import org.fugerit.java.doc.mod.fop.PdfFopTypeHandler;
1420
import org.fugerit.java.doc.mod.poi.XlsxPoiTypeHandler;
21+
import org.fugerit.java.doc.playground.facade.BasicInput;
1522
import org.fugerit.java.doc.playground.facade.InputFacade;
1623
import org.slf4j.Logger;
1724
import org.slf4j.LoggerFactory;
@@ -51,73 +58,92 @@ private byte[] generateHelper( GenerateInput input, DocTypeHandler handler) thro
5158
}
5259
return result;
5360
}
61+
62+
private DocTypeHandler findHandler( BasicInput input ) {
63+
DocTypeHandler handler = new PdfFopTypeHandler();
64+
if ( "XLSX".equalsIgnoreCase( input.getOutputFormat() ) ) {
65+
handler = XlsxPoiTypeHandler.HANDLER;
66+
} else if ( "HTML".equalsIgnoreCase( input.getOutputFormat() ) ) {
67+
handler = FreeMarkerHtmlFragmentTypeHandler.HANDLER;
68+
}
69+
return handler;
70+
}
5471

55-
@POST
56-
@Consumes(MediaType.APPLICATION_JSON)
57-
@Produces(MediaType.APPLICATION_OCTET_STREAM)
58-
@Path("/PDF")
59-
public Response pdf( GenerateInput input) {
60-
Response res = Response.status(Response.Status.BAD_REQUEST).build();
61-
try {
62-
PdfFopTypeHandler handler = new PdfFopTypeHandler();
63-
byte[] data = this.generateHelper(input, handler);
64-
res = Response.ok().entity( data ).build();
65-
} catch (Exception e) {
66-
logger.info("Error : " + e, e);
67-
res = Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
72+
private DocParser findParser( BasicInput input ) {
73+
int sourceType = DocFacadeSource.SOURCE_TYPE_XML;
74+
if ( InputFacade.FORMAT_JSON.equalsIgnoreCase( input.getInputFormat() ) ) {
75+
sourceType = DocFacadeSource.SOURCE_TYPE_JSON;
76+
} else if ( InputFacade.FORMAT_YAML.equalsIgnoreCase( input.getInputFormat() ) ) {
77+
sourceType = DocFacadeSource.SOURCE_TYPE_YAML;
6878
}
69-
return res;
79+
return DocFacadeSource.getInstance().getParserForSource( sourceType );
7080
}
7181

7282
@POST
7383
@Consumes(MediaType.APPLICATION_JSON)
74-
@Produces(MediaType.TEXT_HTML)
75-
@Path("/HTML")
76-
public Response html( GenerateInput input) {
84+
@Produces(MediaType.APPLICATION_JSON)
85+
@Path("/document")
86+
public Response document( GenerateInput input) {
7787
Response res = Response.status(Response.Status.BAD_REQUEST).build();
7888
try {
79-
byte[] data = this.generateHelper(input, FreeMarkerHtmlTypeHandler.HANDLER);
80-
res = Response.ok().entity( data ).build();
89+
DocTypeHandler handler = this.findHandler(input);
90+
byte[] data = this.generateHelper(input, handler);
91+
GenerateOutput output = new GenerateOutput();
92+
output.setDocOutputBase64( Base64.getEncoder().encodeToString( data ) );
93+
res = Response.ok().entity( output ).build();
8194
} catch (Exception e) {
8295
logger.info("Error : " + e, e);
8396
res = Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
8497
}
8598
return res;
8699
}
87100

88-
@POST
89-
@Consumes(MediaType.APPLICATION_JSON)
90-
@Produces(MediaType.TEXT_HTML)
91-
@Path("/XLSX")
92-
public Response xlsx( GenerateInput input) {
93-
Response res = Response.status(Response.Status.BAD_REQUEST).build();
94-
try {
95-
byte[] data = this.generateHelper(input, XlsxPoiTypeHandler.HANDLER);
96-
res = Response.ok().entity( data ).build();
97-
} catch (Exception e) {
98-
logger.info("Error : " + e, e);
99-
res = Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
101+
private void addRow( SimpleTable simpleTableModel, int count, String level, String message ) {
102+
SimpleRow errorRow = new SimpleRow();
103+
if ( count > 0 ) {
104+
errorRow.addCell( String.valueOf( count ) );
105+
} else {
106+
errorRow.addCell( "-" );
100107
}
101-
return res;
108+
errorRow.addCell( level );
109+
errorRow.addCell( message );
110+
simpleTableModel.addRow( errorRow );
102111
}
103112

104113
@POST
105114
@Consumes(MediaType.APPLICATION_JSON)
106115
@Produces(MediaType.APPLICATION_JSON)
107-
@Path("/document")
108-
public Response output( GenerateInput input) {
116+
@Path("/validate")
117+
public Response validate( GenerateInput input) {
109118
Response res = Response.status(Response.Status.BAD_REQUEST).build();
110119
try {
111-
DocTypeHandler handler = new PdfFopTypeHandler();;
112-
if ( "XLSX".equalsIgnoreCase( input.getOutputFormat() ) ) {
113-
handler = XlsxPoiTypeHandler.HANDLER;
114-
} else if ( "HTML".equalsIgnoreCase( input.getOutputFormat() ) ) {
115-
handler = FreeMarkerHtmlFragmentTypeHandler.HANDLER;
120+
DocParser parser = this.findParser(input);
121+
try ( StringReader reader = new StringReader( input.getDocContent() );
122+
ByteArrayOutputStream buffer = new ByteArrayOutputStream() ) {
123+
DocValidationResult result = parser.validateResult( reader );
124+
DocTypeHandler handler = this.findHandler(input);
125+
SimpleTable simpleTableModel = SimpleTableFacade.newTable( 15, 20, 65 );
126+
SimpleRow headerRow = new SimpleRow( BooleanUtils.BOOLEAN_TRUE );
127+
headerRow.addCell( "#" );
128+
headerRow.addCell( "level" );
129+
headerRow.addCell( "message" );
130+
simpleTableModel.addRow( headerRow );
131+
int count = 1;
132+
for ( String message : result.getErrorList() ) {
133+
this.addRow(simpleTableModel, count, "error", message);
134+
count++;
135+
}
136+
for ( String message : result.getInfoList() ) {
137+
this.addRow(simpleTableModel, count, "info", message);
138+
count++;
139+
}
140+
this.addRow(simpleTableModel, 0, "result", "Document valid? : "+ ( DocValidationResult.RESULT_CODE_OK == result.getResultCode() ) );
141+
SimpleTableDocConfig docConfig = SimpleTableDocConfig.newConfig();
142+
docConfig.processSimpleTable(simpleTableModel, handler, buffer);
143+
GenerateOutput output = new GenerateOutput();
144+
output.setDocOutputBase64( Base64.getEncoder().encodeToString( buffer.toByteArray() ) );
145+
res = Response.ok().entity( output ).build();
116146
}
117-
byte[] data = this.generateHelper(input, handler);
118-
GenerateOutput output = new GenerateOutput();
119-
output.setDocOutputBase64( Base64.getEncoder().encodeToString( data ) );
120-
res = Response.ok().entity( output ).build();
121147
} catch (Exception e) {
122148
logger.info("Error : " + e, e);
123149
res = Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();

0 commit comments

Comments
 (0)