Example usage for org.dom4j.tree DefaultElement elements

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

Introduction

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

Prototype

public List<Element> elements() 

Source Link

Usage

From source file:com.belle.yitiansystem.merchant.service.impl.MerchantsService.java

/**
 * distributeAuthResourceToChildRen:?????? 
 * @author li.n1 //w  ww.  j  a  v a 2  s .  co  m
 * @param userId ?ID
 * @param id ??ID
 * @throws Exception 
 * @since JDK 1.6
 */
private void distributeAuthResourceToChildRen(String userId, String id) throws Exception {
    // ?
    long enter = System.currentTimeMillis();
    Set<String> authrityUrls = null;
    Set<String> childauthrityUrls = null;
    Document document = XmlTool
            .createDocument(MerchantsService.class.getClassLoader().getResource("authority.xml").getPath());
    Map<String, List<MerchantsAuthorityVo>> _map = null;
    Map<String, List<MerchantsAuthorityVo>> _childmap = null;
    List<MerchantsAuthorityVo> auths = this.getMerchantAuthorityById(id);
    Map<String, Set<String>> authReseMap = (Map<String, Set<String>>) redisTemplate.opsForHash()
            .get(Constant.C_USER_REOURCE_AUTH, "authReourecesMap");
    //????url
    if (authReseMap != null) {
        authrityUrls = authReseMap.get(userId);
        childauthrityUrls = authReseMap.get(id);
    }
    Map<String, Map<String, List<MerchantsAuthorityVo>>> aMap = (Map<String, Map<String, List<MerchantsAuthorityVo>>>) redisTemplate
            .opsForHash().get(Constant.C_USER_AUTH, "authMap");
    //?????
    if (aMap != null) {
        _map = aMap.get(userId);
        _childmap = aMap.get(id);
    }
    for (MerchantsAuthorityVo _authority : auths) {
        if (authrityUrls != null && !(authrityUrls.contains("/" + _authority.getAuthrityURL()))) {
            //????????????
            DefaultElement node = (DefaultElement) XmlTool.findElementByProperty(document, "url",
                    _authority.getAuthrityURL());
            if (node != null) {
                for (Object obj : node.elements()) {
                    childauthrityUrls.remove(XmlTool.getAttributeVal((Element) obj, "url"));
                }
            }
            childauthrityUrls.remove("/" + _authority.getAuthrityURL());
        }
        MerchantsAuthority auth = merchantsAuthorityDaoImpl.findUniqueBy("id", _authority.getParentAuthrityId(),
                false);
        if (_map != null && !(_map.containsKey(auth.getAuthrityName() + "@~" + auth.getSortNo()))) {
            //????????????
            _childmap.remove(auth.getAuthrityName() + "@~" + auth.getSortNo());
        }
    }
    if (authReseMap != null) {
        authReseMap.remove(id);
        authReseMap.put(id, childauthrityUrls);
        redisTemplate.opsForHash().put(Constant.C_USER_REOURCE_AUTH, "authReourecesMap", authReseMap);
    }
    if (aMap != null) {
        aMap.remove(id);
        aMap.put(id, _childmap);
        redisTemplate.opsForHash().put(Constant.C_USER_AUTH, "authMap", aMap);
    }
    //this.addUserAuthority(id, id, null);
    System.out.println("======spent time(unit:s):=======" + (System.currentTimeMillis() - enter) / 1000.0);
}

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());

        if (this.tasker.getMacros().containsKey(e.attributeValue("name"))) {
            throw new RuntimeException("Macro " + e.attributeValue("name") + " already exists in map.");
        }/*  w  ww .j a va 2  s. com*/

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

}

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);//www . ja va2 s.  co m

    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;
            }/*from w  w  w. j a va  2 s  .c  o  m*/

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

    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.template.processors.docx.WordXMLRawTemplateProcessor.java

License:Open Source License

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

    File workingDir = getWorkingDir();

    Blob blob = templateDocument.getTemplateBlob(templateName);
    String fileName = blob.getFilename();
    List<TemplateInput> params = templateDocument.getParams(templateName);
    File sourceZipFile = File.createTempFile("WordXMLTemplate", ".zip");
    blob.transferTo(sourceZipFile);//www .j  ava2 s . 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.template.processors.docx.WordXMLRawTemplateProcessor.java

License:Open Source License

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

    Blob blob = templateDocument.getTemplateBlob(templateName);

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

    if (xmlContent == null) {
        return templateDocument.getAdaptedDoc();
    }/*w ww .ja  v  a 2s. co  m*/

    Document xmlDoc = DocumentHelper.parseText(xmlContent);

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

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

    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(templateName, params, false);
    return adaptedDoc;
}