1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.dom4j.datatype; |
9 |
| |
10 |
| import org.dom4j.Attribute; |
11 |
| import org.dom4j.Document; |
12 |
| import org.dom4j.DocumentFactory; |
13 |
| import org.dom4j.Element; |
14 |
| import org.dom4j.Namespace; |
15 |
| import org.dom4j.QName; |
16 |
| import org.dom4j.io.SAXReader; |
17 |
| |
18 |
| import org.xml.sax.EntityResolver; |
19 |
| import org.xml.sax.InputSource; |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| public class DatatypeDocumentFactory extends DocumentFactory { |
32 |
| |
33 |
| private static final boolean DO_INTERN_QNAME = false; |
34 |
| |
35 |
| |
36 |
| protected static transient DatatypeDocumentFactory singleton |
37 |
| = new DatatypeDocumentFactory(); |
38 |
| |
39 |
| private static final Namespace XSI_NAMESPACE = Namespace.get("xsi", |
40 |
| "http://www.w3.org/2001/XMLSchema-instance"); |
41 |
| |
42 |
| private static final QName XSI_SCHEMA_LOCATION = QName.get( |
43 |
| "schemaLocation", XSI_NAMESPACE); |
44 |
| |
45 |
| private static final QName XSI_NO_SCHEMA_LOCATION = QName.get( |
46 |
| "noNamespaceSchemaLocation", XSI_NAMESPACE); |
47 |
| |
48 |
| |
49 |
| private SchemaParser schemaBuilder; |
50 |
| |
51 |
| |
52 |
| private SAXReader xmlSchemaReader = new SAXReader(); |
53 |
| |
54 |
| |
55 |
| private boolean autoLoadSchema = true; |
56 |
| |
57 |
27
| public DatatypeDocumentFactory() {
|
58 |
27
| schemaBuilder = new SchemaParser(this);
|
59 |
| } |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
33
| public static DocumentFactory getInstance() {
|
69 |
33
| return singleton;
|
70 |
| } |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
22
| public void loadSchema(Document schemaDocument) {
|
80 |
22
| schemaBuilder.build(schemaDocument);
|
81 |
| } |
82 |
| |
83 |
3
| public void loadSchema(Document schemaDocument, Namespace targetNamespace) {
|
84 |
3
| schemaBuilder.build(schemaDocument, targetNamespace);
|
85 |
| } |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
408
| public DatatypeElementFactory getElementFactory(QName elementQName) {
|
97 |
408
| DatatypeElementFactory result = null;
|
98 |
| |
99 |
408
| if (DO_INTERN_QNAME) {
|
100 |
0
| elementQName = intern(elementQName);
|
101 |
| } |
102 |
| |
103 |
408
| DocumentFactory factory = elementQName.getDocumentFactory();
|
104 |
408
| if (factory instanceof DatatypeElementFactory) {
|
105 |
5
| result = (DatatypeElementFactory) factory;
|
106 |
| } |
107 |
| |
108 |
408
| return result;
|
109 |
| } |
110 |
| |
111 |
| |
112 |
| |
113 |
450
| public Attribute createAttribute(Element owner, QName qname, String value) {
|
114 |
450
| if (autoLoadSchema && qname.equals(XSI_NO_SCHEMA_LOCATION)) {
|
115 |
2
| Document document = (owner != null) ? owner.getDocument() : null;
|
116 |
2
| loadSchema(document, value);
|
117 |
448
| } else if (autoLoadSchema && qname.equals(XSI_SCHEMA_LOCATION)) {
|
118 |
0
| Document document = (owner != null) ? owner.getDocument() : null;
|
119 |
0
| String uri = value.substring(0, value.indexOf(' '));
|
120 |
0
| Namespace namespace = owner.getNamespaceForURI(uri);
|
121 |
0
| loadSchema(document, value.substring(value.indexOf(' ') + 1),
|
122 |
| namespace); |
123 |
| } |
124 |
| |
125 |
450
| return super.createAttribute(owner, qname, value);
|
126 |
| } |
127 |
| |
128 |
| |
129 |
| |
130 |
2
| protected void loadSchema(Document document, String schemaInstanceURI) {
|
131 |
2
| try {
|
132 |
2
| EntityResolver resolver = document.getEntityResolver();
|
133 |
| |
134 |
2
| if (resolver == null) {
|
135 |
0
| String msg = "No EntityResolver available for resolving URI: ";
|
136 |
0
| throw new InvalidSchemaException(msg + schemaInstanceURI);
|
137 |
| } |
138 |
| |
139 |
2
| InputSource inputSource = resolver.resolveEntity(null,
|
140 |
| schemaInstanceURI); |
141 |
| |
142 |
2
| if (resolver == null) {
|
143 |
0
| throw new InvalidSchemaException("Could not resolve the URI: "
|
144 |
| + schemaInstanceURI); |
145 |
| } |
146 |
| |
147 |
2
| Document schemaDocument = xmlSchemaReader.read(inputSource);
|
148 |
2
| loadSchema(schemaDocument);
|
149 |
| } catch (Exception e) { |
150 |
0
| System.out.println("Failed to load schema: " + schemaInstanceURI);
|
151 |
0
| System.out.println("Caught: " + e);
|
152 |
0
| e.printStackTrace();
|
153 |
0
| throw new InvalidSchemaException("Failed to load schema: "
|
154 |
| + schemaInstanceURI); |
155 |
| } |
156 |
| } |
157 |
| |
158 |
0
| protected void loadSchema(Document document, String schemaInstanceURI,
|
159 |
| Namespace namespace) { |
160 |
0
| try {
|
161 |
0
| EntityResolver resolver = document.getEntityResolver();
|
162 |
| |
163 |
0
| if (resolver == null) {
|
164 |
0
| String msg = "No EntityResolver available for resolving URI: ";
|
165 |
0
| throw new InvalidSchemaException(msg + schemaInstanceURI);
|
166 |
| } |
167 |
| |
168 |
0
| InputSource inputSource = resolver.resolveEntity(null,
|
169 |
| schemaInstanceURI); |
170 |
| |
171 |
0
| if (resolver == null) {
|
172 |
0
| throw new InvalidSchemaException("Could not resolve the URI: "
|
173 |
| + schemaInstanceURI); |
174 |
| } |
175 |
| |
176 |
0
| Document schemaDocument = xmlSchemaReader.read(inputSource);
|
177 |
0
| loadSchema(schemaDocument, namespace);
|
178 |
| } catch (Exception e) { |
179 |
0
| System.out.println("Failed to load schema: " + schemaInstanceURI);
|
180 |
0
| System.out.println("Caught: " + e);
|
181 |
0
| e.printStackTrace();
|
182 |
0
| throw new InvalidSchemaException("Failed to load schema: "
|
183 |
| + schemaInstanceURI); |
184 |
| } |
185 |
| } |
186 |
| } |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |