Skip to content

Commit 6cddb4c

Browse files
Use XSLT to transform component .plist file
1 parent 1707c34 commit 6cddb4c

File tree

2 files changed

+68
-24
lines changed

2 files changed

+68
-24
lines changed

src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacPkgPackager.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828
import static jdk.jpackage.internal.util.PathUtils.normalizedAbsolutePathString;
2929

30+
import java.io.ByteArrayInputStream;
3031
import java.io.IOException;
31-
import java.io.PrintWriter;
3232
import java.io.UncheckedIOException;
3333
import java.net.URI;
3434
import java.net.URISyntaxException;
@@ -44,6 +44,10 @@
4444
import java.util.stream.Stream;
4545
import javax.xml.stream.XMLStreamException;
4646
import javax.xml.stream.XMLStreamWriter;
47+
import javax.xml.transform.TransformerException;
48+
import javax.xml.transform.TransformerFactory;
49+
import javax.xml.transform.stream.StreamResult;
50+
import javax.xml.transform.stream.StreamSource;
4751
import jdk.internal.util.Architecture;
4852
import jdk.internal.util.OSVersion;
4953
import jdk.jpackage.internal.PackagingPipeline.PackageTaskID;
@@ -53,6 +57,7 @@
5357
import jdk.jpackage.internal.model.MacPkgPackage;
5458
import jdk.jpackage.internal.model.Package;
5559
import jdk.jpackage.internal.model.PackagerException;
60+
import jdk.jpackage.internal.resources.ResourceLocator;
5661
import jdk.jpackage.internal.util.XmlUtils;
5762

5863
record MacPkgPackager(MacPkgPackage pkg, BuildEnv env, Optional<Services> services, Path outputDir) {
@@ -457,29 +462,13 @@ private void prepareConfigFiles() throws IOException {
457462
}
458463

459464
private void patchCPLFile(Path cpl) throws IOException {
460-
String cplData = Files.readString(cpl);
461-
String[] lines = cplData.split("\n");
462-
try (PrintWriter out = new PrintWriter(Files.newBufferedWriter(cpl))) {
463-
int skip = 0;
464-
// Used to skip Java.runtime bundle, since
465-
// pkgbuild with --root will find two bundles app and Java runtime.
466-
// We cannot generate component proprty list when using
467-
// --component argument.
468-
for (int i = 0; i < lines.length; i++) {
469-
if (lines[i].trim().equals("<key>BundleIsRelocatable</key>")) {
470-
out.println(lines[i]);
471-
out.println("<false/>");
472-
i++;
473-
} else if (lines[i].trim().equals("<key>ChildBundles</key>")) {
474-
++skip;
475-
} else if ((skip > 0) && lines[i].trim().equals("</array>")) {
476-
--skip;
477-
} else {
478-
if (skip == 0) {
479-
out.println(lines[i]);
480-
}
481-
}
482-
}
465+
try (final var xsltResource = ResourceLocator.class.getResourceAsStream("adjust-component-plist.xsl")) {
466+
final var srcXml = new StreamSource(new ByteArrayInputStream(Files.readAllBytes(cpl)));
467+
final var dstXml = new StreamResult(cpl.toFile());
468+
final var xslt = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltResource));
469+
xslt.transform(srcXml, dstXml);
470+
} catch (TransformerException ex) {
471+
throw new RuntimeException(ex);
483472
}
484473
}
485474

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
/*
4+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
5+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6+
*
7+
* This code is free software; you can redistribute it and/or modify it
8+
* under the terms of the GNU General Public License version 2 only, as
9+
* published by the Free Software Foundation. Oracle designates this
10+
* particular file as subject to the "Classpath" exception as provided
11+
* by Oracle in the LICENSE file that accompanied this code.
12+
*
13+
* This code is distributed in the hope that it will be useful, but WITHOUT
14+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16+
* version 2 for more details (a copy is included in the LICENSE file that
17+
* accompanied this code).
18+
*
19+
* You should have received a copy of the GNU General Public License version
20+
* 2 along with this work; if not, write to the Free Software Foundation,
21+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22+
*
23+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
24+
* or visit www.oracle.com if you need additional information or have any
25+
* questions.
26+
*/
27+
-->
28+
29+
<!--
30+
This stylesheet adjusts .plist file produced by `/usr/bin/pkgbuild &#45;&#45;analyze`.
31+
-->
32+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
33+
34+
<!--
35+
Make bundle NOT-relocatable
36+
-->
37+
<xsl:template match="true[preceding-sibling::key[1] = 'BundleIsRelocatable']">
38+
<false/>
39+
</xsl:template>
40+
41+
<!--
42+
Remove `ChildBundles` key with value
43+
-->
44+
<xsl:template match="key[. = 'ChildBundles']|array[preceding-sibling::key[1] = 'ChildBundles']"/>
45+
46+
<!--
47+
Identity transform
48+
-->
49+
<xsl:template match="@*|node()">
50+
<xsl:copy>
51+
<xsl:apply-templates select="@*|node()"/>
52+
</xsl:copy>
53+
</xsl:template>
54+
55+
</xsl:stylesheet>

0 commit comments

Comments
 (0)