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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.xml2.parsers;
031:
032: import com.caucho.xml2.Xml;
033: import com.caucho.xml2.XmlParser;
034:
035: import org.xml.sax.Parser;
036: import org.xml.sax.SAXNotRecognizedException;
037: import org.xml.sax.XMLReader;
038:
039: import javax.xml.parsers.SAXParser;
040: import javax.xml.parsers.SAXParserFactory;
041:
042: /**
043: * JAXP SAX parser factory for strict XML parsing.
044: */
045: public class XmlSAXParserFactory extends SAXParserFactory {
046: private int _namespaces = -1;
047: private int _namespacePrefixes = -1;
048: private int _validation = -1;
049:
050: public XmlSAXParserFactory() {
051: }
052:
053: /**
054: * Creates a new SAX Parser
055: */
056: public SAXParser newSAXParser() {
057: return new XmlSAXParser(this );
058: }
059:
060: public Object getProperty(String key) {
061: return null;
062: }
063:
064: public void setProperty(String key, Object value) {
065: }
066:
067: public boolean getFeature(String key) {
068: if (key.equals("http://xml.org/sax/features/namespaces"))
069: return isNamespaceAware();
070: else if (key
071: .equals("http://xml.org/sax/features/namespace-prefixes"))
072: return isNamespacePrefixes();
073: else if (key.equals("http://xml.org/sax/features/validation"))
074: return _validation != 0;
075: else
076: return false;
077: }
078:
079: public void setFeature(String key, boolean value)
080: throws SAXNotRecognizedException {
081: if (key.equals("http://xml.org/sax/features/namespaces"))
082: _namespaces = value ? 1 : 0;
083: else if (key
084: .equals("http://xml.org/sax/features/namespace-prefixes"))
085: _namespacePrefixes = value ? 1 : 0;
086: else if (key.equals("http://xml.org/sax/features/validation"))
087: _validation = value ? 1 : 0;
088: else
089: throw new SAXNotRecognizedException(key);
090: }
091:
092: public boolean isNamespacePrefixes() {
093: if (_namespacePrefixes >= 0)
094: return _namespacePrefixes == 1;
095: else if (_namespaces >= 0)
096: return _namespaces == 1;
097: else
098: return false;
099: }
100:
101: public boolean isNamespaceAware() {
102: if (_namespacePrefixes >= 0)
103: return true;
104: else if (_namespaces >= 0)
105: return _namespaces == 1;
106: else
107: return super .isNamespaceAware();
108: }
109:
110: public class XmlSAXParser extends SAXParser {
111: private XmlSAXParserFactory _factory;
112: private XmlParser _parser;
113:
114: XmlSAXParser(XmlSAXParserFactory factory) {
115: _factory = factory;
116: _parser = new Xml();
117: _parser.setNamespaceAware(_factory.isNamespaceAware());
118: _parser
119: .setNamespacePrefixes(_factory
120: .isNamespacePrefixes());
121: // _parser.setValidating(_factory.isValidating());
122: _parser.setValidating(true);
123: }
124:
125: public Object getProperty(String key) {
126: return null;
127: }
128:
129: public void setProperty(String key, Object value) {
130: }
131:
132: public Parser getParser() {
133: return _parser;
134: }
135:
136: public XMLReader getXMLReader() {
137: return _parser;
138: }
139:
140: /**
141: * Returns true if the factory is namespace aware.
142: */
143: public boolean isNamespaceAware() {
144: return _factory.isNamespaceAware();
145: }
146:
147: /**
148: * true if the factory is namespace aware.
149: */
150: public void setNamespaceAware(boolean isAware) {
151: _factory.setNamespaceAware(isAware);
152: }
153:
154: /**
155: * True if the factory is validating
156: */
157: public boolean isValidating() {
158: return _factory.isValidating();
159: }
160:
161: /**
162: * True if the factory is validating
163: */
164: public void setValidating(boolean isValidating) {
165: _factory.setValidating(isValidating);
166: }
167: }
168: }
|