Example usage for org.dom4j.tree DefaultElement attribute

List of usage examples for org.dom4j.tree DefaultElement attribute

Introduction

In this page you can find the example usage for org.dom4j.tree DefaultElement attribute.

Prototype

public Attribute attribute(QName qName) 

Source Link

Usage

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  w  w  w  . j  av  a2  s .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:org.nuxeo.ecm.platform.scanimporter.service.ScannedFileMapperComponent.java

License:Open Source License

@Override
public ScanFileBlobHolder parseMetaData(File xmlFile) throws Exception {

    Map<String, Serializable> data = new HashMap<String, Serializable>();

    if (mappingDesc == null) {
        return null;
    }//from  ww  w.j a  v a 2  s.c o  m

    String xmlData = FileUtils.readFile(xmlFile);

    Document xmlDoc = DocumentHelper.parseText(xmlData);

    for (ScanFileFieldMapping fieldMap : mappingDesc.getFieldMappings()) {

        XPath xpath = new Dom4jXPath(fieldMap.getSourceXPath());
        List<?> nodes = xpath.selectNodes(xmlDoc);
        if (nodes.size() == 1) {
            DefaultElement elem = (DefaultElement) nodes.get(0);
            String value = null;
            if ("TEXT".equals(fieldMap.getSourceAttribute())) {
                value = elem.getText();
            } else {
                value = elem.attribute(fieldMap.getSourceAttribute()).getValue();
            }

            String target = fieldMap.getTargetXPath();
            if ("string".equalsIgnoreCase(fieldMap.getTargetType())) {
                data.put(target, value);
                continue;
            } else if ("integer".equalsIgnoreCase(fieldMap.getTargetType())) {
                data.put(target, Integer.parseInt(value));
                continue;
            } else if ("double".equalsIgnoreCase(fieldMap.getTargetType())) {
                data.put(target, Double.parseDouble(value));
                continue;
            } else if ("date".equalsIgnoreCase(fieldMap.getTargetType())) {
                data.put(target, fieldMap.getDateFormat().parse(value));
                continue;
            } else if ("boolean".equalsIgnoreCase(fieldMap.getTargetType())) {
                data.put(target, Boolean.parseBoolean(value));
                continue;
            }
            log.error("Unknown target type, please look the scan importer configuration: "
                    + fieldMap.getTargetType());
        }
        log.error("Mulliple or no element(s) found for: " + fieldMap.sourceXPath + " for "
                + xmlFile.getAbsolutePath());

    }

    List<Blob> blobs = new ArrayList<Blob>();

    for (ScanFileBlobMapping blobMap : mappingDesc.getBlobMappings()) {
        XPath xpath = new Dom4jXPath(blobMap.getSourceXPath());
        List<?> nodes = xpath.selectNodes(xmlDoc);
        for (Object node : nodes) {
            DefaultElement elem = (DefaultElement) node;
            String filePath = elem.attributeValue(blobMap.getSourcePathAttribute());
            String fileName = elem.attributeValue(blobMap.getSourceFilenameAttribute());

            // Mainly for tests
            if (filePath.startsWith("$TMP")) {
                filePath = filePath.replace("$TMP", Framework.getProperty("nuxeo.import.tmpdir"));
            }

            File file = new File(filePath);
            if (file.exists()) {
                Blob blob = new FileBlob(file);
                if (fileName != null) {
                    blob.setFilename(fileName);
                } else {
                    blob.setFilename(file.getName());
                }
                String target = blobMap.getTargetXPath();
                if (target == null) {
                    blobs.add(blob);
                } else {
                    data.put(target, (Serializable) blob);
                }
            } else {
                log.error("File " + file.getAbsolutePath() + " is referenced by " + xmlFile.getAbsolutePath()
                        + " but was not found");
            }
        }
    }

    String targetType = getTargetLeafType();
    DocumentTypeMapper mapper = mappingDesc.getTargetLeafTypeMapper();
    if (mapper != null) {
        targetType = mapper.getTargetDocumentType(xmlDoc, xmlFile);
    }
    ScanFileBlobHolder bh = new ScanFileBlobHolder(blobs, data, targetType);
    return bh;
}

From source file:org.nuxeo.ecm.platform.template.XMLSerializer.java

License:Open Source License

public static List<TemplateInput> readFromXml(String xml) throws Exception {

    List<TemplateInput> result = new ArrayList<TemplateInput>();

    Document xmlDoc = DocumentHelper.parseText(xml);

    List nodes = xmlDoc.getRootElement().elements(fieldTag);

    for (Object node : nodes) {

        DefaultElement elem = (DefaultElement) node;
        Attribute name = elem.attribute("name");
        TemplateInput param = new TemplateInput(name.getValue());

        InputType type = InputType.StringValue;

        if (elem.attribute("type") != null) {
            type = InputType.getByValue(elem.attribute("type").getValue());
            param.setType(type);//from  ww  w. j  a  va 2s. c  om
        }

        String strValue = elem.attributeValue("value");
        if (InputType.StringValue.equals(type)) {
            param.setStringValue(strValue);
        } else if (InputType.DateValue.equals(type)) {
            param.setDateValue(dateFormat.parse(strValue));
        } else if (InputType.BooleanValue.equals(type)) {
            param.setBooleanValue(new Boolean(strValue));
        } else {
            param.setSource(elem.attributeValue("source"));
        }

        if (elem.attribute("readonly") != null) {
            param.setReadOnly(Boolean.parseBoolean(elem.attributeValue("readonly")));
        }

        if (elem.attribute("autoloop") != null) {
            param.setAutoLoop(Boolean.parseBoolean(elem.attributeValue("autoloop")));
        }

        param.setDesciption(elem.getText());

        result.add(param);
    }

    return result;
}

From source file:org.nuxeo.template.XMLSerializer.java

License:Open Source License

public static List<TemplateInput> readFromXml(String xml) throws Exception {

    List<TemplateInput> result = new ArrayList<TemplateInput>();

    Document xmlDoc = DocumentHelper.parseText(xml);

    @SuppressWarnings("rawtypes")
    List nodes = xmlDoc.getRootElement().elements(fieldTag);

    for (Object node : nodes) {

        DefaultElement elem = (DefaultElement) node;
        Attribute name = elem.attribute("name");
        TemplateInput param = new TemplateInput(name.getValue());

        InputType type = InputType.StringValue;

        if (elem.attribute("type") != null) {
            type = InputType.getByValue(elem.attribute("type").getValue());
            param.setType(type);//from   w ww  . j a v a  2 s .com
        }

        String strValue = elem.attributeValue("value");
        if (InputType.StringValue.equals(type)) {
            param.setStringValue(strValue);
        } else if (InputType.DateValue.equals(type)) {
            param.setDateValue(dateFormat.parse(strValue));
        } else if (InputType.BooleanValue.equals(type)) {
            param.setBooleanValue(new Boolean(strValue));
        } else {
            param.setSource(elem.attributeValue("source"));
        }

        if (elem.attribute("readonly") != null) {
            param.setReadOnly(Boolean.parseBoolean(elem.attributeValue("readonly")));
        }

        if (elem.attribute("autoloop") != null) {
            param.setAutoLoop(Boolean.parseBoolean(elem.attributeValue("autoloop")));
        }

        param.setDesciption(elem.getText());

        result.add(param);
    }

    return result;
}

From source file:org.talend.designer.core.ui.editor.properties.controllers.RetrieveSchemaHelper.java

License:Open Source License

public static Command retrieveSchemasCommand(Node node) {

    IElementParameter param = node.getElementParameter("SCHEMA");
    IMetadataTable inputMeta = node.getMetadataFromConnector("FLOW");
    IMetadataTable inputMetaCopy = inputMeta.clone(true);

    IElementParameter outParam = node.getElementParameter("SCHEMA_OUT");
    IMetadataTable outputMeta = node.getMetadataFromConnector(outParam.getContext());
    IMetadataTable outputMetaCopy = outputMeta.clone(true);

    File xmlFile = new File(
            TalendTextUtils.removeQuotes(node.getElementParameter("PATH_JOBDEF").getValue().toString()));
    if (!xmlFile.exists())
        try {//from w  ww  .  j a  v  a  2s  .  com
            xmlFile.createNewFile();
        } catch (IOException e1) {
            ExceptionHandler.process(e1);
        }
    SAXReader saxReader = new SAXReader();
    Document document;
    AutoApi a = null;
    try {

        // get the schema file from server
        a = new AutoApi();
        String hostName = TalendTextUtils
                .removeQuotes(node.getElementParameter("HOSTNAME").getValue().toString());
        int port = Integer
                .parseInt(TalendTextUtils.removeQuotes(node.getElementParameter("PORT").getValue().toString()));
        String mandant = TalendTextUtils
                .removeQuotes(node.getElementParameter("MANDANT").getValue().toString());
        String userName = TalendTextUtils
                .removeQuotes(node.getElementParameter("USERNAME").getValue().toString());
        String passWord = TalendTextUtils
                .removeQuotes(node.getElementParameter("PASSWORD").getValue().toString());
        String jobDir = TalendTextUtils.removeQuotes(node.getElementParameter("JOB_DIR").getValue().toString());
        String jobName = TalendTextUtils
                .removeQuotes(node.getElementParameter("JOB_NAME").getValue().toString());
        String jobDef = TalendTextUtils
                .removeQuotes(node.getElementParameter("PATH_JOBDEF").getValue().toString());
        a.openConnection(hostName, port, mandant, userName, passWord);
        a.getJobDefinitionFile(jobDir, jobName, jobDef);

        document = saxReader.read(xmlFile);
        List inputList = document
                .selectNodes("//Job//Lines//Line//Steps//Input//Sources//Source//Format//Fields//Field");
        List inputMetaColumnList = new ArrayList<MetadataColumn>();
        for (int i = 0; i < inputList.size(); i++) {
            IMetadataColumn imc = new MetadataColumn();
            DefaultElement de = (DefaultElement) inputList.get(i);
            Element nameElement = de.element("Name");
            imc.setLabel(nameElement.getData().toString());
            Element lengthElement = de.element("Length");
            if (!"".equals(lengthElement.getData().toString())
                    && !"0".equals(lengthElement.getData().toString())) {
                imc.setLength(Integer.parseInt(lengthElement.getData().toString()));
            }
            Element typeElement = de.element("Type");
            JavaType javaType = JavaTypesManager.getJavaTypeFromName(typeElement.getData().toString());
            if (javaType != null) {
                imc.setTalendType(javaType.getId());
            } else {
                imc.setTalendType(JavaTypesManager.STRING.getId());
            }
            inputMetaColumnList.add(imc);
        }
        inputMetaCopy.setListColumns(inputMetaColumnList);

        List outputList = document
                .selectNodes("//Job//Lines//Line//Steps//Output//Targets//Target//Format//Fields//Field");
        List outputMetaColumnList = new ArrayList<MetadataColumn>();
        for (int i = 0; i < outputList.size(); i++) {
            IMetadataColumn imc = new MetadataColumn();
            DefaultElement de = (DefaultElement) outputList.get(i);
            Element nameElement = de.element("Name");
            imc.setLabel(nameElement.getData().toString());
            Element lengthElement = de.element("Length");
            if (!"".equals(lengthElement.getData().toString())
                    && !"0".equals(lengthElement.getData().toString())) {
                imc.setLength(Integer.parseInt(lengthElement.getData().toString()));
            }
            Element typeElement = de.element("Type");
            JavaType javaType = JavaTypesManager.getJavaTypeFromName(typeElement.getData().toString());
            if (javaType != null) {
                imc.setTalendType(javaType.getId());
            } else {
                imc.setTalendType(JavaTypesManager.STRING.getId());
            }
            outputMetaColumnList.add(imc);
        }
        outputMetaCopy.setListColumns(outputMetaColumnList);

        // set advanced setting info
        DefaultElement de = (DefaultElement) document.selectObject(
                "//Job//Lines//Line//Steps//Input//Sources//Source//Format//FileInfo//Record//FieldSeparator");
        int separator = Integer.parseInt(de.getData().toString());
        node.getElementParameter("IN_FIELD_SEP")
                .setValue(TalendTextUtils.addQuotes(new Character((char) separator).toString()));

        de = (DefaultElement) document.selectObject(
                "//Job//Lines//Line//Steps//Input//Sources//Source//Format//FileInfo//Record//HeaderRecordCount");
        node.getElementParameter("IN_HEADER_COUNT").setValue(de.getData().toString());

        de = (DefaultElement) document.selectObject(
                "//Job//Lines//Line//Steps//Input//Sources//Source//Format//FileInfo//FileLocation//Directory");
        node.getElementParameter("IN_DIR").setValue(TalendTextUtils.addQuotes(de.getData().toString()));

        de = (DefaultElement) document.selectObject(
                "//Job//Lines//Line//Steps//Input//Sources//Source//Format//FileInfo//FileLocation//FileName");
        node.getElementParameter("IN_FILENAME").setValue(TalendTextUtils.addQuotes(de.getData().toString()));

        de = (DefaultElement) document.selectObject(
                "//Job//Lines//Line//Steps//Input//Sources//Source//Format//FileInfo//FileLocation");
        node.getElementParameter("IN_MODE").setValue(de.attribute("Mode").getValue());

        de = (DefaultElement) document.selectObject(
                "//Job//Lines//Line//Steps//Output//Targets//Target//Format//FileInfo//Record//FieldSeparator");
        separator = Integer.parseInt(de.getData().toString());
        node.getElementParameter("OUT_FIELD_SEP")
                .setValue(TalendTextUtils.addQuotes(new Character((char) separator).toString()));

        de = (DefaultElement) document.selectObject(
                "//Job//Lines//Line//Steps//Output//Targets//Target//Format//FileInfo//Record//HeaderRecordCount");
        node.getElementParameter("OUT_HEADER_COUNT").setValue(de.getData().toString());

        de = (DefaultElement) document.selectObject(
                "//Job//Lines//Line//Steps//Output//Targets//Target//Format//FileInfo//FileLocation//Directory");
        node.getElementParameter("OUT_DIR").setValue(TalendTextUtils.addQuotes(de.getData().toString()));

        de = (DefaultElement) document.selectObject(
                "//Job//Lines//Line//Steps//Output//Targets//Target//Format//FileInfo//FileLocation//FileName");
        node.getElementParameter("OUT_FILENAME").setValue(TalendTextUtils.addQuotes(de.getData().toString()));

        de = (DefaultElement) document.selectObject(
                "//Job//Lines//Line//Steps//Output//Targets//Target//Format//FileInfo//FileLocation");
        node.getElementParameter("OUT_MODE").setValue(de.attribute("Mode").getValue());
    } catch (Exception e) {
        ExceptionHandler.process(e);
    } finally {
        try {
            a.closeConnection();
        } catch (Exception e) {
            ExceptionHandler.process(e);
        }
    }

    CompoundCommand cc = new CompoundCommand();
    cc.add(new ChangeMetadataCommand(node, param, inputMeta, inputMetaCopy));
    cc.add(new ChangeMetadataCommand(node, param, outputMeta, outputMetaCopy));
    return cc;
}