1 /* 2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.xml.catalog; 26 27 import java.io.File; 28 import java.net.MalformedURLException; 29 import java.net.URI; 30 import java.net.URISyntaxException; 31 import java.net.URL; 32 import java.nio.file.Path; 33 import java.nio.file.Paths; 34 import java.util.Iterator; 35 import jdk.xml.internal.SecuritySupport; 36 37 /** 38 * 39 * @since 9 40 */ 41 class Util { 42 final static String URN = "urn:publicid:"; 43 final static String PUBLICID_PREFIX = "-//"; 44 final static String PUBLICID_PREFIX_ALT = "+//"; 45 46 /** 47 * Finds an entry in the catalog that matches with the publicId or systemId. 48 * 49 * The resolution follows the following rules determined by the prefer setting: 50 * 51 * prefer "system": attempts to resolve with a system entry; 52 * attempts to resolve with a public entry when only 53 * publicId is specified. 54 * 55 * prefer "public": attempts to resolve with a system entry; 56 * attempts to resolve with a public entry if no matching 57 * system entry is found. 58 * 59 * If no match is found, continue searching uri entries 60 * 61 * @param catalog the catalog 62 * @param publicId the publicId 63 * @param systemId the systemId 64 * @return the resolved systemId if a match is found, null otherwise 65 */ 66 static String resolve(CatalogImpl catalog, String publicId, String systemId) { 67 String resolvedSystemId = null; 68 69 //search the current catalog 70 catalog.reset(); 71 if (systemId != null) { 72 /* 73 If a system identifier is specified, it is used no matter how 74 prefer is set. 75 */ 76 resolvedSystemId = catalog.matchSystem(systemId); 77 } 78 79 if (resolvedSystemId == null && publicId != null) { 80 resolvedSystemId = catalog.matchPublic(publicId); 81 } 82 83 if (resolvedSystemId == null && systemId != null) { 84 resolvedSystemId = catalog.matchURI(systemId); 85 } 86 87 //mark the catalog as having been searched before trying alternatives 88 catalog.markAsSearched(); 89 90 //search alternative catalogs 91 if (resolvedSystemId == null) { 92 Iterator<Catalog> iter = catalog.catalogs().iterator(); 93 while (iter.hasNext()) { 94 resolvedSystemId = resolve((CatalogImpl)iter.next(), publicId, systemId); 95 if (resolvedSystemId != null) { 96 break; 97 } 98 99 } 100 } 101 102 return resolvedSystemId; 103 } 104 105 /** 106 * Resolves the specified file path to an absolute systemId. If it is 107 * relative, it shall be resolved using the base or user.dir property if 108 * base is not specified. 109 * 110 * @param file The specified file path 111 * @param baseURI the base URI 112 * @return The URI 113 * @throws CatalogException if the specified file path can not be converted 114 * to a system id 115 */ 116 static URI verifyAndGetURI(String file, URL baseURI) 117 throws MalformedURLException, URISyntaxException, IllegalArgumentException { 118 URL filepath; 119 URI temp; 120 if (file != null && file.length() > 0) { 121 File f = new File(file); 122 123 if (baseURI != null && !f.isAbsolute()) { 124 filepath = new URL(baseURI, fixSlashes(file)); 125 temp = filepath.toURI(); 126 } else { 127 temp = resolveURI(file); 128 } 129 //Paths.get may throw IllegalArgumentException 130 Path path = Paths.get(temp); 131 if (path.toFile().isFile()) { 132 return temp; 133 } 134 } 135 return null; 136 } 137 138 /** 139 * Resolves the specified uri. If the uri is relative, makes it absolute by 140 * the user.dir directory. 141 * 142 * @param uri The specified URI. 143 * @return The resolved URI 144 */ 145 static URI resolveURI(String uri) throws MalformedURLException { 146 if (uri == null) { 147 uri = ""; 148 } 149 150 URI temp = null; 151 try { 152 URL url = new URL(uri); 153 temp = url.toURI(); 154 } catch (MalformedURLException | URISyntaxException mue) { 155 File file = new File(uri); 156 temp = file.toURI(); 157 } 158 159 return temp; 160 } 161 162 /** 163 * Replace backslashes with forward slashes. (URLs always use forward 164 * slashes.) 165 * 166 * @param sysid The input system identifier. 167 * @return The same system identifier with backslashes turned into forward 168 * slashes. 169 */ 170 static String fixSlashes(String sysid) { 171 return sysid.replace('\\', '/'); 172 } 173 174 /** 175 * Find catalog file paths by reading the system property, and then 176 * jaxp.properties if the system property is not specified. 177 * 178 * @param sysPropertyName the name of system property 179 * @return the catalog file paths, or null if not found. 180 */ 181 static String[] getCatalogFiles(String sysPropertyName) { 182 String value = SecuritySupport.getJAXPSystemProperty(sysPropertyName); 183 if (value != null && !value.equals("")) { 184 return value.split(";"); 185 } 186 return null; 187 } 188 189 /** 190 * Checks whether the specified string is null or empty, returns the original 191 * string with leading and trailing spaces removed if not. 192 * @param test the string to be tested 193 * @return the original string with leading and trailing spaces removed, 194 * or null if it is null or empty 195 * 196 */ 197 static String getNotNullOrEmpty(String test) { 198 if (test == null) { 199 return test; 200 } else { 201 String temp = test.trim(); 202 if (temp.length() == 0) { 203 return null; 204 } else { 205 return temp; 206 } 207 } 208 } 209 }