@@ -23,6 +23,7 @@ This file is part of the iText (R) project.
2323package com .itextpdf .html2pdf .attach .impl ;
2424
2525import com .itextpdf .commons .datastructures .Tuple2 ;
26+ import com .itextpdf .html2pdf .attach .IOutlineMarkExtractor ;
2627import com .itextpdf .html2pdf .html .TagConstants ;
2728import com .itextpdf .html2pdf .logs .Html2PdfLogMessageConstant ;
2829import com .itextpdf .html2pdf .attach .ITagWorker ;
@@ -45,7 +46,8 @@ This file is part of the iText (R) project.
4546import java .util .Map ;
4647
4748/**
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}.
4951 * <p>
5052 * This class is not reusable and a new instance shall be created for every new conversion process.
5153 */
@@ -68,86 +70,182 @@ public class OutlineHandler {
6870 /**
6971 * The current outline.
7072 */
71- private PdfOutline currentOutline ;
73+ protected PdfOutline currentOutline ;
7274
7375 /**
7476 * The destinations in process.
7577 */
76- private Deque <Tuple2 <String , PdfDictionary >> destinationsInProcess = new LinkedList <Tuple2 <String , PdfDictionary >>();
78+ protected Deque <Tuple2 <String , PdfDictionary >> destinationsInProcess = new LinkedList <Tuple2 <String , PdfDictionary >>();
7779
7880 /**
7981 * The levels in process.
8082 */
81- private Deque <Integer > levelsInProcess = new LinkedList <Integer >();
83+ protected Deque <Integer > levelsInProcess = new LinkedList <Integer >();
8284
8385 /**
84- * The tag priorities mapping.
86+ * The mark priorities mapping.
8587 */
86- private Map <String , Integer > tagPrioritiesMapping = new HashMap <String , Integer >();
88+ private Map <String , Integer > markPrioritiesMapping = new HashMap <String , Integer >();
8789
8890 /**
8991 * The destination prefix.
9092 */
9193 private String destinationNamePrefix = DEFAULT_DESTINATION_NAME_PREFIX ;
9294
95+ /**
96+ *The mark extractor defines what part of element will be used to create outline
97+ */
98+ protected IOutlineMarkExtractor markExtractor ;
9399
94100 /**
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.
96108 *
97109 * @return the outline handler
98110 */
99111 public static OutlineHandler createStandardHandler () {
100112 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 ;
107131 return handler ;
108132 }
109133
110134 /**
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.
112155 *
113156 * @param tagName the tag name
114157 * @param priority the priority
115158 * @return the outline handler
159+ * @deprecated use {@link #putMarkPriorityMapping(String, Integer)} instead
116160 */
161+ @ Deprecated
117162 public OutlineHandler putTagPriorityMapping (String tagName , Integer priority ) {
118- tagPrioritiesMapping . put (tagName , priority );
163+ putMarkPriorityMapping (tagName , priority );
119164 return this ;
120165 }
121166
122167 /**
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.
124181 *
125182 * @param mappings the mappings
126183 * @return the outline handler
184+ * @deprecated ue {@link #putAllMarksPriorityMappings(Map)} instead
127185 */
186+ @ Deprecated
128187 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 );
130200 return this ;
131201 }
132202
133203 /**
134- * Gets the tag priority mapping.
204+ * Gets the marks from priority mapping.
135205 *
136206 * @param tagName the tag name
137207 * @return the tag priority mapping
208+ * @deprecated use {@link #getMarkPriorityMapping(String)} instead
138209 */
210+ @ Deprecated
139211 public Integer getTagPriorityMapping (String tagName ) {
140- return tagPrioritiesMapping . get (tagName );
212+ return getMarkPriorityMapping (tagName );
141213 }
142214
143215 /**
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.
145227 *
146228 * @param tagName the tag name
147229 * @return true, if the tag name is listed in the tag priorities mapping
230+ * @deprecated use {@link #hasMarkPriorityMapping(String)} instead
148231 */
232+ @ Deprecated
149233 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+ }
151249 }
152250
153251 /**
@@ -209,10 +307,10 @@ protected String generateUniqueDestinationName(IElementNode element) {
209307 * @return the unique destination name
210308 */
211309 protected String generateOutlineName (IElementNode element ) {
212- String tagName = element . name ( );
310+ String markName = markExtractor . getMark ( element );
213311 String content = ((JsoupElementNode ) element ).text ();
214312 if (content .isEmpty ()) {
215- content = getUniqueID (tagName );
313+ content = getUniqueID (markName );
216314 }
217315 return content ;
218316 }
@@ -228,10 +326,11 @@ protected String generateOutlineName(IElementNode element) {
228326 * @param context the processor context
229327 * @return the outline handler
230328 */
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 );
235334 if (null == currentOutline ) {
236335 currentOutline = context .getPdfDocument ().getOutlines (false );
237336 }
@@ -262,16 +361,16 @@ OutlineHandler addOutlineAndDestToDocument(ITagWorker tagWorker, IElementNode el
262361 * @param element the element
263362 * @return the outline handler
264363 */
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 ) {
268367 Tuple2 <String , PdfDictionary > content = destinationsInProcess .pop ();
269368 if (tagWorker .getElementResult () instanceof IElement ) {
270369 tagWorker .getElementResult ().setProperty (Property .DESTINATION , content );
271370 } else {
272371 Logger logger = LoggerFactory .getLogger (OutlineHandler .class );
273372 logger .warn (MessageFormatUtil .format (
274- Html2PdfLogMessageConstant .NO_IPROPERTYCONTAINER_RESULT_FOR_THE_TAG , tagName ));
373+ Html2PdfLogMessageConstant .NO_IPROPERTYCONTAINER_RESULT_FOR_THE_TAG , markName ));
275374 }
276375 }
277376 return this ;
0 commit comments