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