Example usage for org.dom4j.io SAXReader SAXReader

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

Introduction

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

Prototype

public SAXReader() 

Source Link

Usage

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 ww.j  av  a2 s  .  c om*/

    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.lingxiang2014.util.SettingUtils.java

License:Open Source License

public static Setting get() {
    Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
    net.sf.ehcache.Element cacheElement = cache.get(Setting.CACHE_KEY);
    Setting setting;/*  w  ww  . j  a v a2 s .co m*/
    if (cacheElement != null) {
        setting = (Setting) cacheElement.getObjectValue();
    } else {
        setting = new Setting();
        try {
            File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile();
            Document document = new SAXReader().read(shopxxXmlFile);
            List<Element> elements = document.selectNodes("/shopxx/setting");
            for (Element element : elements) {
                String name = element.attributeValue("name");
                String value = element.attributeValue("value");
                try {
                    beanUtils.setProperty(setting, name, value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    }
    return setting;
}

From source file:com.lingxiang2014.util.SettingUtils.java

License:Open Source License

public static void set(Setting setting) {
    try {//from w  ww  . j av  a  2  s .  co  m
        File shopxxXmlFile = new ClassPathResource(CommonAttributes.SHOPXX_XML_PATH).getFile();
        Document document = new SAXReader().read(shopxxXmlFile);
        List<Element> elements = document.selectNodes("/shopxx/setting");
        for (Element element : elements) {
            try {
                String name = element.attributeValue("name");
                String value = beanUtils.getProperty(setting, name);
                Attribute attribute = element.attribute("value");
                attribute.setValue(value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }

        FileOutputStream fileOutputStream = null;
        XMLWriter xmlWriter = null;
        try {
            OutputFormat outputFormat = OutputFormat.createPrettyPrint();
            outputFormat.setEncoding("UTF-8");
            outputFormat.setIndent(true);
            outputFormat.setIndent("   ");
            outputFormat.setNewlines(true);
            fileOutputStream = new FileOutputStream(shopxxXmlFile);
            xmlWriter = new XMLWriter(fileOutputStream, outputFormat);
            xmlWriter.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (xmlWriter != null) {
                try {
                    xmlWriter.close();
                } catch (IOException e) {
                }
            }
            IOUtils.closeQuietly(fileOutputStream);
        }

        Ehcache cache = cacheManager.getEhcache(Setting.CACHE_NAME);
        cache.put(new net.sf.ehcache.Element(Setting.CACHE_KEY, setting));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

/**
 * ??.//w w  w  .  java 2 s  . com
 * 
 * @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 {//from   w ww . ja  v a2  s  .  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 w w  w .  j  a va  2 s  . c  om
        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 www  . j av a 2  s . com*/
        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  w  w.j  a  va 2s. com*/
        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 w ww. j  av  a  2 s . com*/
    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 ww  w . ja va  2 s  .c  o  m*/
    try {
        document = sax.read(is);// ?
    } catch (DocumentException e) {
        logger.error("DocumentException", e);
    }
    return document;
}