FileDocCategorySizeDatePackage
AndroidManifestParser.javaAPI DocAndroid 1.5 API44048Wed May 06 22:41:10 BST 2009com.android.ide.eclipse.common.project

AndroidManifestParser

public class AndroidManifestParser extends Object

Fields Summary
private static final String
ATTRIBUTE_PACKAGE
private static final String
ATTRIBUTE_NAME
private static final String
ATTRIBUTE_PROCESS
private static final String
ATTRIBUTE_DEBUGGABLE
private static final String
ATTRIBUTE_MIN_SDK_VERSION
private static final String
ATTRIBUTE_TARGET_PACKAGE
private static final String
ATTRIBUTE_EXPORTED
private static final String
NODE_MANIFEST
private static final String
NODE_APPLICATION
private static final String
NODE_ACTIVITY
private static final String
NODE_SERVICE
private static final String
NODE_RECEIVER
private static final String
NODE_PROVIDER
private static final String
NODE_INTENT
private static final String
NODE_ACTION
private static final String
NODE_CATEGORY
private static final String
NODE_USES_SDK
private static final String
NODE_INSTRUMENTATION
private static final String
NODE_USES_LIBRARY
private static final int
LEVEL_MANIFEST
private static final int
LEVEL_APPLICATION
private static final int
LEVEL_ACTIVITY
private static final int
LEVEL_INTENT_FILTER
private static final int
LEVEL_CATEGORY
private static final String
ACTION_MAIN
private static final String
CATEGORY_LAUNCHER
public static final int
INVALID_MIN_SDK
private static SAXParserFactory
sParserFactory
private final String
mJavaPackage
private final Activity[]
mActivities
private final Activity
mLauncherActivity
private final String[]
mProcesses
private final Boolean
mDebuggable
private final int
mApiLevelRequirement
private final Instrumentation[]
mInstrumentations
private final String[]
mLibraries
Constructors Summary
private AndroidManifestParser(String javaPackage, Activity[] activities, Activity launcherActivity, String[] processes, Boolean debuggable, int apiLevelRequirement, Instrumentation[] instrumentations, String[] libraries)
Private constructor to enforce using {@link #parse(IJavaProject, XmlErrorListener, boolean, boolean)}, {@link #parse(IJavaProject, IFile, XmlErrorListener, boolean, boolean)}, or {@link #parseForError(IJavaProject, IFile, XmlErrorListener)} to get an {@link AndroidManifestParser} object.

param
javaPackage the package parsed from the manifest.
param
activities the list of activities parsed from the manifest.
param
launcherActivity the launcher activity parser from the manifest.
param
processes the list of custom processes declared in the manifest.
param
debuggable the debuggable attribute, or null if not set.
param
apiLevelRequirement the minSdkVersion attribute value or 0 if not set.
param
instrumentations the list of instrumentations parsed from the manifest.
param
libraries the list of libraries in use parsed from the manifest.

        mJavaPackage = javaPackage;
        mActivities = activities;
        mLauncherActivity = launcherActivity;
        mProcesses = processes;
        mDebuggable = debuggable;
        mApiLevelRequirement = apiLevelRequirement;
        mInstrumentations = instrumentations;
        mLibraries = libraries;
    
Methods Summary
public static java.lang.StringcombinePackageAndClassName(java.lang.String javaPackage, java.lang.String className)
Combines a java package, with a class value from the manifest to make a fully qualified class name

param
javaPackage the java package from the manifest.
param
className the class name from the manifest.
return
the fully qualified class name.

        if (className == null || className.length() == 0) {
            return javaPackage;
        }
        if (javaPackage == null || javaPackage.length() == 0) {
            return className;
        }

        // the class name can be a subpackage (starts with a '.'
        // char), a simple class name (no dot), or a full java package
        boolean startWithDot = (className.charAt(0) == '.");
        boolean hasDot = (className.indexOf('.") != -1);
        if (startWithDot || hasDot == false) {

            // add the concatenation of the package and class name
            if (startWithDot) {
                return javaPackage + className;
            } else {
                return javaPackage + '." + className;
            }
        } else {
            // just add the class as it should be a fully qualified java name.
            return className;
        }
    
public static java.lang.StringextractActivityName(java.lang.String fullActivityName, java.lang.String packageName)
Given a fully qualified activity name (e.g. com.foo.test.MyClass) and given a project package base name (e.g. com.foo), returns the relative activity name that would be used the "name" attribute of an "activity" element.

param
fullActivityName a fully qualified activity class name, e.g. "com.foo.test.MyClass"
param
packageName The project base package name, e.g. "com.foo"
return
The relative activity name if it can be computed or the original fullActivityName.

        if (packageName != null && fullActivityName != null) {
            if (packageName.length() > 0 && fullActivityName.startsWith(packageName)) {
                String name = fullActivityName.substring(packageName.length());
                if (name.length() > 0 && name.charAt(0) == '.") {
                    return name;
                }
            }
        }

        return fullActivityName;
    
public com.android.ide.eclipse.common.project.AndroidManifestParser$Activity[]getActivities()
Returns the list of activities found in the manifest.

return
An array of {@link Activity}, or empty if no activity were found.

        return mActivities;
    
public intgetApiLevelRequirement()
Returns the minSdkVersion attribute, or {@link #INVALID_MIN_SDK} if it's not set.

        return mApiLevelRequirement;
    
public java.lang.BooleangetDebuggable()
Returns the debuggable attribute value or null if it is not set.

        return mDebuggable;
    
public com.android.ide.eclipse.common.project.AndroidManifestParser$Instrumentation[]getInstrumentations()
Returns the list of instrumentations found in the manifest.

return
An array of {@link Instrumentation}, or empty if no instrumentations were found.

        return mInstrumentations;
    
public com.android.ide.eclipse.common.project.AndroidManifestParser$ActivitygetLauncherActivity()
Returns the name of one activity found in the manifest, that is configured to show up in the HOME screen.

return
The {@link Activity} representing a HOME activity or null if none were found.

        return mLauncherActivity;
    
public static org.eclipse.core.resources.IFilegetManifest(org.eclipse.core.resources.IProject project)
Returns an IFile object representing the manifest for the specified project.

param
project The project containing the manifest file.
return
An IFile object pointing to the manifest or null if the manifest is missing.

        IResource r = project.findMember(AndroidConstants.WS_SEP
                + AndroidConstants.FN_ANDROID_MANIFEST);

        if (r == null || r.exists() == false || (r instanceof IFile) == false) {
            return null;
        }
        return (IFile) r;
    
public java.lang.StringgetPackage()
Returns the package defined in the manifest, if found.

return
The package name or null if not found.

        return mJavaPackage;
    
public java.lang.String[]getProcesses()
Returns the list of process names declared by the manifest.

        return mProcesses;
    
public java.lang.String[]getUsesLibraries()
Returns the list of libraries in use found in the manifest.

return
An array of library names, or empty if no uses-library declarations were found.

        return mLibraries;
    
public static com.android.ide.eclipse.common.project.AndroidManifestParserparse(org.eclipse.jdt.core.IJavaProject javaProject, org.eclipse.core.resources.IFile manifestFile, com.android.ide.eclipse.common.project.XmlErrorHandler.XmlErrorListener errorListener, boolean gatherData, boolean markErrors)
Parses the Android Manifest, and returns an object containing the result of the parsing.

This method is useful to parse a specific {@link IFile} in a Java project.

If you only want to gather data, consider {@link #parseForData(IFile)} instead.

param
javaProject The java project.
param
manifestFile the {@link IFile} representing the manifest file.
param
errorListener
param
gatherData indicates whether the parsing will extract data from the manifest.
param
markErrors indicates whether the error found during parsing should put a marker on the file. For class validation errors to put a marker, gatherData must be set to true
return
an {@link AndroidManifestParser} or null if the parsing failed.
throws
CoreException

        sParserFactory = SAXParserFactory.newInstance();
        sParserFactory.setNamespaceAware(true);
    
        try {
            SAXParser parser = sParserFactory.newSAXParser();

            ManifestHandler manifestHandler = new ManifestHandler(manifestFile,
                    errorListener, gatherData, javaProject, markErrors);
            parser.parse(new InputSource(manifestFile.getContents()), manifestHandler);
            
            // get the result from the handler
            
            return new AndroidManifestParser(manifestHandler.getPackage(),
                    manifestHandler.getActivities(),
                    manifestHandler.getLauncherActivity(),
                    manifestHandler.getProcesses(),
                    manifestHandler.getDebuggable(),
                    manifestHandler.getApiLevelRequirement(),
                    manifestHandler.getInstrumentations(),
                    manifestHandler.getUsesLibraries());
        } catch (ParserConfigurationException e) {
            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
                    "Bad parser configuration for %s: %s",
                    manifestFile.getFullPath(),
                    e.getMessage());
        } catch (SAXException e) {
            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
                    "Parser exception for %s: %s",
                    manifestFile.getFullPath(),
                    e.getMessage());
        } catch (IOException e) {
            // Don't log a console error when failing to read a non-existing file
            if (!(e instanceof FileNotFoundException)) {
                AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
                        "I/O error for %s: %s",
                        manifestFile.getFullPath(),
                        e.getMessage());
            }
        } 

        return null;
    
private static com.android.ide.eclipse.common.project.AndroidManifestParserparse(java.io.File manifestFile)
Parses the Android Manifest, and returns an object containing the result of the parsing.

This version parses a real {@link File} file given by an actual path, which is useful for parsing a file that is not part of an Eclipse Java project.

It assumes errors cannot be marked on the file and that data gathering is enabled.

param
manifestFile the manifest file to parse.
return
an {@link AndroidManifestParser} or null if the parsing failed.
throws
CoreException

        try {
            SAXParser parser = sParserFactory.newSAXParser();

            ManifestHandler manifestHandler = new ManifestHandler(
                    null, //manifestFile
                    null, //errorListener
                    true, //gatherData
                    null, //javaProject
                    false //markErrors
                    );
            
            parser.parse(new InputSource(new FileReader(manifestFile)), manifestHandler);
            
            // get the result from the handler
            
            return new AndroidManifestParser(manifestHandler.getPackage(),
                    manifestHandler.getActivities(),
                    manifestHandler.getLauncherActivity(),
                    manifestHandler.getProcesses(),
                    manifestHandler.getDebuggable(),
                    manifestHandler.getApiLevelRequirement(),
                    manifestHandler.getInstrumentations(),
                    manifestHandler.getUsesLibraries());
        } catch (ParserConfigurationException e) {
            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
                    "Bad parser configuration for %s: %s",
                    manifestFile.getAbsolutePath(),
                    e.getMessage());
        } catch (SAXException e) {
            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
                    "Parser exception for %s: %s",
                    manifestFile.getAbsolutePath(),
                    e.getMessage());
        } catch (IOException e) {
            // Don't log a console error when failing to read a non-existing file
            if (!(e instanceof FileNotFoundException)) {
                AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
                        "I/O error for %s: %s",
                        manifestFile.getAbsolutePath(),
                        e.getMessage());
            }
        }
        
        return null;
    
public static com.android.ide.eclipse.common.project.AndroidManifestParserparse(org.eclipse.jdt.core.IJavaProject javaProject, com.android.ide.eclipse.common.project.XmlErrorHandler.XmlErrorListener errorListener, boolean gatherData, boolean markErrors)
Parses the Android Manifest for the specified project, and returns an object containing the result of the parsing.

param
javaProject The java project. Required if markErrors is true
param
errorListener the {@link XmlErrorListener} object being notified of the presence of errors. Optional.
param
gatherData indicates whether the parsing will extract data from the manifest.
param
markErrors indicates whether the error found during parsing should put a marker on the file. For class validation errors to put a marker, gatherData must be set to true
return
an {@link AndroidManifestParser} or null if the parsing failed.
throws
CoreException

        
        IFile manifestFile = getManifest(javaProject.getProject());
        
        try {
            SAXParser parser = sParserFactory.newSAXParser();

            if (manifestFile != null) {
                ManifestHandler manifestHandler = new ManifestHandler(manifestFile,
                        errorListener, gatherData, javaProject, markErrors);

                parser.parse(new InputSource(manifestFile.getContents()), manifestHandler);
                
                // get the result from the handler
                return new AndroidManifestParser(manifestHandler.getPackage(),
                        manifestHandler.getActivities(), manifestHandler.getLauncherActivity(),
                        manifestHandler.getProcesses(), manifestHandler.getDebuggable(),
                        manifestHandler.getApiLevelRequirement(), 
                        manifestHandler.getInstrumentations(), manifestHandler.getUsesLibraries());
            }
        } catch (ParserConfigurationException e) {
            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
                    "Bad parser configuration for %s", manifestFile.getFullPath());
        } catch (SAXException e) {
            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
                    "Parser exception for %s", manifestFile.getFullPath());
        } catch (IOException e) {
            AdtPlugin.logAndPrintError(e, AndroidManifestParser.class.getCanonicalName(), 
                    "I/O error for %s", manifestFile.getFullPath());
        } 
        
        return null;
    
public static com.android.ide.eclipse.common.project.AndroidManifestParserparseForData(org.eclipse.core.resources.IFile manifestFile)
Parses the manifest file, and collects data.

param
manifestFile The manifest file to parse.
return
an {@link AndroidManifestParser} or null if the parsing failed.
throws
CoreException for example the file does not exist in the workspace or the workspace needs to be refreshed.

        return parse(null /* javaProject */, manifestFile, null /* errorListener */,
                true /* gatherData */, false /* markErrors */);
    
public static com.android.ide.eclipse.common.project.AndroidManifestParserparseForData(java.lang.String osManifestFilePath)
Parses the manifest file, and collects data.

param
osManifestFilePath The OS path of the manifest file to parse.
return
an {@link AndroidManifestParser} or null if the parsing failed.

        try {
            return parse(new File(osManifestFilePath));
        } catch (CoreException e) {
            // Ignore workspace errors (unlikely to happen since this parses an actual file,
            // not a workspace resource).
            return null;
        }
    
public static com.android.ide.eclipse.common.project.AndroidManifestParserparseForError(org.eclipse.jdt.core.IJavaProject javaProject, org.eclipse.core.resources.IFile manifestFile, com.android.ide.eclipse.common.project.XmlErrorHandler.XmlErrorListener errorListener)
Parses the manifest file, collects data, and checks for errors.

param
javaProject The java project. Required.
param
manifestFile The manifest file to parse.
param
errorListener the {@link XmlErrorListener} object being notified of the presence of errors. Optional.
return
an {@link AndroidManifestParser} or null if the parsing failed.
throws
CoreException

        return parse(javaProject, manifestFile, errorListener, true, true);