KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > AugmentedSource


1 package net.sf.saxon;
2
3 import net.sf.saxon.om.Validation;
4 import net.sf.saxon.event.ProxyReceiver;
5 import org.xml.sax.XMLReader JavaDoc;
6
7 import javax.xml.transform.Source JavaDoc;
8 import javax.xml.transform.sax.SAXSource JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.ArrayList JavaDoc;
11
12 /**
13  * This class wraps a JAXP Source object to provide an extended Source object that
14  * contains options indicating how the Source should be processed: for example,
15  * whether or not it should be validated against a schema. Other options that can
16  * be set include the SAX XMLReader to be used, and the choice of whether a source
17  * in the form of an existing tree should be copied or wrapped.
18  */

19
20 public class AugmentedSource implements Source JavaDoc {
21
22     private Source JavaDoc source;
23     private int schemaValidation = Validation.DEFAULT;
24     private XMLReader JavaDoc parser = null;
25     private Boolean JavaDoc wrapDocument = null;
26     private List JavaDoc filters = null;
27
28     /**
29      * Create an AugmentedSource that wraps a given Source object (which must not itself be an
30      * AugmentedSource)
31      * @param source the Source object to be wrapped
32      * @throws IllegalArgumentException if the wrapped source is an AugmentedSource
33      */

34
35     private AugmentedSource(Source JavaDoc source) {
36         if (source instanceof AugmentedSource) {
37             throw new IllegalArgumentException JavaDoc("Contained source must not be an AugmentedSource");
38         }
39         this.source = source;
40     }
41
42     /**
43      * Create an AugmentedSource that wraps a given Source object. If this is already
44      * an AugmentedSource, the original AugmentedSource is returned.
45      * @param source the Source object to be wrapped
46      */

47
48     public static AugmentedSource makeAugmentedSource(Source JavaDoc source) {
49         if (source instanceof AugmentedSource) {
50             return (AugmentedSource)source;
51         }
52         return new AugmentedSource(source);
53     }
54
55     /**
56      * Add a filter to the list of filters to be applied to the raw input
57      */

58
59     public void addFilter(ProxyReceiver filter) {
60         if (filters == null) {
61             filters = new ArrayList JavaDoc(5);
62         }
63         filters.add(filter);
64     }
65
66     /**
67      * Get the list of filters to be applied to the input. Returns null if there are no filters.
68      */

69
70     public List JavaDoc getFilters() {
71         return filters;
72     }
73
74     /**
75      * Get the Source object wrapped by this AugmentedSource
76      * @return the contained Source object
77      */

78
79     public Source JavaDoc getContainedSource() {
80         return source;
81     }
82
83     /**
84      * Set whether or not schema validation of this source is required
85      * @param option one of {@link Validation#STRICT},
86      * {@link Validation#LAX}, {@link Validation#STRIP},
87      * {@link Validation#PRESERVE}, {@link Validation#DEFAULT}
88      *
89      */

90
91     public void setSchemaValidationMode(int option) {
92         schemaValidation = option;
93     }
94
95     /**
96      * Get whether or not schema validation of this source is required
97      * @return the validation mode requested, or {@link Validation#DEFAULT}
98      * to use the default validation mode from the Configuration.
99      */

100
101     public int getSchemaValidation() {
102         return schemaValidation;
103     }
104
105     public void setXMLReader(XMLReader JavaDoc parser) {
106         this.parser = parser;
107         if (source instanceof SAXSource JavaDoc) {
108             ((SAXSource JavaDoc)source).setXMLReader(parser);
109         }
110     }
111
112     public XMLReader JavaDoc getXMLReader() {
113         if (parser != null) {
114             return parser;
115         } else if (source instanceof SAXSource JavaDoc) {
116             return ((SAXSource JavaDoc)source).getXMLReader();
117         } else {
118             return null;
119         }
120     }
121
122     /**
123      * Assuming that the contained Source is a node in a tree, indicate whether a tree should be created
124      * as a view of this supplied tree, or as a copy.
125      * @param wrap if true, the node in the supplied Source is wrapped, to create a view. If false, the node
126      * and its contained subtree is copied. If null, the system default is chosen.
127      */

128
129     public void setWrapDocument(Boolean JavaDoc wrap) {
130         this.wrapDocument = wrap;
131     }
132
133     /**
134        Assuming that the contained Source is a node in a tree, determine whether a tree will be created
135      * as a view of this supplied tree, or as a copy.
136      * @return if true, the node in the supplied Source is wrapped, to create a view. If false, the node
137      * and its contained subtree is copied. If null, the system default is chosen.
138      */

139
140     public Boolean JavaDoc getWrapDocument() {
141         return wrapDocument;
142     }
143
144     /**
145      * Set the System ID. This sets the System Id on the underlying Source object.
146      * @param id the System ID.
147      */

148
149     public void setSystemId(String JavaDoc id) {
150         source.setSystemId(id);
151     }
152
153     /**
154      * Get the System ID. This gets the System Id on the underlying Source object.
155      * @return the System ID.
156      */

157
158     public String JavaDoc getSystemId() {
159         return source.getSystemId();
160     }
161 }
162
163 //
164
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
165
// you may not use this file except in compliance with the License. You may obtain a copy of the
166
// License at http://www.mozilla.org/MPL/
167
//
168
// Software distributed under the License is distributed on an "AS IS" basis,
169
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
170
// See the License for the specific language governing rights and limitations under the License.
171
//
172
// The Original Code is: all this file.
173
//
174
// The Initial Developer of the Original Code is Michael H. Kay.
175
//
176
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
177
//
178
// Contributor(s): none.
179
//
Popular Tags