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.sun.xml.ws.test.container.AbstractApplicationContainer.java

License:Open Source License

protected void updateWsitClient(WAR war, DeployedService deployedService, String id) throws Exception {
    File wsitClientFile = new File(deployedService.getResources(), "wsit-client.xml");
    if (wsitClientFile.exists()) {
        SAXReader reader = new SAXReader();
        Document document = reader.read(wsitClientFile);
        Element root = document.getRootElement();
        Element policy = root.element("Policy");
        Element sts = policy.element("ExactlyOne").element("All").element("PreconfiguredSTS");

        Attribute endpoint = sts.attribute("endpoint");
        endpoint.setValue(id);/* ww w .  j a v a2s.  c  om*/

        Attribute wsdlLoc = sts.attribute("wsdlLocation");
        String x = deployedService.service.wsdl.get(0).wsdlFile.toURI().toString();
        wsdlLoc.setValue(x);

        XMLWriter writer = new XMLWriter(new FileWriter(wsitClientFile));
        writer.write(document);
        writer.close();
        war.copyWsit(wsitClientFile);
    } else {
        throw new RuntimeException("wsit-client.xml is absent. It is required. \n" + "Please check "
                + deployedService.getResources());
    }
}

From source file:com.suneee.core.util.XMLProperties.java

License:Open Source License

/**
 * Builds the document XML model up based the given reader of XML data.
 * @param in the input stream used to build the xml document
 * @throws java.io.IOException thrown when an error occurs reading the input stream.
 *///w ww.j ava 2s. c o  m
private void buildDoc(Reader in) throws IOException {
    try {
        SAXReader xmlReader = new SAXReader();
        //            xmlReader.setEncoding("GBK");
        document = xmlReader.read(in);
    } catch (Exception e) {
        Log.error("Error reading XML properties", e);
        throw new IOException(e.getMessage());
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:com.surenpi.autotest.suite.parser.XmlSuiteParser.java

License:Apache License

/**
 * ??/*from   www .j  a v a  2 s .  c  om*/
 * @param suiteInputStream ??
 * @return 
 * @throws DocumentException
 */
public Suite parse(InputStream suiteInputStream) throws DocumentException {
    SAXReader reader = new SAXReader();
    reader.setEncoding("utf-8");

    Document document = reader.read(suiteInputStream);

    simpleNamespaceContext.addNamespace("ns", NS_URI);

    XPath xpath = new DefaultXPath("/ns:suite");
    xpath.setNamespaceContext(simpleNamespaceContext);
    Element suiteEle = (Element) xpath.selectSingleNode(document);
    if (suiteEle == null) {
        suiteEle = document.getRootElement();
        //         throw new RuntimeException("Can not found suite config.");
    }

    Suite suite = new Suite();
    String xmlConfPath = suiteEle.attributeValue("pageConfig");
    String pagePackage = suiteEle.attributeValue("pagePackage", "");
    String rows = suiteEle.attributeValue("rows", "1");
    String lackLines = suiteEle.attributeValue("lackLines", "nearby");
    String errorLines = suiteEle.attributeValue("errorLines", "stop");
    String afterSleep = suiteEle.attributeValue("afterSleep", "0");

    suite.setXmlConfPath(xmlConfPath);
    suite.setPagePackage(pagePackage);
    suite.setRows(rows);
    suite.setLackLines(lackLines);
    suite.setErrorLines(errorLines);
    suite.setAfterSleep(Long.parseLong(afterSleep));

    pagesParse(document, suite);

    return suite;
}

From source file:com.surenpi.autotest.suite.util.SuiteUtils.java

License:Apache License

/**
 * ?suitexml?//  ww w .  j  a v  a2  s. c  om
 * @param content
 * @return
 */
public static boolean isSuiteXml(byte[] content) {
    SAXReader reader = new SAXReader();
    try {
        Document doc = reader.read(new ByteArrayInputStream(content));
        Element rootEle = doc.getRootElement();

        String rootEleName = rootEle.getName();

        return "suite".equals(rootEleName);
    } catch (DocumentException e) {
        e.printStackTrace();
    }

    return false;
}

From source file:com.synesoft.fisp.app.common.utils.urlcfg.UrlBackXmlCfg.java

License:Open Source License

public static UrlBackConfiguration analyzeXmlFile() throws DocumentException {

    SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(new File(urlcfgPath + "urlback.xml"));
    // get root element
    Element messageElement = document.getRootElement();

    // get root's sub element
    Iterator<Element> oneLevelElementItor = messageElement.elementIterator();

    List<UrlCfg> urlList = new ArrayList<UrlCfg>();
    List<UrlCfgs> urlCfgsList = new ArrayList<UrlCfgs>();

    while (oneLevelElementItor.hasNext()) {
        Element e1 = oneLevelElementItor.next();
        Iterator<Element> twoLevelElementItor = e1.elementIterator();
        // get url list
        if (e1.getName().equals("url-list")) {
            while (twoLevelElementItor.hasNext()) {
                Element e2 = twoLevelElementItor.next();
                UrlCfg uc = new UrlCfg();
                uc.setId(e2.attributeValue("id"));
                uc.setValue(e2.attributeValue("value"));
                urlList.add(uc);// www  .j a v a 2 s  .  c o m
            }
        } else if (e1.getName().equals("url-cfgs")) {
            // get url back cfgs
            while (twoLevelElementItor.hasNext()) {
                UrlCfgs urlcfgs = new UrlCfgs();
                Element e2 = twoLevelElementItor.next();
                List<UrlCfg> urlBackCfgList = new ArrayList<UrlCfg>();
                Iterator<Element> threeLevelElementItor = e2.elementIterator();
                while (threeLevelElementItor.hasNext()) {
                    Element e3 = threeLevelElementItor.next();
                    UrlCfg uc = new UrlCfg();
                    uc.setId(e3.attributeValue("id"));
                    urlBackCfgList.add(uc);
                }
                urlcfgs.setUrlCfgList(urlBackCfgList);
                urlcfgs.setId(e2.attributeValue("id"));
                urlCfgsList.add(urlcfgs);
            }
        }
    }

    UrlBackConfiguration ubcfg = new UrlBackConfiguration();
    ubcfg.setUrlList(urlList);
    ubcfg.setUrlCfgs(urlCfgsList);
    return ubcfg;
}

From source file:com.synesoft.fisp.app.common.utils.XmlFileUtil.java

License:Open Source License

/**
 * read all elements/*from   w ww  .j av  a2  s  .co  m*/
 * @param filePathName
 * @return
 */
public static List<Element> readAllElements(String filePathName) {
    // read xml document
    SAXReader saxReader = new SAXReader();
    Document doc = null;
    try {
        doc = saxReader.read(new FileInputStream(new File(filePathName)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
    return doc.getRootElement().elements();
}

From source file:com.taobao.android.builder.tasks.library.publish.UpdatePomTask.java

License:Apache License

private void updatePomXml(File xml) throws IOException, DocumentException {

    Map<String, DependencyExtraInfo> extraInfoMap = getExtraMap();

    File backupFile = new File(xml.getParentFile(), "pom-backup.xml");
    FileUtils.deleteQuietly(backupFile);

    FileUtils.moveFile(xml, backupFile);

    XMLWriter writer = null;// XML
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");// XML??
    FileOutputStream fos = new FileOutputStream(xml);

    try {// ww w.j a  va  2  s  .  co m
        Document document = reader.read(backupFile);// ?XML

        Element dependencies = document.getRootElement().element("dependencies");

        if ((null != dependencies) && null != dependencies.elements()) {
            List<Element> list = dependencies.elements();

            for (Element element : list) {

                List<Element> itemList = element.elements();

                String group = "";
                String name = "";
                String type;
                String scope;

                Element scopeEl = null;
                Element typeEl = null;

                for (Element element1 : itemList) {
                    if ("groupId".equals(element1.getQName().getName())) {
                        group = element1.getStringValue();
                    } else if ("artifactId".equals(element1.getQName().getName())) {
                        name = element1.getStringValue();
                    } else if ("scope".equals(element1.getQName().getName())) {
                        scope = element1.getStringValue();
                        scopeEl = element1;
                    } else if ("type".equals(element1.getQName().getName())) {
                        type = element1.getStringValue();
                        typeEl = element1;
                    }
                }

                DependencyExtraInfo dependencyExtraInfo = extraInfoMap.get(group + ":" + name);
                if (null == dependencyExtraInfo) {
                    continue;
                }

                //update scope
                if (StringUtils.isNotEmpty(dependencyExtraInfo.scope)) {
                    if (null != scopeEl) {
                        scopeEl.setText(dependencyExtraInfo.scope);
                    } else {
                        Element newEl = element.addElement("scope");
                        newEl.setText(dependencyExtraInfo.scope);
                    }
                }

                if (StringUtils.isNotEmpty(dependencyExtraInfo.type)) {
                    if (null != typeEl) {
                        typeEl.setText(dependencyExtraInfo.type);
                    } else {
                        Element newEl = element.addElement("type");
                        newEl.setText(dependencyExtraInfo.type);
                    }
                }

            }
        }

        writer = new XMLWriter(fos, format);
        writer.write(document);

    } finally {
        if (null != writer) {
            writer.close();
        }
        IOUtils.closeQuietly(fos);
    }

}

From source file:com.taobao.android.builder.tasks.tpatch.DiffBundleInfoTask.java

License:Apache License

private static ArtifactBundleInfo getMainArtifactBundInfo(File manifestFile) {
    ArtifactBundleInfo mainBundleInfo = new ArtifactBundleInfo();
    SAXReader reader = new SAXReader();
    Document document = null;// ?XML
    try {/*from w  ww  .j  a va 2  s. co  m*/
        document = reader.read(manifestFile);
    } catch (DocumentException e) {
        throw new GradleException(e.getMessage(), e);
    }
    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");
            mainBundleInfo.setName(labelAttribute.getValue());
        }
    }

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

    if ("manifest".equalsIgnoreCase(root.getName())) {
        List<Attribute> attributes = root.attributes();
        for (Attribute attr : attributes) {
            if (StringUtils.equalsIgnoreCase(attr.getName(), "versionName")) {
                String versionName = attr.getValue();
                mainBundleInfo.setVersion(versionName);
            }
        }
    }
    String pkgName = root.attributeValue("package");
    mainBundleInfo.setPkgName(pkgName);
    return mainBundleInfo;
}

From source file:com.taobao.android.builder.tools.bundleinfo.BundleInfoUtils.java

License:Apache License

/**
 * ?manifestBundleInfo//from   w  w  w  .j  ava 2  s  . c  o  m
 *
 * @return
 */
private static void update(AwbBundle awbBundle, Map<String, BundleInfo> bundleInfoMap,
        AppVariantContext appVariantContext, String apkVersion) throws DocumentException {

    String artifactId = awbBundle.getResolvedCoordinates().getArtifactId();
    BundleInfo bundleInfo = bundleInfoMap.get(artifactId);
    if (null != bundleInfo) {
        awbBundle.bundleInfo = bundleInfo;
    } else {
        bundleInfo = awbBundle.bundleInfo;
    }

    awbBundle.isRemote = appVariantContext.getAtlasExtension().getTBuildConfig().getOutOfApkBundles()
            .contains(artifactId);
    bundleInfo.setIsInternal(!awbBundle.isRemote);
    bundleInfo.setVersion(apkVersion + "@" + awbBundle.getResolvedCoordinates().getVersion());
    bundleInfo.setPkgName(awbBundle.getPackageName());

    String applicationName = ManifestFileUtils.getApplicationName(awbBundle.getOrgManifestFile());
    if (StringUtils.isNotEmpty(applicationName)) {
        bundleInfo.setApplicationName(applicationName);
    }

    SAXReader reader = new SAXReader();
    Document document = reader.read(awbBundle.getManifest());// ?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());
        }
    }

    addComponents(bundleInfo, root);

    for (AndroidLibrary depLib : awbBundle.getLibraryDependencies()) {
        SAXReader reader2 = new SAXReader();
        Document document2 = reader2.read(depLib.getManifest());// ?XML
        Element root2 = document2.getRootElement();// 
        addComponents(bundleInfo, root2);
    }
}

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

License:Apache License

/**
 * manifest???//from   www  . ja v  a 2 s. c  om
 *
 * @param mainManifest
 * @param libManifestMap
 * @param baseBunfleInfoFile
 * @param manifestOptions
 */
public static void postProcessManifests(File mainManifest, Map<String, File> libManifestMap,
        Multimap<String, File> libDependenciesMaps, File baseBunfleInfoFile, ManifestOptions manifestOptions,
        boolean addMultiDex, Set<String> remoteBundles) throws IOException, DocumentException {
    File backupFile = new File(mainManifest.getParentFile(), "AndroidManifest-backup.xml");
    FileUtils.deleteQuietly(backupFile);
    FileUtils.moveFile(mainManifest, backupFile);

    XMLWriter writer = null;// XML
    SAXReader reader = new SAXReader();
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding("UTF-8");// XML??
    FileOutputStream fos = new FileOutputStream(mainManifest);
    if (mainManifest.exists()) {
        try {
            Document document = reader.read(backupFile);// ?XML
            if (null != baseBunfleInfoFile && baseBunfleInfoFile.exists()) {
                addApplicationMetaData(document, libManifestMap, baseBunfleInfoFile, manifestOptions,
                        remoteBundles);
            }
            if (null != manifestOptions && manifestOptions.isAddBundleLocation()) {
                addBundleLocationToDestManifest(document, libManifestMap, libDependenciesMaps, manifestOptions);
            }
            if (null != manifestOptions && manifestOptions.isReplaceApplication()) {
                replaceManifestApplicationName(document);
            }
            if ((null != manifestOptions && manifestOptions.isAddMultiDexMetaData()) || addMultiDex) {
                addMultiDexMetaData(document);
            }
            if (null != manifestOptions && manifestOptions.isRemoveProvider()) {
                removeProvider(document);
            }
            removeCustomLaunches(document, manifestOptions);
            updatePermission(document, manifestOptions);
            removeComments(document);

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