1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
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 * This resolver provides the means of resolving the imports and includes of a
33 * given schema document. The system will call this default resolver if there
34 * is no other resolver present in the system.
35 */
36 public class DefaultURIResolver implements CollectionURIResolver {
37
38 private String collectionBaseURI;
39
40
41 /**
42 * Try to resolve a schema location to some data.
43 * @param namespace targt namespace.
44 * @param schemaLocation system ID.
45 * @param baseUri base URI for the schema.
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 * Find whether a given uri is relative or not
83 *
84 * @param uri
85 * @return boolean
86 */
87 protected boolean isAbsolute(String uri) {
88 return uri.startsWith("http://");
89 }
90
91 /**
92 * This is essentially a call to "new URL(contextURL, spec)"
93 * with extra handling in case spec is
94 * a file.
95 *
96 * @param contextURL
97 * @param spec
98 * @throws java.io.IOException
99 */
100 protected URL getURL(URL contextURL, String spec) throws IOException {
101
102 // First, fix the slashes as windows filenames may have backslashes
103 // in them, but the URL class wont do the right thing when we later
104 // process this URL as the contextURL.
105 String path = spec.replace('\\', '/');
106
107 // See if we have a good URL.
108 URL url;
109
110 try {
111
112 // first, try to treat spec as a full URL
113 url = new URL(contextURL, path);
114
115 // if we are deail with files in both cases, create a url
116 // by using the directory of the context URL.
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 // try treating is as a file pathname
124 url = getFileURL(contextURL, path);
125 }
126
127 // Everything is OK with this URL, although a file url constructed
128 // above may not exist. This will be caught later when the URL is
129 // accessed.
130 return url;
131 } // getURL
132
133 /**
134 * Method getFileURL
135 *
136 * @param contextURL
137 * @param path
138 * @throws IOException
139 */
140 protected URL getFileURL(URL contextURL, String path)
141 throws IOException {
142
143 if (contextURL != null) {
144
145 // get the parent directory of the contextURL, and append
146 // the spec string to the end.
147 String contextFileName = contextURL.getFile();
148 URL parent = null;
149 //the logic for finding the parent file is this.
150 //1.if the contextURI represents a file then take the parent file
151 //of it
152 //2. If the contextURI represents a directory, then take that as
153 //the parent
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 } // getFileURL
172
173 /**
174 * Get the base URI derived from a schema collection. It serves as a fallback from the specified base.
175 * @return URI
176 */
177 public String getCollectionBaseURI() {
178 return collectionBaseURI;
179 }
180
181 /**
182 * set the collection base URI, which serves as a fallback from the base of the immediate schema.
183 * @param collectionBaseURI the URI.
184 */
185 public void setCollectionBaseURI(String collectionBaseURI) {
186 this.collectionBaseURI = collectionBaseURI;
187 }
188 }