001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xml;
030:
031: import com.caucho.util.FreeList;
032:
033: import org.w3c.dom.DOMImplementation;
034:
035: /**
036: * XML parser interface. The parser can parse directly into the DOM or it
037: * can be used as a SAX parser.
038: *
039: * <p>To parse a file into a DOM Document use
040: * <pre><code>
041: * Document doc = new Xml().parseDocument("foo.xml");
042: * </code></pre>
043: *
044: * <p>To parse a string into a DOM Document use
045: * <pre><code>
046: * String xml = "<top>small test</top>";
047: * Document doc = new Xml().parseDocumentString(xml);
048: * </code></pre>
049: *
050: * <p>To parse a file using the SAX API use
051: * <pre><code>
052: * Xml xml = new Xml();
053: * xml.setContentHandler(myContentHandler);
054: * xml.parse("foo.xml");
055: * </code></pre>
056: */
057: public class Xml extends XmlParser {
058: private static FreeList<Xml> _freeList = new FreeList<Xml>(16);
059:
060: /**
061: * Create a new strict XML parser
062: */
063: public Xml() {
064: super (new XmlPolicy(), null);
065:
066: init();
067: }
068:
069: /**
070: * Initialize the parser.
071: */
072: public void init() {
073: super .init();
074:
075: _strictComments = true;
076: _strictAttributes = true;
077: _strictCharacters = true;
078: _strictXml = true;
079: _singleTopElement = true;
080: _optionalTags = false;
081: }
082:
083: /**
084: * Creates an Xml parser.
085: */
086: public static Xml create() {
087: Xml xml = _freeList.allocate();
088: if (xml == null)
089: xml = new Xml();
090: xml.init();
091:
092: return xml;
093: }
094:
095: /**
096: * Frees an Xml parser.
097: */
098: public void free() {
099: _owner = null;
100: _contentHandler = null;
101: _entityResolver = null;
102: _dtdHandler = null;
103: _errorHandler = null;
104: _dtd = null;
105:
106: _freeList.free(this );
107: }
108:
109: /**
110: * Create a new DOM document
111: */
112: static public CauchoDocument createDocument() {
113: return new QDocument();
114: }
115:
116: /**
117: * Create a new DOM implementation
118: */
119: static public DOMImplementation createDOMImplementation() {
120: return new QDOMImplementation();
121: }
122: }
|