View Javadoc

1   /*
2    * Copyright 2004,2007 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This resolver provides the means of resolving the imports and includes of a
31   * given schema document. The system will call this default resolver if there
32   * is no other resolver present in the system
33   */
34  public class DefaultURIResolver implements URIResolver {
35  
36  
37      /**
38       * As for the resolver the publid ID is the target namespace of the
39       * schema and the schemaLocation is the value of the schema location
40       * @param namespace
41       * @param schemaLocation
42       * @param baseUri
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       * Find whether a given uri is relative or not
73       *
74       * @param uri
75       * @return boolean
76       */
77      protected boolean isAbsolute(String uri) {
78          return uri.startsWith("http://");
79      }
80  
81      /**
82       * This is essentially a call to "new URL(contextURL, spec)"
83       * with extra handling in case spec is
84       * a file.
85       *
86       * @param contextURL
87       * @param spec
88       * @throws java.io.IOException
89       */
90      protected URL getURL(URL contextURL, String spec) throws IOException {
91  
92          // First, fix the slashes as windows filenames may have backslashes
93          // in them, but the URL class wont do the right thing when we later
94          // process this URL as the contextURL.
95          String path = spec.replace('\\', '/');
96  
97          // See if we have a good URL.
98          URL url;
99  
100         try {
101 
102             // first, try to treat spec as a full URL
103             url = new URL(contextURL, path);
104 
105             // if we are deail with files in both cases, create a url
106             // by using the directory of the context URL.
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             // try treating is as a file pathname
114             url = getFileURL(contextURL, path);
115         }
116 
117         // Everything is OK with this URL, although a file url constructed
118         // above may not exist.  This will be caught later when the URL is
119         // accessed.
120         return url;
121     }    // getURL
122 
123     /**
124      * Method getFileURL
125      *
126      * @param contextURL
127      * @param path
128      * @throws IOException
129      */
130     protected URL getFileURL(URL contextURL, String path)
131             throws IOException {
132 
133         if (contextURL != null) {
134 
135             // get the parent directory of the contextURL, and append
136             // the spec string to the end.
137             String contextFileName = contextURL.getFile();
138             URL parent = null;
139             //the logic for finding the parent file is this.
140             //1.if the contextURI represents a file then take the parent file
141             //of it
142             //2. If the contextURI represents a directory, then take that as
143             //the parent
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     }    // getFileURL
162 }