@@ -23,6 +23,7 @@ This file is part of the iText (R) project.
23
23
package com .itextpdf .html2pdf .attach .impl ;
24
24
25
25
import com .itextpdf .commons .datastructures .Tuple2 ;
26
+ import com .itextpdf .html2pdf .attach .IOutlineMarkExtractor ;
26
27
import com .itextpdf .html2pdf .html .TagConstants ;
27
28
import com .itextpdf .html2pdf .logs .Html2PdfLogMessageConstant ;
28
29
import com .itextpdf .html2pdf .attach .ITagWorker ;
@@ -45,7 +46,8 @@ This file is part of the iText (R) project.
45
46
import java .util .Map ;
46
47
47
48
/**
48
- * A {@link OutlineHandler} handles creating outlines for tags.
49
+ * A {@link OutlineHandler} handles creating outlines for marks.
50
+ * Marks are extracted via interface {@link IOutlineMarkExtractor}.
49
51
* <p>
50
52
* This class is not reusable and a new instance shall be created for every new conversion process.
51
53
*/
@@ -68,86 +70,182 @@ public class OutlineHandler {
68
70
/**
69
71
* The current outline.
70
72
*/
71
- private PdfOutline currentOutline ;
73
+ protected PdfOutline currentOutline ;
72
74
73
75
/**
74
76
* The destinations in process.
75
77
*/
76
- private Deque <Tuple2 <String , PdfDictionary >> destinationsInProcess = new LinkedList <Tuple2 <String , PdfDictionary >>();
78
+ protected Deque <Tuple2 <String , PdfDictionary >> destinationsInProcess = new LinkedList <Tuple2 <String , PdfDictionary >>();
77
79
78
80
/**
79
81
* The levels in process.
80
82
*/
81
- private Deque <Integer > levelsInProcess = new LinkedList <Integer >();
83
+ protected Deque <Integer > levelsInProcess = new LinkedList <Integer >();
82
84
83
85
/**
84
- * The tag priorities mapping.
86
+ * The mark priorities mapping.
85
87
*/
86
- private Map <String , Integer > tagPrioritiesMapping = new HashMap <String , Integer >();
88
+ private Map <String , Integer > markPrioritiesMapping = new HashMap <String , Integer >();
87
89
88
90
/**
89
91
* The destination prefix.
90
92
*/
91
93
private String destinationNamePrefix = DEFAULT_DESTINATION_NAME_PREFIX ;
92
94
95
+ /**
96
+ *The mark extractor defines what part of element will be used to create outline
97
+ */
98
+ protected IOutlineMarkExtractor markExtractor ;
93
99
94
100
/**
95
- * Creates an OutlineHandler with standard predefined mappings.
101
+ * Creates an OutlineHandler with standard {@link TagOutlineMarkExtractor}.
102
+ */
103
+ public OutlineHandler (){
104
+ markExtractor = new TagOutlineMarkExtractor ();
105
+ }
106
+ /**
107
+ * Creates an OutlineHandler with standard {@link TagOutlineMarkExtractor} and predefined mappings.
96
108
*
97
109
* @return the outline handler
98
110
*/
99
111
public static OutlineHandler createStandardHandler () {
100
112
OutlineHandler handler = new OutlineHandler ();
101
- handler .putTagPriorityMapping (TagConstants .H1 , 1 );
102
- handler .putTagPriorityMapping (TagConstants .H2 , 2 );
103
- handler .putTagPriorityMapping (TagConstants .H3 , 3 );
104
- handler .putTagPriorityMapping (TagConstants .H4 , 4 );
105
- handler .putTagPriorityMapping (TagConstants .H5 , 5 );
106
- handler .putTagPriorityMapping (TagConstants .H6 , 6 );
113
+ handler .putMarkPriorityMapping (TagConstants .H1 , 1 );
114
+ handler .putMarkPriorityMapping (TagConstants .H2 , 2 );
115
+ handler .putMarkPriorityMapping (TagConstants .H3 , 3 );
116
+ handler .putMarkPriorityMapping (TagConstants .H4 , 4 );
117
+ handler .putMarkPriorityMapping (TagConstants .H5 , 5 );
118
+ handler .putMarkPriorityMapping (TagConstants .H6 , 6 );
119
+ return handler ;
120
+ }
121
+
122
+ /**
123
+ * Creates an OutlineHandler with custom {@link IOutlineMarkExtractor}
124
+ *
125
+ * @param extractor the mark extractor
126
+ * @return the outline handler
127
+ */
128
+ public static OutlineHandler createHandler (IOutlineMarkExtractor extractor ) {
129
+ OutlineHandler handler = new OutlineHandler ();
130
+ handler .markExtractor = extractor ;
107
131
return handler ;
108
132
}
109
133
110
134
/**
111
- * Put tag priority mapping.
135
+ * Get mark extractor.
136
+ *
137
+ * @return the mark extractor
138
+ */
139
+ public IOutlineMarkExtractor getMarkExtractor (){
140
+ return markExtractor ;
141
+ }
142
+
143
+ /**
144
+ * Set mark extractor.
145
+ *
146
+ * @param extractor the mark extractor
147
+ * @return the outline handler
148
+ */
149
+ public OutlineHandler setMarkExtractor (IOutlineMarkExtractor extractor ){
150
+ markExtractor = extractor ;
151
+ return this ;
152
+ }
153
+ /**
154
+ * Put tag into priority mapping.
112
155
*
113
156
* @param tagName the tag name
114
157
* @param priority the priority
115
158
* @return the outline handler
159
+ * @deprecated use {@link #putMarkPriorityMapping(String, Integer)} instead
116
160
*/
161
+ @ Deprecated
117
162
public OutlineHandler putTagPriorityMapping (String tagName , Integer priority ) {
118
- tagPrioritiesMapping . put (tagName , priority );
163
+ putMarkPriorityMapping (tagName , priority );
119
164
return this ;
120
165
}
121
166
122
167
/**
123
- * Put all tag priority mappings.
168
+ * Put mark into priority mapping.
169
+ *
170
+ * @param markName the mark name
171
+ * @param priority the priority
172
+ * @return the outline handler
173
+ */
174
+ public OutlineHandler putMarkPriorityMapping (String markName , Integer priority ) {
175
+ markPrioritiesMapping .put (markName , priority );
176
+ return this ;
177
+ }
178
+
179
+ /**
180
+ * Put all tags into priority mappings.
124
181
*
125
182
* @param mappings the mappings
126
183
* @return the outline handler
184
+ * @deprecated ue {@link #putAllMarksPriorityMappings(Map)} instead
127
185
*/
186
+ @ Deprecated
128
187
public OutlineHandler putAllTagPriorityMappings (Map <String , Integer > mappings ) {
129
- tagPrioritiesMapping .putAll (mappings );
188
+ putAllMarksPriorityMappings (mappings );
189
+ return this ;
190
+ }
191
+
192
+ /**
193
+ * Put all marks into priority mappings.
194
+ *
195
+ * @param mappings the mappings
196
+ * @return the outline handler
197
+ */
198
+ public OutlineHandler putAllMarksPriorityMappings (Map <String , Integer > mappings ) {
199
+ markPrioritiesMapping .putAll (mappings );
130
200
return this ;
131
201
}
132
202
133
203
/**
134
- * Gets the tag priority mapping.
204
+ * Gets the marks from priority mapping.
135
205
*
136
206
* @param tagName the tag name
137
207
* @return the tag priority mapping
208
+ * @deprecated use {@link #getMarkPriorityMapping(String)} instead
138
209
*/
210
+ @ Deprecated
139
211
public Integer getTagPriorityMapping (String tagName ) {
140
- return tagPrioritiesMapping . get (tagName );
212
+ return getMarkPriorityMapping (tagName );
141
213
}
142
214
143
215
/**
144
- * Checks for tag priority mapping.
216
+ * Gets the mark from priority mapping.
217
+ *
218
+ * @param markName the mark name
219
+ * @return the tag priority mapping
220
+ */
221
+ public Integer getMarkPriorityMapping (String markName ) {
222
+ return markPrioritiesMapping .get (markName );
223
+ }
224
+
225
+ /**
226
+ * Checks for tag in priority mapping.
145
227
*
146
228
* @param tagName the tag name
147
229
* @return true, if the tag name is listed in the tag priorities mapping
230
+ * @deprecated use {@link #hasMarkPriorityMapping(String)} instead
148
231
*/
232
+ @ Deprecated
149
233
public boolean hasTagPriorityMapping (String tagName ) {
150
- return tagPrioritiesMapping .containsKey (tagName );
234
+ return hasMarkPriorityMapping (tagName );
235
+ }
236
+
237
+ /**
238
+ * Checks for tag in priority mapping.
239
+ *
240
+ * @param markName the mark name
241
+ * @return true, if the tag name is listed in the tag priorities mapping
242
+ */
243
+ public boolean hasMarkPriorityMapping (String markName ) {
244
+ if (markName == null ){
245
+ return false ;
246
+ } else {
247
+ return markPrioritiesMapping .containsKey (markName );
248
+ }
151
249
}
152
250
153
251
/**
@@ -209,10 +307,10 @@ protected String generateUniqueDestinationName(IElementNode element) {
209
307
* @return the unique destination name
210
308
*/
211
309
protected String generateOutlineName (IElementNode element ) {
212
- String tagName = element . name ( );
310
+ String markName = markExtractor . getMark ( element );
213
311
String content = ((JsoupElementNode ) element ).text ();
214
312
if (content .isEmpty ()) {
215
- content = getUniqueID (tagName );
313
+ content = getUniqueID (markName );
216
314
}
217
315
return content ;
218
316
}
@@ -228,10 +326,11 @@ protected String generateOutlineName(IElementNode element) {
228
326
* @param context the processor context
229
327
* @return the outline handler
230
328
*/
231
- OutlineHandler addOutlineAndDestToDocument (ITagWorker tagWorker , IElementNode element , ProcessorContext context ) {
232
- String tagName = element .name ();
233
- if (null != tagWorker && hasTagPriorityMapping (tagName ) && context .getPdfDocument () != null ) {
234
- int level = (int ) getTagPriorityMapping (tagName );
329
+ protected OutlineHandler addOutlineAndDestToDocument (ITagWorker tagWorker , IElementNode element ,
330
+ ProcessorContext context ) {
331
+ String markName = markExtractor .getMark (element );
332
+ if (null != tagWorker && hasMarkPriorityMapping (markName ) && context .getPdfDocument () != null ) {
333
+ int level = (int ) getMarkPriorityMapping (markName );
235
334
if (null == currentOutline ) {
236
335
currentOutline = context .getPdfDocument ().getOutlines (false );
237
336
}
@@ -262,16 +361,16 @@ OutlineHandler addOutlineAndDestToDocument(ITagWorker tagWorker, IElementNode el
262
361
* @param element the element
263
362
* @return the outline handler
264
363
*/
265
- OutlineHandler setDestinationToElement (ITagWorker tagWorker , IElementNode element ) {
266
- String tagName = element . name ( );
267
- if (null != tagWorker && hasTagPriorityMapping ( tagName ) && destinationsInProcess .size () > 0 ) {
364
+ protected OutlineHandler setDestinationToElement (ITagWorker tagWorker , IElementNode element ) {
365
+ String markName = markExtractor . getMark ( element );
366
+ if (null != tagWorker && hasMarkPriorityMapping ( markName ) && destinationsInProcess .size () > 0 ) {
268
367
Tuple2 <String , PdfDictionary > content = destinationsInProcess .pop ();
269
368
if (tagWorker .getElementResult () instanceof IElement ) {
270
369
tagWorker .getElementResult ().setProperty (Property .DESTINATION , content );
271
370
} else {
272
371
Logger logger = LoggerFactory .getLogger (OutlineHandler .class );
273
372
logger .warn (MessageFormatUtil .format (
274
- Html2PdfLogMessageConstant .NO_IPROPERTYCONTAINER_RESULT_FOR_THE_TAG , tagName ));
373
+ Html2PdfLogMessageConstant .NO_IPROPERTYCONTAINER_RESULT_FOR_THE_TAG , markName ));
275
374
}
276
375
}
277
376
return this ;
0 commit comments