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 package org.exolab.castor.xml.schema.reader;
37
38 import org.exolab.castor.net.URIException;
39 import org.exolab.castor.net.URILocation;
40 import org.exolab.castor.net.URIResolver;
41 import org.exolab.castor.xml.AttributeSet;
42 import org.exolab.castor.xml.XMLException;
43 import org.exolab.castor.xml.schema.Schema;
44 import org.exolab.castor.xml.schema.SchemaContext;
45 import org.exolab.castor.xml.schema.SchemaException;
46 import org.exolab.castor.xml.schema.SchemaNames;
47 import org.xml.sax.Locator;
48 import org.xml.sax.Parser;
49
50 public class ImportUnmarshaller extends ComponentReader {
51
52
53 public ImportUnmarshaller(final SchemaContext schemaContext, final Schema schema,
54 final AttributeSet atts, final URIResolver uriResolver, final Locator locator,
55 final SchemaUnmarshallerState state) throws XMLException {
56 super(schemaContext);
57 setURIResolver(uriResolver);
58
59 URILocation uri = null;
60
61
62 String schemaLocation = atts.getValue(SchemaNames.SCHEMALOCATION_ATTR);
63
64
65 String namespace = atts.getValue("namespace");
66
67 if ((schemaLocation == null) && (namespace == null)) {
68
69 return;
70 }
71
72 boolean hasLocation = (schemaLocation != null);
73 if (hasLocation) {
74
75 if (schemaLocation.indexOf("\\") != -1) {
76 String err = "'" + schemaLocation + "' is not a valid URI as defined by IETF RFC 2396.";
77 err += "The URI mustn't contain '\\'.";
78 throw new SchemaException(err);
79 }
80
81 if (namespace == null)
82 namespace = "";
83
84 uri = derive(locator, schemaLocation);
85 if (uri != null) {
86 schemaLocation = uri.getAbsoluteURI();
87 }
88 } else {
89 schemaLocation = namespace;
90 try {
91 uri = getURIResolver().resolveURN(namespace);
92 } catch (URIException urix) {
93 throw new XMLException(urix);
94 }
95 if (uri == null) {
96 String err =
97 "Unable to resolve Schema corresponding " + "to namespace '" + namespace + "'.";
98 throw new SchemaException(err);
99
100 }
101 }
102
103
104
105
106 if (namespace.equals(schema.getTargetNamespace())) {
107 throw new SchemaException(
108 "the 'namespace' attribute in the <import> element cannot be the same of the targetNamespace of the global schema");
109 }
110
111
112 boolean addSchema = false;
113 Schema importedSchema = schema.getImportedSchema(namespace, true);
114
115
116 if (state.processed(schemaLocation)) {
117 if (importedSchema == null)
118 schema.addImportedSchema(state.getSchema(schemaLocation));
119 return;
120 }
121
122 boolean alreadyLoaded = false;
123 if (importedSchema == null) {
124 if (uri instanceof SchemaLocation) {
125 importedSchema = ((SchemaLocation) uri).getSchema();
126 schema.addImportedSchema(importedSchema);
127 alreadyLoaded = true;
128 } else {
129 importedSchema = new Schema();
130 addSchema = true;
131 }
132 } else {
133
134 if (hasLocation) {
135 String tmpLocation = importedSchema.getSchemaLocation();
136 alreadyLoaded =
137 schemaLocation.equals(tmpLocation) || importedSchema.includeProcessed(schemaLocation);
138
139 if (!alreadyLoaded) {
140 importedSchema.addInclude(tmpLocation);
141 }
142 } else {
143
144
145
146
147
148
149
150
151
152
153 alreadyLoaded = true;
154 }
155 }
156
157 state.markAsProcessed(schemaLocation, importedSchema);
158
159 if (alreadyLoaded)
160 return;
161
162
163 Parser parser = createParser("import");
164
165
166 SchemaUnmarshaller schemaUnmarshaller = new SchemaUnmarshaller(getSchemaContext(), state);
167 schemaUnmarshaller.setURIResolver(getURIResolver());
168 schemaUnmarshaller.setSchema(importedSchema);
169
170
171 parseSchema(parser, schemaUnmarshaller, uri, schemaLocation, "import");
172
173
174 if (addSchema) {
175 importedSchema.setSchemaLocation(schemaLocation);
176 schema.addImportedSchema(importedSchema);
177 }
178 }
179
180
181
182
183
184 public String elementName() {
185 return SchemaNames.IMPORT;
186 }
187
188
189
190
191
192
193 public Object getObject() {
194 return null;
195 }
196
197 }