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.xml.parsers;
031:
032: import com.caucho.xml.Xml;
033: import com.caucho.xml.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:
093: public boolean isNamespacePrefixes() {
094: if (_namespacePrefixes >= 0)
095: return _namespacePrefixes == 1;
096: else if (_namespaces >= 0)
097: return _namespaces == 1;
098: else
099: return false;
100: }
101:
102: public boolean isNamespaceAware() {
103: if (_namespacePrefixes >= 0)
104: return true;
105: else if (_namespaces >= 0)
106: return _namespaces == 1;
107: else
108: return super .isNamespaceAware();
109: }
110:
111: public class XmlSAXParser extends SAXParser {
112: private XmlSAXParserFactory _factory;
113: private XmlParser _parser;
114:
115: XmlSAXParser(XmlSAXParserFactory factory) {
116: _factory = factory;
117: _parser = new Xml();
118: _parser.setNamespaceAware(_factory.isNamespaceAware());
119: _parser
120: .setNamespacePrefixes(_factory
121: .isNamespacePrefixes());
122: // _parser.setValidating(_factory.isValidating());
123: _parser.setValidating(true);
124: }
125:
126: public Object getProperty(String key) {
127: return null;
128: }
129:
130: public void setProperty(String key, Object value) {
131: }
132:
133: public Parser getParser() {
134: return _parser;
135: }
136:
137: public XMLReader getXMLReader() {
138: return _parser;
139: }
140:
141: /**
142: * Returns true if the factory is namespace aware.
143: */
144: public boolean isNamespaceAware() {
145: return _factory.isNamespaceAware();
146: }
147:
148: /**
149: * true if the factory is namespace aware.
150: */
151: public void setNamespaceAware(boolean isAware) {
152: _factory.setNamespaceAware(isAware);
153: }
154:
155: /**
156: * True if the factory is validating
157: */
158: public boolean isValidating() {
159: return _factory.isValidating();
160: }
161:
162: /**
163: * True if the factory is validating
164: */
165: public void setValidating(boolean isValidating) {
166: _factory.setValidating(isValidating);
167: }
168: }
169: }
|