View Javadoc

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                  // if the schema location contain spaces URI parser gives errors
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       * Find whether a given uri is relative or not
86       *
87       * @param uri
88       * @return boolean
89       */
90      protected boolean isAbsolute(String uri) {
91          return uri.startsWith("http://");
92      }
93  
94      /**
95       * This is essentially a call to "new URL(contextURL, spec)"
96       * with extra handling in case spec is
97       * a file.
98       *
99       * @param contextURL
100      * @param spec
101      * @throws java.io.IOException
102      */
103     protected URL getURL(URL contextURL, String spec) throws IOException {
104 
105         // First, fix the slashes as windows filenames may have backslashes
106         // in them, but the URL class wont do the right thing when we later
107         // process this URL as the contextURL.
108         String path = spec.replace('\\', '/');
109 
110         // See if we have a good URL.
111         URL url;
112 
113         try {
114 
115             // first, try to treat spec as a full URL
116             url = new URL(contextURL, path);
117 
118             // if we are deail with files in both cases, create a url
119             // by using the directory of the context URL.
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             // try treating is as a file pathname
127             url = getFileURL(contextURL, path);
128         }
129 
130         // Everything is OK with this URL, although a file url constructed
131         // above may not exist.  This will be caught later when the URL is
132         // accessed.
133         return url;
134     }    // getURL
135 
136     /**
137      * Method getFileURL
138      *
139      * @param contextURL
140      * @param path
141      * @throws IOException
142      */
143     protected URL getFileURL(URL contextURL, String path)
144             throws IOException {
145 
146         if (contextURL != null) {
147 
148             // get the parent directory of the contextURL, and append
149             // the spec string to the end.
150             String contextFileName = contextURL.getFile();
151             URL parent = null;
152             //the logic for finding the parent file is this.
153             //1.if the contextURI represents a file then take the parent file
154             //of it
155             //2. If the contextURI represents a directory, then take that as
156             //the parent
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     }    // getFileURL
175 
176     /**
177      * Get the base URI derived from a schema collection. It serves as a fallback from the specified base.
178      * @return URI
179      */
180 	public String getCollectionBaseURI() {
181 		return collectionBaseURI;
182 	}
183 
184 	/**
185 	 * set the collection base URI, which serves as a fallback from the base of the immediate schema.
186 	 * @param collectionBaseURI the URI.
187 	 */
188 	public void setCollectionBaseURI(String collectionBaseURI) {
189 		this.collectionBaseURI = collectionBaseURI;
190 	}
191 }