Example usage for org.dom4j.tree DefaultElement getName

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

Introduction

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

Prototype

public String getName() 

Source Link

Usage

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);/*  w  w  w.j  av  a 2 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;
            }//w  ww  .ja  v a2  s.com

            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  w  w w . j ava 2s . c o 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);/*  w  ww.ja v a 2 s.  c  o  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.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();
    }//from ww w.  j a  v a2  s  . c o  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;
}

From source file:org.olat.ims.cp.CPCore.java

License:Apache License

/**
 * adds an element to the cp. Adds it after the item with identifier "id"
 * /*www  .  jav a 2 s.c  o  m*/
 * @param newElement
 * @param id
 * @return
 */
public boolean addElementAfter(final DefaultElement newElement, final String id) {
    final DefaultElement beforeElement = rootNode.getElementByIdentifier(id);

    if (beforeElement == null) {
        return false;
    }

    if (beforeElement instanceof CPItem) {
        // beforeElement is a <item>
        // ==> newElement has to be an <item>
        final CPItem beforeItem = (CPItem) beforeElement;
        final DefaultElement parent = beforeItem.getParentElement();
        if (!(newElement instanceof CPItem)) {
            throw new OLATRuntimeException(CPOrganizations.class, "only <item> element allowed",
                    new Exception());
        }
        if (parent instanceof CPItem) {
            final CPItem p = (CPItem) parent;
            p.addItemAt((CPItem) newElement, beforeItem.getPosition() + 1);
        } else if (parent instanceof CPOrganization) {
            final CPOrganization o = (CPOrganization) parent;
            o.addItemAt((CPItem) newElement, beforeItem.getPosition() + 1);
        } else {
            throw new OLATRuntimeException(CPOrganizations.class,
                    "you cannot add an <item> element to a " + parent.getName() + " element", new Exception());
        }

    }

    return true;
}

From source file:org.olat.ims.cp.CPTreeDataModel.java

License:Apache License

@Override
public List<AjaxTreeNode> getChildrenFor(String nodeId) {

    final CPManagerImpl cpMgm = (CPManagerImpl) CPManager.getInstance();
    final Vector<AjaxTreeNode> nodeList = new Vector<AjaxTreeNode>();

    nodeId = getIdentifierForNodeID(nodeId);
    final DefaultElement el = cpMgm.getElementByIdentifier(cp, nodeId);
    if (el == null) {
        log.info("element not found (id " + nodeId + ")");
        return nodeList;
    }/* w  w  w  .  j a va2  s  .c o m*/
    try {
        if (el.getName().equals(CPCore.ORGANIZATION)) {
            final CPOrganization org = (CPOrganization) el;
            for (final Iterator<CPItem> it = org.getItemIterator(); it.hasNext();) {
                final CPItem item = it.next();
                addItem(nodeList, item);
            }
        } else if (el.getName().equals(CPCore.ITEM)) {
            final CPItem pItem = (CPItem) el;
            for (final Iterator<CPItem> it = pItem.getItemIterator(); it.hasNext();) {
                final CPItem item = it.next();
                addItem(nodeList, item);
            }
        } else {
            // element is not item nor orga -> ergo wrong element
            log.info("unknown element while building treemodel for gui (id: " + nodeId + ")");
        }
    } catch (final JSONException e) {
        log.error("error while building treemodel");
    }

    return nodeList;
}

From source file:org.olat.ims.cp.objects.CPDependency.java

License:Apache License

/**
 * this constructor is neeeded when building up the cp (parsing XML manifest file)
 * /* w ww .  ja  va2 s.  c  om*/
 * @param me
 */
public CPDependency(final DefaultElement me) {
    super(me.getName());
    identifierRef = me.attributeValue(CPCore.IDENTIFIERREF);
    if (identifierRef.equals(null)) {
        throw new OLATRuntimeException(CPOrganizations.class,
                "Invalid IMS-Manifest ( <dependency> element without identifierref )", new Exception());
    }
}

From source file:org.olat.ims.cp.objects.CPFile.java

License:Apache License

/**
 * Constructor, used when building up the cp (parsing XML-manifest)
 * /*from   w  w  w.j  a  v a  2 s . c om*/
 * @param me
 * @param xmlBase xmlBase-attribute of the parental resource-element
 * @param packageFolder TODO
 */
public CPFile(final DefaultElement me, final String xmlBase, final VFSContainer rootDir) {
    super(me.getName());
    setContent(me.content());
    this.href = me.attributeValue(CPCore.HREF);
    if (xmlBase.equals("")) {
        file = (VFSLeaf) rootDir.resolve(href);
    } else {
        file = (VFSLeaf) rootDir.resolve(xmlBase + "/" + href);
    }
}

From source file:org.olat.ims.cp.objects.CPFile.java

License:Apache License

/**
 * @see org.olat.ims.cp.objects.CPNode#buildChildren()
 *//*from  w ww . j a  v a  2 s .  c o  m*/
@Override
public void buildChildren() {
    final Iterator<DefaultElement> children = this.elementIterator();
    // iterate through children
    while (children.hasNext()) {
        final DefaultElement child = children.next();
        if (child.getName().equals(CPCore.METADATA)) {
            // TODO: implement METADATA
            metadata = new CPMetadata(child);
        } else {
            throw new OLATRuntimeException(CPOrganizations.class,
                    "Invalid IMS-Manifest ( only <metadata> element under <file> is allowed )",
                    new Exception());
        }
    }
    this.clearContent();
    validateElement();
}