KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > dom > lazydom > PreFormatter


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: PreFormatter.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.dom.lazydom;
25
26 import org.enhydra.xml.io.DOMFormatter;
27 import org.enhydra.xml.io.Formatter;
28 import org.enhydra.xml.io.HTMLElements;
29 import org.enhydra.xml.io.OutputOptions;
30 import org.enhydra.xml.xmlc.codegen.JavaCode;
31 import org.enhydra.xml.xmlc.dom.XMLCDocument;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.Node JavaDoc;
34
35 /**
36  * Create pre-formatted text for inclusion in template DOM. Save's overhead of
37  * formatting the text. This adds character entity references for
38  * CharacterData nodes and formats the entire opening tag, with attributes,
39  * for elements. The default encoding is used to determine if this is an
40  * 7, 8, or 16 bit encoding, and substituting character references as
41  * needed.
42  */

43 class PreFormatter {
44     /** Formatter for this document. */
45     private Formatter fFormatter;
46
47     /** Is this an HTML document? */
48     private boolean fIsHtmlDocument;
49
50     /** Output options being used */
51     private OutputOptions fOptions;
52
53     /**
54      * Constructor.
55      */

56     public PreFormatter(XMLCDocument xmlcDoc) {
57         // Get default options and set encoding. This ensure that the
58
// encoding is available when the preformatting options are check at
59
// runtime to determine if the preformatted text should be used.
60

61         fOptions = DOMFormatter.getDefaultOutputOptions(xmlcDoc.getDocument());
62         String JavaDoc encoding = xmlcDoc.getEncoding();
63         if (encoding != null) {
64             fOptions.setEncoding(encoding);
65         }
66
67         fIsHtmlDocument = xmlcDoc.isHtmlDocument();
68         fFormatter = DOMFormatter.getFormatter(xmlcDoc.getDocument(), fOptions, true);
69     }
70
71     /**
72      * Check to see if this node is a descendent of a HTML script
73      * or style element. If so, no pre-formatting is done, as the
74      * text is written as-is.
75      */

76     private boolean isHtmlFormatProtected(Node JavaDoc node) {
77         if (!(node instanceof Element JavaDoc)) {
78             // Find parent element.
79
Node JavaDoc parent = node.getParentNode();
80             while ((parent != null) && (!(parent instanceof Element JavaDoc))) {
81                 parent = parent.getParentNode();
82             }
83             if ((parent != null) && HTMLElements.isScriptStyle((Element JavaDoc)parent)) {
84                 return true;
85             }
86         }
87         return false;
88     }
89
90     /**
91      * Get pre-formatted text for a node.
92      * @return The text, of null if none is approriate for this node
93      * type.
94      */

95     public String JavaDoc preFormatNode(Node JavaDoc node) {
96         if (fIsHtmlDocument && isHtmlFormatProtected(node)) {
97             return null;
98         } else {
99             return fFormatter.preFormatNode(node);
100         }
101     }
102
103     /**
104      * Get the encoding being used for the preformatted text.
105      */

106     public String JavaDoc getMIMEEncoding() {
107         return fFormatter.getMIMEEncoding();
108     }
109
110     /**
111      * Generate code to recreate the output options used to format this code.
112      */

113     public void createOutputOptionsCodeGenerator(String JavaDoc varName,
114                                                  JavaCode code) {
115         fOptions.createCodeGenerator(varName, true, code);
116     }
117 }
118
Popular Tags