author | joehw |
Tue, 17 May 2016 14:14:15 -0700 | |
changeset 38497 | 06b1977d0f4f |
parent 37382 | c7d898d8da12 |
child 40582 | 1dddef49982c |
permissions | -rw-r--r-- |
33542 | 1 |
/* |
36700
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
33542 | 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 |
||
36700
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
27 |
import java.io.File; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
28 |
import java.net.MalformedURLException; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
29 |
import java.net.URI; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
30 |
import java.net.URISyntaxException; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
31 |
import java.net.URL; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
32 |
import java.nio.file.Path; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
33 |
import java.nio.file.Paths; |
38497 | 34 |
import java.util.Iterator; |
33542 | 35 |
import jdk.xml.internal.SecuritySupport; |
36 |
||
37 |
/** |
|
38 |
* |
|
39 |
* @since 9 |
|
40 |
*/ |
|
41 |
class Util { |
|
38497 | 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 |
} |
|
36700
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
97 |
|
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
98 |
/** |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
99 |
* Resolves the specified file path to an absolute systemId. If it is |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
100 |
* relative, it shall be resolved using the base or user.dir property if |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
101 |
* base is not specified. |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
102 |
* |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
103 |
* @param file The specified file path |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
104 |
* @param baseURI the base URI |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
105 |
* @return The URI |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
106 |
* @throws CatalogException if the specified file path can not be converted |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
107 |
* to a system id |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
108 |
*/ |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
109 |
static URI verifyAndGetURI(String file, URL baseURI) |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
110 |
throws MalformedURLException, URISyntaxException, IllegalArgumentException { |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
111 |
URL filepath; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
112 |
URI temp; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
113 |
if (file != null && file.length() > 0) { |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
114 |
File f = new File(file); |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
115 |
|
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
116 |
if (baseURI != null && !f.isAbsolute()) { |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
117 |
filepath = new URL(baseURI, fixSlashes(file)); |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
118 |
temp = filepath.toURI(); |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
119 |
} else { |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
120 |
temp = resolveURI(file); |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
121 |
} |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
122 |
//Paths.get may throw IllegalArgumentException |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
123 |
Path path = Paths.get(temp); |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
124 |
if (path.toFile().isFile()) { |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
125 |
return temp; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
126 |
} |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
127 |
} |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
128 |
return null; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
129 |
} |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
130 |
|
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
131 |
/** |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
132 |
* Resolves the specified uri. If the uri is relative, makes it absolute by |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
133 |
* the user.dir directory. |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
134 |
* |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
135 |
* @param uri The specified URI. |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
136 |
* @return The resolved URI |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
137 |
*/ |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
138 |
static URI resolveURI(String uri) throws MalformedURLException { |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
139 |
if (uri == null) { |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
140 |
uri = ""; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
141 |
} |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
142 |
|
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
143 |
URI temp = null; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
144 |
try { |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
145 |
URL url = new URL(uri); |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
146 |
temp = url.toURI(); |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
147 |
} catch (MalformedURLException | URISyntaxException mue) { |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
148 |
File file = new File(uri); |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
149 |
temp = file.toURI(); |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
150 |
} |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
151 |
|
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
152 |
return temp; |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
153 |
} |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
154 |
|
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
155 |
/** |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
156 |
* Replace backslashes with forward slashes. (URLs always use forward |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
157 |
* slashes.) |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
158 |
* |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
159 |
* @param sysid The input system identifier. |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
160 |
* @return The same system identifier with backslashes turned into forward |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
161 |
* slashes. |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
162 |
*/ |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
163 |
static String fixSlashes(String sysid) { |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
164 |
return sysid.replace('\\', '/'); |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
165 |
} |
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
166 |
|
33542 | 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 |
*/ |
|
36700
3afcaff239f5
8151154: IllegalArgumentException not thrown when wrong syntax value is set for javax.xml.catalog.files
joehw
parents:
33542
diff
changeset
|
174 |
static String[] getCatalogFiles(String sysPropertyName) { |
33542 | 175 |
String value = SecuritySupport.getJAXPSystemProperty(sysPropertyName); |
176 |
if (value != null && !value.equals("")) { |
|
177 |
return value.split(";"); |
|
178 |
} |
|
179 |
return null; |
|
180 |
} |
|
37382
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
181 |
|
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
182 |
/** |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
183 |
* Checks whether the specified string is null or empty, returns the original |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
184 |
* string with leading and trailing spaces removed if not. |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
185 |
* @param test the string to be tested |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
186 |
* @return the original string with leading and trailing spaces removed, |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
187 |
* or null if it is null or empty |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
188 |
* |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
189 |
*/ |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
190 |
static String getNotNullOrEmpty(String test) { |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
191 |
if (test == null) { |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
192 |
return test; |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
193 |
} else { |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
194 |
String temp = test.trim(); |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
195 |
if (temp.length() == 0) { |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
196 |
return null; |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
197 |
} else { |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
198 |
return temp; |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
199 |
} |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
200 |
} |
c7d898d8da12
8151162: Public entries not searched when prefer='system'
joehw
parents:
36700
diff
changeset
|
201 |
} |
33542 | 202 |
} |