Home | History | Annotate | Download | only in project
      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.common.xml.AndroidManifestParser;
     20 import com.android.ide.common.xml.ManifestData;
     21 import com.android.ide.eclipse.adt.AdtPlugin;
     22 import com.android.ide.eclipse.adt.internal.project.XmlErrorHandler.XmlErrorListener;
     23 import com.android.ide.eclipse.adt.io.IFileWrapper;
     24 import com.android.io.FileWrapper;
     25 import com.android.io.IAbstractFile;
     26 import com.android.io.StreamException;
     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      * @throws ParserConfigurationException
     56      * @throws StreamException
     57      * @throws IOException
     58      * @throws SAXException
     59      */
     60     public static ManifestData parseUnchecked(
     61             IAbstractFile manifestFile,
     62             boolean gatherData,
     63             XmlErrorListener errorListener) throws SAXException, IOException,
     64             StreamException, ParserConfigurationException {
     65         if (manifestFile != null) {
     66             IFile eclipseFile = null;
     67             if (manifestFile instanceof IFileWrapper) {
     68                 eclipseFile = ((IFileWrapper)manifestFile).getIFile();
     69             }
     70             XmlErrorHandler errorHandler = null;
     71             if (errorListener != null) {
     72                 errorHandler = new XmlErrorHandler(eclipseFile, errorListener);
     73             }
     74 
     75             return AndroidManifestParser.parse(manifestFile, gatherData, errorHandler);
     76         }
     77 
     78         return null;
     79     }
     80 
     81     /**
     82      * Parses the Android Manifest, and returns an object containing the result of the parsing.
     83      * <p/>
     84      * This method can also gather XML error during the parsing. This is done by using an
     85      * {@link XmlErrorHandler} to mark the files in case of error, as well as a given
     86      * {@link XmlErrorListener}. To use a different error handler, consider using
     87      * {@link AndroidManifestParser#parse(IAbstractFile, boolean, com.android.sdklib.xml.AndroidManifestParser.ManifestErrorHandler)}
     88      * directly.
     89      *
     90      * @param manifestFile the {@link IFile} representing the manifest file.
     91      * @param gatherData indicates whether the parsing will extract data from the manifest. If null,
     92      * the method will always return null.
     93      * @param errorListener an optional error listener. If non null, then the parser will also
     94      * look for XML errors.
     95      * @return an {@link ManifestData} or null if the parsing failed.
     96      */
     97     public static ManifestData parse(
     98             IAbstractFile manifestFile,
     99             boolean gatherData,
    100             XmlErrorListener errorListener) {
    101         try {
    102             return parseUnchecked(manifestFile, gatherData, errorListener);
    103         } catch (ParserConfigurationException e) {
    104             AdtPlugin.logAndPrintError(e, AndroidManifestHelper.class.getCanonicalName(),
    105                     "Bad parser configuration for %s: %s",
    106                     manifestFile.getOsLocation(),
    107                     e.getMessage());
    108         } catch (SAXException e) {
    109             AdtPlugin.logAndPrintError(e, AndroidManifestHelper.class.getCanonicalName(),
    110                     "Parser exception for %s: %s",
    111                     manifestFile.getOsLocation(),
    112                     e.getMessage());
    113         } catch (IOException e) {
    114             // Don't log a console error when failing to read a non-existing file
    115             if (!(e instanceof FileNotFoundException)) {
    116                 AdtPlugin.logAndPrintError(e, AndroidManifestHelper.class.getCanonicalName(),
    117                         "I/O error for %s: %s",
    118                         manifestFile.getOsLocation(),
    119                         e.getMessage());
    120             }
    121         } catch (StreamException e) {
    122             AdtPlugin.logAndPrintError(e, AndroidManifestHelper.class.getCanonicalName(),
    123                     "Unable to read %s: %s",
    124                     manifestFile.getOsLocation(),
    125                     e.getMessage());
    126         }
    127 
    128         return null;
    129     }
    130 
    131     /**
    132      * Parses the Android Manifest for a given project, and returns an object containing
    133      * the result of the parsing.
    134      * <p/>
    135      * This method can also gather XML error during the parsing. This is done by using an
    136      * {@link XmlErrorHandler} to mark the files in case of error, as well as a given
    137      * {@link XmlErrorListener}. To use a different error handler, consider using
    138      * {@link AndroidManifestParser#parse(IAbstractFile, boolean, com.android.sdklib.xml.AndroidManifestParser.ManifestErrorHandler)}
    139      * directly.
    140      *
    141      * @param javaProject the project containing the manifest to parse.
    142      * @param gatherData indicates whether the parsing will extract data from the manifest. If null,
    143      * the method will always return null.
    144      * @param errorListener an optional error listener. If non null, then the parser will also
    145      * look for XML errors.
    146      * @return an {@link ManifestData} or null if the parsing failed.
    147      */
    148     public static ManifestData parse(
    149             IJavaProject javaProject,
    150             boolean gatherData,
    151             XmlErrorListener errorListener) {
    152 
    153         IFile manifestFile = ProjectHelper.getManifest(javaProject.getProject());
    154         if (manifestFile != null) {
    155             return parse(new IFileWrapper(manifestFile), gatherData, errorListener);
    156         }
    157 
    158         return null;
    159     }
    160 
    161     /**
    162      * Parses the manifest file only for error check.
    163      * @param manifestFile The manifest file to parse.
    164      * @param errorListener the {@link XmlErrorListener} object being notified of the presence
    165      * of errors.
    166      */
    167     public static void parseForError(IFile manifestFile, XmlErrorListener errorListener) {
    168         parse(new IFileWrapper(manifestFile), false, errorListener);
    169     }
    170 
    171     /**
    172      * Parses the manifest file, and collects data.
    173      * @param manifestFile The manifest file to parse.
    174      * @return an {@link ManifestData} or null if the parsing failed.
    175      */
    176     public static ManifestData parseForData(IFile manifestFile) {
    177         return parse(new IFileWrapper(manifestFile), true, null);
    178     }
    179 
    180     /**
    181      * Parses the manifest file, and collects data.
    182      * @param project the project containing the manifest.
    183      * @return an {@link AndroidManifestHelper} or null if the parsing failed.
    184      */
    185     public static ManifestData parseForData(IProject project) {
    186         IFile manifestFile = ProjectHelper.getManifest(project);
    187         if (manifestFile != null) {
    188             return parse(new IFileWrapper(manifestFile), true, null);
    189         }
    190 
    191         return null;
    192     }
    193 
    194     /**
    195      * Parses the manifest file, and collects data.
    196      *
    197      * @param osManifestFilePath The OS path of the manifest file to parse.
    198      * @return an {@link AndroidManifestHelper} or null if the parsing failed.
    199      */
    200     public static ManifestData parseForData(String osManifestFilePath) {
    201         return parse(new FileWrapper(osManifestFilePath), true, null);
    202     }
    203 }
    204