1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.exolab.castor.xml.schema.reader;
47
48 import org.exolab.castor.net.URIException;
49 import org.exolab.castor.net.URILocation;
50 import org.exolab.castor.net.URIResolver;
51 import org.exolab.castor.xml.AttributeSet;
52 import org.exolab.castor.xml.XMLException;
53 import org.exolab.castor.xml.schema.SchemaContext;
54 import org.exolab.castor.xml.schema.Schema;
55 import org.exolab.castor.xml.schema.SchemaException;
56 import org.exolab.castor.xml.schema.SchemaNames;
57 import org.xml.sax.InputSource;
58 import org.xml.sax.Locator;
59 import org.xml.sax.Parser;
60
61 public class ImportUnmarshaller extends ComponentReader
62 {
63
64
65 public ImportUnmarshaller(
66 final SchemaContext schemaContext,
67 final Schema schema,
68 final AttributeSet atts,
69 final URIResolver uriResolver,
70 final Locator locator,
71 final SchemaUnmarshallerState state)
72 throws XMLException {
73 super(schemaContext);
74 setURIResolver(uriResolver);
75
76 URILocation uri = null;
77
78 String schemaLocation = atts.getValue(SchemaNames.SCHEMALOCATION_ATTR);
79
80 String namespace = atts.getValue("namespace");
81
82 if ((schemaLocation == null) && (namespace == null)) {
83
84 return;
85 }
86
87 boolean hasLocation = (schemaLocation != null);
88 if (hasLocation) {
89
90 if (schemaLocation.indexOf("\\") != -1) {
91 String err = "'" + schemaLocation +
92 "' is not a valid URI as defined by IETF RFC 2396.";
93 err += "The URI mustn't contain '\\'.";
94 throw new SchemaException(err);
95 }
96
97 if (namespace == null) namespace = "";
98
99 try {
100 String documentBase = locator.getSystemId();
101 if (documentBase != null) {
102 if (!documentBase.endsWith("/"))
103 documentBase = documentBase.substring(0, documentBase.lastIndexOf('/') +1 );
104 }
105 uri = getURIResolver().resolve(schemaLocation, documentBase);
106 if (uri != null) {
107 schemaLocation = uri.getAbsoluteURI();
108 }
109 }
110 catch (URIException urix) {
111 throw new XMLException(urix);
112 }
113 }
114 else {
115 schemaLocation = namespace;
116 try {
117 uri = getURIResolver().resolveURN(namespace);
118 }
119 catch (URIException urix) {
120 throw new XMLException(urix);
121 }
122 if (uri == null) {
123 String err = "Unable to resolve Schema corresponding " +
124 "to namespace '" + namespace + "'.";
125 throw new SchemaException(err);
126
127 }
128 }
129
130
131
132
133 if (namespace.equals(schema.getTargetNamespace()) )
134 throw new SchemaException("the 'namespace' attribute in the <import> element cannot be the same of the targetNamespace of the global schema");
135
136
137 boolean addSchema = false;
138 Schema importedSchema = schema.getImportedSchema(namespace, true);
139
140
141 if (state.processed(schemaLocation)) {
142 if (importedSchema == null)
143 schema.addImportedSchema(state.getSchema(schemaLocation));
144 return;
145 }
146
147 boolean alreadyLoaded = false;
148 if (importedSchema == null) {
149 if (uri instanceof SchemaLocation) {
150 importedSchema = ((SchemaLocation)uri).getSchema();
151 schema.addImportedSchema(importedSchema);
152 alreadyLoaded = true;
153 }
154 else {
155 importedSchema = new Schema();
156 addSchema = true;
157 }
158 }
159 else {
160
161 if (hasLocation) {
162 String tmpLocation = importedSchema.getSchemaLocation();
163 alreadyLoaded = schemaLocation.equals(tmpLocation) || importedSchema.includeProcessed(schemaLocation);
164
165 if(! alreadyLoaded) {
166 importedSchema.addInclude(tmpLocation);
167 }
168 } else {
169
170
171
172
173
174
175
176
177
178
179 alreadyLoaded = true;
180 }
181 }
182
183 state.markAsProcessed(schemaLocation, importedSchema);
184
185 if (alreadyLoaded) return;
186
187
188 Parser parser = null;
189 try {
190 parser = getSchemaContext().getParser();
191 }
192 catch(RuntimeException rte) {}
193 if (parser == null) {
194 throw new SchemaException("Error failed to create parser for import");
195 }
196
197 SchemaUnmarshaller schemaUnmarshaller = new SchemaUnmarshaller(getSchemaContext(), state);
198 schemaUnmarshaller.setURIResolver(getURIResolver());
199 schemaUnmarshaller.setSchema(importedSchema);
200 Sax2ComponentReader handler = new Sax2ComponentReader(schemaUnmarshaller);
201 parser.setDocumentHandler(handler);
202 parser.setErrorHandler(handler);
203
204 try {
205 InputSource source = new InputSource(uri.getReader());
206 source.setSystemId(uri.getAbsoluteURI());
207 parser.parse(source);
208 }
209 catch(java.io.IOException ioe) {
210 throw new SchemaException("Error reading import file '"+schemaLocation+"': "+ ioe);
211 }
212 catch(org.xml.sax.SAXException sx) {
213 throw new SchemaException(sx);
214 }
215
216
217 if (addSchema)
218 {
219 importedSchema.setSchemaLocation(schemaLocation);
220 schema.addImportedSchema(importedSchema);
221 }
222 }
223
224
225
226
227
228 public String elementName() {
229 return SchemaNames.IMPORT;
230 }
231
232
233
234
235
236 public Object getObject() {
237 return null;
238 }
239
240 }