Skip to content

Commit e125e2c

Browse files
authored
Merge pull request #570 from fugerit-org/569-chore-fj-doc-mod-poi-metadata-handling
569 chore fj doc mod poi metadata handling
2 parents 0634b59 + ddfcedf commit e125e2c

File tree

8 files changed

+177
-21
lines changed

8 files changed

+177
-21
lines changed

fj-doc-mod-poi/src/main/java/org/fugerit/java/doc/mod/poi/PoiUtils.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.apache.poi.hssf.usermodel.HSSFPalette;
1111
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
1212
import org.apache.poi.hssf.util.HSSFColor;
13+
import org.apache.poi.ooxml.POIXMLProperties;
1314
import org.apache.poi.ss.usermodel.Cell;
1415
import org.apache.poi.ss.usermodel.CellStyle;
1516
import org.apache.poi.ss.usermodel.FillPatternType;
@@ -23,8 +24,12 @@
2324
import org.apache.poi.xssf.usermodel.XSSFFont;
2425
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
2526
import org.fugerit.java.core.cfg.ConfigRuntimeException;
27+
import org.fugerit.java.core.function.SafeFunction;
2628
import org.fugerit.java.core.lang.helpers.StringUtils;
29+
import org.fugerit.java.doc.base.config.DocConfig;
2730
import org.fugerit.java.doc.base.config.DocOutput;
31+
import org.fugerit.java.doc.base.config.VenusVersion;
32+
import org.fugerit.java.doc.base.model.DocBase;
2833
import org.fugerit.java.doc.base.model.DocCell;
2934
import org.fugerit.java.doc.base.xml.DocModelUtils;
3035

@@ -103,15 +108,45 @@ public static void closeWorkbook( Workbook workbook, DocOutput docOutput ) throw
103108
workbook.write( docOutput.getOs() );
104109
workbook.close();
105110
}
106-
107-
public static WorkbookHelper newHelper( boolean xlsx, InputStream is ) throws IOException {
111+
112+
private static final String PRODUCER_OVER = "Apache POI";
113+
114+
private static final String PRODUCER_DEFAULT = String.format( VenusVersion.VENUS_PRODUCER_FORMAT_SH1, DocConfig.FUGERIT_VENUS_DOC , PRODUCER_OVER );
115+
116+
private static void propertySetup(DocBase docBase, POIXMLProperties props) {
117+
SafeFunction.applySilent( () -> {
118+
POIXMLProperties.CoreProperties coreProps = props.getCoreProperties();
119+
POIXMLProperties.CustomProperties customProps = props.getCustomProperties();
120+
SafeFunction.applyIfNotNull( docBase.getInfoDocTitle(), () -> coreProps.setTitle( docBase.getInfoDocTitle() ) );
121+
SafeFunction.applyIfNotNull( docBase.getInfoDocSubject(), () -> coreProps.setSubjectProperty( docBase.getInfoDocSubject() ) );
122+
SafeFunction.applyIfNotNull( docBase.getInfoDocVersion(), () -> coreProps.setVersion( docBase.getInfoDocVersion() ) );
123+
SafeFunction.applyIfNotNull( docBase.getInfoDocAuthor(), () -> customProps.addProperty( "Author", docBase.getInfoDocAuthor() ) );
124+
if ( docBase.getInfoDocProducer() != null ) {
125+
customProps.addProperty( "Creator" , docBase.getInfoDocCreator() );
126+
} else {
127+
customProps.addProperty( "Creator" , VenusVersion.VENUS_CREATOR );
128+
}
129+
if ( docBase.getInfoDocProducer() != null ) {
130+
customProps.addProperty( "Producer" , docBase.getInfoDocProducer() );
131+
} else {
132+
customProps.addProperty( "Producer" , PRODUCER_DEFAULT );
133+
}
134+
SafeFunction.applyIfNotNull( docBase.getInfoDocLanguage(), () -> customProps.addProperty( "ContentLanguage" , docBase.getInfoDocLanguage() ) );
135+
} );
136+
}
137+
138+
public static WorkbookHelper newHelper(boolean xlsx, InputStream is, DocBase docBase) throws IOException {
108139
Workbook workbook = null;
109140
if ( xlsx ) {
141+
XSSFWorkbook xssfWorkbook = null;
110142
if ( is == null ) {
111-
workbook = new XSSFWorkbook();
143+
xssfWorkbook = new XSSFWorkbook();
112144
} else {
113-
workbook = new XSSFWorkbook( is );
114-
}
145+
xssfWorkbook = new XSSFWorkbook( is );
146+
}
147+
POIXMLProperties props = xssfWorkbook.getProperties();
148+
propertySetup( docBase, props );
149+
workbook = xssfWorkbook;
115150
} else {
116151
if ( is == null ) {
117152
workbook = new HSSFWorkbook();

fj-doc-mod-poi/src/main/java/org/fugerit/java/doc/mod/poi/XlsPoiTypeHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public XlsPoiTypeHandler() {
2828

2929
@Override
3030
protected WorkbookHelper newWorkbook(DocInput docInput, InputStream is ) throws IOException {
31-
return PoiUtils.newHelper(false, is);
31+
return PoiUtils.newHelper(false, is, docInput.getDoc());
3232
}
3333

3434
@Override

fj-doc-mod-poi/src/main/java/org/fugerit/java/doc/mod/poi/XlsxPoiTypeHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public XlsxPoiTypeHandler() {
2828

2929
@Override
3030
protected WorkbookHelper newWorkbook( DocInput docInput , InputStream is ) throws IOException {
31-
return PoiUtils.newHelper(true, is);
31+
return PoiUtils.newHelper(true, is, docInput.getDoc());
3232
}
3333

3434
@Override

fj-doc-mod-poi/src/main/resources/META-INF/native-image/org.fugerit.java/fj-doc-mod-poi/reflect-config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@
260260
"parameterTypes" : [ ]
261261
}, {
262262
"name" : "newHelper",
263-
"parameterTypes" : [ "boolean", "java.io.InputStream" ]
263+
"parameterTypes" : [ "boolean", "java.io.InputStream", "org.fugerit.java.doc.base.model.DocBase" ]
264264
}, {
265265
"name" : "notify",
266266
"parameterTypes" : [ ]

fj-doc-mod-poi/src/test/java/test/org/fugerit/java/doc/mod/poi/coverage/TestPOIUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.fugerit.java.core.cfg.ConfigRuntimeException;
1010
import org.fugerit.java.core.function.SafeFunction;
1111
import org.fugerit.java.core.lang.helpers.ClassHelper;
12+
import org.fugerit.java.doc.base.model.DocBase;
1213
import org.fugerit.java.doc.base.model.DocCell;
1314
import org.fugerit.java.doc.mod.poi.PoiUtils;
1415
import org.fugerit.java.doc.mod.poi.WorkbookHelper;
@@ -70,16 +71,17 @@ void testFindColor() {
7071

7172
@Test
7273
void testNewHelper() {
74+
DocBase docBase = new DocBase();
7375
SafeFunction.apply( () -> {
7476
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "coverage/xls_as_xls.xls" );
75-
WorkbookHelper helper = PoiUtils.newHelper( false , is ) ) {
77+
WorkbookHelper helper = PoiUtils.newHelper( false , is, docBase ) ) {
7678
log.info( "test 1 -> {}", helper.getIndexedColorMap() );
7779
Assertions.assertNotNull( helper );
7880
}
7981
} );
8082
Assertions.assertNotNull( SafeFunction.get( () -> {
8183
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "coverage/xlsx_as_xlsx.xlsx" );
82-
WorkbookHelper helper = PoiUtils.newHelper( true , is ) ) {
84+
WorkbookHelper helper = PoiUtils.newHelper( true , is, docBase ) ) {
8385
log.info( "test 2 -> {}", helper.getIndexedColorMap() );
8486
return helper;
8587
}

fj-doc-mod-poi/src/test/java/test/org/fugerit/java/doc/mod/poi/coverage/TestPoiCoverage.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,23 @@ void test01() {
4444
}
4545

4646
@Test
47-
void testDoc() throws Exception {
48-
String fileName = "test_doc";
49-
DocTypeHandler handler = XlsxPoiTypeHandler.HANDLER;
50-
String inputXml = String.format( "coverage/xml/%s.xml", fileName );
51-
String outputFile = String.format( "target/%s.%s", fileName, handler.getType() );
52-
File output = new File( outputFile );
53-
try ( FileOutputStream fos = new FileOutputStream( output );
54-
InputStreamReader reader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader(inputXml) );) {
55-
handler.handle( DocInput.newInput( handler.getType(), reader ), DocOutput.newOutput( fos ) );
56-
}
57-
Assertions.assertTrue( output.exists() );
47+
void testDoc() {
48+
SimpleValue<Integer> count = new SimpleValue<>( 0 );
49+
Arrays.asList( "test_doc", "test_doc_alt" ).forEach( fileName -> {
50+
SafeFunction.apply( () -> {
51+
DocTypeHandler handler = XlsxPoiTypeHandler.HANDLER;
52+
String inputXml = String.format( "coverage/xml/%s.xml", fileName );
53+
String outputFile = String.format( "target/%s.%s", fileName, handler.getType() );
54+
File output = new File( outputFile );
55+
try ( FileOutputStream fos = new FileOutputStream( output );
56+
InputStreamReader reader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader(inputXml) );) {
57+
handler.handle( DocInput.newInput( handler.getType(), reader ), DocOutput.newOutput( fos ) );
58+
}
59+
Assertions.assertTrue( output.exists() );
60+
count.setValue( count.getValue()+1 );
61+
});
62+
});
63+
Assertions.assertNotEquals( 0, count.getValue() );
5864
}
5965

6066
}

fj-doc-mod-poi/src/test/resources/coverage/xml/test_doc.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
<!-- documenta meta information -->
1212
<info name="doc-title">Basic example</info>
1313
<info name="doc-subject">fj doc venus sample source xml</info>
14+
<info name="doc-version">001</info>
1415
<info name="doc-author">fugerit79</info>
1516
<info name="doc-language">en</info>
17+
<info name="doc-creator">My Sheet Creator</info>
18+
<info name="doc-producer">My Sheet Producer</info>
1619
<!-- additional properties -->
1720
<info name="set-total-page">true</info>
1821
<info name="html-css-link">/css/test.css</info>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<doc
3+
xmlns="http://javacoredoc.fugerit.org"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://javacoredoc.fugerit.org https://www.fugerit.org/data/java/doc/xsd/doc-2-1.xsd" >
6+
7+
<metadata>
8+
<!-- Margin for document : left;right;top;bottom -->
9+
<info name="margins">10;10;10;30</info>
10+
<info name="excel-table-id">excel-table=print,excel-table-1=print1</info>
11+
<!-- documenta meta information -->
12+
<info name="doc-title">Basic example</info>
13+
<info name="doc-subject">fj doc venus sample source xml</info>
14+
<info name="doc-version">001</info>
15+
<info name="doc-author">fugerit79</info>
16+
<info name="doc-language">en</info>
17+
<!-- additional properties -->
18+
<info name="set-total-page">true</info>
19+
<info name="html-css-link">/css/test.css</info>
20+
<header-ext>
21+
<para align="center" fore-color="#eeeeee">header test</para>
22+
</header-ext>
23+
<footer-ext>
24+
<para align="left">test</para>
25+
<para align="center">${r"${currentPage}"} / ${r"${pageCount}"}</para>
26+
<para align="right">test</para>
27+
</footer-ext>
28+
<bookmark-tree>
29+
<bookmark ref="title">Test</bookmark>
30+
</bookmark-tree>
31+
</metadata>
32+
<body>
33+
<h id="title" head-level="1">main title h1</h>
34+
<para font-name="times-roman" style="bold">Test times roman</para>
35+
<para font-name="courier" style="bolditalic">Courier</para>
36+
<para font-name="symbol" style="italic">Symbol</para>
37+
<para font-name="helvetica" style="underline">Symbol</para>
38+
<para size="3" fore-color="#dddddd">Test default font</para>
39+
<br/>
40+
<page-break/>
41+
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
42+
<row>
43+
<cell align="center" border-color="#000000" border-width="1"><para style="bold">Name</para></cell>
44+
<cell align="center"><para style="bold">Surname</para></cell>
45+
<cell align="center"><para style="bold">Title</para></cell>
46+
</row>
47+
<row>
48+
<cell valign="top"><para><![CDATA[Thorin]]></para></cell>
49+
<cell valign="bottom"><para><![CDATA[Oakshield]]></para></cell>
50+
<cell valign="middle"><para><![CDATA[King]]></para></cell>
51+
</row>
52+
<row>
53+
<cell fore-color="#000000" back-color="#ffffff"><para><![CDATA[Luthien]]></para></cell>
54+
<cell><para><![CDATA[Tinuviel]]></para></cell>
55+
<cell><para><![CDATA[Queen]]></para></cell>
56+
</row>
57+
<row>
58+
<cell valign="top"><para><![CDATA[Thorin]]></para></cell>
59+
<cell valign="bottom"><para><![CDATA[Oakshield]]></para></cell>
60+
<cell valign="middle"><para><![CDATA[King]]></para></cell>
61+
</row>
62+
<row>
63+
<cell align="left"><phrase><![CDATA[Phrase]]></phrase></cell>
64+
<cell align="right"><phrase anchor="1"><![CDATA[Oakshield]]></phrase></cell>
65+
<cell><phrase link="https:///www.fugerit.org"><![CDATA[King]]></phrase></cell>
66+
</row>
67+
<row>
68+
<cell rowspan="2"><phrase>rowspan 2</phrase></cell>
69+
<cell colspan="2"><phrase>colspn 2</phrase></cell>
70+
</row>
71+
<row>
72+
<cell colspan="2"><phrase>colspn 2</phrase></cell>
73+
</row>
74+
</table>
75+
<table columns="3" colwidths="30;30;40" width="100" id="excel-table-1" padding="2">
76+
<row>
77+
<cell align="center" border-color="#000000" border-width="1"><para style="bold">Name</para></cell>
78+
<cell align="center"><para style="bold">Surname</para></cell>
79+
<cell align="center"><para style="bold">Title</para></cell>
80+
</row>
81+
<row>
82+
<cell valign="top"><para><![CDATA[Thorin]]></para></cell>
83+
<cell valign="bottom"><para><![CDATA[Oakshield]]></para></cell>
84+
<cell valign="middle"><para><![CDATA[King]]></para></cell>
85+
</row>
86+
</table>
87+
<list>
88+
<li><para>test 1</para></li>
89+
</list>
90+
<list list-type="ul">
91+
<li><para>test 2</para></li>
92+
</list>
93+
<list list-type="ol">
94+
<li><para>test 3</para></li>
95+
</list>
96+
<list list-type="uld">
97+
<li><para>test 4</para></li>
98+
</list>
99+
<list list-type="ulm">
100+
<li><para>test 5</para></li>
101+
</list>
102+
<list list-type="oll">
103+
<li><para>test 6</para></li>
104+
</list>
105+
<list list-type="oln">
106+
<li><para>test 7</para></li>
107+
</list>
108+
</body>
109+
110+
</doc>

0 commit comments

Comments
 (0)