1   /*
2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3    * 
4    * This software is open source. 
5    * See the bottom of this file for the licence.
6    * 
7    * $Id: JAXPFactory.java,v 1.2 2004/06/25 08:03:47 maartenc Exp $
8    */
9   
10  package org.dom4j;
11  
12  import java.util.Enumeration;
13  import java.util.HashMap;
14  import java.util.Hashtable;
15  import java.util.Iterator;
16  import java.util.Map;
17  import javax.xml.parsers.ParserConfigurationException;
18  import javax.xml.parsers.SAXParser;
19  import javax.xml.parsers.SAXParserFactory;
20  import org.dom4j.io.aelfred2.SAXDriver;
21  import org.xml.sax.Parser;
22  import org.xml.sax.SAXException;
23  import org.xml.sax.SAXNotRecognizedException;
24  import org.xml.sax.SAXNotSupportedException;
25  import org.xml.sax.XMLReader;
26  import org.xml.sax.helpers.XMLReaderAdapter;
27  
28  /*
29   * JAXPFactory.java
30   *
31   * Created on 16 april 2004, 15:14
32   */
33  
34  /***
35   *
36   * @author  Maarten Coene
37   */
38  public class JAXPFactory extends SAXParserFactory {
39      
40      private Map features = new HashMap();
41  
42      /***
43       * Constructs a factory which normally returns a non-validating
44       * parser.
45       */
46      public JAXPFactory () { 
47      }
48  
49      public SAXParser newSAXParser () throws ParserConfigurationException, SAXException {
50          SAXParser result = new InternalParser();
51  	XMLReader parser = result.getXMLReader ();
52  
53  	parser.setFeature(
54                  "http://xml.org/sax/features/namespaces",
55                  isNamespaceAware());
56  	parser.setFeature(
57                  "http://xml.org/sax/features/validation",
58                  isValidating());
59  
60          Iterator it = features.entrySet().iterator();
61  	while (it.hasNext()) {
62              Map.Entry entry = (Map.Entry) it.next();
63              String name = (String) entry.getKey();
64              Boolean value = (Boolean) entry.getValue();
65  	    parser.setFeature (name, value.booleanValue ());
66  	}
67  
68  	return result;
69      }
70  
71      public void setFeature (String name, boolean value) {
72  	features.put(name, new Boolean(value));
73      }
74  
75      public boolean getFeature(String name) {
76  	Boolean	value = (Boolean) features.get (name);
77  	
78  	if (value != null)
79  	    return value.booleanValue ();
80  	else {
81              return false;
82          }
83      }
84  
85      private static class InternalParser extends SAXParser {
86          
87  	private SAXDriver aelfred;
88  	private XMLReaderAdapter parser;
89  
90  	InternalParser() { 
91              super();
92              
93              aelfred = new SAXDriver();
94              parser = new XMLReaderAdapter(aelfred);
95          }
96  
97  	public void setProperty(String id, Object value) throws SAXNotRecognizedException, 
98                                                                  SAXNotSupportedException {
99              aelfred.setProperty(id, value); 
100         }
101 
102 	public Object getProperty(String id) throws SAXNotRecognizedException, 
103                                                     SAXNotSupportedException {
104             return aelfred.getProperty(id); 
105         }
106 
107 	public Parser getParser() { 
108 	    return parser;
109 	}
110 
111 	public XMLReader getXMLReader() {
112             return aelfred; 
113         }
114 
115 	public boolean isNamespaceAware() {
116             try {
117                 return aelfred.getFeature("http://xml.org/sax/features/namespaces");
118             } catch (Exception e) {
119                 return false;
120             }
121 	}
122 
123 	public boolean isValidating()	{
124 	    try {
125 		return aelfred.getFeature("http://xml.org/sax/features/validation");
126 	    } catch (Exception e) {
127 		return false;
128 	    }
129 	}
130     }
131 }
132 
133 
134 
135 
136 /*
137  * Redistribution and use of this software and associated documentation
138  * ("Software"), with or without modification, are permitted provided
139  * that the following conditions are met:
140  *
141  * 1. Redistributions of source code must retain copyright
142  *    statements and notices.  Redistributions must also contain a
143  *    copy of this document.
144  *
145  * 2. Redistributions in binary form must reproduce the
146  *    above copyright notice, this list of conditions and the
147  *    following disclaimer in the documentation and/or other
148  *    materials provided with the distribution.
149  *
150  * 3. The name "DOM4J" must not be used to endorse or promote
151  *    products derived from this Software without prior written
152  *    permission of MetaStuff, Ltd.  For written permission,
153  *    please contact dom4j-info@metastuff.com.
154  *
155  * 4. Products derived from this Software may not be called "DOM4J"
156  *    nor may "DOM4J" appear in their names without prior written
157  *    permission of MetaStuff, Ltd. DOM4J is a registered
158  *    trademark of MetaStuff, Ltd.
159  *
160  * 5. Due credit should be given to the DOM4J Project - 
161  *    http://www.dom4j.org
162  *
163  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
164  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
165  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
166  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
167  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
168  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
169  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
170  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
171  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
172  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
173  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
174  * OF THE POSSIBILITY OF SUCH DAMAGE.
175  *
176  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
177  *
178  * $Id: JAXPFactory.java,v 1.2 2004/06/25 08:03:47 maartenc Exp $
179  */