OSDN Git Service

Imported Classpath 0.18.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / swing / text / html / parser / ParserDelegator.java
1 /* ParserDelegator.java -- Delegator for ParserDocument.
2     Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10
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.
15
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
19 02110-1301 USA.
20
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
24 combination.
25
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. */
37
38 package javax.swing.text.html.parser;
39
40 import gnu.javax.swing.text.html.parser.HTML_401F;
41 import gnu.javax.swing.text.html.parser.htmlAttributeSet;
42
43 import java.io.IOException;
44 import java.io.Reader;
45 import java.io.Serializable;
46
47 import javax.swing.text.BadLocationException;
48 import javax.swing.text.html.HTMLEditorKit;
49 import javax.swing.text.html.HTMLEditorKit.ParserCallback;
50
51 /**
52  * This class instantiates and starts the working instance of
53  * html parser, being responsible for providing the default DTD.
54  *
55  * TODO Later this class must be derived from the totally abstract class
56  * HTMLEditorKit.Parser. HTMLEditorKit that does not yet exist.
57  *
58  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
59  */
60 public class ParserDelegator
61   extends javax.swing.text.html.HTMLEditorKit.Parser
62   implements Serializable
63 {
64   private class gnuParser
65     extends gnu.javax.swing.text.html.parser.support.Parser
66   {
67     private static final long serialVersionUID = 1;
68
69     private gnuParser(DTD d)
70     {
71       super(d);
72     }
73
74     protected final void handleComment(char[] comment)
75     {
76       callBack.handleComment(comment, hTag.where.startPosition);
77     }
78
79     protected final void handleEmptyTag(TagElement tag)
80       throws javax.swing.text.ChangedCharSetException
81     {
82       callBack.handleSimpleTag(tag.getHTMLTag(), getAttributes(),
83                                hTag.where.startPosition
84                               );
85     }
86
87     protected final void handleEndTag(TagElement tag)
88     {
89       callBack.handleEndTag(tag.getHTMLTag(), hTag.where.startPosition);
90     }
91
92     protected final void handleError(int line, String message)
93     {
94       callBack.handleError(message, hTag.where.startPosition);
95     }
96
97     protected final void handleStartTag(TagElement tag)
98     {
99       htmlAttributeSet attributes = gnu.getAttributes();
100
101       if (tag.fictional())
102         attributes.addAttribute(ParserCallback.IMPLIED, Boolean.TRUE);
103
104       callBack.handleStartTag(tag.getHTMLTag(), attributes,
105                               hTag.where.startPosition
106                              );
107     }
108
109     protected final void handleText(char[] text)
110     {
111       callBack.handleText(text, hTag.where.startPosition);
112     }
113
114     DTD getDTD()
115     {
116       // Accessing the inherited gnu.javax.swing.text.html.parser.support.Parser
117       // field. super. is a workaround, required to support JDK1.3's javac.
118       return super.dtd;
119     }
120   }
121
122   /**
123    * Use serialVersionUID for interoperability.
124    */
125   private static final long serialVersionUID = -1276686502624777206L;
126
127   private static DTD dtd = HTML_401F.getInstance();
128
129   /**
130    * The callback.
131    * This is package-private to avoid an accessor method.
132    */
133   HTMLEditorKit.ParserCallback callBack;
134
135   /**
136    * The reference to the working class of HTML parser that is
137    * actually used to parse the document.
138    * This is package-private to avoid an accessor method.
139    */
140   gnuParser gnu;
141
142   /**
143    * Parses the HTML document, calling methods of the provided
144    * callback. This method must be multithread - safe.
145    * @param reader The reader to read the HTML document from
146    * @param a_callback The callback that is notifyed about the presence
147    * of HTML elements in the document.
148    * @param ignoreCharSet If thrue, any charset changes during parsing
149    * are ignored.
150    * @throws java.io.IOException
151    */
152   public void parse(Reader reader, HTMLEditorKit.ParserCallback a_callback,
153                     boolean ignoreCharSet
154                    )
155              throws IOException
156   {
157     callBack = a_callback;
158
159     if (gnu == null || !dtd.equals(gnu.getDTD()))
160       {
161         gnu = new gnuParser(dtd);
162       }
163
164     gnu.parse(reader);
165
166     callBack.handleEndOfLineString(gnu.getEndOfLineSequence());
167     try
168       {
169         callBack.flush();
170       }
171     catch (BadLocationException ex)
172       {
173         // Convert this into the supported type of exception.
174         throw new IOException(ex.getMessage());
175       }
176   }
177
178   /**
179    * Calling this method instructs that, if not specified directly,
180    * the documents will be parsed using the default
181    * DTD of the implementation.
182    */
183   protected static void setDefaultDTD()
184   {
185     dtd = HTML_401F.getInstance();
186   }
187
188   /**
189    * Registers the user - written DTD under the given name, also
190    * making it default for the subsequent parsings. This has effect on
191    * all subsequent calls to the parse(...) . If you need to specify
192    * your DTD locally, simply {@link javax.swing.text.html.parser.Parser}
193    * instead.
194    * @param a_dtd The DTD that will be used to parse documents by this class.
195    * @param name The name of this DTD.
196    * @return No standard is specified on which instance of DTD must be
197    * returned by this method, and it is recommended to leave the returned
198    * value without consideration. This implementation returns the DTD
199    * that was previously set as the default DTD, or the implementations
200    * default DTD if none was set.
201    */
202   protected static DTD createDTD(DTD a_dtd, String name)
203   {
204     DTD.putDTDHash(name, a_dtd);
205
206     DTD dtd_prev = dtd;
207     dtd = a_dtd;
208     return dtd_prev;
209   }
210 }