Example usage for org.dom4j Attribute getValue

List of usage examples for org.dom4j Attribute getValue

Introduction

In this page you can find the example usage for org.dom4j Attribute getValue.

Prototype

String getValue();

Source Link

Document

Returns the value of the attribute.

Usage

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

/**
 * libManifest/*from  w  ww  .  ja va2 s  . co m*/
 *
 * @param libManifestFile
 * @param mainManifestFileObject param updateSdkVersion
 */
public static void updatePreProcessManifestFile(File libManifestFile, ManifestFileObject mainManifestFileObject,
        boolean updateSdkVersion) throws IOException, DocumentException {

    if (!libManifestFile.exists()) {
        return;
    }

    File orgManifestFile = new File(libManifestFile.getParentFile(), "AndroidManifest-org.xml");
    if (orgManifestFile.exists()) {
        return;
    }

    libManifestFile.renameTo(orgManifestFile);

    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");// XML??

    Document document = reader.read(orgManifestFile);// ?XML
    Element root = document.getRootElement();// 
    if (updateSdkVersion) {
        Element useSdkElement = root.element("uses-sdk");

        if (null == useSdkElement) {
            useSdkElement = root.addElement("uses-sdk");
        }

        if (null != useSdkElement) {
            updateElement(useSdkElement, mainManifestFileObject.getUseSdkProperties());
        }
    }

    // ?tools:removetools:replace?ManifestMerge??
    Element applicationElement = root.element("application");
    Map<String, String> replaceAttrs = mainManifestFileObject.getReplaceApplicationAttribute();
    List<String> removeAttrs = mainManifestFileObject.getRemoveApplicationAttribute();

    if (null != applicationElement) {
        // libtools
        List<Attribute> toRomoved = new ArrayList<Attribute>();
        for (Attribute attribute : applicationElement.attributes()) {
            if (attribute.getName().equals("replace") || attribute.getName().equals("remove")) {
                // applicationElement.remove(attribute);
                // applicationElement.attributes().remove(attribute);
                toRomoved.add(attribute);
            }
        }
        if (toRomoved.size() > 0) {
            for (Attribute attribute : toRomoved) {
                applicationElement.remove(attribute);
            }
        }

        updateApplicationElement(applicationElement, replaceAttrs, removeAttrs);
    }

    //?packageName TODO
    String packageName = root.attributeValue("package");
    if (StringUtils.isEmpty(packageName)) {
        packageName = ManifestFileUtils.getPackage(orgManifestFile);
    }

    List<? extends Node> applicatNodes = root.selectNodes("//application");
    for (Node node : applicatNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        if (attribute != null) {
            if (!attribute.getValue().startsWith(packageName)) {
                attribute.setValue(packageName + attribute.getValue());
            }
        }
    }

    fillFullClazzName(root, packageName, "activity");
    fillFullClazzName(root, packageName, "provider");
    fillFullClazzName(root, packageName, "receiver");
    fillFullClazzName(root, packageName, "service");

    saveFile(document, format, libManifestFile);
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

private static void fillFullClazzName(Element root, String packageName, String type) {
    List<? extends Node> applicatNodes = root.selectNodes("//" + type);
    for (Node node : applicatNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        if (attribute != null) {
            if (attribute.getValue().startsWith(".")) {
                attribute.setValue(packageName + attribute.getValue());
            }/*from w ww. ja  v a2s. co m*/
        }
    }
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

/**
 * ?mainifest/*from   ww  w . j a v a  2 s . c o m*/
 *
 * @param manifestFile
 * @return
 */
public static ManifestFileObject getManifestFileObject(File manifestFile) throws DocumentException {
    SAXReader reader = new SAXReader();
    ManifestFileObject manifestFileObject = new ManifestFileObject();
    manifestFileObject.setManifestFile(manifestFile);
    if (manifestFile.exists()) {
        Document document = reader.read(manifestFile);// ?XML
        Element root = document.getRootElement();// 
        for (Attribute attribute : root.attributes()) {
            if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                manifestFileObject.addManifestProperty(
                        attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
            } else {
                manifestFileObject.addManifestProperty(attribute.getName(), attribute.getValue());
            }
        }
        Element useSdkElement = root.element("uses-sdk");
        Element applicationElement = root.element("application");
        if (null != useSdkElement) {
            for (Attribute attribute : useSdkElement.attributes()) {
                if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                    manifestFileObject.addUseSdkProperty(
                            attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
                } else {
                    manifestFileObject.addUseSdkProperty(attribute.getName(), attribute.getValue());
                }
            }
        }
        if (null != applicationElement) {
            for (Attribute attribute : applicationElement.attributes()) {
                if (StringUtils.isNotBlank(attribute.getNamespacePrefix())) {
                    manifestFileObject.addApplicationProperty(
                            attribute.getNamespacePrefix() + ":" + attribute.getName(), attribute.getValue());
                } else {
                    manifestFileObject.addApplicationProperty(attribute.getName(), attribute.getValue());
                }
            }
        }
    }
    return manifestFileObject;
}

From source file:com.taobao.android.builder.tools.manifest.ManifestFileUtils.java

License:Apache License

/**
 * ?minSdkVersiontargetSdkVersion/*from   w  w w  .  j  av a2  s.  c o m*/
 *
 * @param androidManifestFile
 * @throws IOException
 * @throws DocumentException
 */
public static String getVersionName(File androidManifestFile) throws IOException, DocumentException {
    SAXReader reader = new SAXReader();
    String versionName = "";
    if (androidManifestFile.exists()) {
        Document document = reader.read(androidManifestFile);// ?XML
        Element root = document.getRootElement();// 
        if ("manifest".equalsIgnoreCase(root.getName())) {
            List<Attribute> attributes = root.attributes();
            for (Attribute attr : attributes) {
                if (StringUtils.equalsIgnoreCase(attr.getName(), "versionName")) {
                    versionName = attr.getValue();
                }
            }
        }
    }
    return versionName;
}

From source file:com.taobao.android.builder.tools.manifest.ManifestHelper.java

License:Apache License

public static void collectBundleInfo(AppVariantContext appVariantContext, BundleInfo bundleInfo, File manifest,
        List<AndroidLibrary> androidLibraries) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document document = reader.read(getModifyManifestFile(manifest, appVariantContext));// ?XML
    Element root = document.getRootElement();// 

    List<? extends Node> metadataNodes = root.selectNodes("//meta-data");
    for (Node node : metadataNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        if (attribute.getValue().equals("label")) {
            Attribute labelAttribute = element.attribute("value");
            bundleInfo.setName(labelAttribute.getValue());
        } else if (attribute.getValue().equals("description")) {
            Attribute descAttribute = element.attribute("value");
            bundleInfo.setDesc(descAttribute.getValue());
        }//from w  w w  .j  av  a  2  s  . com
    }

    addComponents(bundleInfo, root);

    if (null != androidLibraries) {
        for (AndroidLibrary depLib : androidLibraries) {
            SAXReader reader2 = new SAXReader();
            Document document2 = reader2.read(getModifyManifestFile(depLib.getManifest(), appVariantContext));// ?XML
            Element root2 = document2.getRootElement();// 
            addComponents(bundleInfo, root2);
        }
    }

}

From source file:com.taobao.android.builder.tools.manifest.ManifestHelper.java

License:Apache License

private static void addComponents(BundleInfo bundleInfo, Element root) {
    List<? extends Node> serviceNodes = root.selectNodes("//service");
    for (Node node : serviceNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        addToList(bundleInfo.getServices(), attribute.getValue());
    }// w  w w  .  j  ava2 s.  c  o  m
    List<? extends Node> receiverNodes = root.selectNodes("//receiver");
    for (Node node : receiverNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        addToList(bundleInfo.getReceivers(), attribute.getValue());
    }
    List<? extends Node> providerNodes = root.selectNodes("//provider");
    for (Node node : providerNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        addToList(bundleInfo.getContentProviders(), attribute.getValue());
    }
    List<? extends Node> activityNodes = root.selectNodes("//activity");
    for (Node node : activityNodes) {
        Element element = (Element) node;
        Attribute attribute = element.attribute("name");
        addToList(bundleInfo.getActivities(), attribute.getValue());
    }
}

From source file:com.thoughtworks.cruise.api.feeds.StagesFeedXml.java

License:Apache License

@com.thoughtworks.gauge.Step("Verify stage link for current pipeline with counter <pipelineCounter> and stage <stageName> with stage counter <stageCounter> is approved by <approvedBy>")
public void verifyStageLinkForCurrentPipelineWithCounterAndStageWithStageCounterIsApprovedBy(
        Integer pipelineCounter, String stageName, Integer stageCounter, String approvedBy) throws Exception {
    String xpathPattern = titleXpath(pipelineCounter, stageName, stageCounter) + "/atom:link/@href";

    Attribute href = (Attribute) apiHelper.fetchNode(feed(), xpathPattern);
    assertThat(String.format("xpath %s not found in doc: %s", xpathPattern, feed().asXML()), href,
            is(not(nullValue())));//from  www  .j a  v a 2 s .c o  m

    Document stageDocument = apiHelper.loadXmlDocumentFromUrl(href.getValue());
    assertThat(stageDocument.selectSingleNode("/stage/@name").getStringValue(), is(stageName));

    assertThat(stageDocument.selectSingleNode("/stage/pipeline/@name").getStringValue(),
            is(state.currentRuntimePipelineName()));
    assertThat(stageDocument.selectSingleNode("/stage/pipeline/@counter").getStringValue(),
            is(pipelineCounter.toString()));

    assertThat(stageDocument.selectSingleNode("/stage/state").getStringValue(), is("Completed"));
    assertThat(stageDocument.selectSingleNode("/stage/result").getStringValue(), is("Passed"));
    assertThat(stageDocument.selectSingleNode("/stage/approvedBy").getStringValue(), is(approvedBy));
    assertThat(stageDocument.selectNodes("/stage/jobs/job[contains(@href,'/go/api/jobs/')]").size(),
            is(greaterThan(0)));
}

From source file:com.thoughtworks.cruise.CruiseConfigVerification.java

License:Apache License

@com.thoughtworks.gauge.Step("Verify stage <stageName> has attribute <attribute> set to <value> where null means <defaultValue>")
public void verifyStageHasAttributeSetToWhereNullMeans(String stageName, String attribute, String value,
        String defaultValue) throws Exception {
    CruiseConfigDom dom = configuration.provideDomAsAdmin();
    Element stage = dom.getStage(state.currentRuntimePipelineName(), stageName);
    Attribute attr = stage.attribute(attribute);
    if (attr == null) {
        assertThat(defaultValue, is(value));
    } else {/*ww w.j  a  v a  2 s. c  om*/
        assertThat(attr.getValue(), is(value));
    }
}

From source file:com.thoughtworks.cruise.state.ScenarioState.java

License:Apache License

public void knownPipelineInstanceUrl(String pipelineName, Attribute url) {
    pipelineInstanceUrls.put(pipelineName, url.getValue());
}

From source file:com.thoughtworks.cruise.utils.configfile.CruiseConfigDom.java

License:Apache License

private List<String> attributeValues(String xpath) {
    List<String> result = new ArrayList<String>();
    List<Attribute> list = root().selectNodes(xpath);
    for (Attribute element : list) {
        result.add(element.getValue());
    }//from   ww w  .  j  a v  a 2  s  .  c o m
    return result;
}