1 /* ParserDelegator.java -- Delegator for ParserDocument.
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing.text.html.parser;
40 import gnu.javax.swing.text.html.parser.HTML_401F;
41 import gnu.javax.swing.text.html.parser.htmlAttributeSet;
43 import java.io.IOException;
44 import java.io.Reader;
45 import java.io.Serializable;
47 import javax.swing.text.BadLocationException;
48 import javax.swing.text.html.HTMLEditorKit;
49 import javax.swing.text.html.HTMLEditorKit.ParserCallback;
52 * This class instantiates and starts the working instance of
53 * html parser, being responsible for providing the default DTD.
55 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
57 public class ParserDelegator
58 extends javax.swing.text.html.HTMLEditorKit.Parser
59 implements Serializable
61 private class gnuParser
62 extends gnu.javax.swing.text.html.parser.support.Parser
64 private static final long serialVersionUID = 1;
66 private gnuParser(DTD d)
71 protected final void handleComment(char[] comment)
73 callBack.handleComment(comment, hTag.where.startPosition);
76 protected final void handleEmptyTag(TagElement tag)
77 throws javax.swing.text.ChangedCharSetException
79 callBack.handleSimpleTag(tag.getHTMLTag(), getAttributes(),
80 hTag.where.startPosition
84 protected final void handleEndTag(TagElement tag)
86 callBack.handleEndTag(tag.getHTMLTag(), hTag.where.startPosition);
89 protected final void handleError(int line, String message)
91 callBack.handleError(message, hTag.where.startPosition);
94 protected final void handleStartTag(TagElement tag)
96 htmlAttributeSet attributes = gnu.getAttributes();
99 attributes.addAttribute(ParserCallback.IMPLIED, Boolean.TRUE);
101 callBack.handleStartTag(tag.getHTMLTag(), attributes,
102 hTag.where.startPosition
106 protected final void handleText(char[] text)
108 callBack.handleText(text, hTag.where.startPosition);
113 // Accessing the inherited gnu.javax.swing.text.html.parser.support.Parser
114 // field. super. is a workaround, required to support JDK1.3's javac.
120 * Use serialVersionUID for interoperability.
122 private static final long serialVersionUID = -1276686502624777206L;
124 private static DTD dtd = HTML_401F.getInstance();
128 * This is package-private to avoid an accessor method.
130 HTMLEditorKit.ParserCallback callBack;
133 * The reference to the working class of HTML parser that is
134 * actually used to parse the document.
135 * This is package-private to avoid an accessor method.
140 * Parses the HTML document, calling methods of the provided
141 * callback. This method must be multithread - safe.
142 * @param reader The reader to read the HTML document from
143 * @param a_callback The callback that is notifyed about the presence
144 * of HTML elements in the document.
145 * @param ignoreCharSet If thrue, any charset changes during parsing
147 * @throws java.io.IOException
149 public void parse(Reader reader, HTMLEditorKit.ParserCallback a_callback,
150 boolean ignoreCharSet
154 callBack = a_callback;
156 if (gnu == null || !dtd.equals(gnu.getDTD()))
158 gnu = new gnuParser(dtd);
163 callBack.handleEndOfLineString(gnu.getEndOfLineSequence());
168 catch (BadLocationException ex)
170 // Convert this into the supported type of exception.
171 throw new IOException(ex.getMessage());
176 * Calling this method instructs that, if not specified directly,
177 * the documents will be parsed using the default
178 * DTD of the implementation.
180 protected static void setDefaultDTD()
182 dtd = HTML_401F.getInstance();
186 * Registers the user - written DTD under the given name, also
187 * making it default for the subsequent parsings. This has effect on
188 * all subsequent calls to the parse(...) . If you need to specify
189 * your DTD locally, simply {@link javax.swing.text.html.parser.Parser}
191 * @param a_dtd The DTD that will be used to parse documents by this class.
192 * @param name The name of this DTD.
193 * @return No standard is specified on which instance of DTD must be
194 * returned by this method, and it is recommended to leave the returned
195 * value without consideration. This implementation returns the DTD
196 * that was previously set as the default DTD, or the implementations
197 * default DTD if none was set.
199 protected static DTD createDTD(DTD a_dtd, String name)
201 DTD.putDTDHash(name, a_dtd);