KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > html > parsers > swing > ParserAdaptor


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: ParserAdaptor.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.html.parsers.swing;
25
26 import java.io.IOException JavaDoc;
27 import java.io.Reader JavaDoc;
28
29 import javax.swing.text.ChangedCharSetException JavaDoc;
30 import javax.swing.text.SimpleAttributeSet JavaDoc;
31 import javax.swing.text.html.HTMLEditorKit JavaDoc;
32 import javax.swing.text.html.parser.DTD JavaDoc;
33 import javax.swing.text.html.parser.DocumentParser JavaDoc;
34 import javax.swing.text.html.parser.Element JavaDoc;
35 import javax.swing.text.html.parser.ParserDelegator JavaDoc;
36 import javax.swing.text.html.parser.TagElement JavaDoc;
37
38 import org.enhydra.xml.xmlc.XMLCError;
39
40 /**
41  * Wrapper around the Swing HTML parser that attempts to correct for
42  * various problems in different versions of the Swing. This code
43  * is tightly bound to the Swing code.
44  * <P>
45  * This corrects the following problems
46  * <UL>
47  * <LI> Swing 1.1:
48  * <UL>
49  * <LI>LINK elements were not allowed in the HEAD.
50  * </UL>
51  * </UL>
52  */

53 class ParserAdaptor {
54     /** Swing version constants, can be compared numerically */
55     public static final int SWING_1_1 = 11;
56     public static final int SWING_1_2 = 12;
57     public static final int SWING_1_3 = 13;
58
59     /** Version of Swing being used */
60     private static int swingVersion;
61
62     /**
63      * Magic tag name that is generate as a simple tag at the end of the parse
64      * in Swing 1.2. This is a little string, don't know why they did this
65      * (or why is called `end of line'.
66      */

67     public static final String JavaDoc MAGIC_END_TAG = "__EndOfLineTag__";
68
69     /**
70      * Name of attribute added to HEAD tag when an empty one is created
71      * by the parser.
72      */

73     public static final String JavaDoc IMPLIED_PSEUDO_ATTR = "_implied_";
74
75     /**
76      * Class initializer.
77      */

78     static {
79         // Try to determine Swing version
80
String JavaDoc specVersion = System.getProperty("java.specification.version");
81         if ((specVersion == null) || specVersion.startsWith("1.1")) {
82             swingVersion = SWING_1_1;
83         } else if (specVersion.startsWith("1.2")) {
84             swingVersion = SWING_1_2;
85         } else {
86             // Assume 1.3 or better
87
swingVersion = SWING_1_3;
88         }
89     }
90
91     /*
92      * Class based on ParserDelegator that gets the DTD object.
93      */

94     private static class DTDCreator extends ParserDelegator JavaDoc {
95         /** DTD name used by swing (through at least 1.3) */
96         private static String JavaDoc DTD_NAME = "html32";
97         
98         /** The DTD we have obtained */
99         private static DTD JavaDoc dtd = null;
100
101         /**
102          * Build the DTD on first access.
103          */

104         private static synchronized void buildDTD() throws IOException JavaDoc {
105             DTD JavaDoc newDTD = DTD.getDTD(DTD_NAME);
106             newDTD = createDTD(newDTD, DTD_NAME);
107
108             // Set once initialized.
109
dtd = newDTD;
110         }
111
112         /**
113          * The DTD.
114          */

115         public static DTD JavaDoc getDTD() throws IOException JavaDoc {
116             if (dtd == null) {
117                 buildDTD();
118             }
119             return dtd;
120         }
121     }
122
123     /**
124      * Base for all DocumentParser adaptors.
125      */

126     class DocumentParserBase extends DocumentParser JavaDoc {
127         /** saved callback pointer */
128         protected HTMLEditorKit.ParserCallback JavaDoc fCallback;
129         
130         /**
131          * Constructor.
132          */

133         public DocumentParserBase(DTD JavaDoc dtd) {
134             super(dtd);
135         }
136
137         /**
138          * Override parse to get callback pointer.
139          */

140         public void parse(Reader JavaDoc in,
141                           HTMLEditorKit.ParserCallback JavaDoc callback,
142                           boolean ignoreCharSet) throws IOException JavaDoc {
143             fCallback = callback;
144             super.parse(in, callback, ignoreCharSet);
145         }
146     }
147
148     /**
149      * DocumentParser for 1.1 that allows LINKs in headers.
150      */

151     class DocumentParser11 extends DocumentParserBase {
152         /**
153          * Constructor.
154          */

155         public DocumentParser11(DTD JavaDoc dtd) {
156             super(dtd);
157         }
158         
159         /**
160          * override handleEmptyTag to allow links in headers.
161          */

162         protected void handleEmptyTag(TagElement JavaDoc tag) throws ChangedCharSetException JavaDoc {
163             Element JavaDoc elem = tag.getElement();
164             if (elem.name.equalsIgnoreCase("link")) {
165                 if (tag.fictional()) {
166                     fCallback.handleSimpleTag(tag.getHTMLTag(), new SimpleAttributeSet JavaDoc(), getCurrentPos());
167                 } else {
168                     fCallback.handleSimpleTag(tag.getHTMLTag(), getAttributes(), getCurrentPos());
169                     flushAttributes();
170                 }
171             } else {
172                 super.handleEmptyTag(tag);
173             }
174         }
175     }
176
177     /**
178      * Get the guessed version of swing being used.
179      */

180     public int getSwingVersion() {
181         return swingVersion;
182     }
183
184     /**
185      * Create a parser base on the version of swing we are linked to.
186      */

187     public DocumentParser JavaDoc getParser() {
188         try {
189             DTD JavaDoc dtd = DTDCreator.getDTD();
190             if (swingVersion == SWING_1_1) {
191                 return new DocumentParser11(dtd);
192             } else if (swingVersion == SWING_1_2) {
193                 return new DocumentParser JavaDoc(dtd);
194             } else {
195                 return new DocumentParser JavaDoc(dtd);
196             }
197         } catch (Exception JavaDoc except) {
198             throw new XMLCError("Can't created Swing HTML parser", except);
199         }
200     }
201 }
202
Popular Tags