|
31 | 31 | import java.nio.charset.StandardCharsets; |
32 | 32 | import java.nio.file.Files; |
33 | 33 | import java.nio.file.Path; |
34 | | -import java.util.ArrayList; |
35 | 34 | import java.util.List; |
36 | 35 | import java.util.Optional; |
37 | 36 | import java.util.Set; |
38 | 37 | import java.util.regex.Pattern; |
39 | 38 | import java.util.stream.Collectors; |
40 | 39 | import java.util.stream.Stream; |
41 | | -import javax.xml.parsers.DocumentBuilder; |
42 | | -import javax.xml.parsers.DocumentBuilderFactory; |
43 | | -import javax.xml.parsers.ParserConfigurationException; |
44 | | -import javax.xml.xpath.XPath; |
45 | 40 | import javax.xml.xpath.XPathConstants; |
46 | 41 | import javax.xml.xpath.XPathFactory; |
47 | 42 | import jdk.jpackage.internal.RetryExecutor; |
| 43 | +import jdk.jpackage.internal.util.PListReader; |
48 | 44 | import jdk.jpackage.internal.util.PathUtils; |
| 45 | +import jdk.jpackage.internal.util.XmlUtils; |
49 | 46 | import jdk.jpackage.internal.util.function.ThrowingConsumer; |
50 | 47 | import jdk.jpackage.internal.util.function.ThrowingSupplier; |
51 | 48 | import jdk.jpackage.test.PackageTest.PackageHandlers; |
52 | | -import org.w3c.dom.NodeList; |
53 | | -import org.xml.sax.SAXException; |
54 | 49 |
|
55 | 50 | public final class MacHelper { |
56 | 51 |
|
@@ -128,25 +123,25 @@ public static void withExplodedDmg(JPackageCommand cmd, |
128 | 123 | } |
129 | 124 | } |
130 | 125 |
|
131 | | - public static PListWrapper readPListFromAppImage(Path appImage) { |
| 126 | + public static PListReader readPListFromAppImage(Path appImage) { |
132 | 127 | return readPList(appImage.resolve("Contents/Info.plist")); |
133 | 128 | } |
134 | 129 |
|
135 | | - public static PListWrapper readPList(Path path) { |
| 130 | + public static PListReader readPList(Path path) { |
136 | 131 | TKit.assertReadableFileExists(path); |
137 | 132 | return ThrowingSupplier.toSupplier(() -> readPList(Files.readAllLines( |
138 | 133 | path))).get(); |
139 | 134 | } |
140 | 135 |
|
141 | | - public static PListWrapper readPList(List<String> lines) { |
| 136 | + public static PListReader readPList(List<String> lines) { |
142 | 137 | return readPList(lines.stream()); |
143 | 138 | } |
144 | 139 |
|
145 | | - public static PListWrapper readPList(Stream<String> lines) { |
146 | | - return ThrowingSupplier.toSupplier(() -> new PListWrapper(lines |
| 140 | + public static PListReader readPList(Stream<String> lines) { |
| 141 | + return ThrowingSupplier.toSupplier(() -> new PListReader(lines |
147 | 142 | // Skip leading lines before xml declaration |
148 | 143 | .dropWhile(Pattern.compile("\\s?<\\?xml\\b.+\\?>").asPredicate().negate()) |
149 | | - .collect(Collectors.joining()))).get(); |
| 144 | + .collect(Collectors.joining()).getBytes(StandardCharsets.UTF_8))).get(); |
150 | 145 | } |
151 | 146 |
|
152 | 147 | static PackageHandlers createDmgPackageHandlers() { |
@@ -217,7 +212,7 @@ static PackageHandlers createPkgPackageHandlers() { |
217 | 212 | }).forEach(ThrowingConsumer.toConsumer(pkgDir -> { |
218 | 213 | // Installation root of the package is stored in |
219 | 214 | // /pkg-info@install-location attribute in $pkgDir/PackageInfo xml file |
220 | | - var doc = createDocumentBuilder().parse( |
| 215 | + var doc = XmlUtils.initDocumentBuilder().parse( |
221 | 216 | new ByteArrayInputStream(Files.readAllBytes( |
222 | 217 | pkgDir.resolve("PackageInfo")))); |
223 | 218 | var xPath = XPathFactory.newInstance().newXPath(); |
@@ -341,69 +336,6 @@ private static String getPackageId(JPackageCommand cmd) { |
341 | 336 | }); |
342 | 337 | } |
343 | 338 |
|
344 | | - public static final class PListWrapper { |
345 | | - public String queryValue(String keyName) { |
346 | | - XPath xPath = XPathFactory.newInstance().newXPath(); |
347 | | - // Query for the value of <string> element preceding <key> element |
348 | | - // with value equal to `keyName` |
349 | | - String query = String.format( |
350 | | - "//string[preceding-sibling::key = \"%s\"][1]", keyName); |
351 | | - return ThrowingSupplier.toSupplier(() -> (String) xPath.evaluate( |
352 | | - query, doc, XPathConstants.STRING)).get(); |
353 | | - } |
354 | | - |
355 | | - public Boolean queryBoolValue(String keyName) { |
356 | | - XPath xPath = XPathFactory.newInstance().newXPath(); |
357 | | - // Query boolean element preceding <key> element |
358 | | - // with value equal to `keyName` |
359 | | - String query = String.format( |
360 | | - "name(//*[preceding-sibling::key = \"%s\"])", keyName); |
361 | | - String value = ThrowingSupplier.toSupplier(() -> (String) xPath.evaluate( |
362 | | - query, doc, XPathConstants.STRING)).get(); |
363 | | - return Boolean.valueOf(value); |
364 | | - } |
365 | | - |
366 | | - public List<String> queryArrayValue(String keyName) { |
367 | | - XPath xPath = XPathFactory.newInstance().newXPath(); |
368 | | - // Query string array preceding <key> element with value equal to `keyName` |
369 | | - String query = String.format( |
370 | | - "//array[preceding-sibling::key = \"%s\"]", keyName); |
371 | | - NodeList list = ThrowingSupplier.toSupplier(() -> (NodeList) xPath.evaluate( |
372 | | - query, doc, XPathConstants.NODESET)).get(); |
373 | | - if (list.getLength() != 1) { |
374 | | - throw new RuntimeException( |
375 | | - String.format("Unable to find <array> element for key = \"%s\"]", |
376 | | - keyName)); |
377 | | - } |
378 | | - |
379 | | - NodeList childList = list.item(0).getChildNodes(); |
380 | | - List<String> values = new ArrayList<>(childList.getLength()); |
381 | | - for (int i = 0; i < childList.getLength(); i++) { |
382 | | - if (childList.item(i).getNodeName().equals("string")) { |
383 | | - values.add(childList.item(i).getTextContent()); |
384 | | - } |
385 | | - } |
386 | | - return values; |
387 | | - } |
388 | | - |
389 | | - private PListWrapper(String xml) throws ParserConfigurationException, |
390 | | - SAXException, IOException { |
391 | | - doc = createDocumentBuilder().parse(new ByteArrayInputStream( |
392 | | - xml.getBytes(StandardCharsets.UTF_8))); |
393 | | - } |
394 | | - |
395 | | - private final org.w3c.dom.Document doc; |
396 | | - } |
397 | | - |
398 | | - private static DocumentBuilder createDocumentBuilder() throws |
399 | | - ParserConfigurationException { |
400 | | - DocumentBuilderFactory dbf = DocumentBuilderFactory.newDefaultInstance(); |
401 | | - dbf.setFeature( |
402 | | - "http://apache.org/xml/features/nonvalidating/load-external-dtd", |
403 | | - false); |
404 | | - return dbf.newDocumentBuilder(); |
405 | | - } |
406 | | - |
407 | 339 | private static String getServicePListFileName(String packageName, |
408 | 340 | String launcherName) { |
409 | 341 | try { |
|
0 commit comments