1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.ws.commons.schema.resolver;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.MalformedURLException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.net.URL;
27
28 import org.xml.sax.InputSource;
29
30
31
32
33
34
35
36 public class DefaultURIResolver implements CollectionURIResolver {
37
38 private String collectionBaseURI;
39
40
41
42
43
44
45
46
47 public InputSource resolveEntity(String namespace,
48 String schemaLocation,
49 String baseUri) {
50
51 if (baseUri!=null)
52 {
53 try
54 {
55 File baseFile = new File(baseUri);
56 if (baseFile.exists()) {
57 baseUri = baseFile.toURI().toString();
58 } else if(collectionBaseURI != null) {
59 baseFile = new File(collectionBaseURI);
60 if (baseFile.exists()) {
61 baseUri = baseFile.toURI().toString();
62 }
63 }
64
65
66 schemaLocation = schemaLocation.replace(" ","%20");
67
68 String ref = new URI(baseUri).resolve(new URI(schemaLocation)).toString();
69
70 return new InputSource(ref);
71 }
72 catch (URISyntaxException e1)
73 {
74 throw new RuntimeException(e1);
75 }
76
77 }
78 return new InputSource(schemaLocation);
79
80
81
82 }
83
84
85
86
87
88
89
90 protected boolean isAbsolute(String uri) {
91 return uri.startsWith("http://");
92 }
93
94
95
96
97
98
99
100
101
102
103 protected URL getURL(URL contextURL, String spec) throws IOException {
104
105
106
107
108 String path = spec.replace('\\', '/');
109
110
111 URL url;
112
113 try {
114
115
116 url = new URL(contextURL, path);
117
118
119
120 if ((contextURL != null) && url.getProtocol().equals("file")
121 && contextURL.getProtocol().equals("file")) {
122 url = getFileURL(contextURL, path);
123 }
124 } catch (MalformedURLException me) {
125
126
127 url = getFileURL(contextURL, path);
128 }
129
130
131
132
133 return url;
134 }
135
136
137
138
139
140
141
142
143 protected URL getFileURL(URL contextURL, String path)
144 throws IOException {
145
146 if (contextURL != null) {
147
148
149
150 String contextFileName = contextURL.getFile();
151 URL parent = null;
152
153
154
155
156
157 File parentFile;
158 File contextFile = new File(contextFileName);
159 if (contextFile.isDirectory()){
160 parentFile = contextFile;
161 }else{
162 parentFile = contextFile.getParentFile();
163 }
164
165 if (parentFile != null) {
166 parent = parentFile.toURL();
167 }
168 if (parent != null) {
169 return new URL(parent, path);
170 }
171 }
172
173 return new URL("file", "", path);
174 }
175
176
177
178
179
180 public String getCollectionBaseURI() {
181 return collectionBaseURI;
182 }
183
184
185
186
187
188 public void setCollectionBaseURI(String collectionBaseURI) {
189 this.collectionBaseURI = collectionBaseURI;
190 }
191 }