|
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.Objects; |
37 | 36 | import java.util.Optional; |
38 | 37 | import java.util.Set; |
39 | 38 | import java.util.regex.Pattern; |
40 | 39 | import java.util.stream.Collectors; |
41 | 40 | import java.util.stream.Stream; |
42 | | -import javax.xml.parsers.DocumentBuilder; |
43 | | -import javax.xml.parsers.DocumentBuilderFactory; |
44 | | -import javax.xml.parsers.ParserConfigurationException; |
45 | | -import javax.xml.xpath.XPath; |
46 | 41 | import javax.xml.xpath.XPathConstants; |
47 | 42 | import javax.xml.xpath.XPathFactory; |
48 | 43 | import jdk.jpackage.internal.RetryExecutor; |
| 44 | +import jdk.jpackage.internal.util.PListReader; |
49 | 45 | import jdk.jpackage.internal.util.PathUtils; |
| 46 | +import jdk.jpackage.internal.util.XmlUtils; |
50 | 47 | import jdk.jpackage.internal.util.function.ThrowingConsumer; |
51 | 48 | import jdk.jpackage.internal.util.function.ThrowingSupplier; |
52 | 49 | import jdk.jpackage.test.PackageTest.PackageHandlers; |
53 | | -import org.w3c.dom.NodeList; |
54 | | -import org.xml.sax.SAXException; |
55 | 50 |
|
56 | 51 | public final class MacHelper { |
57 | 52 |
|
@@ -129,25 +124,25 @@ public static void withExplodedDmg(JPackageCommand cmd, |
129 | 124 | } |
130 | 125 | } |
131 | 126 |
|
132 | | - public static PListWrapper readPListFromAppImage(Path appImage) { |
| 127 | + public static PListReader readPListFromAppImage(Path appImage) { |
133 | 128 | return readPList(appImage.resolve("Contents/Info.plist")); |
134 | 129 | } |
135 | 130 |
|
136 | | - public static PListWrapper readPList(Path path) { |
| 131 | + public static PListReader readPList(Path path) { |
137 | 132 | TKit.assertReadableFileExists(path); |
138 | 133 | return ThrowingSupplier.toSupplier(() -> readPList(Files.readAllLines( |
139 | 134 | path))).get(); |
140 | 135 | } |
141 | 136 |
|
142 | | - public static PListWrapper readPList(List<String> lines) { |
| 137 | + public static PListReader readPList(List<String> lines) { |
143 | 138 | return readPList(lines.stream()); |
144 | 139 | } |
145 | 140 |
|
146 | | - public static PListWrapper readPList(Stream<String> lines) { |
147 | | - return ThrowingSupplier.toSupplier(() -> new PListWrapper(lines |
| 141 | + public static PListReader readPList(Stream<String> lines) { |
| 142 | + return ThrowingSupplier.toSupplier(() -> new PListReader(lines |
148 | 143 | // Skip leading lines before xml declaration |
149 | 144 | .dropWhile(Pattern.compile("\\s?<\\?xml\\b.+\\?>").asPredicate().negate()) |
150 | | - .collect(Collectors.joining()))).get(); |
| 145 | + .collect(Collectors.joining()).getBytes(StandardCharsets.UTF_8))).get(); |
151 | 146 | } |
152 | 147 |
|
153 | 148 | public static boolean signPredefinedAppImage(JPackageCommand cmd) { |
@@ -261,7 +256,7 @@ private static Path unpackPkg(JPackageCommand cmd, Path destinationDir) { |
261 | 256 | }).forEach(ThrowingConsumer.toConsumer(pkgDir -> { |
262 | 257 | // Installation root of the package is stored in |
263 | 258 | // /pkg-info@install-location attribute in $pkgDir/PackageInfo xml file |
264 | | - var doc = createDocumentBuilder().parse( |
| 259 | + var doc = XmlUtils.initDocumentBuilder().parse( |
265 | 260 | new ByteArrayInputStream(Files.readAllBytes( |
266 | 261 | pkgDir.resolve("PackageInfo")))); |
267 | 262 | var xPath = XPathFactory.newInstance().newXPath(); |
@@ -370,73 +365,10 @@ private static String getPackageId(JPackageCommand cmd) { |
370 | 365 | }); |
371 | 366 | } |
372 | 367 |
|
373 | | - public static final class PListWrapper { |
374 | | - public String queryValue(String keyName) { |
375 | | - XPath xPath = XPathFactory.newInstance().newXPath(); |
376 | | - // Query for the value of <string> element preceding <key> element |
377 | | - // with value equal to `keyName` |
378 | | - String query = String.format( |
379 | | - "//string[preceding-sibling::key = \"%s\"][1]", keyName); |
380 | | - return ThrowingSupplier.toSupplier(() -> (String) xPath.evaluate( |
381 | | - query, doc, XPathConstants.STRING)).get(); |
382 | | - } |
383 | | - |
384 | | - public Boolean queryBoolValue(String keyName) { |
385 | | - XPath xPath = XPathFactory.newInstance().newXPath(); |
386 | | - // Query boolean element preceding <key> element |
387 | | - // with value equal to `keyName` |
388 | | - String query = String.format( |
389 | | - "name(//*[preceding-sibling::key = \"%s\"])", keyName); |
390 | | - String value = ThrowingSupplier.toSupplier(() -> (String) xPath.evaluate( |
391 | | - query, doc, XPathConstants.STRING)).get(); |
392 | | - return Boolean.valueOf(value); |
393 | | - } |
394 | | - |
395 | | - public List<String> queryArrayValue(String keyName) { |
396 | | - XPath xPath = XPathFactory.newInstance().newXPath(); |
397 | | - // Query string array preceding <key> element with value equal to `keyName` |
398 | | - String query = String.format( |
399 | | - "//array[preceding-sibling::key = \"%s\"]", keyName); |
400 | | - NodeList list = ThrowingSupplier.toSupplier(() -> (NodeList) xPath.evaluate( |
401 | | - query, doc, XPathConstants.NODESET)).get(); |
402 | | - if (list.getLength() != 1) { |
403 | | - throw new RuntimeException( |
404 | | - String.format("Unable to find <array> element for key = \"%s\"]", |
405 | | - keyName)); |
406 | | - } |
407 | | - |
408 | | - NodeList childList = list.item(0).getChildNodes(); |
409 | | - List<String> values = new ArrayList<>(childList.getLength()); |
410 | | - for (int i = 0; i < childList.getLength(); i++) { |
411 | | - if (childList.item(i).getNodeName().equals("string")) { |
412 | | - values.add(childList.item(i).getTextContent()); |
413 | | - } |
414 | | - } |
415 | | - return values; |
416 | | - } |
417 | | - |
418 | | - private PListWrapper(String xml) throws ParserConfigurationException, |
419 | | - SAXException, IOException { |
420 | | - doc = createDocumentBuilder().parse(new ByteArrayInputStream( |
421 | | - xml.getBytes(StandardCharsets.UTF_8))); |
422 | | - } |
423 | | - |
424 | | - private final org.w3c.dom.Document doc; |
425 | | - } |
426 | | - |
427 | 368 | public static boolean isXcodeDevToolsInstalled() { |
428 | 369 | return Inner.XCODE_DEV_TOOLS_INSTALLED; |
429 | 370 | } |
430 | 371 |
|
431 | | - private static DocumentBuilder createDocumentBuilder() throws |
432 | | - ParserConfigurationException { |
433 | | - DocumentBuilderFactory dbf = DocumentBuilderFactory.newDefaultInstance(); |
434 | | - dbf.setFeature( |
435 | | - "http://apache.org/xml/features/nonvalidating/load-external-dtd", |
436 | | - false); |
437 | | - return dbf.newDocumentBuilder(); |
438 | | - } |
439 | | - |
440 | 372 | private static String getServicePListFileName(String packageName, |
441 | 373 | String launcherName) { |
442 | 374 | try { |
|
0 commit comments