1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.adt.internal.project; 18 19 import com.android.ide.eclipse.adt.AdtPlugin; 20 import com.android.ide.eclipse.adt.internal.project.XmlErrorHandler.XmlErrorListener; 21 import com.android.ide.eclipse.adt.io.IFileWrapper; 22 import com.android.sdklib.io.FileWrapper; 23 import com.android.sdklib.io.IAbstractFile; 24 import com.android.sdklib.io.StreamException; 25 import com.android.sdklib.xml.AndroidManifestParser; 26 import com.android.sdklib.xml.ManifestData; 27 28 import org.eclipse.core.resources.IFile; 29 import org.eclipse.core.resources.IProject; 30 import org.eclipse.jdt.core.IJavaProject; 31 import org.xml.sax.SAXException; 32 33 import java.io.FileNotFoundException; 34 import java.io.IOException; 35 36 import javax.xml.parsers.ParserConfigurationException; 37 38 public class AndroidManifestHelper { 39 40 /** 41 * Parses the Android Manifest, and returns an object containing the result of the parsing. 42 * <p/> 43 * This method can also gather XML error during the parsing. This is done by using an 44 * {@link XmlErrorHandler} to mark the files in case of error, as well as a given 45 * {@link XmlErrorListener}. To use a different error handler, consider using 46 * {@link AndroidManifestParser#parse(IAbstractFile, boolean, com.android.sdklib.xml.AndroidManifestParser.ManifestErrorHandler)} 47 * directly. 48 * 49 * @param manifestFile the {@link IFile} representing the manifest file. 50 * @param gatherData indicates whether the parsing will extract data from the manifest. If null, 51 * the method will always return null. 52 * @param errorListener an optional error listener. If non null, then the parser will also 53 * look for XML errors. 54 * @return an {@link ManifestData} or null if the parsing failed. 55 */ parse( IAbstractFile manifestFile, boolean gatherData, XmlErrorListener errorListener)56 public static ManifestData parse( 57 IAbstractFile manifestFile, 58 boolean gatherData, 59 XmlErrorListener errorListener) { 60 try { 61 if (manifestFile != null) { 62 IFile eclipseFile = null; 63 if (manifestFile instanceof IFileWrapper) { 64 eclipseFile = ((IFileWrapper)manifestFile).getIFile(); 65 } 66 XmlErrorHandler errorHandler = null; 67 if (errorListener != null) { 68 errorHandler = new XmlErrorHandler(eclipseFile, errorListener); 69 } 70 71 return AndroidManifestParser.parse(manifestFile, gatherData, errorHandler); 72 } 73 } catch (ParserConfigurationException e) { 74 AdtPlugin.logAndPrintError(e, AndroidManifestHelper.class.getCanonicalName(), 75 "Bad parser configuration for %s: %s", 76 manifestFile.getOsLocation(), 77 e.getMessage()); 78 } catch (SAXException e) { 79 AdtPlugin.logAndPrintError(e, AndroidManifestHelper.class.getCanonicalName(), 80 "Parser exception for %s: %s", 81 manifestFile.getOsLocation(), 82 e.getMessage()); 83 } catch (IOException e) { 84 // Don't log a console error when failing to read a non-existing file 85 if (!(e instanceof FileNotFoundException)) { 86 AdtPlugin.logAndPrintError(e, AndroidManifestHelper.class.getCanonicalName(), 87 "I/O error for %s: %s", 88 manifestFile.getOsLocation(), 89 e.getMessage()); 90 } 91 } catch (StreamException e) { 92 AdtPlugin.logAndPrintError(e, AndroidManifestHelper.class.getCanonicalName(), 93 "Unable to read %s: %s", 94 manifestFile.getOsLocation(), 95 e.getMessage()); 96 } 97 98 return null; 99 } 100 101 /** 102 * Parses the Android Manifest for a given project, and returns an object containing 103 * the result of the parsing. 104 * <p/> 105 * This method can also gather XML error during the parsing. This is done by using an 106 * {@link XmlErrorHandler} to mark the files in case of error, as well as a given 107 * {@link XmlErrorListener}. To use a different error handler, consider using 108 * {@link AndroidManifestParser#parse(IAbstractFile, boolean, com.android.sdklib.xml.AndroidManifestParser.ManifestErrorHandler)} 109 * directly. 110 * 111 * @param javaProject the project containing the manifest to parse. 112 * @param gatherData indicates whether the parsing will extract data from the manifest. If null, 113 * the method will always return null. 114 * @param errorListener an optional error listener. If non null, then the parser will also 115 * look for XML errors. 116 * @return an {@link ManifestData} or null if the parsing failed. 117 */ parse( IJavaProject javaProject, boolean gatherData, XmlErrorListener errorListener)118 public static ManifestData parse( 119 IJavaProject javaProject, 120 boolean gatherData, 121 XmlErrorListener errorListener) { 122 123 IFile manifestFile = ProjectHelper.getManifest(javaProject.getProject()); 124 if (manifestFile != null) { 125 return parse(new IFileWrapper(manifestFile), gatherData, errorListener); 126 } 127 128 return null; 129 } 130 131 /** 132 * Parses the manifest file only for error check. 133 * @param manifestFile The manifest file to parse. 134 * @param errorListener the {@link XmlErrorListener} object being notified of the presence 135 * of errors. 136 */ parseForError(IFile manifestFile, XmlErrorListener errorListener)137 public static void parseForError(IFile manifestFile, XmlErrorListener errorListener) { 138 parse(new IFileWrapper(manifestFile), false, errorListener); 139 } 140 141 /** 142 * Parses the manifest file, and collects data. 143 * @param manifestFile The manifest file to parse. 144 * @return an {@link ManifestData} or null if the parsing failed. 145 */ parseForData(IFile manifestFile)146 public static ManifestData parseForData(IFile manifestFile) { 147 return parse(new IFileWrapper(manifestFile), true, null); 148 } 149 150 /** 151 * Parses the manifest file, and collects data. 152 * @param project the project containing the manifest. 153 * @return an {@link AndroidManifestHelper} or null if the parsing failed. 154 */ parseForData(IProject project)155 public static ManifestData parseForData(IProject project) { 156 IFile manifestFile = ProjectHelper.getManifest(project); 157 if (manifestFile != null) { 158 return parse(new IFileWrapper(manifestFile), true, null); 159 } 160 161 return null; 162 } 163 164 /** 165 * Parses the manifest file, and collects data. 166 * 167 * @param osManifestFilePath The OS path of the manifest file to parse. 168 * @return an {@link AndroidManifestHelper} or null if the parsing failed. 169 */ parseForData(String osManifestFilePath)170 public static ManifestData parseForData(String osManifestFilePath) { 171 return parse(new FileWrapper(osManifestFilePath), true, null); 172 } 173 } 174