001: package net.sf.saxon;
002:
003: import net.sf.saxon.om.Validation;
004: import net.sf.saxon.event.ProxyReceiver;
005: import org.xml.sax.XMLReader;
006:
007: import javax.xml.transform.Source;
008: import javax.xml.transform.sax.SAXSource;
009: import java.util.List;
010: import java.util.ArrayList;
011:
012: /**
013: * This class wraps a JAXP Source object to provide an extended Source object that
014: * contains options indicating how the Source should be processed: for example,
015: * whether or not it should be validated against a schema. Other options that can
016: * be set include the SAX XMLReader to be used, and the choice of whether a source
017: * in the form of an existing tree should be copied or wrapped.
018: */
019:
020: public class AugmentedSource implements Source {
021:
022: private Source source;
023: private int schemaValidation = Validation.DEFAULT;
024: private XMLReader parser = null;
025: private Boolean wrapDocument = null;
026: private int stripSpace;
027: private List filters = null;
028:
029: /**
030: * Create an AugmentedSource that wraps a given Source object (which must not itself be an
031: * AugmentedSource)
032: * @param source the Source object to be wrapped
033: * @throws IllegalArgumentException if the wrapped source is an AugmentedSource
034: */
035:
036: private AugmentedSource(Source source) {
037: if (source instanceof AugmentedSource) {
038: throw new IllegalArgumentException(
039: "Contained source must not be an AugmentedSource");
040: }
041: this .source = source;
042: }
043:
044: /**
045: * Create an AugmentedSource that wraps a given Source object. If this is already
046: * an AugmentedSource, the original AugmentedSource is returned.
047: * @param source the Source object to be wrapped
048: */
049:
050: public static AugmentedSource makeAugmentedSource(Source source) {
051: if (source instanceof AugmentedSource) {
052: return (AugmentedSource) source;
053: }
054: return new AugmentedSource(source);
055: }
056:
057: /**
058: * Add a filter to the list of filters to be applied to the raw input
059: */
060:
061: public void addFilter(ProxyReceiver filter) {
062: if (filters == null) {
063: filters = new ArrayList(5);
064: }
065: filters.add(filter);
066: }
067:
068: /**
069: * Get the list of filters to be applied to the input. Returns null if there are no filters.
070: */
071:
072: public List getFilters() {
073: return filters;
074: }
075:
076: /**
077: * Get the Source object wrapped by this AugmentedSource
078: * @return the contained Source object
079: */
080:
081: public Source getContainedSource() {
082: return source;
083: }
084:
085: /**
086: * Set the space-stripping action to be applied to the source document
087: */
088:
089: public void setStripSpace(int stripAction) {
090: stripSpace = stripAction;
091: }
092:
093: /**
094: * Get the space-stripping action to be applied to the source document
095: */
096:
097: public int getStripSpace() {
098: return stripSpace;
099: }
100:
101: /**
102: * Set whether or not schema validation of this source is required
103: * @param option one of {@link Validation#STRICT},
104: * {@link Validation#LAX}, {@link Validation#STRIP},
105: * {@link Validation#PRESERVE}, {@link Validation#DEFAULT}
106: *
107: */
108:
109: public void setSchemaValidationMode(int option) {
110: schemaValidation = option;
111: }
112:
113: /**
114: * Get whether or not schema validation of this source is required
115: * @return the validation mode requested, or {@link Validation#DEFAULT}
116: * to use the default validation mode from the Configuration.
117: */
118:
119: public int getSchemaValidation() {
120: return schemaValidation;
121: }
122:
123: public void setXMLReader(XMLReader parser) {
124: this .parser = parser;
125: if (source instanceof SAXSource) {
126: ((SAXSource) source).setXMLReader(parser);
127: }
128: }
129:
130: public XMLReader getXMLReader() {
131: if (parser != null) {
132: return parser;
133: } else if (source instanceof SAXSource) {
134: return ((SAXSource) source).getXMLReader();
135: } else {
136: return null;
137: }
138: }
139:
140: /**
141: * Assuming that the contained Source is a node in a tree, indicate whether a tree should be created
142: * as a view of this supplied tree, or as a copy.
143: * @param wrap if true, the node in the supplied Source is wrapped, to create a view. If false, the node
144: * and its contained subtree is copied. If null, the system default is chosen.
145: */
146:
147: public void setWrapDocument(Boolean wrap) {
148: this .wrapDocument = wrap;
149: }
150:
151: /**
152: Assuming that the contained Source is a node in a tree, determine whether a tree will be created
153: * as a view of this supplied tree, or as a copy.
154: * @return if true, the node in the supplied Source is wrapped, to create a view. If false, the node
155: * and its contained subtree is copied. If null, the system default is chosen.
156: */
157:
158: public Boolean getWrapDocument() {
159: return wrapDocument;
160: }
161:
162: /**
163: * Set the System ID. This sets the System Id on the underlying Source object.
164: * @param id the System ID.
165: */
166:
167: public void setSystemId(String id) {
168: source.setSystemId(id);
169: }
170:
171: /**
172: * Get the System ID. This gets the System Id on the underlying Source object.
173: * @return the System ID.
174: */
175:
176: public String getSystemId() {
177: return source.getSystemId();
178: }
179: }
180:
181: //
182: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
183: // you may not use this file except in compliance with the License. You may obtain a copy of the
184: // License at http://www.mozilla.org/MPL/
185: //
186: // Software distributed under the License is distributed on an "AS IS" basis,
187: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
188: // See the License for the specific language governing rights and limitations under the License.
189: //
190: // The Original Code is: all this file.
191: //
192: // The Initial Developer of the Original Code is Michael H. Kay.
193: //
194: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
195: //
196: // Contributor(s): none.
197: //
|