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