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 URIResolver {
37
38
39 /**
40 * As for the resolver the publid ID is the target namespace of the
41 * schema and the schemaLocation is the value of the schema location
42 * @param namespace
43 * @param schemaLocation
44 * @param baseUri
45 */
46 public InputSource resolveEntity(String namespace,
47 String schemaLocation,
48 String baseUri){
49
50 if (baseUri!=null)
51 {
52 try
53 {
54 File baseFile = new File(baseUri);
55 if (baseFile.exists()) baseUri = baseFile.toURI().toString();
56
57 String ref = new URI(baseUri).resolve(new URI(schemaLocation)).toString();
58
59 return new InputSource(ref);
60 }
61 catch (URISyntaxException e1)
62 {
63 throw new RuntimeException(e1);
64 }
65
66 }
67 return new InputSource(schemaLocation);
68
69
70
71 }
72
73 /**
74 * Find whether a given uri is relative or not
75 *
76 * @param uri
77 * @return boolean
78 */
79 protected boolean isAbsolute(String uri) {
80 return uri.startsWith("http://");
81 }
82
83 /**
84 * This is essentially a call to "new URL(contextURL, spec)"
85 * with extra handling in case spec is
86 * a file.
87 *
88 * @param contextURL
89 * @param spec
90 * @throws java.io.IOException
91 */
92 protected URL getURL(URL contextURL, String spec) throws IOException {
93
94 // First, fix the slashes as windows filenames may have backslashes
95 // in them, but the URL class wont do the right thing when we later
96 // process this URL as the contextURL.
97 String path = spec.replace('\\', '/');
98
99 // See if we have a good URL.
100 URL url;
101
102 try {
103
104 // first, try to treat spec as a full URL
105 url = new URL(contextURL, path);
106
107 // if we are deail with files in both cases, create a url
108 // by using the directory of the context URL.
109 if ((contextURL != null) && url.getProtocol().equals("file")
110 && contextURL.getProtocol().equals("file")) {
111 url = getFileURL(contextURL, path);
112 }
113 } catch (MalformedURLException me) {
114
115 // try treating is as a file pathname
116 url = getFileURL(contextURL, path);
117 }
118
119 // Everything is OK with this URL, although a file url constructed
120 // above may not exist. This will be caught later when the URL is
121 // accessed.
122 return url;
123 } // getURL
124
125 /**
126 * Method getFileURL
127 *
128 * @param contextURL
129 * @param path
130 * @throws IOException
131 */
132 protected URL getFileURL(URL contextURL, String path)
133 throws IOException {
134
135 if (contextURL != null) {
136
137 // get the parent directory of the contextURL, and append
138 // the spec string to the end.
139 String contextFileName = contextURL.getFile();
140 URL parent = null;
141 //the logic for finding the parent file is this.
142 //1.if the contextURI represents a file then take the parent file
143 //of it
144 //2. If the contextURI represents a directory, then take that as
145 //the parent
146 File parentFile;
147 File contextFile = new File(contextFileName);
148 if (contextFile.isDirectory()){
149 parentFile = contextFile;
150 }else{
151 parentFile = contextFile.getParentFile();
152 }
153
154 if (parentFile != null) {
155 parent = parentFile.toURL();
156 }
157 if (parent != null) {
158 return new URL(parent, path);
159 }
160 }
161
162 return new URL("file", "", path);
163 } // getFileURL
164 }