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