Example usage for org.dom4j.io SAXReader read

List of usage examples for org.dom4j.io SAXReader read

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader read.

Prototype

public Document read(InputSource in) throws DocumentException 

Source Link

Document

Reads a Document from the given InputSource using SAX

Usage

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

License:Apache License

/**
 * manifestmetadata?atals//w  ww.  j ava 2  s. c o m
 *
 * @param document
 * @param libManifestMap
 * @param baseBunfleInfoFile
 * @param manifestOptions
 */
private static void addApplicationMetaData(Document document, Map<String, File> libManifestMap,
        File baseBunfleInfoFile, ManifestOptions manifestOptions, Set<String> remoteBundles)
        throws IOException, DocumentException {
    Map<String, BundleInfo> bundleFileMap = Maps.newHashMap();
    // ??
    if (null != baseBunfleInfoFile && baseBunfleInfoFile.exists() && baseBunfleInfoFile.canRead()) {
        String bundleBaseInfo = FileUtils.readFileToString(baseBunfleInfoFile, "utf-8");
        bundleFileMap = JSON.parseObject(bundleBaseInfo, new TypeReference<Map<String, BundleInfo>>() {
        });
    }
    Map<String, LibBundleInfo> awbManifestMap = Maps.newHashMap();
    for (Map.Entry<String, File> entry : libManifestMap.entrySet()) {
        String artifactId = entry.getKey();

        String libName = artifactId.substring(artifactId.indexOf("-") + 1);

        File libManifest = entry.getValue();
        libManifest = getOrgManifestFile(libManifest);
        if (libManifest.exists()) {
            SAXReader reader = new SAXReader();
            Document libDocument = reader.read(libManifest);// ?XML
            Element libRoot = libDocument.getRootElement();// 
            String packageName = libRoot.attributeValue("package");
            Element applicationElement = libRoot.element("application");
            String applicationName = null;
            if (null != applicationElement) {
                applicationName = applicationElement.attributeValue("name");
            }
            LibBundleInfo libBundleInfo = new LibBundleInfo(artifactId, packageName, applicationName,
                    bundleFileMap.get(libName), libName);
            awbManifestMap.put(artifactId, libBundleInfo);
        }
    }

    // meta-data?
    Element root = document.getRootElement();// 
    List<? extends Node> nodes = root.selectNodes("//application");
    for (Node node : nodes) {
        Element element = (Element) node;
        for (String artifactId : libManifestMap.keySet()) {
            LibBundleInfo libBundleInfo = awbManifestMap.get(artifactId);
            if (StringUtils.isNotBlank(libBundleInfo.applicationName)) {
                String bundlePackageName = libBundleInfo.packageName;
                BundleInfo bundleInfo = libBundleInfo.bundleInfo;
                Element metaData = element.addElement("meta-data");

                String bundleDepValue = "";
                if (null != bundleInfo && bundleInfo.getDependency() != null) {
                    bundleDepValue = StringUtils.join(bundleInfo.getDependency(), "|");
                }
                String value = libBundleInfo.applicationName + ","
                        + !remoteBundles.contains(libBundleInfo.libName) + "," + bundleDepValue;
                logger.info("[bundleInfo] add bundle value : " + value + " to manifest");
                metaData.addAttribute("android:name", "bundle_" + bundlePackageName);
                metaData.addAttribute("android:value", value);
            }
        }
    }
}

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

License:Apache License

/**
 * manifestbundleLocation/*from  w  w  w  .  j av a2  s .co  m*/
 *
 * @param document
 * @param libManifestMap
 * @param manifestOptions
 * @throws DocumentException
 */
private static void addBundleLocationToDestManifest(Document document, Map<String, File> libManifestMap,
        Multimap<String, File> libDependenciesMaps, ManifestOptions manifestOptions) throws DocumentException {
    Table<String, String, String> bundleInfoTable = HashBasedTable.create();
    Map<String, String> packageNameMap = new HashMap<String, String>();
    for (Map.Entry<String, File> entry : libManifestMap.entrySet()) {
        File libManifest = entry.getValue();
        if (libManifest.exists()) {
            SAXReader reader = new SAXReader();
            Document libDocument = reader.read(libManifest);// ?XML
            Element libRoot = libDocument.getRootElement();// 
            String packageName = libRoot.attributeValue("package");
            packageNameMap.put(entry.getKey(), packageName);
            List<? extends Node> nodes = libRoot.selectNodes("//activity|//service|//receiver");
            for (Node node : nodes) {
                Element e = (Element) node;
                String name = e.attributeValue("name");
                String type = e.getName();
                bundleInfoTable.put(type, name, packageName);
            }
        }
    }

    // ??manifest?
    for (String key : libDependenciesMaps.keySet()) {
        Collection<File> libManifests = libDependenciesMaps.get(key);
        for (File libManifest : libManifests) {
            if (libManifest.exists()) {
                SAXReader reader = new SAXReader();
                Document libDocument = reader.read(libManifest);// ?XML
                Element libRoot = libDocument.getRootElement();// 
                String packageName = packageNameMap.get(key);
                List<? extends Node> nodes = libRoot.selectNodes("//activity|//service|//receiver");
                for (Node node : nodes) {
                    Element e = (Element) node;
                    String name = e.attributeValue("name");
                    String type = e.getName();
                    bundleInfoTable.put(type, name, packageName);
                }
            }
        }
    }

    Element root = document.getRootElement();// 
    List<? extends Node> nodes = root.selectNodes("//activity|//service|//receiver");
    for (Node node : nodes) {
        Element e = (Element) node;
        String name = e.attributeValue("name");
        String type = e.getName();
        String packageName = bundleInfoTable.get(type, name);
        if (null != packageName) {
            Element metaData = e.addElement("meta-data");
            metaData.addAttribute("android:name", "bundleLocation");
            metaData.addAttribute("android:value", packageName);
        }
    }
}

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

License:Apache License

/**
 * ?manifestpackageId/*from   ww w .j a  v  a  2  s.  c  o m*/
 *
 * @param manifestFile
 * @return
 */
public static String getApplicationId(File manifestFile) {
    SAXReader reader = new SAXReader();
    if (manifestFile.exists()) {
        Document document = null;// ?XML
        try {
            document = reader.read(manifestFile);
            Element root = document.getRootElement();// 
            String packageName = root.attributeValue("package");
            return packageName;
        } catch (DocumentException e) {
        }
    }
    return null;
}

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

License:Apache License

/**
 * libManifest/*  www. ja va 2 s  .  c  om*/
 *
 * @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

/**
 * ?mainifest/*from   w  w  w. ja 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//  w w  w  . j a  va 2  s .  co  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.ManifestFileUtils.java

License:Apache License

public static void minifyManifest(File mainManifest, File destManifest) throws IOException, DocumentException {

    XMLWriter writer = null;// XML
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");// XML??
    FileOutputStream fos = new FileOutputStream(destManifest);
    if (mainManifest.exists()) {
        try {//from  ww  w  .  j  ava2 s . com
            Document document = reader.read(mainManifest);// ?XML

            //                removeComments(document);

            Element element = document.getRootElement();

            element.clearContent();

            writer = new XMLWriter(fos, format);
            writer.write(document);
        } finally {
            if (null != writer) {
                writer.close();
            }
            IOUtils.closeQuietly(fos);
        }
    }
}