Debian Codegraph @

bundles/org.eclipse.equinox.p2.touchpoint.eclipse/src/org/eclipse/equinox/internal/p2/update/ConfigurationParser.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2015 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.equinox.internal.p2.update;
15
16 import java.io.*;
17 import java.net.*;
18 import java.util.ArrayList;
19 import java.util.StringTokenizer;
20 import javax.xml.parsers.*;
21 import org.eclipse.core.runtime.URIUtil;
22 import org.eclipse.equinox.internal.p2.core.helpers.SecureXMLUtil;
23 import org.eclipse.equinox.p2.core.ProvisionException;
24 import org.eclipse.osgi.util.NLS;
25 import org.w3c.dom.*;
26 import org.xml.sax.SAXException;
27
28 /**
29 * Parser for platform.xml files.
30 *
31 * @since 1.0
32 */
33 public class ConfigurationParser implements ConfigurationConstants {
34 static final String PLATFORM_BASE = "platform:/base/"; //$NON-NLS-1$
35 private URL osgiInstallArea;
36
37 /*
38 * Parse the given file handle which points to a platform.xml file and a
39 * configuration object. Returns null if the file doesn't exist.
40 */
41 static Configuration parse(File file, URL osgiInstallArea) throws ProvisionException {
42 return new ConfigurationParser(osgiInstallArea).internalParse(file);
43 }
44
45 private ConfigurationParser(URL osgiInstallArea) {
46 this.osgiInstallArea = osgiInstallArea;
47 }
48
49 /*
50 * Create a feature object based on the given DOM node. Return the new feature.
51 */
52 private static Feature createFeature(Node node, Site site) {
53 Feature result = new Feature(site);
54 String id = getAttribute(node, ATTRIBUTE_ID);
55 if (id != null)
56 result.setId(id);
57 String url = getAttribute(node, ATTRIBUTE_URL);
58 if (url != null)
59 result.setUrl(url);
60 String version = getAttribute(node, ATTRIBUTE_VERSION);
61 if (version != null)
62 result.setVersion(version);
63 String pluginIdentifier = getAttribute(node, ATTRIBUTE_PLUGIN_IDENTIFIER);
64 if (pluginIdentifier != null)
65 result.setPluginIdentifier(pluginIdentifier);
66 String pluginVersion = getAttribute(node, ATTRIBUTE_PLUGIN_VERSION);
67 // plug-in version is the same as the feature version if it is missing
68 if (pluginVersion == null)
69 pluginVersion = version;
70 if (pluginVersion != null)
71 result.setPluginVersion(pluginVersion);
72 String application = getAttribute(node, ATTRIBUTE_APPLICATION);
73 if (application != null)
74 result.setApplication(application);
75
76 // get primary flag
77 String flag = getAttribute(node, ATTRIBUTE_PRIMARY);
78 if (flag != null && Boolean.parseBoolean(flag))
79 result.setPrimary(true);
80
81 // get install locations
82 String locations = getAttribute(node, ATTRIBUTE_ROOT);
83 if (locations != null) {
84 StringTokenizer tokenizer = new StringTokenizer(locations, ","); //$NON-NLS-1$
85 ArrayList<URL> rootList = new ArrayList<>();
86 while (tokenizer.hasMoreTokens()) {
87 try {
88 URL rootEntry = new URL(tokenizer.nextToken().trim());
89 rootList.add(rootEntry);
90 } catch (MalformedURLException e) {
91 // skip bad entries ...
92 }
93 }
94 URL[] roots = rootList.toArray(new URL[rootList.size()]);
95 result.setRoots(roots);
96 }
97
98 return result;
99 }
100
101 /*
102 * Create the features from the given DOM node.
103 */
104 private static void createFeatures(Node node, Site site) {
105 NodeList children = node.getChildNodes();
106 int size = children.getLength();
107 for (int i = 0; i < size; i++) {
108 Node child = children.item(i);
109 if (child.getNodeType() != Node.ELEMENT_NODE)
110 continue;
111 if (!ELEMENT_FEATURE.equalsIgnoreCase(child.getNodeName()))
112 continue;
113 Feature feature = createFeature(child, site);
114 if (feature != null)
115 site.addFeature(feature);
116 }
117 }
118
119 /*
120 * Create a site based on the given DOM node.
121 */
122 private Site createSite(Node node) {
123 Site result = new Site();
124 String policy = getAttribute(node, ATTRIBUTE_POLICY);
125 if (policy != null)
126 result.setPolicy(policy);
127 String enabled = getAttribute(node, ATTRIBUTE_ENABLED);
128 if (enabled != null)
129 result.setEnabled(Boolean.parseBoolean(enabled));
130 String updateable = getAttribute(node, ATTRIBUTE_UPDATEABLE);
131 if (updateable != null)
132 result.setUpdateable(Boolean.parseBoolean(updateable));
133 String url = getAttribute(node, ATTRIBUTE_URL);
134 if (url != null) {
135 try {
136 // do this to ensure the location is an encoded URI
137 URI uri = URIUtil.fromString(url);
138 URI osgiURI = osgiInstallArea != null ? URIUtil.toURI(osgiInstallArea) : null;
139 result.setUrl(getLocation(uri, osgiURI).toString());
140 } catch (URISyntaxException e) {
141 result.setUrl(url);
142 }
143 }
144 String linkFile = getAttribute(node, ATTRIBUTE_LINKFILE);
145 if (linkFile != null)
146 result.setLinkFile(linkFile);
147 String list = getAttribute(node, ATTRIBUTE_LIST);
148 if (list != null)
149 for (StringTokenizer tokenizer = new StringTokenizer(list, ","); tokenizer.hasMoreTokens();) //$NON-NLS-1$
150 result.addPlugin(tokenizer.nextToken());
151 createFeatures(node, result);
152 return result;
153 }
154
155 /*
156 * Convert the given url string to an absolute url. If the string is
157 * platform:/base/ then return a string which represents the osgi install area.
158 */
159 private static URI getLocation(URI location, URI osgiArea) {
160 if (osgiArea == null)
161 return location;
162 if (PLATFORM_BASE.equals(location.toString()))
163 return osgiArea;
164 return URIUtil.makeAbsolute(location, osgiArea);
165 }
166
167 /*
168 * Return the attribute with the given name, or null if it does not exist.
169 */
170 private static String getAttribute(Node node, String name) {
171 NamedNodeMap attributes = node.getAttributes();
172 Node temp = attributes.getNamedItem(name);
173 return temp == null ? null : temp.getNodeValue();
174 }
175
176 /*
177 * Load the given file into a DOM document.
178 */
179 private static Document load(InputStream input) throws ParserConfigurationException, IOException, SAXException {
180 // load the feature xml
181 DocumentBuilderFactory factory = SecureXMLUtil.newSecureDocumentBuilderFactory();
182 DocumentBuilder builder = factory.newDocumentBuilder();
183 input = new BufferedInputStream(input);
184 try {
185 return builder.parse(input);
186 } finally {
187 if (input != null)
188 try {
189 input.close();
190 } catch (IOException e) {
191 // ignore
192 }
193 }
194 }
195
196 /*
197 * Parse the given file handle which points to a platform.xml file and a
198 * configuration object. Returns null if the file doesn't exist.
199 */
200 private Configuration internalParse(File file) throws ProvisionException {
201 if (!file.exists()) {
202 // remove from cache since it doesn't exist anymore on disk
203 ConfigurationCache.put(file, null);
204 return null;
205 }
206 // have we read this before?
207 Configuration result = ConfigurationCache.get(file);
208 if (result != null)
209 return result;
210 try {
211 InputStream input = new BufferedInputStream(new FileInputStream(file));
212 Document document = load(input);
213 result = process(document);
214 // save for future use
215 ConfigurationCache.put(file, result);
216 return result;
217 } catch (IOException e) {
218 throw new ProvisionException(NLS.bind(Messages.error_reading_config, file), e);
219 } catch (ParserConfigurationException e) {
220 throw new ProvisionException(Messages.error_parsing_config, e);
221 } catch (SAXException e) {
222 throw new ProvisionException(Messages.error_parsing_config, e);
223 }
224 }
225
226 /*
227 * Process the given DOM document and create the appropriate site objects.
228 */
229 private Configuration process(Document document) {
230 Node node = getConfigElement(document);
231 if (node == null)
232 return null;
233 Configuration configuration = createConfiguration(node);
234 NodeList children = node.getChildNodes();
235 int size = children.getLength();
236 for (int i = 0; i < size; i++) {
237 Node child = children.item(i);
238 if (child.getNodeType() != Node.ELEMENT_NODE)
239 continue;
240 if (!ELEMENT_SITE.equalsIgnoreCase(child.getNodeName()))
241 continue;
242 Site site = createSite(child);
243 if (site != null)
244 configuration.add(site);
245 }
246 return configuration;
247 }
248
249 private static Configuration createConfiguration(Node node) {
250 Configuration result = new Configuration();
251 String value = getAttribute(node, ATTRIBUTE_DATE);
252 if (value != null)
253 result.setDate(value);
254 value = getAttribute(node, ATTRIBUTE_TRANSIENT);
255 if (value != null)
256 result.setTransient(Boolean.parseBoolean(value));
257 value = getAttribute(node, ATTRIBUTE_SHARED_UR);
258 if (value != null)
259 result.setSharedUR(value);
260 value = getAttribute(node, ATTRIBUTE_VERSION);
261 if (value != null)
262 result.setVersion(value);
263 return result;
264 }
265
266 private static Node getConfigElement(Document doc) {
267 NodeList children = doc.getChildNodes();
268 int size = children.getLength();
269 for (int i = 0; i < size; i++) {
270 Node child = children.item(i);
271 if (child.getNodeType() != Node.ELEMENT_NODE)
272 continue;
273 if (ELEMENT_CONFIG.equalsIgnoreCase(child.getNodeName()))
274 return child;
275 }
276 return null;
277 }
278 }