KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > Xml


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml;
30
31 import com.caucho.util.FreeList;
32
33 import org.w3c.dom.DOMImplementation JavaDoc;
34
35 /**
36  * XML parser interface. The parser can parse directly into the DOM or it
37  * can be used as a SAX parser.
38  *
39  * <p>To parse a file into a DOM Document use
40  * <pre><code>
41  * Document doc = new Xml().parseDocument("foo.xml");
42  * </code></pre>
43  *
44  * <p>To parse a string into a DOM Document use
45  * <pre><code>
46  * String xml = "&lt;top>small test&lt;/top>";
47  * Document doc = new Xml().parseDocumentString(xml);
48  * </code></pre>
49  *
50  * <p>To parse a file using the SAX API use
51  * <pre><code>
52  * Xml xml = new Xml();
53  * xml.setContentHandler(myContentHandler);
54  * xml.parse("foo.xml");
55  * </code></pre>
56  */

57 public class Xml extends XmlParser {
58   private static FreeList<Xml> _freeList = new FreeList<Xml>(16);
59   
60   /**
61    * Create a new strict XML parser
62    */

63   public Xml()
64   {
65     super(new XmlPolicy(), null);
66
67     init();
68   }
69
70   /**
71    * Initialize the parser.
72    */

73   public void init()
74   {
75     super.init();
76     
77     _strictComments = true;
78     _strictAttributes = true;
79     _strictCharacters = true;
80     _strictXml = true;
81     _singleTopElement = true;
82     _optionalTags = false;
83   }
84
85   /**
86    * Creates an Xml parser.
87    */

88   public static Xml create()
89   {
90     Xml xml = _freeList.allocate();
91     if (xml == null)
92       xml = new Xml();
93     xml.init();
94
95     return xml;
96   }
97
98   /**
99    * Frees an Xml parser.
100    */

101   public void free()
102   {
103     _owner = null;
104     _contentHandler = null;
105     _entityResolver = null;
106     _dtdHandler = null;
107     _errorHandler = null;
108     _dtd = null;
109   
110     _freeList.free(this);
111   }
112
113   /**
114    * Create a new DOM document
115    */

116   static public CauchoDocument createDocument()
117   {
118     return new QDocument();
119   }
120
121   /**
122    * Create a new DOM implementation
123    */

124   static public DOMImplementation JavaDoc createDOMImplementation()
125   {
126     return new QDOMImplementation();
127   }
128 }
129
Popular Tags