001    /* ParserDelegator.java -- Delegator for ParserDocument.
002        Copyright (C) 2005 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    package javax.swing.text.html.parser;
039    
040    import gnu.javax.swing.text.html.parser.HTML_401F;
041    
042    import java.io.IOException;
043    import java.io.Reader;
044    import java.io.Serializable;
045    
046    import javax.swing.text.BadLocationException;
047    import javax.swing.text.SimpleAttributeSet;
048    import javax.swing.text.html.HTMLEditorKit;
049    import javax.swing.text.html.HTMLEditorKit.ParserCallback;
050    
051    /**
052     * This class instantiates and starts the working instance of
053     * html parser, being responsible for providing the default DTD.
054     *
055     * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
056     */
057    public class ParserDelegator
058      extends javax.swing.text.html.HTMLEditorKit.Parser
059      implements Serializable
060    {
061      private class gnuParser
062        extends gnu.javax.swing.text.html.parser.support.Parser
063      {
064        private static final long serialVersionUID = 1;
065    
066        private gnuParser(DTD d)
067        {
068          super(d);
069        }
070    
071        protected final void handleComment(char[] comment)
072        {
073          callBack.handleComment(comment, hTag.where.startPosition);
074        }
075    
076        protected final void handleEmptyTag(TagElement tag)
077          throws javax.swing.text.ChangedCharSetException
078        {
079          callBack.handleSimpleTag(tag.getHTMLTag(), getAttributes(),
080                                   hTag.where.startPosition
081                                  );
082        }
083    
084        protected final void handleEndTag(TagElement tag)
085        {
086          callBack.handleEndTag(tag.getHTMLTag(), hTag.where.startPosition);
087        }
088    
089        protected final void handleError(int line, String message)
090        {
091          callBack.handleError(message, hTag.where.startPosition);
092        }
093    
094        protected final void handleStartTag(TagElement tag)
095        {
096          SimpleAttributeSet attributes = gnu.getAttributes();
097    
098          if (tag.fictional())
099            attributes.addAttribute(ParserCallback.IMPLIED, Boolean.TRUE);
100    
101          callBack.handleStartTag(tag.getHTMLTag(), attributes,
102                                  hTag.where.startPosition
103                                 );
104        }
105    
106        protected final void handleText(char[] text)
107        {
108          callBack.handleText(text, hTag.where.startPosition);
109        }
110    
111        DTD getDTD()
112        {
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.
115          return super.dtd;
116        }
117      }
118    
119      /**
120       * Use serialVersionUID for interoperability.
121       */
122      private static final long serialVersionUID = -1276686502624777206L;
123    
124      private static DTD dtd = HTML_401F.getInstance();
125    
126      /**
127       * The callback.
128       * This is package-private to avoid an accessor method.
129       */
130      HTMLEditorKit.ParserCallback callBack;
131    
132      /**
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.
136       */
137      gnuParser gnu;
138    
139      /**
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
146       * are ignored.
147       * @throws java.io.IOException
148       */
149      public void parse(Reader reader, HTMLEditorKit.ParserCallback a_callback,
150                        boolean ignoreCharSet
151                       )
152                 throws IOException
153      {
154        callBack = a_callback;
155    
156        if (gnu == null || !dtd.equals(gnu.getDTD()))
157          {
158            gnu = new gnuParser(dtd);
159          }
160    
161        gnu.parse(reader);
162    
163        callBack.handleEndOfLineString(gnu.getEndOfLineSequence());
164        try
165          {
166            callBack.flush();
167          }
168        catch (BadLocationException ex)
169          {
170            // Convert this into the supported type of exception.
171            throw new IOException(ex.getMessage());
172          }
173      }
174    
175      /**
176       * Calling this method instructs that, if not specified directly,
177       * the documents will be parsed using the default
178       * DTD of the implementation.
179       */
180      protected static void setDefaultDTD()
181      {
182        dtd = HTML_401F.getInstance();
183      }
184    
185      /**
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}
190       * instead.
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.
198       */
199      protected static DTD createDTD(DTD a_dtd, String name)
200      {
201        DTD.putDTDHash(name, a_dtd);
202    
203        DTD dtd_prev = dtd;
204        dtd = a_dtd;
205        return dtd_prev;
206      }
207    }