KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > parsers > XmlSAXParserFactory


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.xml.parsers;
31
32 import com.caucho.xml.Xml;
33 import com.caucho.xml.XmlParser;
34
35 import org.xml.sax.Parser JavaDoc;
36 import org.xml.sax.SAXNotRecognizedException JavaDoc;
37 import org.xml.sax.XMLReader JavaDoc;
38
39 import javax.xml.parsers.SAXParser JavaDoc;
40 import javax.xml.parsers.SAXParserFactory JavaDoc;
41
42 /**
43  * JAXP SAX parser factory for strict XML parsing.
44  */

45 public class XmlSAXParserFactory extends SAXParserFactory JavaDoc {
46   private int _namespaces = -1;
47   private int _namespacePrefixes = -1;
48   private int _validation = -1;
49
50   public XmlSAXParserFactory()
51   {
52   }
53   
54   /**
55    * Creates a new SAX Parser
56    */

57   public SAXParser JavaDoc newSAXParser()
58   {
59     return new XmlSAXParser(this);
60   }
61
62   public Object JavaDoc getProperty(String JavaDoc key)
63   {
64     return null;
65   }
66
67   public void setProperty(String JavaDoc key, Object JavaDoc value)
68   {
69   }
70
71   public boolean getFeature(String JavaDoc key)
72   {
73     if (key.equals("http://xml.org/sax/features/namespaces"))
74       return isNamespaceAware();
75     else if (key.equals("http://xml.org/sax/features/namespace-prefixes"))
76       return isNamespacePrefixes();
77     else if (key.equals("http://xml.org/sax/features/validation"))
78       return _validation != 0;
79     else
80       return false;
81   }
82
83   public void setFeature(String JavaDoc key, boolean value)
84     throws SAXNotRecognizedException JavaDoc
85   {
86     if (key.equals("http://xml.org/sax/features/namespaces"))
87       _namespaces = value ? 1 : 0;
88     else if (key.equals("http://xml.org/sax/features/namespace-prefixes"))
89       _namespacePrefixes = value ? 1 : 0;
90     else if (key.equals("http://xml.org/sax/features/validation"))
91       _validation = value ? 1 : 0;
92     else
93       throw new SAXNotRecognizedException JavaDoc(key);
94   }
95
96   public boolean isNamespacePrefixes()
97   {
98     if (_namespacePrefixes >= 0)
99       return _namespacePrefixes == 1;
100     else if (_namespaces >= 0)
101       return _namespaces == 1;
102     else
103       return false;
104   }
105
106   public boolean isNamespaceAware()
107   {
108     if (_namespacePrefixes >= 0)
109       return true;
110     else if (_namespaces >= 0)
111       return _namespaces == 1;
112     else
113       return super.isNamespaceAware();
114   }
115
116   public class XmlSAXParser extends SAXParser JavaDoc {
117     private XmlSAXParserFactory _factory;
118     private XmlParser _parser;
119
120     XmlSAXParser(XmlSAXParserFactory factory)
121     {
122       _factory = factory;
123       _parser = new Xml();
124       _parser.setNamespaceAware(_factory.isNamespaceAware());
125       _parser.setNamespacePrefixes(_factory.isNamespacePrefixes());
126       // _parser.setValidating(_factory.isValidating());
127
_parser.setValidating(true);
128     }
129
130     public Object JavaDoc getProperty(String JavaDoc key)
131     {
132       return null;
133     }
134
135     public void setProperty(String JavaDoc key, Object JavaDoc value)
136     {
137     }
138
139     public Parser getParser()
140     {
141       return _parser;
142     }
143
144     public XMLReader JavaDoc getXMLReader()
145     {
146       return _parser;
147     }
148
149     /**
150      * Returns true if the factory is namespace aware.
151      */

152     public boolean isNamespaceAware()
153     {
154       return _factory.isNamespaceAware();
155     }
156
157     /**
158      * true if the factory is namespace aware.
159      */

160     public void setNamespaceAware(boolean isAware)
161     {
162       _factory.setNamespaceAware(isAware);
163     }
164     
165     /**
166      * True if the factory is validating
167      */

168     public boolean isValidating()
169     {
170       return _factory.isValidating();
171     }
172     
173     /**
174      * True if the factory is validating
175      */

176     public void setValidating(boolean isValidating)
177     {
178       _factory.setValidating(isValidating);
179     }
180   }
181 }
182
Popular Tags