@@ -45,13 +45,11 @@ This file is part of the iText (R) project.
4545import com .itextpdf .html2pdf .attach .ITagWorker ;
4646import com .itextpdf .html2pdf .attach .ITagWorkerFactory ;
4747import com .itextpdf .html2pdf .attach .ProcessorContext ;
48- import com .itextpdf .html2pdf .exceptions . TagWorkerInitializationException ;
48+ import com .itextpdf .html2pdf .attach . impl . DefaultTagWorkerMapping . ITagWorkerCreator ;
4949import com .itextpdf .html2pdf .util .TagProcessorMapping ;
5050import com .itextpdf .html2pdf .css .CssConstants ;
5151import com .itextpdf .styledxmlparser .node .IElementNode ;
5252
53- import java .lang .reflect .Constructor ;
54-
5553/**
5654 * The default implementation of a tag worker factory, mapping tags to
5755 * tag worker implementations.
@@ -61,13 +59,13 @@ public class DefaultTagWorkerFactory implements ITagWorkerFactory {
6159 private static final ITagWorkerFactory INSTANCE = new DefaultTagWorkerFactory ();
6260
6361 /** The default mapping. */
64- private final TagProcessorMapping defaultMapping ;
62+ private final TagProcessorMapping < ITagWorkerCreator > defaultMapping ;
6563
6664 /**
6765 * Instantiates a new default tag worker factory.
6866 */
6967 public DefaultTagWorkerFactory () {
70- this .defaultMapping = DefaultTagWorkerMapping .getDefaultTagWorkerMapping ();
68+ this .defaultMapping = new DefaultTagWorkerMapping () .getDefaultTagWorkerMapping ();
7169 }
7270
7371 /**
@@ -86,46 +84,40 @@ public final ITagWorker getTagWorker(IElementNode tag, ProcessorContext context)
8684 ITagWorker tagWorker = getCustomTagWorker (tag , context );
8785
8886 if (tagWorker == null ) {
89- Class <?> tagWorkerClass = getTagWorkerClass (this .defaultMapping , tag );
87+ final ITagWorkerCreator tagWorkerCreator = getTagWorkerCreator (this .defaultMapping , tag );
9088
91- if (tagWorkerClass == null ) {
89+ if (tagWorkerCreator == null ) {
9290 return null ;
9391 }
9492
95- // Use reflection to create an instance
96- try {
97- Constructor ctor = tagWorkerClass .getDeclaredConstructor (new Class <?>[]{IElementNode .class , ProcessorContext .class });
98- ITagWorker res = (ITagWorker ) ctor .newInstance (new Object []{tag , context });
99- return res ;
100- } catch (Exception e ) {
101- throw new TagWorkerInitializationException (TagWorkerInitializationException .REFLECTION_IN_TAG_WORKER_FACTORY_IMPLEMENTATION_FAILED , tagWorkerClass .getName (), tag .name (), e );
102- }
93+ return tagWorkerCreator .create (tag , context );
10394 }
10495
10596 return tagWorker ;
10697 }
10798
108- TagProcessorMapping getDefaultMapping () {
99+ TagProcessorMapping < ITagWorkerCreator > getDefaultMapping () {
109100 return defaultMapping ;
110101 }
111102
112103 /**
113- * Gets the tag worker class for a specific element node.
104+ * Gets the tag worker creator for a specific element node.
114105 *
115106 * @param mapping the mapping
116107 * @param tag the element node
117- * @return the tag worker class
108+ * @return the tag worker class creator
118109 */
119- private Class <?> getTagWorkerClass (TagProcessorMapping mapping , IElementNode tag ) {
120- Class <?> tagWorkerClass = null ;
110+ private static ITagWorkerCreator getTagWorkerCreator (TagProcessorMapping <ITagWorkerCreator > mapping ,
111+ IElementNode tag ) {
112+ ITagWorkerCreator tagWorkerCreator = null ;
121113 String display = tag .getStyles () != null ? tag .getStyles ().get (CssConstants .DISPLAY ) : null ;
122114 if (display != null ) {
123- tagWorkerClass = mapping .getMapping (tag .name (), display );
115+ tagWorkerCreator = ( ITagWorkerCreator ) mapping .getMapping (tag .name (), display );
124116 }
125- if (tagWorkerClass == null ) {
126- tagWorkerClass = mapping .getMapping (tag .name ());
117+ if (tagWorkerCreator == null ) {
118+ tagWorkerCreator = ( ITagWorkerCreator ) mapping .getMapping (tag .name ());
127119 }
128- return tagWorkerClass ;
120+ return tagWorkerCreator ;
129121 }
130122
131123 /**
0 commit comments