001package com.hfg.xml.parser;
002
003import org.xml.sax.InputSource;
004import org.xml.sax.XMLReader;
005import org.xml.sax.SAXException;
006import org.xml.sax.helpers.XMLReaderFactory;
007
008import com.hfg.xml.XMLComment;
009import com.hfg.xml.XMLException;
010import com.hfg.xml.XMLTag;
011import com.hfg.xml.Doctype;
012
013import java.io.IOException;
014import java.io.Reader;
015import java.util.Collection;
016import java.util.List;
017
018//------------------------------------------------------------------------------
019/**
020 Wrapper class for doing SAX parsing. The default parser is SaxyParser.
021
022 @author J. Alex Taylor, hairyfatguy.com
023 */
024//------------------------------------------------------------------------------
025// com.hfg XML/HTML Coding Library
026//
027// This library is free software; you can redistribute it and/or
028// modify it under the terms of the GNU Lesser General Public
029// License as published by the Free Software Foundation; either
030// version 2.1 of the License, or (at your option) any later version.
031//
032// This library is distributed in the hope that it will be useful,
033// but WITHOUT ANY WARRANTY; without even the implied warranty of
034// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
035// Lesser General Public License for more details.
036//
037// You should have received a copy of the GNU Lesser General Public
038// License along with this library; if not, write to the Free Software
039// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
040//
041// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
042// jataylor@hairyfatguy.com
043//------------------------------------------------------------------------------
044
045public class XMLTagReader
046{
047   private Doctype mDoctype;
048   private XMLTag  mRootNode;
049   private List<XMLComment> mTopLevelComments;
050
051   private XMLTagDocHandler mDocHandler;
052   private XMLReader mParser;
053//   private static String     sParserClass = "org.apache.xerces.parsers.SAXParser";
054
055   //----------------------------------------------------------------------
056   public XMLTag getRootNode()
057   {
058      return mRootNode;
059   }
060
061   //----------------------------------------------------------------------
062   public Doctype getDoctype()
063   {
064      return mDoctype;
065   }
066
067   //---------------------------------------------------------------------------
068   public List<XMLComment> getTopLevelComments()
069   {
070      return mTopLevelComments;
071   }
072
073   //----------------------------------------------------------------------
074   public synchronized void parse(Reader inXML)
075         throws XMLException, IOException
076   {
077      parse(new InputSource(inXML));
078   }
079
080   //----------------------------------------------------------------------
081   public synchronized void parse(InputSource inXML)
082         throws XMLException, IOException
083   {
084      try
085      {
086         XMLReader parser = getParser();
087         parser.setContentHandler(getDocHandler());
088         parser.setProperty("http://xml.org/sax/properties/lexical-handler", getDocHandler());
089         parser.parse(inXML);
090
091         mRootNode = getDocHandler().getRootTag();
092
093         if (parser instanceof SaxyParser)
094         {
095            mDoctype = ((SaxyParser)parser).getDoctype();
096         }
097
098         mTopLevelComments = getDocHandler().getTopLevelComments();
099      }
100      catch (IOException e)
101      {
102         throw e;
103      }
104      catch (Exception e)
105      {
106         throw new XMLException(e);
107      }
108   }
109
110   //--------------------------------------------------------------------------
111   private XMLTagDocHandler getDocHandler()
112   {
113      if (null == mDocHandler)
114      {
115         mDocHandler = new XMLTagDocHandler();
116      }
117
118      return mDocHandler;
119   }
120
121   //--------------------------------------------------------------------------
122   public XMLReader getParser()
123   {
124      if (null == mParser)
125      {
126         // If an XML parser was specified by the 'org.xml.sax.driver' system
127         // property then use it.
128         // I used to do this without the enclosing if-block but by doing it this
129         // way I can avoid the problems that Xerces seems to have with escaped ISO-control
130         // characters and we aren't dependant on Xerces.
131         if (System.getProperty("org.xml.sax.driver") != null)
132         {
133            try
134            {
135               mParser = XMLReaderFactory.createXMLReader();
136            }
137            catch (SAXException e)
138            {
139            }
140         }
141
142         if (null == mParser)
143         {
144            // Otherwise, use the default parser.
145            try
146            {
147               mParser = new SaxyParser();
148            }
149            catch (Exception e)
150            {
151               throw new XMLException(e);
152            }
153         }
154      }
155
156      return mParser;
157   }
158
159}