@@ -45,13 +45,11 @@ This file is part of the iText (R) project.
45
45
import com .itextpdf .html2pdf .attach .ITagWorker ;
46
46
import com .itextpdf .html2pdf .attach .ITagWorkerFactory ;
47
47
import com .itextpdf .html2pdf .attach .ProcessorContext ;
48
- import com .itextpdf .html2pdf .exceptions . TagWorkerInitializationException ;
48
+ import com .itextpdf .html2pdf .attach . impl . DefaultTagWorkerMapping . ITagWorkerCreator ;
49
49
import com .itextpdf .html2pdf .util .TagProcessorMapping ;
50
50
import com .itextpdf .html2pdf .css .CssConstants ;
51
51
import com .itextpdf .styledxmlparser .node .IElementNode ;
52
52
53
- import java .lang .reflect .Constructor ;
54
-
55
53
/**
56
54
* The default implementation of a tag worker factory, mapping tags to
57
55
* tag worker implementations.
@@ -61,13 +59,13 @@ public class DefaultTagWorkerFactory implements ITagWorkerFactory {
61
59
private static final ITagWorkerFactory INSTANCE = new DefaultTagWorkerFactory ();
62
60
63
61
/** The default mapping. */
64
- private final TagProcessorMapping defaultMapping ;
62
+ private final TagProcessorMapping < ITagWorkerCreator > defaultMapping ;
65
63
66
64
/**
67
65
* Instantiates a new default tag worker factory.
68
66
*/
69
67
public DefaultTagWorkerFactory () {
70
- this .defaultMapping = DefaultTagWorkerMapping .getDefaultTagWorkerMapping ();
68
+ this .defaultMapping = new DefaultTagWorkerMapping () .getDefaultTagWorkerMapping ();
71
69
}
72
70
73
71
/**
@@ -86,46 +84,40 @@ public final ITagWorker getTagWorker(IElementNode tag, ProcessorContext context)
86
84
ITagWorker tagWorker = getCustomTagWorker (tag , context );
87
85
88
86
if (tagWorker == null ) {
89
- Class <?> tagWorkerClass = getTagWorkerClass (this .defaultMapping , tag );
87
+ final ITagWorkerCreator tagWorkerCreator = getTagWorkerCreator (this .defaultMapping , tag );
90
88
91
- if (tagWorkerClass == null ) {
89
+ if (tagWorkerCreator == null ) {
92
90
return null ;
93
91
}
94
92
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 );
103
94
}
104
95
105
96
return tagWorker ;
106
97
}
107
98
108
- TagProcessorMapping getDefaultMapping () {
99
+ TagProcessorMapping < ITagWorkerCreator > getDefaultMapping () {
109
100
return defaultMapping ;
110
101
}
111
102
112
103
/**
113
- * Gets the tag worker class for a specific element node.
104
+ * Gets the tag worker creator for a specific element node.
114
105
*
115
106
* @param mapping the mapping
116
107
* @param tag the element node
117
- * @return the tag worker class
108
+ * @return the tag worker class creator
118
109
*/
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 ;
121
113
String display = tag .getStyles () != null ? tag .getStyles ().get (CssConstants .DISPLAY ) : null ;
122
114
if (display != null ) {
123
- tagWorkerClass = mapping .getMapping (tag .name (), display );
115
+ tagWorkerCreator = ( ITagWorkerCreator ) mapping .getMapping (tag .name (), display );
124
116
}
125
- if (tagWorkerClass == null ) {
126
- tagWorkerClass = mapping .getMapping (tag .name ());
117
+ if (tagWorkerCreator == null ) {
118
+ tagWorkerCreator = ( ITagWorkerCreator ) mapping .getMapping (tag .name ());
127
119
}
128
- return tagWorkerClass ;
120
+ return tagWorkerCreator ;
129
121
}
130
122
131
123
/**
0 commit comments