Java Code Examples for com.android.io.IAbstractFile

The following examples show how to use com.android.io.IAbstractFile. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Merges all properties from the given file into the current properties.
 * <p/>
 * This emulates the Ant behavior: existing properties are <em>not</em> overridden.
 * Only new undefined properties become defined.
 * <p/>
 * Typical usage:
 * <ul>
 * <li>Create a ProjectProperties with {@code PropertyType#ANT}
 * <li>Merge in values using {@code PropertyType#PROJECT}
 * <li>The result is that this contains all the properties from default plus those
 *     overridden by the build.properties file.
 * </ul>
 *
 * @param type One the possible {@link ProjectProperties.PropertyType}s.
 * @return this object, for chaining.
 */
public synchronized ProjectPropertiesWorkingCopy merge(PropertyType type) {
    if (mProjectFolder.exists() && mType != type) {
        IAbstractFile propFile = mProjectFolder.getFile(type.getFilename());
        if (propFile.exists()) {
            Map<String, String> map = parsePropertyFile(propFile, null /* log */);
            if (map != null) {
                for (Entry<String, String> entry : map.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    if (!mProperties.containsKey(key) && value != null) {
                        mProperties.put(key, value);
                    }
                }
            }
        }
    }
    return this;
}
 
Example 2
Source Project: java-n-IDE-for-Android   Source File: AndroidManifest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the value of the minSdkVersion attribute.
 * <p/>
 * If the attribute is set with an int value, the method returns an Integer object.
 * <p/>
 * If the attribute is set with a codename, it returns the codename as a String object.
 * <p/>
 * If the attribute is not set, it returns null.
 *
 * @param manifestFile the manifest file to read the attribute from.
 * @return the attribute value.
 * @throws XPathExpressionException
 * @throws StreamException If any error happens when reading the manifest.
 */
@Nullable
public static Object getMinSdkVersion(IAbstractFile manifestFile)
        throws XPathExpressionException, StreamException {
    String result = getStringValue(manifestFile,
            "/" + NODE_MANIFEST +
            "/" + NODE_USES_SDK +
            "/@" + AndroidXPathFactory.DEFAULT_NS_PREFIX +
            ":" + ATTRIBUTE_MIN_SDK_VERSION);

    try {
        return Integer.valueOf(result);
    } catch (NumberFormatException e) {
        return !result.isEmpty() ? result : null;
    }
}
 
Example 3
Source Project: java-n-IDE-for-Android   Source File: AndroidManifest.java    License: Apache License 2.0 6 votes vote down vote up
private static String getStringValue(@NonNull IAbstractFile file, @NonNull String xPath)
        throws StreamException, XPathExpressionException {
    XPath xpath = AndroidXPathFactory.newXPath();

    InputStream is = null;
    try {
        is = file.getContents();
        return xpath.evaluate(xPath, new InputSource(is));
    } finally {
        try {
            Closeables.close(is, true /* swallowIOException */);
        } catch (IOException e) {
            // cannot happen
        }
    }
}
 
Example 4
Source Project: java-n-IDE-for-Android   Source File: ResourceFolder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Processes a file and adds it to its parent folder resource.
 *
 * @param file the underlying resource file.
 * @param kind the file change kind.
 * @param context a context object with state for the current update, such
 *            as a place to stash errors encountered
 * @return the {@link ResourceFile} that was created.
 */
public ResourceFile processFile(IAbstractFile file, ResourceDeltaKind kind,
        ScanningContext context) {
    // look for this file if it's already been created
    ResourceFile resFile = getFile(file, context);

    if (resFile == null) {
        if (kind != ResourceDeltaKind.REMOVED) {
            // create a ResourceFile for it.

            resFile = createResourceFile(file);
            resFile.load(context);

            // add it to the folder
            addFile(resFile);
        }
    } else {
        if (kind == ResourceDeltaKind.REMOVED) {
            removeFile(resFile, context);
        } else {
            resFile.update(context);
        }
    }

    return resFile;
}
 
Example 5
Source Project: java-n-IDE-for-Android   Source File: ResourceFolder.java    License: Apache License 2.0 6 votes vote down vote up
private ResourceFile createResourceFile(IAbstractFile file) {
    // check if that's a single or multi resource type folder. For now we define this by
    // the number of possible resource type output by files in the folder.
    // We have a special case for layout/menu folders which can also generate IDs.
    // This does
    // not make the difference between several resource types from a single file or
    // the ability to have 2 files in the same folder generating 2 different types of
    // resource. The former is handled by MultiResourceFile properly while we don't
    // handle the latter. If we were to add this behavior we'd have to change this call.
    List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(mType);

    ResourceFile resFile = null;
    if (types.size() == 1) {
        resFile = new SingleResourceFile(file, this);
    } else if (types.contains(ResourceType.LAYOUT)) {
        resFile = new IdGeneratingResourceFile(file, this, ResourceType.LAYOUT);
    } else if (types.contains(ResourceType.MENU)) {
        resFile = new IdGeneratingResourceFile(file, this, ResourceType.MENU);
    } else {
        resFile = new MultiResourceFile(file, this);
    }
    return resFile;
}
 
Example 6
Source Project: java-n-IDE-for-Android   Source File: ResourceFolder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the {@link ResourceFile} matching a {@link IAbstractFile} object.
 *
 * @param file The {@link IAbstractFile} object.
 * @param context a context object with state for the current update, such
 *            as a place to stash errors encountered
 * @return the {@link ResourceFile} or null if no match was found.
 */
private ResourceFile getFile(IAbstractFile file, ScanningContext context) {
    assert mFolder.equals(file.getParentFolder());

    if (mNames != null) {
        ResourceFile resFile = mNames.get(file.getName());
        if (resFile != null) {
            return resFile;
        }
    }

    // If the file actually exists, the resource folder  may not have been
    // scanned yet; add it lazily
    if (file.exists()) {
        ResourceFile resFile = createResourceFile(file);
        resFile.load(context);
        addFile(resFile);
        return resFile;
    }

    return null;
}
 
Example 7
Source Project: java-n-IDE-for-Android   Source File: SingleResourceFile.java    License: Apache License 2.0 6 votes vote down vote up
public SingleResourceFile(IAbstractFile file, ResourceFolder folder) {
    super(file, folder);

    // we need to infer the type of the resource from the folder type.
    // This is easy since this is a single Resource file.
    List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folder.getType());
    mType = types.get(0);

    // compute the resource name
    mResourceName = getResourceName(mType);

    // test if there's a density qualifier associated with the resource
    DensityQualifier qualifier = folder.getConfiguration().getDensityQualifier();

    if (qualifier == null) {
        mValue = new ResourceValue(mType, getResourceName(mType),
                file.getOsLocation(), isFramework());
    } else {
        mValue = new DensityBasedResourceValue(
                mType,
                getResourceName(mType),
                file.getOsLocation(),
                qualifier.getValue(),
                isFramework());
    }
}
 
Example 8
Source Project: java-n-IDE-for-Android   Source File: SingleResourceFile.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Validates the associated resource file to make sure the attribute references are valid
 *
 * @return true if parsing succeeds and false if it fails
 */
private boolean validateAttributes(ScanningContext context) {
    // We only need to check if it's a non-framework file (and an XML file; skip .png's)
    if (!isFramework() && SdkUtils.endsWith(getFile().getName(), DOT_XML)) {
        ValidatingResourceParser parser = new ValidatingResourceParser(context, false);
        try {
            IAbstractFile file = getFile();
            return parser.parse(file.getOsLocation(), file.getContents());
        } catch (Exception e) {
            context.needsFullAapt();
        }

        return false;
    }

    return true;
}
 
Example 9
/**
 * Merges all properties from the given file into the current properties.
 * <p/>
 * This emulates the Ant behavior: existing properties are <em>not</em> overridden.
 * Only new undefined properties become defined.
 * <p/>
 * Typical usage:
 * <ul>
 * <li>Create a ProjectProperties with {@code PropertyType#ANT}
 * <li>Merge in values using {@code PropertyType#PROJECT}
 * <li>The result is that this contains all the properties from default plus those
 *     overridden by the build.properties file.
 * </ul>
 *
 * @param type One the possible {@link ProjectProperties.PropertyType}s.
 * @return this object, for chaining.
 */
public synchronized ProjectPropertiesWorkingCopy merge(PropertyType type) {
    if (mProjectFolder.exists() && mType != type) {
        IAbstractFile propFile = mProjectFolder.getFile(type.getFilename());
        if (propFile.exists()) {
            Map<String, String> map = parsePropertyFile(propFile, null /* log */);
            if (map != null) {
                for (Entry<String, String> entry : map.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    if (!mProperties.containsKey(key) && value != null) {
                        mProperties.put(key, value);
                    }
                }
            }
        }
    }
    return this;
}
 
Example 10
Source Project: javaide   Source File: ResourceFolder.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Processes a file and adds it to its parent folder resource.
 *
 * @param file the underlying resource file.
 * @param kind the file change kind.
 * @param context a context object with state for the current update, such
 *            as a place to stash errors encountered
 * @return the {@link ResourceFile} that was created.
 */
public ResourceFile processFile(IAbstractFile file, ResourceDeltaKind kind,
        ScanningContext context) {
    // look for this file if it's already been created
    ResourceFile resFile = getFile(file, context);

    if (resFile == null) {
        if (kind != ResourceDeltaKind.REMOVED) {
            // create a ResourceFile for it.

            resFile = createResourceFile(file);
            resFile.load(context);

            // add it to the folder
            addFile(resFile);
        }
    } else {
        if (kind == ResourceDeltaKind.REMOVED) {
            removeFile(resFile, context);
        } else {
            resFile.update(context);
        }
    }

    return resFile;
}
 
Example 11
Source Project: javaide   Source File: ResourceFolder.java    License: GNU General Public License v3.0 6 votes vote down vote up
private ResourceFile createResourceFile(IAbstractFile file) {
    // check if that's a single or multi resource type folder. For now we define this by
    // the number of possible resource type output by files in the folder.
    // We have a special case for layout/menu folders which can also generate IDs.
    // This does
    // not make the difference between several resource types from a single file or
    // the ability to have 2 files in the same folder generating 2 different types of
    // resource. The former is handled by MultiResourceFile properly while we don't
    // handle the latter. If we were to add this behavior we'd have to change this call.
    List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(mType);

    ResourceFile resFile = null;
    if (types.size() == 1) {
        resFile = new SingleResourceFile(file, this);
    } else if (types.contains(ResourceType.LAYOUT)) {
        resFile = new IdGeneratingResourceFile(file, this, ResourceType.LAYOUT);
    } else if (types.contains(ResourceType.MENU)) {
        resFile = new IdGeneratingResourceFile(file, this, ResourceType.MENU);
    } else {
        resFile = new MultiResourceFile(file, this);
    }
    return resFile;
}
 
Example 12
Source Project: javaide   Source File: ResourceFolder.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the {@link ResourceFile} matching a {@link IAbstractFile} object.
 *
 * @param file The {@link IAbstractFile} object.
 * @param context a context object with state for the current update, such
 *            as a place to stash errors encountered
 * @return the {@link ResourceFile} or null if no match was found.
 */
private ResourceFile getFile(IAbstractFile file, ScanningContext context) {
    assert mFolder.equals(file.getParentFolder());

    if (mNames != null) {
        ResourceFile resFile = mNames.get(file.getName());
        if (resFile != null) {
            return resFile;
        }
    }

    // If the file actually exists, the resource folder  may not have been
    // scanned yet; add it lazily
    if (file.exists()) {
        ResourceFile resFile = createResourceFile(file);
        resFile.load(context);
        addFile(resFile);
        return resFile;
    }

    return null;
}
 
Example 13
Source Project: javaide   Source File: SingleResourceFile.java    License: GNU General Public License v3.0 6 votes vote down vote up
public SingleResourceFile(IAbstractFile file, ResourceFolder folder) {
    super(file, folder);

    // we need to infer the type of the resource from the folder type.
    // This is easy since this is a single Resource file.
    List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(folder.getType());
    mType = types.get(0);

    // compute the resource name
    mResourceName = getResourceName(mType);

    // test if there's a density qualifier associated with the resource
    DensityQualifier qualifier = folder.getConfiguration().getDensityQualifier();

    if (qualifier == null) {
        mValue = new ResourceValue(mType, getResourceName(mType),
                file.getOsLocation(), isFramework());
    } else {
        mValue = new DensityBasedResourceValue(
                mType,
                getResourceName(mType),
                file.getOsLocation(),
                qualifier.getValue(),
                isFramework());
    }
}
 
Example 14
Source Project: javaide   Source File: SingleResourceFile.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Validates the associated resource file to make sure the attribute references are valid
 *
 * @return true if parsing succeeds and false if it fails
 */
private boolean validateAttributes(ScanningContext context) {
    // We only need to check if it's a non-framework file (and an XML file; skip .png's)
    if (!isFramework() && SdkUtils.endsWith(getFile().getName(), DOT_XML)) {
        ValidatingResourceParser parser = new ValidatingResourceParser(context, false);
        try {
            IAbstractFile file = getFile();
            return parser.parse(file.getOsLocation(), file.getContents());
        } catch (Exception e) {
            context.needsFullAapt();
        }

        return false;
    }

    return true;
}
 
Example 15
Source Project: javaide   Source File: AndroidManifest.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the value of the minSdkVersion attribute.
 * <p/>
 * If the attribute is set with an int value, the method returns an Integer object.
 * <p/>
 * If the attribute is set with a codename, it returns the codename as a String object.
 * <p/>
 * If the attribute is not set, it returns null.
 *
 * @param manifestFile the manifest file to read the attribute from.
 * @return the attribute value.
 * @throws XPathExpressionException
 * @throws StreamException If any error happens when reading the manifest.
 */
@Nullable
public static Object getMinSdkVersion(IAbstractFile manifestFile)
        throws XPathExpressionException, StreamException {
    String result = getStringValue(manifestFile,
            "/" + NODE_MANIFEST +
            "/" + NODE_USES_SDK +
            "/@" + AndroidXPathFactory.DEFAULT_NS_PREFIX +
            ":" + ATTRIBUTE_MIN_SDK_VERSION);

    try {
        return Integer.valueOf(result);
    } catch (NumberFormatException e) {
        return !result.isEmpty() ? result : null;
    }
}
 
Example 16
Source Project: javaide   Source File: AndroidManifest.java    License: GNU General Public License v3.0 6 votes vote down vote up
private static String getStringValue(@NonNull IAbstractFile file, @NonNull String xPath)
        throws StreamException, XPathExpressionException {
    XPath xpath = AndroidXPathFactory.newXPath();

    InputStream is = null;
    try {
        is = file.getContents();
        return xpath.evaluate(xPath, new InputSource(is));
    } finally {
        try {
            Closeables.close(is, true /* swallowIOException */);
        } catch (IOException e) {
            // cannot happen
        }
    }
}
 
Example 17
Source Project: NBANDROID-V2   Source File: IdGeneratingResourceFile.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the resource value associated with this whole file as a layout resource
 * @param file the file handler that represents this file
 * @param folder the folder this file is under
 * @return a resource value associated with this layout
 */
private ResourceValue getFileValue(IAbstractFile file, ResourceFolder folder) {
    // test if there's a density qualifier associated with the resource
    DensityQualifier qualifier = folder.getConfiguration().getDensityQualifier();

    ResourceValue value;
    if (!ResourceQualifier.isValid(qualifier)) {
        value =
                new ResourceValueImpl(
                        new ResourceReference(mFileType, mFileName, isFramework()),
                        file.getOsLocation());
    } else {
        value =
                new DensityBasedResourceValueImpl(
                        new ResourceReference(mFileType, mFileName, isFramework()),
                        file.getOsLocation(),
                        qualifier.getValue());
    }
    return value;
}
 
Example 18
Source Project: NBANDROID-V2   Source File: ResourceFolder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Processes a file and adds it to its parent folder resource.
 *
 * @param file the underlying resource file.
 * @param kind the file change kind.
 * @param context a context object with state for the current update, such
 *            as a place to stash errors encountered
 * @return the {@link ResourceFile} that was created.
 */
public ResourceFile processFile(IAbstractFile file, ResourceDeltaKind kind,
        ScanningContext context) {
    // look for this file if it's already been created
    ResourceFile resFile = getFile(file, context);

    if (resFile == null) {
        if (kind != ResourceDeltaKind.REMOVED) {
            // create a ResourceFile for it.

            resFile = createResourceFile(file);
            resFile.load(context);

            // add it to the folder
            addFile(resFile);
        }
    } else {
        if (kind == ResourceDeltaKind.REMOVED) {
            removeFile(resFile, context);
        } else {
            resFile.update(context);
        }
    }

    return resFile;
}
 
Example 19
Source Project: NBANDROID-V2   Source File: ResourceFolder.java    License: Apache License 2.0 6 votes vote down vote up
private ResourceFile createResourceFile(IAbstractFile file) {
    // check if that's a single or multi resource type folder. We have a special case
    // for ID generating resource types (layout/menu, and XML drawables, etc.).
    // MultiResourceFile handles the case when several resource types come from a single file
    // (values files).

    ResourceFile resFile;
    if (mType != ResourceFolderType.VALUES) {
        if (FolderTypeRelationship.isIdGeneratingFolderType(mType) &&
            SdkUtils.endsWithIgnoreCase(file.getName(), SdkConstants.DOT_XML)) {
            List<ResourceType> types = FolderTypeRelationship.getRelatedResourceTypes(mType);
            ResourceType primaryType = types.get(0);
            resFile = new IdGeneratingResourceFile(file, this, primaryType);
        } else {
            resFile = new SingleResourceFile(file, this);
        }
    } else {
        resFile = new MultiResourceFile(file, this, namespace);
    }
    return resFile;
}
 
Example 20
Source Project: NBANDROID-V2   Source File: ResourceFolder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the {@link ResourceFile} matching a {@link IAbstractFile} object.
 *
 * @param file The {@link IAbstractFile} object.
 * @param context a context object with state for the current update, such
 *            as a place to stash errors encountered
 * @return the {@link ResourceFile} or null if no match was found.
 */
private ResourceFile getFile(IAbstractFile file, ScanningContext context) {
    assert mFolder.equals(file.getParentFolder());

    if (mNames != null) {
        ResourceFile resFile = mNames.get(file.getName());
        if (resFile != null) {
            return resFile;
        }
    }

    // If the file actually exists, the resource folder  may not have been
    // scanned yet; add it lazily
    if (file.exists()) {
        ResourceFile resFile = createResourceFile(file);
        resFile.load(context);
        addFile(resFile);
        return resFile;
    }

    return null;
}
 
Example 21
Source Project: NBANDROID-V2   Source File: SingleResourceFile.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Validates the associated resource file to make sure the attribute references are valid
 *
 * @return true if parsing succeeds and false if it fails
 */
private boolean validateAttributes(ScanningContext context) {
    // We only need to check if it's a non-framework file (and an XML file; skip .png's)
    if (!isFramework() && SdkUtils.endsWith(getFile().getName(), DOT_XML)) {
        ValidatingResourceParser parser = new ValidatingResourceParser(context, false);
        try {
            IAbstractFile file = getFile();
            return parser.parse(file.getOsLocation(), file.getContents());
        } catch (Exception e) {
            context.needsFullAapt();
        }

        return false;
    }

    return true;
}
 
Example 22
Source Project: java-n-IDE-for-Android   Source File: ProjectProperties.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Loads a project properties file and return a {@link ProjectProperties} object
 * containing the properties.
 *
 * @param projectFolder the project folder.
 * @param type One the possible {@link PropertyType}s.
 */
public static ProjectProperties load(IAbstractFolder projectFolder, PropertyType type) {
    if (projectFolder.exists()) {
        IAbstractFile propFile = projectFolder.getFile(type.mFilename);
        if (propFile.exists()) {
            Map<String, String> map = parsePropertyFile(propFile, null /* log */);
            if (map != null) {
                return new ProjectProperties(projectFolder, map, type);
            }
        }
    }
    return null;
}
 
Example 23
Source Project: java-n-IDE-for-Android   Source File: ProjectProperties.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Deletes a project properties file.
 *
 * @param projectFolder the project folder.
 * @param type One the possible {@link PropertyType}s.
 * @return true if success.
 */
public static boolean delete(IAbstractFolder projectFolder, PropertyType type) {
    if (projectFolder.exists()) {
        IAbstractFile propFile = projectFolder.getFile(type.mFilename);
        if (propFile.exists()) {
            return propFile.delete();
        }
    }

    return false;
}
 
Example 24
Source Project: java-n-IDE-for-Android   Source File: ProjectProperties.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Reloads the properties from the underlying file.
 */
public synchronized void reload() {
    if (mProjectFolder.exists()) {
        IAbstractFile propFile = mProjectFolder.getFile(mType.mFilename);
        if (propFile.exists()) {
            Map<String, String> map = parsePropertyFile(propFile, null /* log */);
            if (map != null) {
                mProperties.clear();
                mProperties.putAll(map);
            }
        }
    }
}
 
Example 25
Source Project: java-n-IDE-for-Android   Source File: AndroidManifest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns an {@link IAbstractFile} object representing the manifest for the given project.
 *
 * @param projectFolder The project containing the manifest file.
 * @return An IAbstractFile object pointing to the manifest or null if the manifest
 *         is missing.
 */
public static IAbstractFile getManifest(IAbstractFolder projectFolder) {
    IAbstractFile file = projectFolder.getFile(SdkConstants.FN_ANDROID_MANIFEST_XML);
    if (file != null && file.exists()) {
        return file;
    }

    return null;
}
 
Example 26
Source Project: java-n-IDE-for-Android   Source File: AndroidManifest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the package for a given project.
 * @param projectFolder the folder of the project.
 * @return the package info or null (or empty) if not found.
 * @throws XPathExpressionException
 * @throws StreamException If any error happens when reading the manifest.
 */
public static String getPackage(IAbstractFolder projectFolder)
        throws XPathExpressionException, StreamException {
    IAbstractFile file = getManifest(projectFolder);
    if (file != null) {
        return getPackage(file);
    }

    return null;
}
 
Example 27
Source Project: java-n-IDE-for-Android   Source File: AndroidManifest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the manifest is set to make the application debuggable.
 *
 * If the give manifest does not contain the debuggable attribute then the application
 * is considered to not be debuggable.
 *
 * @param manifestFile the manifest to parse.
 * @return true if the application is debuggable.
 * @throws XPathExpressionException
 * @throws StreamException If any error happens when reading the manifest.
 */
public static boolean getDebuggable(IAbstractFile manifestFile)
        throws XPathExpressionException, StreamException {
    String value = getStringValue(manifestFile,
            "/"  + NODE_MANIFEST +
            "/"  + NODE_APPLICATION +
            "/@" + AndroidXPathFactory.DEFAULT_NS_PREFIX +
            ":"  + ATTRIBUTE_DEBUGGABLE);

    // default is not debuggable, which is the same behavior as parseBoolean
    return Boolean.parseBoolean(value);
}
 
Example 28
Source Project: java-n-IDE-for-Android   Source File: AndroidManifest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of the versionCode attribute or -1 if the value is not set.
 * @param manifestFile the manifest file to read the attribute from.
 * @return the integer value or -1 if not set.
 * @throws XPathExpressionException
 * @throws StreamException If any error happens when reading the manifest.
 */
public static int getVersionCode(IAbstractFile manifestFile)
        throws XPathExpressionException, StreamException {
    String result = getStringValue(manifestFile,
            "/" + NODE_MANIFEST +
            "/@" + AndroidXPathFactory.DEFAULT_NS_PREFIX +
            ":" + ATTRIBUTE_VERSIONCODE);

    try {
        return Integer.parseInt(result);
    } catch (NumberFormatException e) {
        return -1;
    }
}
 
Example 29
Source Project: java-n-IDE-for-Android   Source File: AndroidManifest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns whether the version Code attribute is set in a given manifest.
 * @param manifestFile the manifest to check
 * @return true if the versionCode attribute is present and its value is not empty.
 * @throws XPathExpressionException
 * @throws StreamException If any error happens when reading the manifest.
 */
public static boolean hasVersionCode(IAbstractFile manifestFile)
        throws XPathExpressionException, StreamException {
    XPath xPath = AndroidXPathFactory.newXPath();

    InputStream is = null;
    try {
        is = manifestFile.getContents();
        Object result = xPath.evaluate(
                "/"  + NODE_MANIFEST +
                "/@" + AndroidXPathFactory.DEFAULT_NS_PREFIX +
                ":"  + ATTRIBUTE_VERSIONCODE,
                new InputSource(is),
                XPathConstants.NODE);

        if (result != null) {
            Node node  = (Node)result;
            if (!node.getNodeValue().isEmpty()) {
                return true;
            }
        }
    } finally {
        try {
            Closeables.close(is, true /* swallowIOException */);
        } catch (IOException e) {
            // cannot happen
        }
    }

    return false;
}
 
Example 30
Source Project: java-n-IDE-for-Android   Source File: AndroidManifest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of the targetSdkVersion attribute.
 * <p/>
 * If the attribute is set with an int value, the method returns an Integer object.
 * <p/>
 * If the attribute is set with a codename, it returns the codename as a String object.
 * <p/>
 * If the attribute is not set, it returns null.
 *
 * @param manifestFile the manifest file to read the attribute from.
 * @return the integer value or -1 if not set.
 * @throws XPathExpressionException
 * @throws StreamException If any error happens when reading the manifest.
 */
@Nullable
public static Object getTargetSdkVersion(IAbstractFile manifestFile)
        throws XPathExpressionException, StreamException {
    String result = getStringValue(manifestFile,
            "/" + NODE_MANIFEST +
            "/" + NODE_USES_SDK +
            "/@" + AndroidXPathFactory.DEFAULT_NS_PREFIX +
            ":" + ATTRIBUTE_TARGET_SDK_VERSION);
    try {
        return Integer.valueOf(result);
    } catch (NumberFormatException e) {
        return !result.isEmpty() ? result : null;
    }
}