|
4 | 4 | import java.io.StringReader; |
5 | 5 | import java.util.Base64; |
6 | 6 |
|
| 7 | +import org.fugerit.java.core.lang.helpers.BooleanUtils; |
7 | 8 | import org.fugerit.java.doc.base.config.DocInput; |
8 | 9 | import org.fugerit.java.doc.base.config.DocOutput; |
9 | 10 | import org.fugerit.java.doc.base.config.DocTypeHandler; |
10 | 11 | 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; |
11 | 14 | 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; |
13 | 19 | import org.fugerit.java.doc.mod.fop.PdfFopTypeHandler; |
14 | 20 | import org.fugerit.java.doc.mod.poi.XlsxPoiTypeHandler; |
| 21 | +import org.fugerit.java.doc.playground.facade.BasicInput; |
15 | 22 | import org.fugerit.java.doc.playground.facade.InputFacade; |
16 | 23 | import org.slf4j.Logger; |
17 | 24 | import org.slf4j.LoggerFactory; |
@@ -51,73 +58,92 @@ private byte[] generateHelper( GenerateInput input, DocTypeHandler handler) thro |
51 | 58 | } |
52 | 59 | return result; |
53 | 60 | } |
| 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 | + } |
54 | 71 |
|
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; |
68 | 78 | } |
69 | | - return res; |
| 79 | + return DocFacadeSource.getInstance().getParserForSource( sourceType ); |
70 | 80 | } |
71 | 81 |
|
72 | 82 | @POST |
73 | 83 | @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) { |
77 | 87 | Response res = Response.status(Response.Status.BAD_REQUEST).build(); |
78 | 88 | 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(); |
81 | 94 | } catch (Exception e) { |
82 | 95 | logger.info("Error : " + e, e); |
83 | 96 | res = Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); |
84 | 97 | } |
85 | 98 | return res; |
86 | 99 | } |
87 | 100 |
|
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( "-" ); |
100 | 107 | } |
101 | | - return res; |
| 108 | + errorRow.addCell( level ); |
| 109 | + errorRow.addCell( message ); |
| 110 | + simpleTableModel.addRow( errorRow ); |
102 | 111 | } |
103 | 112 |
|
104 | 113 | @POST |
105 | 114 | @Consumes(MediaType.APPLICATION_JSON) |
106 | 115 | @Produces(MediaType.APPLICATION_JSON) |
107 | | - @Path("/document") |
108 | | - public Response output( GenerateInput input) { |
| 116 | + @Path("/validate") |
| 117 | + public Response validate( GenerateInput input) { |
109 | 118 | Response res = Response.status(Response.Status.BAD_REQUEST).build(); |
110 | 119 | 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(); |
116 | 146 | } |
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(); |
121 | 147 | } catch (Exception e) { |
122 | 148 | logger.info("Error : " + e, e); |
123 | 149 | res = Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); |
|
0 commit comments