1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 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 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 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 14 * the License. 15 */ 16 package com.android.ide.eclipse.tests.functests.sampleProjects; 17 18 import com.android.SdkConstants; 19 import com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper; 20 import com.android.xml.AndroidManifest; 21 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; 27 28 import java.io.File; 29 import java.io.IOException; 30 import java.util.logging.Level; 31 import java.util.logging.Logger; 32 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; 44 45 /** 46 * Helper class for modifying an AndroidManifest. 47 * <p/> 48 * TODO: consider merging this with AndroidManifestParser. 49 */ 50 class AndroidManifestWriter { 51 52 private static final Logger sLogger = Logger.getLogger(AndroidManifestWriter.class.getName()); 53 54 private final Document mDoc; 55 private final String mOsManifestFilePath; 56 57 private AndroidManifestWriter(Document doc, String osManifestFilePath) { 58 mDoc = doc; 59 mOsManifestFilePath = osManifestFilePath; 60 } 61 62 /** 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 66 */ 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); 72 } else { 73 usesSdkElement = mDoc.createElement(AndroidManifest.NODE_USES_SDK); 74 mDoc.getDocumentElement().appendChild(usesSdkElement); 75 } 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(); 83 } 84 85 private boolean saveXmlToFile() { 86 try { 87 // Prepare the DOM document for writing 88 Source source = new DOMSource(mDoc); 89 90 // Prepare the output file 91 File file = new File(mOsManifestFilePath); 92 Result result = new StreamResult(file); 93 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); 99 return false; 100 } catch (TransformerException e) { 101 sLogger.log(Level.SEVERE, "Failed to write xml file", e); 102 return false; 103 } 104 return true; 105 } 106 107 /** 108 * Parses the manifest file, and collects data. 109 * 110 * @param osManifestFilePath The OS path of the manifest file to parse. 111 * @return an {@link AndroidManifestHelper} or null if parsing failed 112 */ 113 public static AndroidManifestWriter parse(String osManifestFilePath) { 114 try { 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); 122 return null; 123 } catch (SAXException e) { 124 sLogger.log(Level.SEVERE, "Error parsing file", e); 125 return null; 126 } catch (IOException e) { 127 sLogger.log(Level.SEVERE, "Error parsing file", e); 128 return null; 129 } 130 } 131 } 132