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.liferay.portal.tools.service.builder.ServiceBuilder.java

License:Open Source License

private void _createRemotingXml() throws Exception {
    StringBundler sb = new StringBundler();

    SAXReader saxReader = _getSAXReader();

    Document document = saxReader.read(new File(_springFileName));

    Element rootElement = document.getRootElement();

    List<Element> beanElements = rootElement.elements("bean");

    for (Element beanElement : beanElements) {
        String beanId = beanElement.attributeValue("id");

        if (beanId.endsWith("Service") && !beanId.endsWith("LocalService")) {

            String entityName = beanId;

            entityName = StringUtil.replaceLast(entityName, ".service.", ".");

            int pos = entityName.lastIndexOf("Service");

            entityName = entityName.substring(0, pos);

            Entity entity = getEntity(entityName);

            String serviceName = beanId;

            String serviceMapping = serviceName;

            serviceMapping = StringUtil.replaceLast(serviceMapping, ".service.", ".service.spring.");
            serviceMapping = StringUtil.replace(serviceMapping, StringPool.PERIOD, StringPool.UNDERLINE);

            Map<String, Object> context = _getContext();

            context.put("entity", entity);
            context.put("serviceName", serviceName);
            context.put("serviceMapping", serviceMapping);

            sb.append(_processTemplate(_tplRemotingXml, context));
        }/*from  w ww .j  a  v  a  2 s . c  o m*/
    }

    File outputFile = new File(_remotingFileName);

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

    String content = FileUtils.readFileToString(outputFile);
    String newContent = content;

    int x = content.indexOf("<bean ");
    int y = content.lastIndexOf("</bean>") + 8;

    if (x != -1) {
        newContent = content.substring(0, x - 1) + sb.toString() + content.substring(y);
    } else {
        x = content.indexOf("</beans>");

        if (x != -1) {
            newContent = content.substring(0, x) + sb.toString() + content.substring(x);
        } else {
            x = content.indexOf("<beans/>");
            y = x + 8;

            newContent = content.substring(0, x) + "<beans>" + sb.toString() + "</beans>"
                    + content.substring(y);
        }
    }

    writeFileRaw(outputFile, _formatXml(newContent), _modifiedFileNames);
}

From source file:com.liferay.portal.tools.WebSiteBuilder.java

License:Open Source License

public static List getWebSites() throws Exception {
    File file = new File("../web-sites/web-sites.xml");

    SAXReader reader = new SAXReader();

    Document doc = null;//  w w w  .j av  a 2s .  c  o m

    try {
        doc = reader.read(file);
    } catch (DocumentException de) {
        Logger.error(WebSiteBuilder.class, de.getMessage(), de);
    }

    Element root = doc.getRootElement();

    List webSites = new ArrayList();

    Iterator itr = root.elements("web-site").iterator();

    while (itr.hasNext()) {
        Element webSite = (Element) itr.next();

        String id = webSite.attributeValue("id");
        boolean httpEnabled = GetterUtil.getBoolean(webSite.attributeValue("http-enabled"), true);
        String keystore = GetterUtil.getString(webSite.attributeValue("keystore"));
        String keystorePassword = GetterUtil.getString(webSite.attributeValue("keystore-password"));
        String virtualHosts = GetterUtil.getString(webSite.attributeValue("virtual-hosts"));
        String forwardURL = GetterUtil.getString(webSite.attributeValue("forward-url"), "/c");

        webSites.add(new WebSite(id, httpEnabled, keystore, keystorePassword, virtualHosts, forwardURL));
    }

    return webSites;
}

From source file:com.liferay.util.xml.XMLMergerRunner.java

License:Open Source License

private String _merge(String masterXml, String slaveXml) throws ClassNotFoundException, DocumentException,
        IllegalAccessException, InstantiationException, IOException {

    int pos = masterXml.indexOf("<!DOCTYPE");
    String masterDoctype = "";
    if (pos >= 0) {
        masterDoctype = masterXml.substring(pos, masterXml.indexOf(">", pos) + 1);
        masterXml = StringUtil.replace(masterXml, masterDoctype, "");
    }//from  w  w w. j a  v a  2  s. c  o  m

    pos = slaveXml.indexOf("<!DOCTYPE");
    String slaveDoctype = "";
    if (pos >= 0) {
        slaveDoctype = slaveXml.substring(pos, slaveXml.indexOf(">", pos) + 1);
        slaveXml = StringUtil.replace(slaveXml, slaveDoctype, "");
    }

    String doctype = null;
    if (Validator.isNotNull(masterDoctype)) {
        doctype = masterDoctype;
    } else {
        doctype = slaveDoctype;
    }

    SAXReader reader = new SAXReader();

    Document masterDoc = reader.read(new StringReader(masterXml));
    Document slaveDoc = reader.read(new StringReader(slaveXml));

    XMLDescriptor descriptor = null;
    if (_descriptorClassName.equals(_AUTO_DESCRIPTOR)) {
        descriptor = XMLTypeDetector.determineType(doctype, masterDoc);
    } else {
        descriptor = (XMLDescriptor) Class.forName(_descriptorClassName).newInstance();
    }

    XMLMerger merger = new XMLMerger(descriptor);

    Document mergedDoc = merger.merge(masterDoc, slaveDoc);

    return _documentToString(mergedDoc, doctype);
}

From source file:com.littcore.license.LicenseManager.java

/**
 * ??./*from w  ww  .jav  a2 s.c  o  m*/
 * 
 * @param licenseFile ?
 * 
 * @return ?
 * 
 * @throws FileNotFoundException 
 * @throws DocumentException ?
 */
public static License loadLicense(File licenseFile) throws DocumentException {
    SAXReader saxReader = new SAXReader();
    Document document = saxReader.read(licenseFile);
    return loadLicense(document);
}

From source file:com.log4ic.compressor.utils.MemcachedUtils.java

License:Open Source License

@SuppressWarnings("unchecked")
private static MemcachedConfig getMemcachedConfig(InputStream in) throws DocumentException {
    SAXReader reader = new SAXReader();

    MemcachedConfig config = new MemcachedConfig();

    Document doc = reader.read(in);

    logger.debug("??...");
    List<Node> nodeList = doc.selectNodes("/memcached/servers/server");
    if (nodeList.isEmpty()) {
        throw new DocumentException(CONFIG_FILE_PATH + " file memcached.servers server element empty!");
    } else {// w  w  w  .j  a va  2s .c o  m
        logger.debug("???" + nodeList.size() + ".");
    }

    AddressConfig addrConf = biludAddrMapConfig(nodeList);

    config.builder = new XMemcachedClientBuilder(addrConf.addressMap, addrConf.widgets);

    Element el = (Element) doc.selectSingleNode("/memcached");
    logger.debug("??...");
    Attribute attr = el.attribute("connectionPoolSize");
    if (attr != null) {
        String connPoolSize = attr.getValue();
        if (StringUtils.isNotBlank(connPoolSize)) {
            try {
                config.builder.setConnectionPoolSize(Integer.parseInt(connPoolSize));
                logger.debug("?" + connPoolSize);
            } catch (Exception e) {
                logger.error("???", e);
            }
        } else {
            logger.error("???");
        }
    } else {
        logger.warn("??");
    }
    logger.debug("??...");
    attr = el.attribute("enableHeartBeat");
    if (attr != null) {
        String enableHeartBeatS = attr.getValue();
        if (StringUtils.isNotBlank(enableHeartBeatS)) {
            try {
                config.enableHeartBeat = Boolean.parseBoolean(enableHeartBeatS);
                logger.debug("???" + enableHeartBeatS);
            } catch (Exception e) {
                logger.error("?????", e);
            }
        } else {
            logger.error("?????");
        }
    } else {
        logger.warn("????");
    }
    logger.debug("?...");
    attr = el.attribute("sessionIdleTimeout");
    if (attr != null) {
        String sessionIdleTimeout = attr.getValue();
        if (StringUtils.isNotBlank(sessionIdleTimeout)) {
            try {
                config.builder.getConfiguration().setSessionIdleTimeout(Long.parseLong(sessionIdleTimeout));
                logger.debug("?" + sessionIdleTimeout);
            } catch (Exception e) {
                logger.error("???", e);
            }
        } else {
            logger.error("???");
        }
    } else {
        logger.warn("??");
    }
    //?
    logger.debug("?...");
    attr = el.attribute("statisticsServer");
    if (attr != null) {
        String statisticsServer = attr.getValue();
        if (StringUtils.isNotBlank(statisticsServer)) {
            try {
                config.builder.getConfiguration().setStatisticsServer(Boolean.parseBoolean(statisticsServer));
                logger.debug("?" + statisticsServer);
            } catch (Exception e) {
                logger.error("???", e);
            }
        } else {
            logger.error("???");
        }
    } else {
        logger.warn("??");
    }
    logger.debug("?...");
    attr = el.attribute("statisticsInterval");
    if (attr != null) {
        String statisticsInterval = attr.getValue();
        if (StringUtils.isNotBlank(statisticsInterval)) {
            try {
                config.builder.getConfiguration().setStatisticsInterval(Long.parseLong(statisticsInterval));
                logger.debug("?" + statisticsInterval);
            } catch (Exception e) {
                logger.error("???", e);
            }
        } else {
            logger.error("???");
        }
    } else {
        logger.warn("??");
    }
    logger.debug("????...");
    attr = el.attribute("optimizeMergeBuffer");
    if (attr != null) {
        String optimizeMergeBufferS = attr.getValue();
        if (StringUtils.isNotBlank(optimizeMergeBufferS)) {
            try {
                config.optimizeMergeBuffer = Boolean.parseBoolean(optimizeMergeBufferS);
                logger.debug("????" + optimizeMergeBufferS);
            } catch (Exception e) {
                logger.error("??????", e);
            }
        } else {
            logger.error("??????");
        }
    } else {
        logger.warn("?????");
    }
    logger.debug("??...");
    attr = el.attribute("mergeFactor");
    if (attr != null) {
        String mergeFactorS = attr.getValue();
        if (StringUtils.isNotBlank(mergeFactorS)) {
            try {
                config.mergeFactor = Integer.parseInt(mergeFactorS);
                logger.debug("?" + mergeFactorS);
            } catch (Exception e) {
                logger.error("???", e);
            }
        } else {
            logger.error("???");
        }
    } else {
        logger.warn("??");
    }
    logger.debug("??...");
    attr = el.attribute("commandFactory");
    if (attr != null) {
        String commandFactory = attr.getValue();
        if (StringUtils.isNotBlank(commandFactory)) {
            try {
                config.builder.setCommandFactory((CommandFactory) Class.forName(commandFactory).newInstance());
                logger.debug("??" + commandFactory);
            } catch (Exception e) {
                logger.error("????", e);
            }
        } else {
            logger.error("????");
        }
    } else {
        logger.warn("???");
    }
    config.builder.setCommandFactory(new BinaryCommandFactory());
    logger.debug("...");
    nodeList = doc.selectNodes("/memcached/socketOption/*");
    if (!nodeList.isEmpty()) {
        for (Node n : nodeList) {
            try {
                attr = ((Element) n).attribute("type");
                if (attr == null) {
                    logger.error("type attribute undefined");
                } else {
                    String type = attr.getValue();
                    if (StringUtils.isNotBlank(type)) {
                        String name = n.getName();
                        String value = n.getStringValue();
                        Class valueType = Class.forName(type);
                        Constructor constructor = SocketOption.class.getConstructor(String.class, Class.class);
                        SocketOption socketOption = (SocketOption) constructor.newInstance(name, valueType);
                        constructor = valueType.getConstructor(String.class);
                        config.builder.setSocketOption(socketOption, constructor.newInstance(value));
                        logger.debug("[" + name + "]" + value);
                    }
                }
            } catch (NoSuchMethodException e) {
                logger.error("NoSuchMethodException", e);
            } catch (InvocationTargetException e) {
                logger.error("InvocationTargetException", e);
            } catch (InstantiationException e) {
                logger.error("InstantiationException", e);
            } catch (IllegalAccessException e) {
                logger.error("IllegalAccessException", e);
            } catch (ClassNotFoundException e) {
                logger.error("ClassNotFoundException", e);
            }
        }
    } else {
        logger.warn("?");
    }
    logger.debug("Failure?...");
    attr = el.attribute("failureMode");
    if (attr != null) {
        String failureMode = attr.getValue();
        if (StringUtils.isNotBlank(failureMode)) {
            try {
                config.builder.setFailureMode(Boolean.parseBoolean(failureMode));
                logger.debug("Failure?" + failureMode);
            } catch (Exception e) {
                logger.error("Failure???", e);
            }
        } else {
            logger.error("Failure???");
        }
    } else {
        logger.warn("Failure??");
    }
    return config;
}

From source file:com.love320.templateparser.factory.AppFactory.java

License:Apache License

private void xmlStr() {
    SAXReader sax = new SAXReader();
    try {/*from ww w. j  a  va 2  s  . co  m*/
        Document document = sax.read(FileUtil.get(conPath[0]));
        DOCROOT = document.getRootElement();
    } catch (DocumentException e) {
        logger.error("DocumentException", e);
    }
}

From source file:com.love320.templateparser.factory.impl.FactoryBakImpl.java

License:Apache License

private void configXMLPath(String configPath) {
    SAXReader sax = new SAXReader();
    try {//from   ww  w  . ja v  a  2s.co  m
        Document document = sax.read(configPath);
        docroot = document.getRootElement();
    } catch (DocumentException e) {
        logger.error("DocumentException", e);
    }
}

From source file:com.love320.templateparser.factory.impl.LabelBeanFactoryImpl.java

License:Apache License

public void setConfigPath(String configPath) {
    this.configPath = configPath;

    SAXReader sax = new SAXReader();
    try {/*from w ww  .  j  a va  2  s.c o m*/
        Document document = sax.read(configPath);
        DOCROOT = document.getRootElement();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:com.love320.templateparser.label.impl.LabelBeanDaoImpl.java

License:Apache License

public void setConfigPath(String confpath) {

    this.configPath = appPath(confpath);//

    ///*from   www.j  a  va2  s .c  o  m*/
    File labelFile = new File(this.configPath);
    if (!labelFile.exists()) {
        logger.warn("Label Bean Dao read config file null the not found :" + confpath);
        return;
    }

    SAXReader sax = new SAXReader();
    try {
        Document document = sax.read(labelFile);
        DOCROOT = document.getRootElement();
    } catch (DocumentException e) {
        logger.error("DocumentException", e);
    }
}

From source file:com.love320.templateparser.util.ConfigBeanXML.java

License:Apache License

private static Document getXML(InputStream is) {
    SAXReader sax = new SAXReader();
    Document document = null;/*from w  ww  .ja  v a2  s .c o  m*/
    try {
        document = sax.read(is);// ?
    } catch (DocumentException e) {
        logger.error("DocumentException", e);
    }
    return document;
}