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 * @param catalog the catalog 59 * @param publicId the publicId 60 * @param systemId the systemId 61 * @return the resolved systemId if a match is found, null otherwise 62 */ 63 static String resolve(CatalogImpl catalog, String publicId, String systemId) { 64 String resolvedSystemId = null; 65 66 //search the current catalog 67 catalog.reset(); 68 if (systemId != null) { 69 /* 70 If a system identifier is specified, it is used no matter how 71 prefer is set. 72 */ 73 resolvedSystemId = catalog.matchSystem(systemId); 74 } 75 76 if (resolvedSystemId == null && publicId != null) { 77 resolvedSystemId = catalog.matchPublic(publicId); 78 } 79 80 //mark the catalog as having been searched before trying alternatives 81 catalog.markAsSearched(); 82 83 //search alternative catalogs 84 if (resolvedSystemId == null) { 85 Iterator<Catalog> iter = catalog.catalogs().iterator(); 86 while (iter.hasNext()) { 87 resolvedSystemId = resolve((CatalogImpl)iter.next(), publicId, systemId); 88 if (resolvedSystemId != null) { 89 break; 90 } 91 92 } 93 } 94 95 return resolvedSystemId; 96 } 97 98 /** 99 * Resolves the specified file path to an absolute systemId. If it is 100 * relative, it shall be resolved using the base or user.dir property if 101 * base is not specified. 102 * 103 * @param file The specified file path 104 * @param baseURI the base URI 105 * @return The URI 106 * @throws CatalogException if the specified file path can not be converted 107 * to a system id 108 */ 109 static URI verifyAndGetURI(String file, URL baseURI) 110 throws MalformedURLException, URISyntaxException, IllegalArgumentException { 111 URL filepath; 112 URI temp; 113 if (file != null && file.length() > 0) { 114 File f = new File(file); 115 116 if (baseURI != null && !f.isAbsolute()) { 117 filepath = new URL(baseURI, fixSlashes(file)); 118 temp = filepath.toURI(); 119 } else { 120 temp = resolveURI(file); 121 } 122 //Paths.get may throw IllegalArgumentException 123 Path path = Paths.get(temp); 124 if (path.toFile().isFile()) { 125 return temp; 126 } 127 } 128 return null; 129 } 130 131 /** 132 * Resolves the specified uri. If the uri is relative, makes it absolute by 133 * the user.dir directory. 134 * 135 * @param uri The specified URI. 136 * @return The resolved URI 137 */ 138 static URI resolveURI(String uri) throws MalformedURLException { 139 if (uri == null) { 140 uri = ""; 141 } 142 143 URI temp = null; 144 try { 145 URL url = new URL(uri); 146 temp = url.toURI(); 147 } catch (MalformedURLException | URISyntaxException mue) { 148 File file = new File(uri); 149 temp = file.toURI(); 150 } 151 152 return temp; 153 } 154 155 /** 156 * Replace backslashes with forward slashes. (URLs always use forward 157 * slashes.) 158 * 159 * @param sysid The input system identifier. 160 * @return The same system identifier with backslashes turned into forward 161 * slashes. 162 */ 163 static String fixSlashes(String sysid) { 164 return sysid.replace('\\', '/'); 165 } 166 167 /** 168 * Find catalog file paths by reading the system property, and then 169 * jaxp.properties if the system property is not specified. 170 * 171 * @param sysPropertyName the name of system property 172 * @return the catalog file paths, or null if not found. 173 */ 174 static String[] getCatalogFiles(String sysPropertyName) { 175 String value = SecuritySupport.getJAXPSystemProperty(sysPropertyName); 176 if (value != null && !value.equals("")) { 177 return value.split(";"); 178 } 179 return null; 180 } 181 182 /** 183 * Checks whether the specified string is null or empty, returns the original 184 * string with leading and trailing spaces removed if not. 185 * @param test the string to be tested 186 * @return the original string with leading and trailing spaces removed, 187 * or null if it is null or empty 188 * 189 */ 190 static String getNotNullOrEmpty(String test) { 191 if (test == null) { 192 return test; 193 } else { 194 String temp = test.trim(); 195 if (temp.length() == 0) { 196 return null; 197 } else { 198 return temp; 199 } 200 } 201 } 202 }