001: /*
002: * $Id: PropertyManager.java,v 1.6 2007/04/06 13:49:02 spericas Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * [Name of File] [ver.__] [Date]
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.stream;
030:
031: import java.util.HashMap;
032: import javax.xml.stream.XMLInputFactory;
033: import javax.xml.stream.XMLOutputFactory;
034: import javax.xml.stream.XMLResolver;
035:
036: /**
037: * This class manages different properties related to Stax specification and its implementation.
038: * This class constructor also takes itself (PropertyManager object) as parameter and initializes the
039: * object with the property taken from the object passed.
040: *
041: * @author Neeraj Bajaj, neeraj.bajaj@sun.com
042: * @author K.Venugopal@sun.com
043: */
044:
045: public class PropertyManager {
046:
047: protected static final String STAX_NOTATIONS = "javax.xml.stream.notations";
048: protected static final String STAX_ENTITIES = "javax.xml.stream.entities";
049:
050: private static final String STRING_INTERNING = "http://xml.org/sax/features/string-interning";
051:
052: HashMap supportedProps = new HashMap();
053:
054: public static final int CONTEXT_READER = 1;
055: public static final int CONTEXT_WRITER = 2;
056:
057: /** Creates a new instance of PropertyManager */
058: public PropertyManager(int context) {
059: switch (context) {
060: case CONTEXT_READER: {
061: initConfigurableReaderProperties();
062: break;
063: }
064: case CONTEXT_WRITER: {
065: initWriterProps();
066: break;
067: }
068: }
069: }
070:
071: /**
072: * Initialize this object with the properties taken from passed PropertyManager object.
073: */
074: public PropertyManager(PropertyManager propertyManager) {
075:
076: HashMap properties = propertyManager.getProperties();
077: supportedProps.putAll(properties);
078: }
079:
080: private HashMap getProperties() {
081: return supportedProps;
082: }
083:
084: /**
085: * Important point:
086: * 1. We are not exposing Xerces namespace property. Application should configure namespace through
087: * Stax specific property.
088: *
089: */
090: private void initConfigurableReaderProperties() {
091: //spec default values
092: supportedProps.put(XMLInputFactory.IS_NAMESPACE_AWARE,
093: Boolean.TRUE);
094: supportedProps
095: .put(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
096: supportedProps.put(
097: XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
098: Boolean.TRUE);
099: supportedProps.put(
100: XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
101: Boolean.TRUE);
102: supportedProps
103: .put(XMLInputFactory.IS_COALESCING, Boolean.FALSE);
104: supportedProps.put(XMLInputFactory.SUPPORT_DTD, Boolean.TRUE);
105: supportedProps.put(XMLInputFactory.REPORTER, null);
106: supportedProps.put(XMLInputFactory.RESOLVER, null);
107: supportedProps.put(XMLInputFactory.ALLOCATOR, null);
108: supportedProps.put(STAX_NOTATIONS, null);
109:
110: //zephyr (implementation) specific properties which can be set by the application.
111: //interning is always done
112: supportedProps.put(Constants.SAX_FEATURE_PREFIX
113: + Constants.STRING_INTERNING_FEATURE, Boolean.TRUE);
114: //recognizing java encoding names by default
115: supportedProps.put(Constants.XERCES_FEATURE_PREFIX
116: + Constants.ALLOW_JAVA_ENCODINGS_FEATURE, Boolean.TRUE);
117: supportedProps.put(Constants.REUSE_INSTANCE, Boolean.FALSE);
118: supportedProps.put(Constants.ZEPHYR_PROPERTY_PREFIX
119: + Constants.STAX_REPORT_CDATA_EVENT, Boolean.FALSE);
120: supportedProps.put(Constants.XERCES_FEATURE_PREFIX
121: + Constants.WARN_ON_DUPLICATE_ATTDEF_FEATURE,
122: Boolean.FALSE);
123: supportedProps.put(Constants.XERCES_FEATURE_PREFIX
124: + Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE,
125: Boolean.FALSE);
126: supportedProps.put(Constants.XERCES_FEATURE_PREFIX
127: + Constants.WARN_ON_UNDECLARED_ELEMDEF_FEATURE,
128: Boolean.FALSE);
129: supportedProps.put(Constants.ZEPHYR_PROPERTY_PREFIX
130: + Constants.IMPLEMENTATION_NAME, "sjsxp");
131: }
132:
133: private void initWriterProps() {
134: supportedProps.put(XMLOutputFactory.IS_REPAIRING_NAMESPACES,
135: Boolean.FALSE);
136: //default value of escaping characters is 'true'
137: supportedProps.put(Constants.ESCAPE_CHARACTERS, Boolean.TRUE);
138: supportedProps.put(Constants.REUSE_INSTANCE, Boolean.FALSE);
139: supportedProps.put(Constants.ZEPHYR_PROPERTY_PREFIX
140: + Constants.IMPLEMENTATION_NAME, "sjsxp");
141: supportedProps.put(Constants.ZEPHYR_PROPERTY_PREFIX
142: + Constants.OUTPUTSTREAM, null);
143: }
144:
145: /**
146: * public void reset(){
147: * supportedProps.clear() ;
148: * }
149: */
150: public boolean containsProperty(String property) {
151: return supportedProps.containsKey(property);
152: }
153:
154: public Object getProperty(String property) {
155: return supportedProps.get(property);
156: }
157:
158: public void setProperty(String property, Object value) {
159: String equivalentProperty = null;
160: if (property == XMLInputFactory.IS_NAMESPACE_AWARE
161: || property.equals(XMLInputFactory.IS_NAMESPACE_AWARE)) {
162: equivalentProperty = Constants.XERCES_FEATURE_PREFIX
163: + Constants.NAMESPACES_FEATURE;
164: } else if (property == XMLInputFactory.IS_VALIDATING
165: || property.equals(XMLInputFactory.IS_VALIDATING)) {
166: if ((value instanceof Boolean)
167: && ((Boolean) value).booleanValue()) {
168: throw new java.lang.IllegalArgumentException(
169: "true value of isValidating not supported");
170: }
171: } else if (property == STRING_INTERNING
172: || property.equals(STRING_INTERNING)) {
173: if ((value instanceof Boolean)
174: && !((Boolean) value).booleanValue()) {
175: throw new java.lang.IllegalArgumentException(
176: "false value of " + STRING_INTERNING
177: + "feature is not supported");
178: }
179: } else if (property == XMLInputFactory.RESOLVER
180: || property.equals(XMLInputFactory.RESOLVER)) {
181: //add internal stax property
182: supportedProps.put(Constants.XERCES_PROPERTY_PREFIX
183: + Constants.STAX_ENTITY_RESOLVER_PROPERTY,
184: new StaxEntityResolverWrapper((XMLResolver) value));
185: }
186: supportedProps.put(property, value);
187: if (equivalentProperty != null) {
188: supportedProps.put(equivalentProperty, value);
189: }
190: }
191:
192: public String toString() {
193: return supportedProps.toString();
194: }
195:
196: }//PropertyManager
|