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.URILocation;
39 import org.exolab.castor.net.URIResolver;
40 import org.exolab.castor.xml.AttributeSet;
41 import org.exolab.castor.xml.XMLException;
42 import org.exolab.castor.xml.schema.Schema;
43 import org.exolab.castor.xml.schema.SchemaContext;
44 import org.exolab.castor.xml.schema.SchemaException;
45 import org.exolab.castor.xml.schema.SchemaNames;
46 import org.xml.sax.Locator;
47 import org.xml.sax.Parser;
48
49
50
51
52
53
54
55
56
57
58 public class IncludeUnmarshaller extends ComponentReader {
59
60
61 public IncludeUnmarshaller(final SchemaContext schemaContext, final Schema schema,
62 final AttributeSet atts, final URIResolver uriResolver, final Locator locator,
63 final SchemaUnmarshallerState state) throws XMLException {
64 super(schemaContext);
65
66 setURIResolver(uriResolver);
67 URILocation uri = null;
68
69 String include = atts.getValue("schemaLocation");
70 if (include == null)
71 throw new SchemaException("'schemaLocation' attribute missing on 'include'");
72
73 if (include.indexOf("\\") != -1) {
74 String err = include + " is not a valid URI as defined by IETF RFC 2396.";
75 err += "The URI must not contain '\\'.";
76 throw new SchemaException(err);
77 }
78
79 uri = derive(locator, include);
80 if (uri != null)
81 include = uri.getAbsoluteURI();
82
83
84 if (schema.includeProcessed(include)) {
85 return;
86 } else if (include.equals(schema.getSchemaLocation())) {
87 return;
88 }
89
90 Schema includedSchema = null;
91 boolean alreadyLoaded = false;
92
93
94 if (state.cacheIncludedSchemas) {
95 if (uri instanceof SchemaLocation) {
96 includedSchema = ((SchemaLocation) uri).getSchema();
97 schema.cacheIncludedSchema(includedSchema);
98 alreadyLoaded = true;
99 }
100
101 if (state.processed(include)) {
102 includedSchema = state.getSchema(include);
103 schema.cacheIncludedSchema(includedSchema);
104 alreadyLoaded = true;
105 }
106 }
107
108 if (includedSchema == null) {
109 includedSchema = new Schema();
110 } else {
111 state.markAsProcessed(include, includedSchema);
112 }
113
114
115 schema.addInclude(include);
116
117 if (alreadyLoaded) {
118 return;
119 }
120
121 Parser parser = createParser("include");
122
123 SchemaUnmarshaller schemaUnmarshaller =
124 new SchemaUnmarshaller(getSchemaContext(), true, state, getURIResolver());
125
126 if (state.cacheIncludedSchemas) {
127 schemaUnmarshaller.setSchema(includedSchema);
128 } else {
129 schemaUnmarshaller.setSchema(schema);
130 }
131
132 parseSchema(parser, schemaUnmarshaller, uri, include, "include");
133
134 if (state.cacheIncludedSchemas) {
135 String ns = includedSchema.getTargetNamespace();
136 if (ns == null || ns == "")
137 includedSchema.setTargetNamespace(schema.getTargetNamespace());
138 else if (!ns.equals(schema.getTargetNamespace()))
139 throw new SchemaException(
140 "The target namespace of the included components must be the same as the target namespace of the including schema");
141 schema.cacheIncludedSchema(includedSchema);
142 }
143 }
144
145
146
147
148
149 public String elementName() {
150 return SchemaNames.INCLUDE;
151 }
152
153
154
155
156
157
158 public Object getObject() {
159 return null;
160 }
161
162 }