Skip to content

Commit 250afdc

Browse files
committed
Handler registration allowed by id
1 parent c8db7e1 commit 250afdc

File tree

9 files changed

+110
-11
lines changed

9 files changed

+110
-11
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Map;
99

1010
import org.fugerit.java.core.cfg.ConfigException;
11+
import org.fugerit.java.core.cfg.ConfigRuntimeException;
1112
import org.fugerit.java.core.util.collection.ListMapStringKey;
1213
import org.fugerit.java.doc.base.config.DocInput;
1314
import org.fugerit.java.doc.base.config.DocOutput;
@@ -74,7 +75,23 @@ public void registerHandler( DocTypeHandler handler, boolean registerForType, bo
7475
log.info( "list keys current -> {} : list {}", handler, this.mapHandlers.keySet() );
7576
log.debug( "test" );
7677
}
77-
78+
79+
public void registerHandlerAndId( String id, DocTypeHandler handler ) throws Exception {
80+
this.registerHandlerAndId(id, handler, false);
81+
}
82+
83+
public void registerHandlerAndId( String id, DocTypeHandler handler, boolean allowDuplicatedId ) throws Exception {
84+
if ( this.mapHandlers.containsKey( id ) ) {
85+
if ( allowDuplicatedId ) {
86+
log.warn( "duplicated id for : id {}, handler : {}", id, handler );
87+
} else {
88+
throw new ConfigRuntimeException( "Duplicate handler id not allowd : "+id );
89+
}
90+
}
91+
this.mapHandlers.put( id , handler);
92+
this.registerHandler( handler, DEFAULT_REGISTER_FOR_TYPE, DEFAULT_ERROR_ON_DUPLICATE );
93+
}
94+
7895
public void registerHandler( DocTypeHandler handler ) throws Exception {
7996
this.registerHandler( handler, DEFAULT_REGISTER_FOR_TYPE, DEFAULT_ERROR_ON_DUPLICATE );
8097
}

fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/process/FreemarkerDocProcessConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.fugerit.java.core.cfg.xml.ListMapConfig;
77
import org.fugerit.java.core.util.filterchain.MiniFilterChain;
88
import org.fugerit.java.core.util.filterchain.MiniFilterMap;
9+
import org.fugerit.java.doc.base.config.DocInput;
10+
import org.fugerit.java.doc.base.config.DocOutput;
11+
import org.fugerit.java.doc.base.config.DocTypeHandler;
912
import org.fugerit.java.doc.base.facade.DocHandlerFacade;
1013
import org.fugerit.java.doc.base.process.DocProcessConfig;
1114
import org.fugerit.java.doc.base.process.DocProcessContext;
@@ -50,6 +53,11 @@ public void process( String chainId, DocProcessContext context, DocProcessData d
5053
chain.apply( context , data );
5154
}
5255

56+
public void process( String chainId, DocProcessContext context, DocProcessData data, DocTypeHandler handler, DocOutput docOutput ) throws Exception {
57+
this.process(chainId, context, data);
58+
handler.handle( DocInput.newInput( handler.getType() , data.getCurrentXmlReader() ) , docOutput );
59+
}
60+
5361
@Override
5462
public MiniFilterChain getChain(String id) throws Exception {
5563
return this.docProcessConfig.getChain( id );

fj-doc-freemarker/src/main/java/org/fugerit/java/doc/freemarker/process/FreemarkerDocProcessConfigFacade.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.fugerit.java.core.cfg.helpers.UnsafeHelper;
1414
import org.fugerit.java.core.cfg.xml.XmlBeanHelper;
1515
import org.fugerit.java.core.io.helper.StreamHelper;
16+
import org.fugerit.java.core.lang.helpers.BooleanUtils;
1617
import org.fugerit.java.core.lang.helpers.ClassHelper;
1718
import org.fugerit.java.core.lang.helpers.StringUtils;
1819
import org.fugerit.java.core.util.filterchain.MiniFilterBase;
@@ -120,16 +121,23 @@ public static FreemarkerDocProcessConfig loadConfig( Reader xmlReader ) throws C
120121
NodeList docHandlerConfigList = doc.getElementsByTagName( ATT_DOC_HANDLER_CONFIG );
121122
if ( docHandlerConfigList.getLength() == 1 ) {
122123
Element docHandlerConfigTag = (Element) docHandlerConfigList.item( 0 );
124+
boolean registerById = BooleanUtils.isTrue( docHandlerConfigTag.getAttribute( "registerById" ) );
125+
boolean allowDuplicatedId = BooleanUtils.isTrue( docHandlerConfigTag.getAttribute( "allowDuplicatedId" ) );
123126
NodeList docHandlerList = docHandlerConfigTag.getElementsByTagName( "docHandler" );
124127
log.info( "docHandlerList -> {}", docHandlerList.getLength() );
125128
for ( int k=0; k<docHandlerList.getLength(); k++ ) {
126129
Element currentHandlerTag = (Element)docHandlerList.item( k );
127130
DocTypeHandler handler = createHelper( currentHandlerTag );
128131
if ( handler != null ) {
129-
config.getFacade().registerHandler( handler );
132+
String id = currentHandlerTag.getAttribute( "id" );
133+
log.info( "register handler id {}, handler {}", id, handler );
134+
if ( registerById ) {
135+
config.getFacade().registerHandlerAndId( id, handler, allowDuplicatedId );
136+
} else {
137+
config.getFacade().registerHandler( handler);
138+
}
130139
}
131140
}
132-
133141
}
134142
// docChain reading
135143
NodeList docChainLisgt = doc.getElementsByTagName( ATT_DOC_CHAIN );

fj-doc-freemarker/src/main/resources/config_fm_xsd/freemarker-doc-process-1-0.xsd

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* @project : org.fugerit.java.doc.base
88
* @creation : 2023-07-12
9-
* @version : 1.0.0-rc.003 (2023-07-19)
9+
* @version : 1.0.0-rc.004 (2023-07-30)
1010
*
1111
* XSD for Freemarker Doc Process Configuration
1212
*/
@@ -46,6 +46,16 @@
4646
<xsd:sequence>
4747
<xsd:element ref="fdp:docHandler" minOccurs="0" maxOccurs="unbounded"></xsd:element>
4848
</xsd:sequence>
49+
<xsd:attribute name="registerById" type="xsd:boolean" use="optional" default="false">
50+
<xsd:annotation>
51+
<xsd:documentation>If set to true, the handlers will be registered by id</xsd:documentation>
52+
</xsd:annotation>
53+
</xsd:attribute>
54+
<xsd:attribute name="allowDuplicatedId" type="xsd:boolean" use="optional" default="false">
55+
<xsd:annotation>
56+
<xsd:documentation>If set to true, duplicated id will be allowed (the last one will overwrite the others)</xsd:documentation>
57+
</xsd:annotation>
58+
</xsd:attribute>
4959
</xsd:complexType>
5060

5161
<xsd:element name='docHandler' type="fdp:docHandlerType">

fj-doc-sample/src/main/resources/config/freemarker-doc-process.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="https://freemarkerdocprocess.fugerit.org https://www.fugerit.org/data/java/doc/xsd/freemarker-doc-process-1-0.xsd" >
66

7-
<docHandlerConfig>
8-
<docHandler id="PDF/A-1a" info="pdf" type="org.fugerit.java.doc.mod.fop.PdfFopTypeHandler">
7+
<docHandlerConfig registerById="true">
8+
<docHandler id="pdfa-fop" info="pdf" type="org.fugerit.java.doc.mod.fop.PdfFopTypeHandler">
99
<docHandlerCustomConfig charset="UTF-8" fop-config-mode="classloader" fop-config-classloader-path="fop-config-pdfa.xml" pdf-a-mode="PDF/A-1a"/>
1010
</docHandler>
1111
<docHandler id="md-ext" info="md" type="org.fugerit.java.doc.base.typehandler.markdown.SimpleMarkdownExtTypeHandler" />

fj-doc-sample/src/main/resources/fop-config-pdfa.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
-->
1919
<renderers>
2020
<renderer mime="application/pdf">
21-
<!-- In this configuration the pdf-a-mode has been set via the freemarker-doc-process.xml
21+
<pdf-ua-mode>PDF/UA-1</pdf-ua-mode>
2222
<pdf-a-mode>PDF/A-1a</pdf-a-mode>
2323
<version>1.4</version>
24-
-->
2524
<fonts>
2625
<font embed-url="classpath://font/TitilliumWeb-Regular.ttf" embedding-mode="full">
2726
<font-triplet name="TitilliumWeb" style="normal" weight="normal"/>

fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/facade/BasicFacadeTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.slf4j.Logger;
2929
import org.slf4j.LoggerFactory;
3030

31+
import lombok.Getter;
32+
3133
public class BasicFacadeTest {
3234

3335
protected final static Logger logger = LoggerFactory.getLogger( BasicFacadeTest.class );
@@ -87,7 +89,7 @@ private static FreemarkerDocProcessConfig init() {
8789
return config;
8890
}
8991

90-
protected static FreemarkerDocProcessConfig PROCESS_CONFIG = init();
92+
@Getter protected static FreemarkerDocProcessConfig PROCESSCONFIG = init();
9193

9294
private int getSourceType() {
9395
int sourceType = DocFacadeSource.SOURCE_TYPE_DEFAULT;
@@ -142,7 +144,7 @@ protected DocBase getDocBase() throws Exception {
142144
}
143145

144146
public void produce( File outputFolder, String facadeId, DocBase doc, Reader reader, String baseName, String format ) throws Exception {
145-
DocHandlerFacade facade = PROCESS_CONFIG.getFacade();
147+
DocHandlerFacade facade = PROCESSCONFIG.getFacade();
146148
DocTypeHandler handler = facade.findHandler( format );
147149
StringBuilder append = new StringBuilder();
148150
if ( handler == null ) {

fj-doc-sample/src/test/java/test/org/fugerit/java/doc/sample/freemarker/BasicFreeMarkerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ protected Reader getXmlReader() throws Exception {
2626
public Reader process( String chainId ) throws Exception {
2727
DocProcessContext context = new DocProcessContext();
2828
DocProcessData data = new DocProcessData();
29-
PROCESS_CONFIG.process( chainId, context, data );
29+
PROCESSCONFIG.process( chainId, context, data );
3030
return data.getCurrentXmlReader();
3131
}
3232

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package test.org.fugerit.java.doc.sample.freemarker;
2+
3+
4+
import static org.junit.Assert.fail;
5+
6+
import java.io.File;
7+
import java.io.FileOutputStream;
8+
import java.io.OutputStream;
9+
10+
import org.fugerit.java.doc.base.config.DocConfig;
11+
import org.fugerit.java.doc.base.config.DocOutput;
12+
import org.fugerit.java.doc.base.config.DocTypeHandler;
13+
import org.fugerit.java.doc.base.process.DocProcessContext;
14+
import org.fugerit.java.doc.base.process.DocProcessData;
15+
import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfig;
16+
import org.junit.Test;
17+
18+
import lombok.extern.slf4j.Slf4j;
19+
import test.org.fugerit.java.doc.sample.facade.BasicFacadeTest;
20+
21+
@Slf4j
22+
public class TestPdfADirect {
23+
24+
private void testWorker( String handlerRef ) {
25+
FreemarkerDocProcessConfig config = BasicFacadeTest.getPROCESSCONFIG();
26+
DocTypeHandler handler = config.getFacade().findHandler( handlerRef );
27+
log.info( "handler : {}, type : {}, format : {}", handler, handler.getType(), handler.getFormat() );
28+
DocProcessContext context = DocProcessContext.newContext();
29+
DocProcessData data = new DocProcessData();
30+
String chainId = "pdf_a_test";
31+
try ( OutputStream os = new FileOutputStream( new File( "target/", "direct1_"+chainId+"."+handler.getType() ) ) ) {
32+
config.process( chainId, context, data, handler, DocOutput.newOutput(os) );
33+
} catch (Exception e) {
34+
String message = "Error : "+e;
35+
log.error( message, e );
36+
fail( message );
37+
}
38+
}
39+
40+
@Test
41+
public void testPDFADirectFormat() {
42+
this.testWorker( DocConfig.FORMAT_PDF_A_1A );
43+
}
44+
45+
@Test
46+
public void testPDFADirectId() {
47+
this.testWorker( "pdfa-fop" );
48+
}
49+
50+
@Test
51+
public void testPDFDirectType() {
52+
this.testWorker( DocConfig.TYPE_PDF );
53+
}
54+
55+
}

0 commit comments

Comments
 (0)