2 * Copyright (C) 2009 The Android Open Source Project
4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); you
5 * may not use this file except in compliance with the License. You may obtain a
6 * copy of the License at
8 * http://www.eclipse.org/org/documents/epl-v10.php
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
16 package com.android.ide.eclipse.tests.functests.sampleProjects;
18 import com.android.ide.eclipse.adt.internal.project.AndroidManifestParser;
19 import com.android.sdklib.SdkConstants;
20 import com.android.sdklib.xml.AndroidManifest;
22 import org.w3c.dom.Attr;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25 import org.w3c.dom.NodeList;
26 import org.xml.sax.SAXException;
29 import java.io.IOException;
30 import java.util.logging.Level;
31 import java.util.logging.Logger;
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
36 import javax.xml.transform.Result;
37 import javax.xml.transform.Source;
38 import javax.xml.transform.Transformer;
39 import javax.xml.transform.TransformerConfigurationException;
40 import javax.xml.transform.TransformerException;
41 import javax.xml.transform.TransformerFactory;
42 import javax.xml.transform.dom.DOMSource;
43 import javax.xml.transform.stream.StreamResult;
46 * Helper class for modifying an AndroidManifest.
48 * TODO: consider merging this with AndroidManifestParser.
50 class AndroidManifestWriter {
52 private static final Logger sLogger = Logger.getLogger(AndroidManifestWriter.class.getName());
54 private final Document mDoc;
55 private final String mOsManifestFilePath;
57 private AndroidManifestWriter(Document doc, String osManifestFilePath) {
59 mOsManifestFilePath = osManifestFilePath;
63 * Sets the minimum SDK version for this manifest
64 * @param minSdkVersion - the minimim sdk version to use
65 * @returns <code>true</code> on success, false otherwise
67 public boolean setMinSdkVersion(String minSdkVersion) {
68 Element usesSdkElement = null;
69 NodeList nodeList = mDoc.getElementsByTagName(AndroidManifest.NODE_USES_SDK);
70 if (nodeList.getLength() > 0) {
71 usesSdkElement = (Element) nodeList.item(0);
73 usesSdkElement = mDoc.createElement(AndroidManifest.NODE_USES_SDK);
74 mDoc.getDocumentElement().appendChild(usesSdkElement);
76 Attr minSdkAttr = mDoc.createAttributeNS(SdkConstants.NS_RESOURCES,
77 AndroidManifest.ATTRIBUTE_MIN_SDK_VERSION);
78 String prefix = mDoc.lookupPrefix(SdkConstants.NS_RESOURCES);
79 minSdkAttr.setPrefix(prefix);
80 minSdkAttr.setValue(minSdkVersion);
81 usesSdkElement.setAttributeNodeNS(minSdkAttr);
82 return saveXmlToFile();
85 private boolean saveXmlToFile() {
87 // Prepare the DOM document for writing
88 Source source = new DOMSource(mDoc);
90 // Prepare the output file
91 File file = new File(mOsManifestFilePath);
92 Result result = new StreamResult(file);
94 // Write the DOM document to the file
95 Transformer xformer = TransformerFactory.newInstance().newTransformer();
96 xformer.transform(source, result);
97 } catch (TransformerConfigurationException e) {
98 sLogger.log(Level.SEVERE, "Failed to write xml file", e);
100 } catch (TransformerException e) {
101 sLogger.log(Level.SEVERE, "Failed to write xml file", e);
108 * Parses the manifest file, and collects data.
110 * @param osManifestFilePath The OS path of the manifest file to parse.
111 * @return an {@link AndroidManifestParser} or null if parsing failed
113 public static AndroidManifestWriter parse(String osManifestFilePath) {
115 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
116 docFactory.setNamespaceAware(true);
117 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
118 Document doc = docBuilder.parse(osManifestFilePath);
119 return new AndroidManifestWriter(doc, osManifestFilePath);
120 } catch (ParserConfigurationException e) {
121 sLogger.log(Level.SEVERE, "Error parsing file", e);
123 } catch (SAXException e) {
124 sLogger.log(Level.SEVERE, "Error parsing file", e);
126 } catch (IOException e) {
127 sLogger.log(Level.SEVERE, "Error parsing file", e);