|
1 /* |
|
2 * Copyright (c) 2019, 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 jdk.jpackage.internal; |
|
26 |
|
27 import java.io.BufferedWriter; |
|
28 import java.io.FileInputStream; |
|
29 import java.io.FileWriter; |
|
30 import java.io.IOException; |
|
31 import java.io.Writer; |
|
32 import java.nio.file.Path; |
|
33 import java.util.ArrayList; |
|
34 import java.util.Collections; |
|
35 import java.util.List; |
|
36 import java.util.ArrayList; |
|
37 import java.util.Map; |
|
38 import java.util.regex.Matcher; |
|
39 import java.util.regex.Pattern; |
|
40 import java.util.stream.Collectors; |
|
41 import javax.xml.parsers.DocumentBuilder; |
|
42 import javax.xml.parsers.DocumentBuilderFactory; |
|
43 import javax.xml.parsers.ParserConfigurationException; |
|
44 import javax.xml.stream.XMLOutputFactory; |
|
45 import javax.xml.stream.XMLStreamException; |
|
46 import javax.xml.stream.XMLStreamWriter; |
|
47 import javax.xml.xpath.XPath; |
|
48 import javax.xml.xpath.XPathConstants; |
|
49 import javax.xml.xpath.XPathExpressionException; |
|
50 import javax.xml.xpath.XPathFactory; |
|
51 import org.w3c.dom.Document; |
|
52 import org.w3c.dom.NodeList; |
|
53 import org.xml.sax.SAXException; |
|
54 |
|
55 import static jdk.jpackage.internal.StandardBundlerParam.*; |
|
56 |
|
57 class AppImageFile { |
|
58 |
|
59 // These values will be loaded from AppImage xml file. |
|
60 private final String creatorVersion; |
|
61 private final String creatorPlatform; |
|
62 private final String launcherName; |
|
63 private final List<String> addLauncherNames; |
|
64 |
|
65 final static String XML_FILENAME = ".jpackage.xml"; |
|
66 |
|
67 private final static Map<Platform, String> PLATFORM_LABELS = Map.of( |
|
68 Platform.LINUX, "linux", Platform.WINDOWS, "windows", Platform.MAC, |
|
69 "macOS"); |
|
70 |
|
71 |
|
72 private AppImageFile() { |
|
73 this(null, null, null, null); |
|
74 } |
|
75 |
|
76 private AppImageFile(String launcherName, List<String> addLauncherNames, |
|
77 String creatorVersion, String creatorPlatform) { |
|
78 this.launcherName = launcherName; |
|
79 this.addLauncherNames = addLauncherNames; |
|
80 this.creatorVersion = creatorVersion; |
|
81 this.creatorPlatform = creatorPlatform; |
|
82 } |
|
83 |
|
84 /** |
|
85 * Would return null to indicate stored command line is invalid. |
|
86 */ |
|
87 List<String> getAddLauncherNames() { |
|
88 return addLauncherNames; |
|
89 } |
|
90 |
|
91 String getLauncherName() { |
|
92 return launcherName; |
|
93 } |
|
94 |
|
95 void verifyCompatible() throws ConfigException { |
|
96 // Just do nohing for now. |
|
97 } |
|
98 |
|
99 static void save(Path appImage, Map<String, Object> params) |
|
100 throws IOException { |
|
101 Path xmlFile = appImage.resolve(XML_FILENAME); |
|
102 XMLOutputFactory xmlFactory = XMLOutputFactory.newInstance(); |
|
103 |
|
104 try (Writer w = new BufferedWriter(new FileWriter(xmlFile.toFile()))) { |
|
105 XMLStreamWriter xml = xmlFactory.createXMLStreamWriter(w); |
|
106 |
|
107 xml.writeStartDocument(); |
|
108 xml.writeStartElement("jpackage-state"); |
|
109 xml.writeAttribute("version", getVersion()); |
|
110 xml.writeAttribute("platform", getPlatform()); |
|
111 |
|
112 xml.writeStartElement("main-launcher"); |
|
113 xml.writeCharacters(APP_NAME.fetchFrom(params)); |
|
114 xml.writeEndElement(); |
|
115 |
|
116 List<Map<String, ? super Object>> addLaunchers = |
|
117 ADD_LAUNCHERS.fetchFrom(params); |
|
118 |
|
119 for (int i = 0; i < addLaunchers.size(); i++) { |
|
120 Map<String, ? super Object> sl = addLaunchers.get(i); |
|
121 xml.writeStartElement("add-launcher"); |
|
122 xml.writeCharacters(APP_NAME.fetchFrom(sl)); |
|
123 xml.writeEndElement(); |
|
124 } |
|
125 |
|
126 xml.writeEndElement(); |
|
127 xml.writeEndDocument(); |
|
128 xml.flush(); |
|
129 xml.close(); |
|
130 |
|
131 } catch (XMLStreamException ex) { |
|
132 Log.verbose(ex); |
|
133 throw new IOException(ex); |
|
134 } |
|
135 } |
|
136 |
|
137 static AppImageFile load(Path appImageDir) throws IOException { |
|
138 try { |
|
139 Path path = appImageDir.resolve(XML_FILENAME); |
|
140 DocumentBuilderFactory dbf = |
|
141 DocumentBuilderFactory.newDefaultInstance(); |
|
142 dbf.setFeature( |
|
143 "http://apache.org/xml/features/nonvalidating/load-external-dtd", |
|
144 false); |
|
145 DocumentBuilder b = dbf.newDocumentBuilder(); |
|
146 Document doc = b.parse(new FileInputStream(path.toFile())); |
|
147 |
|
148 XPath xPath = XPathFactory.newInstance().newXPath(); |
|
149 |
|
150 String mainLauncher = xpathQueryNullable(xPath, |
|
151 "/jpackage-state/main-launcher/text()", doc); |
|
152 if (mainLauncher == null) { |
|
153 // No main launcher, this is fatal. |
|
154 return new AppImageFile(); |
|
155 } |
|
156 |
|
157 List<String> addLaunchers = new ArrayList<String>(); |
|
158 |
|
159 String platform = xpathQueryNullable(xPath, |
|
160 "/jpackage-state/@platform", doc); |
|
161 |
|
162 String version = xpathQueryNullable(xPath, |
|
163 "/jpackage-state/@version", doc); |
|
164 |
|
165 NodeList launcherNameNodes = (NodeList) xPath.evaluate( |
|
166 "/jpackage-state/add-launcher/text()", doc, |
|
167 XPathConstants.NODESET); |
|
168 |
|
169 for (int i = 0; i != launcherNameNodes.getLength(); i++) { |
|
170 addLaunchers.add(launcherNameNodes.item(i).getNodeValue()); |
|
171 } |
|
172 |
|
173 AppImageFile file = new AppImageFile( |
|
174 mainLauncher, addLaunchers, version, platform); |
|
175 if (!file.isValid()) { |
|
176 file = new AppImageFile(); |
|
177 } |
|
178 return file; |
|
179 } catch (ParserConfigurationException | SAXException ex) { |
|
180 // Let caller sort this out |
|
181 throw new IOException(ex); |
|
182 } catch (XPathExpressionException ex) { |
|
183 // This should never happen as XPath expressions should be correct |
|
184 throw new RuntimeException(ex); |
|
185 } |
|
186 } |
|
187 |
|
188 private static String xpathQueryNullable(XPath xPath, String xpathExpr, |
|
189 Document xml) throws XPathExpressionException { |
|
190 NodeList nodes = (NodeList) xPath.evaluate(xpathExpr, xml, |
|
191 XPathConstants.NODESET); |
|
192 if (nodes != null && nodes.getLength() > 0) { |
|
193 return nodes.item(0).getNodeValue(); |
|
194 } |
|
195 return null; |
|
196 } |
|
197 |
|
198 private static String getVersion() { |
|
199 return System.getProperty("java.version"); |
|
200 } |
|
201 |
|
202 private static String getPlatform() { |
|
203 return PLATFORM_LABELS.get(Platform.getPlatform()); |
|
204 } |
|
205 |
|
206 private boolean isValid() { |
|
207 if (launcherName == null || launcherName.length() == 0 || |
|
208 addLauncherNames.indexOf("") != -1) { |
|
209 // Some launchers have empty names. This is invalid. |
|
210 return false; |
|
211 } |
|
212 |
|
213 // Add more validation. |
|
214 |
|
215 return true; |
|
216 } |
|
217 |
|
218 } |