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.ai.tools.generator.util.DocumentUtil.java

License:Open Source License

public static Document readDocumentFromURL(String url, boolean validate) throws DocumentException, IOException {
    SAXReader reader = SAXReaderFactory.getInstance(validate);

    return reader.read(new URL(url));
}

From source file:com.ai.tools.generator.util.DocumentUtil.java

License:Open Source License

public static Document readDocumentFromXML(String xml, boolean validate) throws DocumentException {
    SAXReader reader = SAXReaderFactory.getInstance(validate);

    return reader.read(new XMLSafeReader(xml));
}

From source file:com.alibaba.stonelab.toolkit.autoconf.Autoconf.java

License:Open Source License

@SuppressWarnings("unchecked")
private void parser(String base, String file) throws Exception {
    SAXReader reader = new SAXReader();
    Document doc = reader.read(new File(file));
    List<DefaultElement> pnodes = doc.selectNodes("config/group/property");

    StringBuilder defaults = new StringBuilder();
    for (DefaultElement node : pnodes) {
        String prop = node.attributeValue("name");
        props.add(new Propinfo(StringUtils.replace(prop, ".", "_"), file));
        // default valus
        defaults.append(node.attributeValue("defaultValue"));
    }/*from   www  .j  a v  a 2s  .co  m*/

    List<DefaultElement> tnodes = doc.selectNodes("config/script/generate");
    for (DefaultElement node : tnodes) {
        templates.add(new AutoconfTemplate(base + "/" + node.attribute("template").getText()));
    }

    Set<String> tmp = AutoconfUtil.parsePlaceholder(defaults.toString());
    for (String str : tmp) {
        usedProps.add(StringUtils.replace(str, ".", "_"));
    }
}

From source file:com.alibaba.stonelab.toolkit.learning.xml.Dom4jParser.java

License:Open Source License

public static void dom4j() throws Exception {
    SAXReader reader = new SAXReader(true);
    reader.setEntityResolver(new EntityResolver());
    reader.setFeature("http://xml.org/sax/features/validation", true);
    reader.setFeature("http://apache.org/xml/features/validation/schema", true);
    Document doc = reader.read(Dom4jParser.class.getResourceAsStream(XML_LOCATION));
    System.out.println(doc);//from w  w  w  . j  a v a  2  s .  c  o m
}

From source file:com.aliyun.odps.ogg.handler.datahub.ConfigureReader.java

License:Apache License

public static Configure reader(String configueFileName) throws DocumentException {
    logger.info("Begin read configure[" + configueFileName + "]");

    Configure configure = new Configure();
    SAXReader reader = new SAXReader();
    File file = new File(configueFileName);

    Document document = reader.read(file);
    Element root = document.getRootElement();

    String elementText = root.elementTextTrim("batchSize");
    if (StringUtils.isNotBlank(elementText)) {
        configure.setBatchSize(Integer.parseInt(elementText));
    }/*from   w w w  .ja v  a2 s. c o m*/

    elementText = root.elementTextTrim("dirtyDataContinue");
    if (StringUtils.isNotBlank(elementText)) {
        configure.setDirtyDataContinue(Boolean.parseBoolean(elementText));
    }

    elementText = root.elementTextTrim("dirtyDataFile");
    if (StringUtils.isNotBlank(elementText)) {
        configure.setDirtyDataFile(elementText);
    }

    elementText = root.elementTextTrim("dirtyDataFileMaxSize");
    if (StringUtils.isNotBlank(elementText)) {
        configure.setDirtyDataFileMaxSize(Integer.parseInt(elementText));
    }

    elementText = root.elementTextTrim("retryTimes");
    if (StringUtils.isNotBlank(elementText)) {
        configure.setRetryTimes(Integer.parseInt(elementText));
    }

    elementText = root.elementTextTrim("retryInterval");
    if (StringUtils.isNotBlank(elementText)) {
        configure.setRetryInterval(Integer.parseInt(elementText));
    }

    elementText = root.elementTextTrim("disableCheckPointFile");
    if (StringUtils.isNotBlank(elementText)) {
        configure.setDisableCheckPointFile(Boolean.parseBoolean(elementText));
    }

    elementText = root.elementTextTrim("checkPointFileName");
    if (StringUtils.isNotBlank(elementText)) {
        configure.setCheckPointFileName(elementText);
    }

    Element element = root.element("defaultOracleConfigure");
    if (element == null) {
        throw new RuntimeException("defaultOracleConfigure is null");
    }

    elementText = element.elementTextTrim("sid");
    if (StringUtils.isBlank(elementText)) {
        throw new RuntimeException("defaultOracleConfigure.sid is null");
    }
    configure.setSid(elementText);

    String defaultOracleSchema = element.elementTextTrim("schema");

    SimpleDateFormat defaultSimpleDateFormat;
    elementText = element.elementTextTrim("dateFormat");
    if (StringUtils.isNotBlank(elementText)) {
        defaultSimpleDateFormat = new SimpleDateFormat(elementText);
    } else {
        defaultSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    element = root.element("defalutDatahubConfigure");
    if (element == null) {
        throw new RuntimeException("defalutDatahubConfigure is null");
    }

    String endPoint = element.elementText("endPoint");
    if (StringUtils.isBlank(endPoint)) {
        throw new RuntimeException("defalutDatahubConfigure.endPoint is null");

    }

    String defaultDatahubProject = element.elementText("project");
    String defaultDatahubAccessID = element.elementText("accessId");
    String defaultDatahubAccessKey = element.elementText("accessKey");

    Field defaultCTypeField = null;
    String defaultCTypeColumn = element.elementText("ctypeColumn");
    if (StringUtils.isNotBlank(defaultCTypeColumn)) {
        defaultCTypeField = new Field(defaultCTypeColumn, FieldType.STRING);
    }
    Field defaultCTimeField = null;
    String defaultCTimeColumn = element.elementText("ctimeColumn");
    if (StringUtils.isNotBlank(defaultCTimeColumn)) {
        defaultCTimeField = new Field(defaultCTimeColumn, FieldType.STRING);
    }
    Field defaultCidField = null;
    String defaultCidColumn = element.elementText("cidColumn");
    if (StringUtils.isNotBlank(defaultCidColumn)) {
        defaultCidField = new Field(defaultCidColumn, FieldType.STRING);
    }

    String defaultConstColumnMapStr = element.elementText("constColumnMap");
    Map<String, String> defalutConstColumnMappings = Maps.newHashMap();
    Map<String, Field> defaultConstColumnFieldMappings = Maps.newHashMap();
    parseConstColumnMap(defaultConstColumnMapStr, defalutConstColumnMappings, defaultConstColumnFieldMappings);

    element = root.element("mappings");
    if (element == null) {
        throw new RuntimeException("mappings is null");
    }

    List<Element> mappingElements = element.elements("mapping");
    if (mappingElements == null || mappingElements.size() == 0) {
        throw new RuntimeException("mappings.mapping is null");
    }

    //init table mapping
    for (Element e : mappingElements) {
        String oracleSchema = e.elementTextTrim("oracleSchema");
        if (StringUtils.isNotBlank(oracleSchema)) {
            //nothing
        } else if (StringUtils.isNotBlank(defaultOracleSchema)) {
            oracleSchema = defaultOracleSchema;
        } else {
            throw new RuntimeException(
                    "both mappings.mapping.oracleSchema and defaultOracleConfigure.schema is null");
        }

        String oracleTable = e.elementTextTrim("oracleTable");
        if (StringUtils.isBlank(oracleTable)) {
            throw new RuntimeException("mappings.mapping.oracleTable is null");
        }

        String datahubProject = e.elementTextTrim("datahubProject");
        if (StringUtils.isNotBlank(datahubProject)) {
            //nothing
        } else if (StringUtils.isNotBlank(defaultOracleSchema)) {
            datahubProject = defaultDatahubProject;
        } else {
            throw new RuntimeException(
                    "both mappings.mapping.datahubProject and defalutDatahubConfigure.project is null");
        }

        String datahubAccessId = e.elementTextTrim("datahubAccessId");
        if (StringUtils.isNotBlank(datahubAccessId)) {
            //nothing
        } else if (StringUtils.isNotBlank(defaultDatahubAccessID)) {
            datahubAccessId = defaultDatahubAccessID;
        } else {
            throw new RuntimeException(
                    "both mappings.mapping.datahubAccessId and defalutDatahubConfigure.accessId is null");
        }

        String datahubAccessKey = e.elementTextTrim("datahubAccessKey");
        if (StringUtils.isNotBlank(datahubAccessKey)) {
            //nothing
        } else if (StringUtils.isNotBlank(defaultDatahubAccessKey)) {
            datahubAccessKey = defaultDatahubAccessKey;
        } else {
            throw new RuntimeException(
                    "both mappings.mapping.datahubAccessKey and defalutDatahubConfigure.accessKey is null");
        }

        String topicName = e.elementTextTrim("datahubTopic");
        if (topicName == null) {
            throw new RuntimeException("mappings.mapping.datahubTopic is null");
        }

        String ctypeColumn = e.elementText("ctypeColumn");
        String ctimeColumn = e.elementText("ctimeColumn");
        String cidColumn = e.elementText("cidColumn");

        DatahubConfiguration datahubConfiguration = new DatahubConfiguration(
                new AliyunAccount(datahubAccessId, datahubAccessKey), endPoint);
        Project project = Project.Builder.build(datahubProject, datahubConfiguration);
        Topic topic = project.getTopic(topicName);
        if (topic == null) {
            throw new RuntimeException("Can not find datahub topic[" + topicName + "]");
        } else {
            logger.info(
                    "topic name: " + topicName + ", topic schema: " + topic.getRecordSchema().toJsonString());
        }

        TableMapping tableMapping = new TableMapping();
        tableMapping.setTopic(topic);
        tableMapping.setOracleSchema(oracleSchema.toLowerCase());
        tableMapping.setOracleTableName(oracleTable.toLowerCase());
        tableMapping.setOracleFullTableName(
                tableMapping.getOracleSchema() + "." + tableMapping.getOracleTableName());
        tableMapping
                .setCtypeField(StringUtils.isNotBlank(ctypeColumn) ? new Field(ctypeColumn, FieldType.STRING)
                        : defaultCTypeField);
        tableMapping
                .setCtimeField(StringUtils.isNotBlank(ctimeColumn) ? new Field(ctimeColumn, FieldType.STRING)
                        : defaultCTimeField);
        tableMapping.setCidField(
                StringUtils.isNotBlank(cidColumn) ? new Field(cidColumn, FieldType.STRING) : defaultCidField);

        String constColumnMapStr = e.elementText("constColumnMap");
        Map<String, String> constColumnMappings = Maps.newHashMap();
        Map<String, Field> constColumnFieldMappings = Maps.newHashMap();
        parseConstColumnMap(constColumnMapStr, constColumnMappings, constColumnFieldMappings);

        tableMapping.setConstColumnMappings(
                constColumnMappings.isEmpty() ? defalutConstColumnMappings : constColumnMappings);
        tableMapping.setConstFieldMappings(constColumnFieldMappings.isEmpty() ? defaultConstColumnFieldMappings
                : constColumnFieldMappings);

        Map<String, ColumnMapping> columnMappings = Maps.newHashMap();
        tableMapping.setColumnMappings(columnMappings);

        elementText = e.elementTextTrim("shardId");
        if (StringUtils.isNotBlank(elementText)) {
            tableMapping.setShardId(elementText);
        }

        configure.addTableMapping(tableMapping);

        RecordSchema recordSchema = topic.getRecordSchema();

        Element columnMappingElement = e.element("columnMapping");
        List<Element> columns = columnMappingElement.elements("column");
        for (Element columnElement : columns) {
            String oracleColumnName = columnElement.attributeValue("src");
            if (StringUtils.isBlank(oracleColumnName)) {
                throw new RuntimeException("Topic[" + topicName + "] src attribute is null");
            }

            oracleColumnName = oracleColumnName.toLowerCase();
            ColumnMapping columnMapping = new ColumnMapping();
            columnMappings.put(oracleColumnName, columnMapping);
            columnMapping.setOracleColumnName(oracleColumnName);

            String datahubFieldName = columnElement.attributeValue("dest");
            if (datahubFieldName == null) {
                throw new RuntimeException("Topic[" + topicName + "] dest attribute is null");
            }

            Field field = recordSchema.getField(datahubFieldName.toLowerCase());
            if (field == null) {
                throw new RuntimeException(
                        "Topic[" + topicName + "] Field[" + datahubFieldName + "] is not exist");
            }

            columnMapping.setField(field);

            String datahubOldFieldName = columnElement.attributeValue("destOld");
            if (StringUtils.isNotBlank(datahubOldFieldName)) {
                Field oldField = recordSchema.getField(datahubOldFieldName);

                if (field == null) {
                    throw new RuntimeException(
                            "Topic[" + topicName + "] Field[" + datahubOldFieldName + "] is not exist");
                }
                columnMapping.setOldFiled(oldField);
            }

            String isShardColumn = columnElement.attributeValue("isShardColumn");
            if (StringUtils.isNotBlank(isShardColumn) && Boolean.TRUE.equals(Boolean.valueOf(isShardColumn))) {
                tableMapping.setIsShardHash(true);
                columnMapping.setIsShardColumn(true);
            } else {
                columnMapping.setIsShardColumn(false);
            }

            String isKeyColumn = columnElement.attributeValue("isKeyColumn");
            if (StringUtils.isNotBlank(isKeyColumn) && Boolean.TRUE.equals(Boolean.valueOf(isKeyColumn))) {
                columnMapping.setIsKeyColumn(true);
            } else {
                columnMapping.setIsKeyColumn(false);
            }

            String dateFormat = columnElement.attributeValue("dateFormat");

            if (StringUtils.isNotBlank(dateFormat)) {
                columnMapping.setSimpleDateFormat(new SimpleDateFormat(dateFormat));
            } else {
                columnMapping.setSimpleDateFormat(defaultSimpleDateFormat);
            }

            String isDateFormat = columnElement.attributeValue("isDateFormat");

            if (StringUtils.isNotBlank(isDateFormat) && Boolean.FALSE.equals(Boolean.valueOf(isDateFormat))) {
                columnMapping.setIsDateFormat(false);
            } else {
                columnMapping.setIsDateFormat(true);
            }
        }
    }

    logger.info("Read configure success: " + JsonHelper.beanToJson(configure));
    return configure;
}

From source file:com.allinfinance.common.grid.GridConfigUtil.java

License:Open Source License

/**
 * ??//from w  ww . ja v  a2s.  c o m
 * @param context
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static void initGirdConfig(ServletContext context) throws Exception {
    SAXReader reader = new SAXReader();

    Document document = null;
    document = reader.read(context.getResourceAsStream(Constants.GRID_CONFIG_CONTEXTPATH));

    Element root = document.getRootElement();

    List<Element> gridInfoList = root.elements(GridConfigConstants.NODE_GRID);

    for (Element gridNode : gridInfoList) {
        String gridId = gridNode.attributeValue(GridConfigConstants.GRID_ID);
        String gridConfigType = gridNode.attributeValue(GridConfigConstants.GRID_TYPE);

        //?
        GridModel gridModel = new GridModel();
        gridModel.setId(gridId);
        gridModel.setType(gridConfigType);
        gridModel.setColumns(gridNode.elementText(GridConfigConstants.COLUMNS).trim());

        if (GridConfigConstants.TYPE_SQL.equals(gridConfigType)) {

            SqlMode sqlMode = new SqlMode();
            //SQL?
            Element sqlModeNode = gridNode.element(GridConfigConstants.TYPE_SQLMODE);
            //?
            Element wheresNode = sqlModeNode.element(GridConfigConstants.WHERES);

            List<Element> whereList = wheresNode.elements(GridConfigConstants.WHERE);

            sqlMode.setSql(sqlModeNode.elementText(GridConfigConstants.SQL).trim());
            sqlMode.setDao(sqlModeNode.elementText(GridConfigConstants.QUERY_DAO).trim());

            //?
            if (whereList != null && whereList.size() > 0) {
                WheresModel wheresModel = new WheresModel();
                List<WhereModel> whereModelList = new ArrayList<WhereModel>();
                for (Element whereNode : whereList) {
                    WhereModel whereModel = new WhereModel();
                    whereModel.setType(whereNode.attributeValue(GridConfigConstants.WHERE_TYPE));
                    whereModel.setOperator(whereNode.attributeValue(GridConfigConstants.WHERE_OPERATOR));
                    whereModel.setLogic(whereNode.attributeValue(GridConfigConstants.WHERE_LOGIC));
                    whereModel.setDataBaseColumn(
                            whereNode.elementText(GridConfigConstants.WHERE_DATABASE_COLUMN).trim());
                    whereModel.setQueryColumn(
                            whereNode.elementText(GridConfigConstants.WHERE_QUERY_COLUMN).trim());
                    whereModelList.add(whereModel);
                }
                wheresModel.setWhereModelList(whereModelList);
                sqlMode.setWheresModel(wheresModel);
            }

            // ???
            if (sqlModeNode.element(GridConfigConstants.ORDERS) != null) {
                OrdersModel ordersModel = new OrdersModel();
                Element orderModels = sqlModeNode.element(GridConfigConstants.ORDERS);
                ordersModel.setSort(orderModels.attributeValue("sort"));
                List<Element> orderList = orderModels.elements(GridConfigConstants.ORDER);
                for (Element element : orderList) {
                    ordersModel.getOrders().add(element.getText());
                }
                sqlMode.setOrdersModel(ordersModel);
            }

            gridModel.setSqlMode(sqlMode);
        } else if (GridConfigConstants.TYPE_SYNC.equals(gridConfigType)) {

            SyncMode syncMode = new SyncMode();

            Element syncModeNode = gridNode.element(GridConfigConstants.TYPE_SYNCMODE);

            Element methodNode = syncModeNode.element(GridConfigConstants.SYNC_METHOD);

            syncMode.setMethod(methodNode.attributeValue(GridConfigConstants.SYNC_METHOD_VALUE));

            gridModel.setSyncMode(syncMode);
        } else {
            throw new Exception("???[ id:" + gridId + " ]");
        }

        gridConfigMap.put(gridId, gridModel);
    }
}

From source file:com.amalto.workbench.utils.LocalTreeObjectRepository.java

License:Open Source License

public void startUp(String ur, String user, String pwd) {
    TMDMService service = null;/*from   ww  w .  j av a 2s.c om*/
    Document doc = null;
    SAXReader saxReader = new SAXReader();

    try {
        service = Util.getMDMService(new URL(ur), user, pwd);
        WSCategoryData category = service.getMDMCategory(null);
        doc = saxReader.read(new StringReader(category.getCategorySchema()));
        saveCredential(ur, user, pwd, doc, service, true);
        doUpgrade(UnifyUrl(ur));
    } catch (Exception e) {

        log.error(e.getMessage(), e);
        String empty = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";//$NON-NLS-1$
        empty += "<" + ICoreConstants.DEFAULT_CATEGORY_ROOT + "/>";//$NON-NLS-1$//$NON-NLS-2$
        WSCategoryData newData = new WSCategoryData();
        newData.setCategorySchema(empty);
        try {
            newData = service.getMDMCategory(newData);
            doc = saxReader.read(new StringReader(newData.getCategorySchema()));
            saveCredential(ur, user, pwd, doc, service, true);
        } catch (Exception e1) {
            saveCredential(ur, user, pwd, doc, service, false);
        }

    }
}

From source file:com.amalto.workbench.utils.LocalTreeObjectRepository.java

License:Open Source License

private Element parseElements(String schema) {
    SAXReader saxReader = new SAXReader();
    try {/*from  w  w  w  .j  av  a2 s  . c  o m*/
        Document doc = saxReader.read(new StringReader(schema));
        return doc.getRootElement();
    } catch (DocumentException e) {
        log.error(e.getMessage(), e);
    }

    return null;
}

From source file:com.amalto.workbench.utils.MDMServerHelper.java

License:Open Source License

private static Element getRootElement() {
    Document logininfoDocument;//from   w ww.  j  a v a  2 s  .co  m
    Element rootElement;
    if (new File(workbenchConfigFile).exists()) {
        try {
            SAXReader reader = new SAXReader();
            logininfoDocument = reader.read(new File(workbenchConfigFile));
        } catch (DocumentException e) {
            log.error(e.getMessage(), e);
            return null;
        }
        rootElement = logininfoDocument.getRootElement();
    } else {
        logininfoDocument = DocumentHelper.createDocument();
        rootElement = logininfoDocument.addElement(ROOT);
    }
    return rootElement;
}

From source file:com.amalto.workbench.utils.ResourcesUtil.java

License:Open Source License

private static Document parsXMLString(String responseBody) {
    SAXReader saxReader = new SAXReader();
    Document document = null;/*from   www.  j  a  v  a 2 s  .c  o  m*/
    try {
        document = saxReader.read(new StringReader(responseBody));
    } catch (DocumentException e) {

        return null;
    }
    return document;

}