KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > htmlconverter > CmsHtmlConverter


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/htmlconverter/CmsHtmlConverter.java,v $
3  * Date : $Date: 2006/03/27 14:53:04 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2001 The OpenCms Group
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about OpenCms, please see the
22  * OpenCms Website: http://www.opencms.org
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  */

28
29 package com.opencms.htmlconverter;
30
31 import org.opencms.i18n.CmsEncoder;
32
33 import java.io.BufferedInputStream JavaDoc;
34 import java.io.ByteArrayInputStream JavaDoc;
35 import java.io.ByteArrayOutputStream JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.io.PrintWriter JavaDoc;
41 import java.io.Reader JavaDoc;
42 import java.io.StringReader JavaDoc;
43 import java.io.StringWriter JavaDoc;
44 import java.io.UnsupportedEncodingException JavaDoc;
45 import java.io.Writer JavaDoc;
46 import java.net.URL JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.StringTokenizer JavaDoc;
49 import java.util.Vector JavaDoc;
50
51 import org.w3c.dom.Document JavaDoc;
52 import org.w3c.dom.NamedNodeMap JavaDoc;
53 import org.w3c.dom.Node JavaDoc;
54 import org.w3c.dom.NodeList JavaDoc;
55 import org.w3c.tidy.Tidy;
56
57 /**
58  * Implementation of interface I_CmsHtmlConverterInterface:
59  * Tool to check and transform HTML input code
60  * to user-defined output, for example JSP, JavaScript or other syntax.
61  * @author Andreas Zahner
62  * @version 1.0
63  *
64  * @deprecated Will not be supported past the OpenCms 6 release.
65  */

66 public final class CmsHtmlConverter implements I_CmsHtmlConverterInterface {
67
68     /** Filename for JTidy configuration. */
69     private String JavaDoc m_tidyConfFile = "";
70     /** Checks if JTidy is already defined. */
71     private boolean m_tidyConfFileDefined;
72     /** Filename for CmsHtmlConverter configuration. */
73     private String JavaDoc m_converterConfFile = "";
74     /** Checks if CmsHtmlConverter is configured from file. */
75     private boolean m_converterConfFileDefined;
76     /** Checks if CmsHtmlConverter is configured. */
77     private boolean m_converterConfigDefined;
78     /** stores number of predefined replaceTags. */
79     private int m_numberReplaceTags;
80     /** stores number of predefined replaceTags. */
81     private int m_numberReplaceBlocks;
82     /** temporary buffer used in transformation method. */
83     private StringBuffer JavaDoc m_tempString;
84     /** instance of JTidy. */
85     private Tidy m_tidy = new Tidy();
86     /** instance of CmsHtmlConverterTools. */
87     private CmsHtmlConverterTools m_tools = new CmsHtmlConverterTools();
88     /** configuration object for CmsHtmlConverter. */
89     private CmsHtmlConverterConfig m_configuration = new CmsHtmlConverterConfig();
90     /** object stores data for replacing tags. */
91     private CmsHtmlConverterObjectReplaceTags m_tagObject = new CmsHtmlConverterObjectReplaceTags();
92     /** object stores data for replacing blocks. */
93     private CmsHtmlConverterObjectReplaceBlocks m_blockObject = new CmsHtmlConverterObjectReplaceBlocks();
94     // replacestring for the modifyParameter methode. Used for the html editor replacement
95
private String JavaDoc m_servletPrefix;
96     // replace String for relative uris
97
private String JavaDoc m_relativeRoot;
98     //the url object for links that should not be replaced
99
private URL JavaDoc m_url;
100     /** Vector stores tag names, after the end-tag, a "\n" is added to the output. */
101     private Vector JavaDoc m_enterTags = new Vector JavaDoc();
102
103     /**
104      * Default constructor.<p>
105      */

106     public CmsHtmlConverter() {
107
108         m_tidy.setTidyMark(false);
109         m_tidy.setShowWarnings(false);
110         m_tidy.setQuiet(true);
111         initialiseTags();
112     }
113
114     /**
115      * Constructor with name of Tidy configuration file as parameter.<p>
116      *
117      * @param tidyConfFileName String with Tidy configuration file name
118      */

119     public CmsHtmlConverter(String JavaDoc tidyConfFileName) {
120
121         this.setTidyConfFile(tidyConfFileName);
122         initialiseTags();
123     }
124
125     /**
126      * Constructor with name of Tidy and Converter configuration files as parameters.<p>
127      *
128      * @param tidyConfFileName String with Tidy configuration file name
129      * @param confFile String with Converter configuration file name
130      */

131     public CmsHtmlConverter(String JavaDoc tidyConfFileName, String JavaDoc confFile) {
132
133         this.setTidyConfFile(tidyConfFileName);
134         this.setConverterConfFile(confFile);
135         initialiseTags();
136     }
137
138     /**
139      * Initialises Vector m_enterTags with tag names.<p>
140      */

141     private void initialiseTags() {
142
143         StringTokenizer JavaDoc T = new StringTokenizer JavaDoc(
144             "p,table,tr,td,body,head,script,pre,title,style,h1,h2,h3,h4,h5,h6,ul,ol,li",
145             ",");
146         while (T.hasMoreTokens()) {
147             m_enterTags.addElement(new String JavaDoc(T.nextToken()));
148         }
149     }
150
151     /**
152      * Sets the prefix and the relative root.<p>
153      *
154      * @param prefix the servletprefix
155      * @param relativeRoot the relative root
156      */

157     public void setServletPrefix(String JavaDoc prefix, String JavaDoc relativeRoot) {
158
159         m_servletPrefix = prefix;
160         m_relativeRoot = relativeRoot;
161     }
162
163     /**
164      * Sets the url.<p>
165      *
166      * @param orgUrl object
167      */

168     public void setOriginalUrl(URL JavaDoc orgUrl) {
169
170         m_url = orgUrl;
171     }
172
173     /**
174      * Configures JTidy from file.<p>
175      *
176      * @param fileName filename of JTidy configuration file
177      */

178     public void setTidyConfFile(String JavaDoc fileName) {
179
180         m_tidyConfFile = fileName;
181         m_tidyConfFileDefined = true;
182         m_tidy.setConfigurationFromFile(m_tidyConfFile);
183     }
184
185     /**
186      * If defined, returns JTidy configuration filename.<p>
187      *
188      * @return filename of JTidy configuration file
189      */

190     public String JavaDoc getTidyConfFile() {
191
192         if (m_tidyConfFileDefined) {
193             return m_tidyConfFile;
194         } else {
195             return "";
196         }
197     }
198
199     /**
200      * Checks whether JTidy is already configured or not.<p>
201      *
202      * @return true if JTidy configuration file is set, otherwise false
203      */

204     public boolean tidyConfigured() {
205
206         return m_tidyConfFileDefined;
207     }
208
209     /**
210      * Configures CmsHtmlConverter from file.<p>
211      *
212      * @param confFile filename of configuration file
213      */

214     public void setConverterConfFile(String JavaDoc confFile) {
215
216         try {
217             InputStream JavaDoc in = new FileInputStream JavaDoc(confFile);
218             m_configuration.init(in);
219         } catch (IOException JavaDoc e) {
220             System.err.println("Configuration error: Configuration file no found!");
221             return;
222         }
223         m_converterConfFileDefined = true;
224         m_converterConfigDefined = true;
225         m_numberReplaceTags = m_configuration.getReplaceTags().size();
226         m_numberReplaceBlocks = m_configuration.getReplaceBlocks().size();
227     }
228
229     /**
230      * Configures CmsHtmlConverter from string.<p>
231      *
232      * @param configuration string with CmsHtmlConverter configuration
233      */

234     public void setConverterConfString(String JavaDoc configuration) {
235
236         m_configuration.init(configuration);
237         m_converterConfFileDefined = false;
238         m_converterConfigDefined = true;
239         m_numberReplaceTags = m_configuration.getReplaceTags().size();
240         m_numberReplaceBlocks = m_configuration.getReplaceBlocks().size();
241     }
242
243     /**
244      * If defined, returns filename of CmsHtmlConverter configuration file.<p>
245      *
246      * @return filename of configuration file
247      */

248     public String JavaDoc getConverterConfFile() {
249
250         if (m_converterConfFileDefined) {
251             return m_converterConfFile;
252         } else {
253             return "";
254         }
255     }
256
257     /**
258      * Checks whether CmsHtmlConverter is already configured or not.<p>
259      *
260      * @return true if CmsHtmlConverter configuration is set, otherwise false
261      */

262     public boolean converterConfigured() {
263
264         return m_converterConfigDefined;
265     }
266
267     /**
268      * Checks if HTML code has errors.<p>
269      *
270      * @param inString String with HTML code
271      * @return true if errors were detected, otherwise false
272      */

273     public boolean hasErrors(String JavaDoc inString) {
274
275         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(inString.getBytes());
276         return this.hasErrors(in);
277     }
278
279     /**
280      * Checks if HTML code has errors.<p>
281      *
282      * @param input InputStream with HTML code
283      * @return true if errors were detected, otherwise false
284      */

285     public boolean hasErrors(InputStream JavaDoc input) {
286
287         /* initialise JTidy */
288         m_tidy.setOnlyErrors(true);
289         m_tidy.setShowWarnings(false);
290         m_tidy.setQuiet(true);
291         m_tidy.setErrout(null);
292         /* parse InputStream */
293         m_tidy.parse(input, null);
294         /* check number of errors */
295         return m_tidy.getParseErrors() != 0;
296     }
297
298     /**
299      * Returns number of found errors in last parsed html code.<p>
300      *
301      * @return int with number of errors
302      */

303     public int getNumberErrors() {
304
305         return m_tidy.getParseErrors();
306     }
307
308     /**
309      * Checks if HTML code has errors and lists errors.<p>
310      *
311      * @param inString String with HTML code
312      * @return String with detected errors
313      */

314     public String JavaDoc showErrors(String JavaDoc inString) {
315
316         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(inString.getBytes());
317         OutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
318         this.showErrors(in, out);
319         return out.toString();
320     }
321
322     /**
323      * Checks if HTML code has errors and lists errors.<p>
324      *
325      * @param input InputStream with HTML code
326      * @param output OutputStream with detected errors
327      */

328     public void showErrors(InputStream JavaDoc input, OutputStream JavaDoc output) {
329
330         /* initialise JTidy */
331         m_tidy.setOnlyErrors(true);
332         m_tidy.setQuiet(true);
333         m_tidy.setShowWarnings(false);
334         InputStream JavaDoc in = new BufferedInputStream JavaDoc(input);
335         PrintWriter JavaDoc errorLog = new PrintWriter JavaDoc(output);
336         m_tidy.setErrout(errorLog);
337         m_tidy.parse(in, null);
338         if (m_tidy.getParseErrors() == 0) {
339             errorLog.println("HTML code ok!\nNo errors detected.");
340         }
341         errorLog.close();
342     }
343
344     /**
345      * Transforms HTML code into user defined output.<p>
346      *
347      * @param inString String with HTML code
348      * @return String with transformed code
349      */

350     public String JavaDoc convertHTML(String JavaDoc inString) {
351
352         Reader JavaDoc in = new StringReader JavaDoc(inString);
353         Writer JavaDoc out = new StringWriter JavaDoc();
354         this.convertHTML(in, out);
355         return out.toString();
356     }
357
358     /**
359      * Transforms HTML code into user defined output.<p>
360      *
361      * @param input Reader with HTML code
362      * @param output Writer with transformed code
363      */

364     public void convertHTML(Reader JavaDoc input, Writer JavaDoc output) {
365
366         /* local variables */
367         StringBuffer JavaDoc htmlString = new StringBuffer JavaDoc();
368         Node JavaDoc node;
369         String JavaDoc outString = "";
370         /* initialise JTidy */
371         m_tidy.setShowWarnings(false);
372         m_tidy.setQuiet(true);
373         if (!m_tidyConfFileDefined) {
374             m_tidy.setOnlyErrors(false);
375             m_tidy.setTidyMark(false);
376         }
377         /* print errorlog in ByteArray */
378         PrintWriter JavaDoc errorLog = new PrintWriter JavaDoc(new ByteArrayOutputStream JavaDoc(), true);
379         m_tidy.setErrout(errorLog);
380         try {
381             /* write InputStream input in StringBuffer htmlString */
382             int c;
383             while ((c = input.read()) != -1) {
384                 htmlString.append((char)c);
385             }
386         } catch (IOException JavaDoc e) {
387             System.err.println("Conversion error: " + e.toString());
388             return;
389         }
390         outString = htmlString.toString();
391         /* first step: replace subStrings in htmlString run #1*/
392         outString = m_tools.scanContent(outString, m_configuration.getReplaceContent());
393         /* convert htmlString in InputStream for parseDOM */
394         InputStream JavaDoc in;
395         try {
396             in = new ByteArrayInputStream JavaDoc(outString.getBytes(CmsEncoder.ENCODING_UTF_8));
397             //m_tidy.setCharEncoding(org.w3c.tidy.Configuration.UTF8);
398
m_tidy.setOutputEncoding(CmsEncoder.ENCODING_UTF_8);
399             m_tidy.setInputEncoding(CmsEncoder.ENCODING_UTF_8);
400         } catch (UnsupportedEncodingException JavaDoc e) {
401             in = new ByteArrayInputStream JavaDoc(outString.getBytes());
402             //m_tidy.setCharEncoding(org.w3c.tidy.Configuration.LATIN1);
403
m_tidy.setOutputEncoding("LATIN1");
404             m_tidy.setInputEncoding("LATIN1");
405         }
406         node = m_tidy.parseDOM(in, null);
407         /* check if html code has errors */
408         if (m_tidy.getParseErrors() != 0) {
409             System.err.println("Conversion error: HTML code has errors!");
410         }
411         /* second step: create transformed output with printDocument from DOM */
412         this.printDocument(node);
413         /* third step: replace Strings run #2 */
414         outString = m_tools.scanString(m_tempString.toString(), m_configuration.getReplaceStrings());
415         outString = this.cleanOutput(outString);
416         try {
417             output.write(outString);
418             output.close();
419         } catch (IOException JavaDoc e) {
420             System.err.println("Conversion error: " + e.toString());
421             return;
422         }
423     }
424
425     /**
426      * Private method to parse DOM and create user defined output.<p>
427      *
428      * @param node Node of DOM from HTML code
429      */

430     private void printDocument(Node JavaDoc node) {
431
432         // if node is empty do nothing... (Recursion)
433
if (node == null) {
434             return;
435         }
436         // initialise local variables
437
int type = node.getNodeType();
438         int replaceTag = -1;
439         int replaceBlock = -1;
440         // detect node type
441
switch (type) {
442             case Node.DOCUMENT_NODE:
443                 // initialise m_tempString and add global prefix
444
m_tempString = new StringBuffer JavaDoc(m_configuration.getGlobalPrefix());
445                 this.printDocument(((Document JavaDoc)node).getDocumentElement());
446                 break;
447             case Node.ELEMENT_NODE:
448                 // analyse element node and transform it
449
replaceBlock = this.indexReplaceBlock(node);
450                 replaceTag = this.indexReplaceTag(node);
451                 // scan element node; if a block has to be removed or replaced,
452
// break and discard child nodes
453
if (this.transformStartElement(node, replaceBlock, replaceTag)) {
454                     break;
455                 }
456                 // test if node has children
457
NodeList JavaDoc children = node.getChildNodes();
458                 if (children != null) {
459                     int len = children.getLength();
460                     for (int i = 0; i < len; i++) {
461                         // recursively call printDocument with all child nodes
462
this.printDocument(children.item(i));
463                     }
464                 }
465                 break;
466             case Node.TEXT_NODE:
467                 // replace subStrings in text nodes
468
this.transformTextNode(node);
469                 break;
470             default:
471                 // TODO: check what to do if node type is unknown
472
break;
473         }
474         // end of recursion, add eventual endtags and suffixes
475
switch (type) {
476             case Node.ELEMENT_NODE:
477                 // analyse endtags and add them to output
478
this.transformEndElement(node, replaceBlock, replaceTag);
479                 break;
480             case Node.DOCUMENT_NODE:
481                 // add suffix to end of output
482
this.transformEndDocument();
483                 break;
484             default:
485                 // TODO: check what to do if node type is unknown
486
break;
487         }
488     }
489
490     /**
491      * Private method to transform element nodes and create start tags in output.<p>
492      *
493      * @param node actual element node
494      * @param replaceBlock index of object m_replaceBlocks
495      * @param replaceTag index of object m_replaceTags
496      * @return true if recursion has to be interrupted, otherwise false
497      */

498     private boolean transformStartElement(Node JavaDoc node, int replaceBlock, int replaceTag) {
499
500         String JavaDoc tempReplaceString, valueParam;
501         /* remove complete block, interrupt recursion in printDocument */
502         if (m_tools.checkTag(node.getNodeName(), m_configuration.getRemoveBlocks())) {
503             return true;
504         }
505         /* if tag has to be removed return, otherwise test other cases */
506         if (!m_tools.checkTag(node.getNodeName(), m_configuration.getRemoveTags())) {
507             /* test if a block has to be replaced */
508             if (replaceBlock != -1) {
509                 m_blockObject = (CmsHtmlConverterObjectReplaceBlocks)m_configuration.getReplaceBlocks().get(
510                     replaceBlock);
511                 /* if a parameter is used, get it from node attribute value,
512                  insert it into replaceString */

513                 tempReplaceString = m_blockObject.getReplaceString();
514                 if (!m_blockObject.getParameter().equals("")) {
515                     valueParam = m_tools.scanNodeAttrs(node, m_blockObject.getParameter());
516                     tempReplaceString = m_tools.replaceString(tempReplaceString, "$parameter$", valueParam);
517                 }
518                 m_tempString.append(tempReplaceString);
519                 /* remove temporary object from ArrayList replaceBlocks */
520                 if (replaceBlock > (m_numberReplaceBlocks - 1)) {
521                     m_configuration.removeObjectReplaceBlock(replaceBlock);
522                 }
523                 /* ignore child elements in block, interrupt recursion in printDocument */
524                 return true;
525             } else {
526                 /* test if actual element (start tag) has to be replaced */
527                 if (replaceTag != -1) {
528                     m_tagObject = (CmsHtmlConverterObjectReplaceTags)m_configuration.getReplaceTags().get(replaceTag);
529                     tempReplaceString = m_tagObject.getReplaceStartTag();
530                     /* if a parameter is used, get it from node attribute value,
531                      insert it into replaceString */

532                     if (!m_tagObject.getParameter().equals("")) {
533                         valueParam = m_tools.scanNodeAttrs(node, m_tagObject.getParameter());
534                         // HACK: only replace attribute value of parameter attribute!
535
if (m_tagObject.getReplaceParamAttr()) {
536                             if (!m_tools.shouldReplaceUrl(m_url, valueParam, m_servletPrefix)) {
537                                 tempReplaceString = "$parameter$";
538                             } else {
539                                 valueParam = m_tools.modifyParameter(m_url, valueParam, m_servletPrefix, m_relativeRoot);
540                             }
541                             tempReplaceString = m_tools.reconstructTag(
542                                 tempReplaceString,
543                                 node,
544                                 m_tagObject.getParameter(),
545                                 m_configuration.getQuotationmark());
546                         }
547                         tempReplaceString = m_tools.replaceString(tempReplaceString, "$parameter$", valueParam);
548                     }
549                     m_tempString.append(tempReplaceString);
550                 } else {
551                     /* no replacement needed: append original element to output */
552                     m_tempString.append("<");
553                     m_tempString.append(node.getNodeName());
554                     NamedNodeMap JavaDoc attrs = node.getAttributes();
555                     for (int i = attrs.getLength() - 1; i >= 0; i--) {
556                         m_tempString.append(" "
557                             + attrs.item(i).getNodeName()
558                             + "="
559                             + m_configuration.getQuotationmark());
560                         /* scan attribute values and replace subStrings */
561                         String JavaDoc helpString = attrs.item(i).getNodeValue();
562                         helpString = m_tools.scanString(helpString, m_configuration.getReplaceStrings());
563                         m_tempString.append(helpString + m_configuration.getQuotationmark());
564                     }
565                     if (m_configuration.getXhtmlOutput()
566                         && m_tools.checkTag(node.getNodeName(), m_configuration.getInlineTags())) {
567                         m_tempString.append("/");
568                     }
569                     m_tempString.append(">");
570                 }
571             } /* OPTION: Here I can add a "\n" after every starttag */
572         }
573         return false;
574     }
575
576     /**
577      * Private method to transform element nodes and create end tags in output.<p>
578      *
579      * @param node actual element node
580      * @param replaceBlock index of object m_replaceBlocks
581      * @param replaceTag index of object m_replaceTags
582      */

583     private void transformEndElement(Node JavaDoc node, int replaceBlock, int replaceTag) {
584
585         /* test if block has to be removed */
586         if (!m_tools.checkTag(node.getNodeName(), m_configuration.getRemoveBlocks())) {
587             /* test if tag has to be removed */
588             if (!m_tools.checkTag(node.getNodeName(), m_configuration.getRemoveTags())) {
589                 /* continue, if block is not replaced */
590                 if (replaceBlock == -1) {
591                     /* replace end tag and discard inline tags */
592                     if (replaceTag != -1) {
593                         m_tagObject = (CmsHtmlConverterObjectReplaceTags)m_configuration.getReplaceTags().get(
594                             replaceTag);
595                         if (!m_tagObject.getInline()) {
596                             String JavaDoc tempReplaceString = m_tagObject.getReplaceEndTag();
597                             /* if parameter is used, get it from node attribute value,
598                              insert it into replaceString */

599                             if (!m_tagObject.getParameter().equals("")) {
600                                 String JavaDoc valueParam = m_tools.scanNodeAttrs(node, m_tagObject.getParameter());
601                                 tempReplaceString = m_tools.replaceString(tempReplaceString, "$parameter$", valueParam);
602                             }
603                             m_tempString.append(tempReplaceString);
604                         }
605                         /* remove temporary object from ArrayList replaceTags */
606                         if (replaceTag > (m_numberReplaceTags - 1)) {
607                             m_configuration.removeObjectReplaceTag(replaceTag);
608                         }
609                     } else {
610                         /* catch inline tags and discard them */
611                         if (!m_tools.checkTag(node.getNodeName(), m_configuration.getInlineTags())) {
612                             m_tempString.append("</");
613                             m_tempString.append(node.getNodeName());
614                             m_tempString.append(">");
615                             // append a "\n" to output String
616
if (m_configuration.getGlobalAddEveryLine()) {
617                                 // check if a "\n" can be added to output
618
boolean added = false;
619                                 for (int i = 0; i < m_enterTags.size(); i++) {
620                                     if (!added && node.getNodeName().equalsIgnoreCase((String JavaDoc)m_enterTags.elementAt(i))) {
621                                         m_tempString.append(m_configuration.getGlobalSuffix()
622                                             + "\n"
623                                             + m_configuration.getGlobalPrefix());
624                                         added = true;
625                                     }
626                                     // if tag was found, return
627
if (added) {
628                                         return;
629                                     }
630                                 }
631                             } else {
632                                 // check if a "\n" can be added to output
633
boolean added = false;
634                                 for (int i = 0; i < m_enterTags.size(); i++) {
635                                     if (!added && node.getNodeName().equalsIgnoreCase((String JavaDoc)m_enterTags.elementAt(i))) {
636                                         m_tempString.append("\n");
637                                         added = true;
638                                     }
639                                     // if tag was found, return
640
if (added) {
641                                         return;
642                                     }
643                                 }
644                             }
645                         }
646                     }
647                 }
648             } // end of removetag
649
} // end of removeblock
650
}
651
652     /**
653      * Private method to transform output at end of document.<p>
654      */

655     private void transformEndDocument() {
656
657         m_tempString.append(m_configuration.getGlobalSuffix());
658     }
659
660     /**
661      * Private method to transform text nodes.<p>
662      *
663      * @param node actual text node
664      */

665     private void transformTextNode(Node JavaDoc node) {
666
667         String JavaDoc helpString = node.getNodeValue();
668         /* do not scan text nodes between <script> tags! */
669         if (!node.getParentNode().getNodeName().equalsIgnoreCase("script")
670             && !node.getParentNode().getNodeName().equalsIgnoreCase("style")) {
671             helpString = m_tools.scanChar(helpString, m_configuration.getReplaceExtendedChars());
672         }
673         /* replace quotationsmarks if configured */
674         if (m_configuration.getEncodeQuotationmarks()) {
675             helpString = m_tools.replaceString(helpString, "\"", m_configuration.getQuotationmark());
676         }
677         /* test if prefix and suffix have to be added every new line */
678         if (m_configuration.getGlobalAddEveryLine()) {
679             helpString = m_tools.replaceString(
680                 helpString,
681                 "\n",
682                 (m_configuration.getGlobalSuffix() + "\n" + m_configuration.getGlobalPrefix()));
683         }
684         m_tempString.append(helpString);
685     }
686
687     /**
688      * Private method to delete empty "prefix+suffix" lines in output String.<p>
689      *
690      * @param cleanString string for cleaning up
691      * @return the cleaned string
692      */

693     private String JavaDoc cleanOutput(String JavaDoc cleanString) {
694
695         if (m_configuration.getGlobalAddEveryLine()) {
696             cleanString += "\n";
697             String JavaDoc cutString = m_configuration.getGlobalPrefix() + m_configuration.getGlobalSuffix() + "\n";
698             /* delete empty "prefix-suffix" lines if suffix and prefix are not empty */
699             if (!m_configuration.getGlobalPrefix().equals("") && !m_configuration.getGlobalSuffix().equals("")) {
700                 cleanString = m_tools.replaceString(cleanString, cutString, "");
701             }
702         }
703         return cleanString;
704     }
705
706     /**
707      * Tests if specified tag has to be replaced and, if it is found,
708      * delivers index of hit in ArrayLis.<p>
709      *
710      * @param node DOM Node which might be replaced
711      * @return "-1" if tag is not found, otherwise index of list with hit
712      */

713     private int indexReplaceTag(Node JavaDoc node) {
714
715         ArrayList JavaDoc replaceTags = m_configuration.getReplaceTags();
716         NamedNodeMap JavaDoc attrs = node.getAttributes();
717         CmsHtmlConverterObjectReplaceTags testObject = new CmsHtmlConverterObjectReplaceTags();
718         for (int index = 0; index < replaceTags.size(); index++) {
719             testObject = (CmsHtmlConverterObjectReplaceTags)(replaceTags.get(index));
720             // cw 09.09.2003 added general qualifier *
721
if (node.getNodeName().equals(testObject.getTagName()) || "*".equals(testObject.getTagName())) {
722                 /* if no identifier attribute is defined, replace all nodes */
723                 if (testObject.getTagAttrib().equals("")) {
724                     /* test if replaceStrings have to be retrieved from attributes */
725                     if (testObject.getReplaceFromAttrs()) {
726                         return scanTagElementAttrs(node, testObject);
727                     }
728                     return index;
729                 }
730                 for (int i = attrs.getLength() - 1; i >= 0; i--) {
731                     if (attrs.item(i).getNodeName().equals(testObject.getTagAttrib())
732                         && (attrs.item(i).getNodeValue().equals(testObject.getTagAttribValue()) || testObject.getTagAttribValue().equals(
733                             ""))) {
734                         /* test if replaceStrings have to be retrieved from attributes */
735                         if (testObject.getReplaceFromAttrs()) {
736                             return scanTagElementAttrs(node, testObject);
737                         }
738                         return index;
739                     }
740                 }
741             }
742         }
743         return -1;
744     }
745
746     /**
747      * Scans node attributes and creates new CmsHtmlConverterObjectReplaceTags.<p>
748      *
749      * @param node DOM node which is scanned
750      * @param testObject parent replaceTag object
751      * @return index of new object in ArrayList replaceTags
752      */

753     private int scanTagElementAttrs(Node JavaDoc node, CmsHtmlConverterObjectReplaceTags testObject) {
754
755         NamedNodeMap JavaDoc attrs = node.getAttributes();
756         String JavaDoc prefix = testObject.getPrefix();
757         String JavaDoc suffix = testObject.getSuffix();
758         String JavaDoc name = testObject.getTagName();
759         String JavaDoc attrib = testObject.getTagAttrib();
760         String JavaDoc attrValue = testObject.getTagAttribValue();
761         String JavaDoc startAttribute = testObject.getStartAttribute();
762         String JavaDoc endAttribute = testObject.getEndAttribute();
763         String JavaDoc replaceStartTag = "";
764         String JavaDoc replaceEndTag = "";
765         String JavaDoc parameter = testObject.getParameter();
766         String JavaDoc attrName = "";
767         boolean replaceParamAttr = testObject.getReplaceParamAttr();
768         /* scan attributes for replaceStrings */
769         for (int i = 0; i < attrs.getLength(); i++) {
770             attrName = attrs.item(i).getNodeName();
771             if (attrName.equalsIgnoreCase(startAttribute)) {
772                 replaceStartTag = attrs.item(i).getNodeValue();
773             }
774             if (attrName.equalsIgnoreCase(endAttribute)) {
775                 replaceEndTag = attrs.item(i).getNodeValue();
776             }
777         }
778         /* replace encoded brackets if defined */
779         if (m_configuration.getUseBrackets()) {
780             replaceStartTag = m_configuration.scanBrackets(replaceStartTag);
781             replaceEndTag = m_configuration.scanBrackets(replaceEndTag);
782         }
783         /* add temporary object to ArrayList replaceTags */
784         m_configuration.addObjectReplaceTag(
785             prefix,
786             name,
787             attrib,
788             attrValue,
789             replaceStartTag,
790             replaceEndTag,
791             suffix,
792             false,
793             "",
794             "",
795             parameter,
796             replaceParamAttr);
797         return m_configuration.getReplaceTags().size() - 1;
798     }
799
800     /**
801      * Tests if specified block has to be replaced and, if it is found,
802      * delivers index of hit in ArrayList.<p>
803      *
804      * @param node DOM Node which might be replaced
805      * @return "-1" if tag is not found, otherwise index of list with hit
806      */

807     private int indexReplaceBlock(Node JavaDoc node) {
808
809         ArrayList JavaDoc replaceBlocks = m_configuration.getReplaceBlocks();
810         NamedNodeMap JavaDoc attrs = node.getAttributes();
811         CmsHtmlConverterObjectReplaceBlocks testObject = new CmsHtmlConverterObjectReplaceBlocks();
812         for (int index = 0; index < replaceBlocks.size(); index++) {
813             testObject = (CmsHtmlConverterObjectReplaceBlocks)(replaceBlocks.get(index));
814             // cw 09.09.2003 added general qualifier *
815
if (node.getNodeName().equals(testObject.getTagName()) || "*".equals(testObject.getTagName())) {
816                 if (testObject.getTagAttrib().equals("")) {
817                     /* test if replaceStrings has to be retrieved from attributes */
818                     if (testObject.getReplaceFromAttrs()) {
819                         return scanBlockElementAttrs(node, testObject);
820                     }
821                     return index;
822                 }
823                 for (int i = (attrs.getLength() - 1); i >= 0; i--) {
824                     if (attrs.item(i).getNodeName().equals(testObject.getTagAttrib())
825                         && (attrs.item(i).getNodeValue().equals(testObject.getTagAttribValue()) || testObject.getTagAttribValue().equals(
826                             ""))) {
827                         /* test if replaceString has to be retrieved from attributes */
828                         if (testObject.getReplaceFromAttrs()) {
829                             return scanBlockElementAttrs(node, testObject);
830                         }
831                         return index;
832                     }
833                 }
834             }
835         }
836         return -1;
837     }
838
839     /**
840      * Scans node attributes and creates new CmsHtmlConverterObjectReplaceBlocks.<p>
841      *
842      * @param node DOM node which is scanned
843      * @param testObject parent replaceBlock object
844      * @return index of new object in ArrayList replaceBlocks
845      */

846     private int scanBlockElementAttrs(Node JavaDoc node, CmsHtmlConverterObjectReplaceBlocks testObject) {
847
848         NamedNodeMap JavaDoc attrs = node.getAttributes();
849         String JavaDoc prefix = testObject.getPrefix();
850         String JavaDoc suffix = testObject.getSuffix();
851         String JavaDoc name = testObject.getTagName();
852         String JavaDoc attrib = testObject.getTagAttrib();
853         String JavaDoc attrValue = testObject.getTagAttribValue();
854         String JavaDoc replaceString = "";
855         String JavaDoc replaceAttribute = testObject.getReplaceAttribute();
856         String JavaDoc attrName = "";
857         String JavaDoc parameter = testObject.getParameter();
858         /* scan attributes for replaceString */
859         for (int i = 0; i < attrs.getLength(); i++) {
860             attrName = attrs.item(i).getNodeName();
861             if (attrName.equalsIgnoreCase(replaceAttribute)) {
862                 replaceString = attrs.item(i).getNodeValue();
863             }
864         }
865         /* replace encoded brackets if defined */
866         if (m_configuration.getUseBrackets()) {
867             replaceString = m_configuration.scanBrackets(replaceString);
868         }
869         /* add temporary object to ArrayList replaceBlocks */
870         m_configuration.addObjectReplaceBlock(
871             prefix,
872             name,
873             attrib,
874             attrValue,
875             replaceString,
876             suffix,
877             false,
878             "",
879             parameter);
880         return m_configuration.getReplaceBlocks().size() - 1;
881     }
882 }
Popular Tags