Search in sources :

Example 11 with TypeReference

use of com.alibaba.fastjson.TypeReference in project atlas by alibaba.

the class ManifestFileUtils method addApplicationMetaData.

/**
     * 往主的manifest中添加metadata信息,作为atals的兜底方案
     *
     * @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();
            // 读取XML文件
            Document libDocument = reader.read(libManifest);
            // 得到根节点
            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);
            }
        }
    }
}
Also used : SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Node(org.dom4j.Node) Document(org.dom4j.Document) BundleInfo(com.taobao.android.builder.tools.bundleinfo.model.BundleInfo) TypeReference(com.alibaba.fastjson.TypeReference) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Aggregations

TypeReference (com.alibaba.fastjson.TypeReference)11 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)3 JSONReader (com.alibaba.fastjson.JSONReader)2 BundleInfo (com.taobao.android.builder.tools.bundleinfo.model.BundleInfo)2 File (java.io.File)2 StringReader (java.io.StringReader)2 JSONException (com.alibaba.fastjson.JSONException)1 JSONObject (com.alibaba.fastjson.JSONObject)1 AutoKeeperCluster (com.alibaba.otter.shared.common.model.autokeeper.AutoKeeperCluster)1 ColumnGroup (com.alibaba.otter.shared.common.model.config.data.ColumnGroup)1 ColumnPair (com.alibaba.otter.shared.common.model.config.data.ColumnPair)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 Document (org.dom4j.Document)1 Element (org.dom4j.Element)1 Node (org.dom4j.Node)1