Example usage for org.dom4j.tree DefaultElement attributeValue

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

Introduction

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

Prototype

public String attributeValue(String name) 

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 www.j  av a 2s.  c  om*/

    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.benasmussen.tools.testeditor.IdExtractor.java

License:Apache License

public Vector<Vector> parse() throws Exception {
    Tidy tidy = new Tidy();
    tidy.setXmlOut(true);//  ww w  .  j  a va2  s. c o m
    tidy.setPrintBodyOnly(true);

    Vector<Vector> data = null;
    try {
        is = new FileInputStream(file);

        // jtidy parse to odm
        org.w3c.dom.Document parseDOM = tidy.parseDOM(is, null);

        // w3c to dom4j
        DOMReader domReader = new DOMReader();
        Document document = domReader.read(parseDOM);

        if (logger.isDebugEnabled()) {
            logger.debug("XML: " + document.asXML());
        }

        data = new Vector<Vector>();

        // select all elements with attribute @id
        List<DefaultElement> elements = document.selectNodes("//*[@id]");
        for (DefaultElement element : elements) {
            Vector<String> row = new Vector<String>();

            row.addElement(element.attributeValue("id"));
            row.addElement(element.getTextTrim());
            data.addElement(row);
        }

    } catch (Exception e) {
        throw e;
    } finally {
        IOUtils.closeQuietly(is);
    }

    return data;
}

From source file:org.brunocvcunha.taskerbox.core.TaskerboxXmlReader.java

License:Apache License

private void handleMacrosNode(Element xmlChannel) throws IOException {

    for (Object attrObj : xmlChannel.elements()) {
        DefaultElement e = (DefaultElement) attrObj;

        StringWriter sw = new StringWriter();
        XMLWriter writer = new XMLWriter(sw);
        writer.write(e.elements());/*  w ww .jav a 2 s . c  om*/

        if (this.tasker.getMacros().containsKey(e.attributeValue("name"))) {
            throw new RuntimeException("Macro " + e.attributeValue("name") + " already exists in map.");
        }

        this.tasker.getMacros().put(e.attributeValue("name"), sw.toString());
        this.tasker.getMacroAttrs().put(e.attributeValue("name"), e.attributes());
    }

}

From source file:org.brunocvcunha.taskerbox.core.TaskerboxXmlReader.java

License:Apache License

private void handleSystemPropertiesNode(Element xmlChannel) {
    for (Object attrObj : xmlChannel.elements()) {
        DefaultElement e = (DefaultElement) attrObj;
        System.setProperty(e.attributeValue("name"), e.attributeValue("value"));
        handleDefaultPropertiesNode(xmlChannel);
    }/*from   ww  w .  ja  v  a  2  s  .  com*/
}

From source file:org.brunocvcunha.taskerbox.core.TaskerboxXmlReader.java

License:Apache License

private void handleDefaultPropertiesNode(Element xmlChannel) {
    for (Object attrObj : xmlChannel.elements()) {
        DefaultElement e = (DefaultElement) attrObj;

        String propertyValue = e.attributeValue("value");

        for (String defaultAttr : this.tasker.getDefaultProperties().keySet()) {
            propertyValue = propertyValue.replace("{" + defaultAttr + "}",
                    this.tasker.getDefaultProperties().get(defaultAttr));
        }//from  w w  w.j a v  a  2  s .  c o m

        this.tasker.getDefaultProperties().put(e.attributeValue("name"), propertyValue);

    }
}

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   w ww  . j a v  a2 s .  co  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.processors.docx.WordXMLRawTemplateProcessor.java

License:Open Source License

@SuppressWarnings("rawtypes")
public Blob renderTemplate(TemplateBasedDocument templateDocument) throws Exception {

    File workingDir = getWorkingDir();

    Blob blob = templateDocument.getTemplateBlob();
    String fileName = blob.getFilename();
    List<TemplateInput> params = templateDocument.getParams();
    File sourceZipFile = File.createTempFile("WordXMLTemplate", ".zip");
    blob.transferTo(sourceZipFile);/* ww w  .ja  va  2s .com*/

    ZipUtils.unzip(sourceZipFile, workingDir);

    File xmlCustomFile = new File(workingDir.getAbsolutePath() + "/docProps/custom.xml");

    String xmlContent = FileUtils.readFile(xmlCustomFile);

    Document xmlDoc = DocumentHelper.parseText(xmlContent);

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

    for (Object node : nodes) {
        DefaultElement elem = (DefaultElement) node;
        if ("property".equals(elem.getName())) {
            String name = elem.attributeValue("name");
            TemplateInput param = getParamByName(name, params);
            DefaultElement valueElem = (DefaultElement) elem.elements().get(0);
            String strValue = "";
            if (param.isSourceValue()) {
                Property property = templateDocument.getAdaptedDoc().getProperty(param.getSource());
                if (property != null) {
                    Serializable value = templateDocument.getAdaptedDoc().getPropertyValue(param.getSource());
                    if (value != null) {
                        if (value instanceof Date) {
                            strValue = wordXMLDateFormat.format((Date) value);
                        } else {
                            strValue = value.toString();
                        }
                    }
                }
            } else {
                if (InputType.StringValue.equals(param.getType())) {
                    strValue = param.getStringValue();
                } else if (InputType.BooleanValue.equals(param.getType())) {
                    strValue = param.getBooleanValue().toString();
                } else if (InputType.DateValue.equals(param.getType())) {
                    strValue = wordXMLDateFormat.format(param.getDateValue());
                }
            }
            valueElem.setText(strValue);
        }
    }

    String newXMLContent = xmlDoc.asXML();

    File newZipFile = File.createTempFile("newWordXMLTemplate", ".docx");
    xmlCustomFile.delete();
    File newXMLFile = new File(xmlCustomFile.getAbsolutePath());
    FileUtils.writeFile(newXMLFile, newXMLContent);

    File[] files = workingDir.listFiles();
    ZipUtils.zip(files, newZipFile);

    // clean up
    FileUtils.deleteTree(workingDir);
    sourceZipFile.delete();
    newZipFile.deleteOnExit();

    Blob newBlob = new FileBlob(newZipFile);
    newBlob.setFilename(fileName);

    return newBlob;
}

From source file:org.nuxeo.ecm.platform.template.processors.docx.WordXMLRawTemplateProcessor.java

License:Open Source License

@SuppressWarnings("rawtypes")
public List<TemplateInput> getInitialParametersDefinition(Blob blob) throws Exception {
    List<TemplateInput> params = new ArrayList<TemplateInput>();

    String xmlContent = readPropertyFile(blob.getStream());

    Document xmlDoc = DocumentHelper.parseText(xmlContent);

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

    for (Object node : nodes) {
        DefaultElement elem = (DefaultElement) node;
        if ("property".equals(elem.getName())) {
            String name = elem.attributeValue("name");
            DefaultElement valueElem = (DefaultElement) elem.elements().get(0);
            String wordType = valueElem.getName();
            InputType nxType = InputType.StringValue;
            if (wordType.contains("lpwstr")) {
                nxType = InputType.StringValue;
            } else if (wordType.contains("filetime")) {
                nxType = InputType.DateValue;
            } else if (wordType.contains("bool")) {
                nxType = InputType.BooleanValue;
            }// ww  w  .  j  a  v a2 s  .  c  om

            TemplateInput input = new TemplateInput(name);
            input.setType(nxType);
            params.add(input);
        }
    }
    return params;
}

From source file:org.nuxeo.ecm.platform.template.processors.docx.WordXMLRawTemplateProcessor.java

License:Open Source License

@SuppressWarnings("rawtypes")
public DocumentModel updateDocumentFromBlob(TemplateBasedDocument templateDocument) throws Exception {

    Blob blob = templateDocument.getTemplateBlob();

    String xmlContent = readPropertyFile(blob.getStream());

    if (xmlContent == null) {
        return templateDocument.getAdaptedDoc();
    }//from ww  w  .  j  ava2  s . com

    Document xmlDoc = DocumentHelper.parseText(xmlContent);

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

    DocumentModel adaptedDoc = templateDocument.getAdaptedDoc();
    List<TemplateInput> params = templateDocument.getParams();

    for (Object node : nodes) {
        DefaultElement elem = (DefaultElement) node;
        if ("property".equals(elem.getName())) {
            String name = elem.attributeValue("name");
            TemplateInput param = getParamByName(name, params);
            DefaultElement valueElem = (DefaultElement) elem.elements().get(0);
            String xmlValue = valueElem.getTextTrim();
            if (param.isSourceValue()) {
                // XXX this needs to be rewritten

                if (String.class.getSimpleName().equals(param.getType())) {
                    adaptedDoc.setPropertyValue(param.getSource(), xmlValue);
                } else if (InputType.BooleanValue.equals(param.getType())) {
                    adaptedDoc.setPropertyValue(param.getSource(), new Boolean(xmlValue));
                } else if (Date.class.getSimpleName().equals(param.getType())) {
                    adaptedDoc.setPropertyValue(param.getSource(), wordXMLDateFormat.parse(xmlValue));
                }
            } else {
                if (InputType.StringValue.equals(param.getType())) {
                    param.setStringValue(xmlValue);
                } else if (InputType.BooleanValue.equals(param.getType())) {
                    param.setBooleanValue(new Boolean(xmlValue));
                } else if (InputType.DateValue.equals(param.getType())) {
                    param.setDateValue(wordXMLDateFormat.parse(xmlValue));
                }
            }
        }
    }
    adaptedDoc = templateDocument.saveParams(params, false);
    return adaptedDoc;
}

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);/*w  w  w . j av  a  2s.co m*/
        }

        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;
}