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 String ref = new URI(baseUri).resolve(new URI(schemaLocation)).toString();
66
67 return new InputSource(ref);
68 }
69 catch (URISyntaxException e1)
70 {
71 throw new RuntimeException(e1);
72 }
73
74 }
75 return new InputSource(schemaLocation);
76
77
78
79 }
80
81
82
83
84
85
86
87 protected boolean isAbsolute(String uri) {
88 return uri.startsWith("http://");
89 }
90
91
92
93
94
95
96
97
98
99
100 protected URL getURL(URL contextURL, String spec) throws IOException {
101
102
103
104
105 String path = spec.replace('\\', '/');
106
107
108 URL url;
109
110 try {
111
112
113 url = new URL(contextURL, path);
114
115
116
117 if ((contextURL != null) && url.getProtocol().equals("file")
118 && contextURL.getProtocol().equals("file")) {
119 url = getFileURL(contextURL, path);
120 }
121 } catch (MalformedURLException me) {
122
123
124 url = getFileURL(contextURL, path);
125 }
126
127
128
129
130 return url;
131 }
132
133
134
135
136
137
138
139
140 protected URL getFileURL(URL contextURL, String path)
141 throws IOException {
142
143 if (contextURL != null) {
144
145
146
147 String contextFileName = contextURL.getFile();
148 URL parent = null;
149
150
151
152
153
154 File parentFile;
155 File contextFile = new File(contextFileName);
156 if (contextFile.isDirectory()){
157 parentFile = contextFile;
158 }else{
159 parentFile = contextFile.getParentFile();
160 }
161
162 if (parentFile != null) {
163 parent = parentFile.toURL();
164 }
165 if (parent != null) {
166 return new URL(parent, path);
167 }
168 }
169
170 return new URL("file", "", path);
171 }
172
173
174
175
176
177 public String getCollectionBaseURI() {
178 return collectionBaseURI;
179 }
180
181
182
183
184
185 public void setCollectionBaseURI(String collectionBaseURI) {
186 this.collectionBaseURI = collectionBaseURI;
187 }
188 }