Example usage for org.dom4j DocumentHelper parseText

List of usage examples for org.dom4j DocumentHelper parseText

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper parseText.

Prototype

public static Document parseText(String text) throws DocumentException 

Source Link

Document

parseText parses the given text as an XML document and returns the newly created Document.

Usage

From source file:org.fornax.cartridges.sculptor.framework.test.PersistenceXmlParser.java

License:Apache License

@SuppressWarnings("unchecked")
void parse(String xml) {
    try {/*from   w  w  w. j  av a  2  s .  c  o m*/
        Document document = DocumentHelper.parseText(xml);
        Element rootElement = document.getRootElement();

        Iterator<Element> elementIterator = rootElement.elementIterator("persistence-unit");
        while (elementIterator.hasNext()) {
            parsePersistentUnit(elementIterator.next());
        }

    } catch (DocumentException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:org.hibernate.eclipse.console.test.utils.ResourceReadUtils.java

License:Open Source License

/**
 * Parse, i.e. adjust xml text so attributes for same xmls 
 * will be in one order./*w  ww .  j av  a  2 s .c  om*/
 * 
 * @param sample
 * @return adjusted xml
 */
public static String adjustXmlText(String sample) {
    Document doc = null;
    try {
        doc = DocumentHelper.parseText(sample);
    } catch (DocumentException e) {
        // ignore
    }
    if (doc == null) {
        return sample;
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ConfigurationXMLFactory.dump(baos, doc.getRootElement());
    return baos.toString().trim();
}

From source file:org.hibernate.eclipse.launch.ExportersXMLAttributeDescription.java

License:Open Source License

private static Document getDocument() {
    InputStream input = getResInputStream(ANT_TASKS_DESCRIPTION_PATH, ExportersXMLAttributeDescription.class);
    if (input == null) {
        HibernateConsolePlugin.getDefault()
                .logErrorMessage("Can't read resource: " + ANT_TASKS_DESCRIPTION_PATH, (Throwable) null); //$NON-NLS-1$
        return null;
    }//from  w ww  .j a v a2  s.  c o m
    StringBuffer cbuf = new StringBuffer();
    InputStreamReader isReader = null;
    BufferedReader in = null;
    try {
        String ls = System.getProperties().getProperty("line.separator", "\n"); //$NON-NLS-1$//$NON-NLS-2$
        isReader = new InputStreamReader(input);
        in = new BufferedReader(isReader);
        String str;
        while ((str = in.readLine()) != null) {
            cbuf.append(str + ls);
        }
    } catch (IOException e) {
        HibernateConsolePlugin.getDefault().logErrorMessage("IOException: ", e); //$NON-NLS-1$
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                // ignore
            }
        }
        if (isReader != null) {
            try {
                isReader.close();
            } catch (IOException e) {
                // ignore
            }
        }
        try {
            input.close();
        } catch (IOException e) {
            // ignore
        }
    }
    Document res = null;
    try {
        res = DocumentHelper.parseText(cbuf.toString());
    } catch (DocumentException e) {
        HibernateConsolePlugin.getDefault().logErrorMessage("DocumentException: ", e); //$NON-NLS-1$
    }
    return res;
}

From source file:org.hlc.demo.mybatis.mapper.DocumentAppender.java

License:Apache License

public void execute() {

    File extFile = new File(CodeGeneratorRuner.class.getResource("/" + this.path).getFile());
    try {/*from  w w w  .  j av  a  2 s . c  om*/
        SAXReader reader = new SAXReader();
        org.dom4j.Document source = DocumentHelper.parseText(reader.read(extFile).asXML());
        @SuppressWarnings("unchecked")
        List<Element> elements = source.getRootElement().elements();
        for (Element temp : elements) {
            this.document.getRootElement().addElement(new TextElement(temp.asXML()));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.infoglue.cms.applications.common.actions.SimpleXmlServiceAction.java

License:Open Source License

public String doGetChangeNotifications() throws IOException {
    String id = getRequest().getSession().getId();
    StringWriter buffer = (StringWriter) changeNotificationBuffer.get(id);
    if (buffer == null) {
        buffer = new StringWriter();
        buffer.write("<changeNotifications>");
        changeNotificationBuffer.put(id, buffer);
        XMLNotificationWriter streamWriter = new XMLNotificationWriter(buffer, ENCODING, "", null, true, true);
        ChangeNotificationController.getInstance().registerListener(streamWriter);
    }/*from  w  w  w.j  a v  a 2 s.  c  o  m*/

    buffer.write("</changeNotifications>");
    try {
        out(getFormattedDocument(DocumentHelper.parseText(buffer.toString())));
    } catch (Exception e) {
        out("<exception/>");
    }
    buffer.getBuffer().delete(0, buffer.getBuffer().length());
    buffer.write("<changeNotifications>");
    return null;
}

From source file:org.infoglue.cms.applications.contenttool.actions.UpdateContentVersionAction.java

License:Open Source License

public String doStandaloneXML() throws Exception {
    try {/*from ww  w  .  j av  a  2  s  .  c  om*/
        String xmlResult = null;
        getResponse().setContentType("text/xml; charset=UTF-8");
        getResponse().setHeader("Cache-Control", "no-cache");
        getResponse().setHeader("Pragma", "no-cache");
        getResponse().setDateHeader("Expires", 0);
        PrintWriter out = getResponse().getWriter();
        XMLWriter xmlWriter = new XMLWriter(out);
        XStream xStream = new XStream();
        xStream.omitField(contentVersionVO.getClass(), "versionValue");

        /*
        System.out.println("contentVersionId:" + this.contentVersionId);
        System.out.println("contentId:" + this.contentId);
        System.out.println("languageId:" + this.languageId);
        System.out.println("this.contentVersionVO:" + this.contentVersionVO);
        */
        ceb.throwIfNotEmpty();

        if (this.attributeName == null)
            this.attributeName = "";

        if (this.currentEditorId == null)
            this.currentEditorId = 1;

        try {
            this.contentVersionVO.setVersionModifier(this.getInfoGluePrincipal().getName());
            this.contentVersionVO = ContentVersionControllerProxy.getController().acUpdate(
                    this.getInfoGluePrincipal(), this.contentId, this.languageId, this.contentVersionVO);
            this.contentVersionVO = ContentVersionController.getContentVersionController()
                    .getContentVersionVOWithId(this.contentVersionVO.getId());
            xmlResult = xStream.toXML(this.contentVersionVO);
        } catch (ConstraintException ce) {
            super.contentVersionVO = this.contentVersionVO;
            xmlResult = xStream.toXML(ce);
        }

        //System.out.println("xmlResult:" + xmlResult);
        /*
         * Output
         */
        xmlWriter.write(DocumentHelper.parseText(xmlResult));
        xmlWriter.flush();
    } catch (Exception e) {
        logger.warn("Error in UpdateContentVersion.doStandaloneXML: " + e.getMessage());
        if (logger.isInfoEnabled())
            logger.info("Error in UpdateContentVersion.doStandaloneXML: " + e.getMessage(), e);
    }

    return NONE;
}

From source file:org.infoglue.cms.applications.contenttool.actions.UpdateContentVersionAction.java

License:Open Source License

public String doXml() throws IOException, SystemException, Bug, DocumentException {
    try {/*from   ww w .  j a  v a 2 s  .co  m*/
        String xmlResult = null;
        getResponse().setContentType("text/xml; charset=UTF-8");
        getResponse().setHeader("Cache-Control", "no-cache");
        getResponse().setHeader("Pragma", "no-cache");
        getResponse().setDateHeader("Expires", 0);
        PrintWriter out = getResponse().getWriter();
        XMLWriter xmlWriter = new XMLWriter(out);
        XStream xStream = new XStream();
        xStream.omitField(contentVersionVO.getClass(), "versionValue");

        // super.initialize(this.contentVersionId, this.contentId, this.languageId);

        ContentVersionVO currentContentVersionVO = null;
        ContentVersionVO activeContentVersionVO = ContentVersionController.getContentVersionController()
                .getLatestActiveContentVersionVO(contentId, languageId);

        //System.out.println("activeContentVersionVO:" + activeContentVersionVO);
        //System.out.println("this.contentVersionVO:" + this.contentVersionVO);
        /*
         * Are we trying to update the active version?
         */
        if (activeContentVersionVO.getContentVersionId().equals(this.contentVersionVO.getContentVersionId())) {
            if (this.contentVersionVO.getId() != null) {
                currentContentVersionVO = ContentVersionController.getContentVersionController()
                        .getContentVersionVOWithId(this.contentVersionVO.getId());
            }

            if (currentContentVersionVO == null
                    || this.oldModifiedDateTime == currentContentVersionVO.getModifiedDateTime().getTime()) {
                this.contentVersionVO.setVersionModifier(this.getInfoGluePrincipal().getName());

                try {
                    if (activeContentVersionVO.getStateId().equals(ContentVersionVO.WORKING_STATE)) {
                        this.contentVersionVO = ContentVersionControllerProxy.getController().acUpdate(
                                this.getInfoGluePrincipal(), this.contentId, this.languageId,
                                this.contentVersionVO);
                        this.contentVersionVO = ContentVersionController.getContentVersionController()
                                .getContentVersionVOWithId(this.contentVersionVO.getId());
                        this.oldModifiedDateTime = this.contentVersionVO.getModifiedDateTime().getTime();
                        xmlResult = xStream.toXML(contentVersionVO);
                    } else {
                        xmlResult = "<invalidstate/>";
                    }
                } catch (ConstraintException ce) {
                    ce.printStackTrace();
                    xmlResult = xStream.toXML(ce);
                } catch (Exception e) {
                    e.printStackTrace();
                    xmlResult = xStream.toXML(e);
                }
            } else {
                this.contentVersionVO.setVersionModifier(this.getInfoGluePrincipal().getName());
                super.contentVersionVO = this.contentVersionVO;
                concurrentModification = true;
                xmlResult = "<concurrentmodification/>";
            }
        } else {
            /*
             * Not updating active version
             */
            xmlResult = "<invalidversion/>";
        }

        //System.out.println("xmlResult:" + xmlResult);
        /*
         * Output
         */
        xmlWriter.write(DocumentHelper.parseText(xmlResult));
        xmlWriter.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return NONE;
}

From source file:org.infoglue.igide.cms.InfoglueCMS.java

License:Open Source License

public static String getAttributeValue(ContentVersion contentVersionVO, String key) {
    String value = "";
    String xpath = "//*[local-name()='" + key + "']";
    if (contentVersionVO != null) {
        try {//w  w w  .  ja va2  s  . c om
            Document doc = DocumentHelper.parseText(contentVersionVO.getValue());
            Element root = doc.getRootElement();
            Node nodeValue = root.selectSingleNode(xpath);
            if (nodeValue != null) {
                value = nodeValue.getText();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return value;
}

From source file:org.infoglue.igide.cms.InfoglueCMS.java

License:Open Source License

public static Map getAttributeValueMap(IResource resource) {
    Map attributeValueMap = new HashMap();
    String contentVersionXML = Utils.getIFileContentAsString(resource);
    if (contentVersionXML != null)
        try {/*from  ww  w .  j a v  a2 s . c  o m*/
            Document doc = DocumentHelper.parseText(contentVersionXML);
            Element root = doc.getRootElement();
            Element attributesElement = root.element("attributes");
            List attributeElementList = attributesElement.elements();
            String name;
            String value;
            for (Iterator attributeElementListIterator = attributeElementList
                    .iterator(); attributeElementListIterator.hasNext(); attributeValueMap.put(name, value)) {
                Element attributeElement = (Element) attributeElementListIterator.next();
                name = attributeElement.getName();
                value = attributeElement.getText();
            }

        } catch (Exception e) {
            Logger.logConsole((new StringBuilder("Error parsing xml:")).append(e.getMessage()).toString());
            /*
            try
            {
            PrintWriter pout = new PrintWriter(new FileWriter(new File((new StringBuilder("c:\\logs\\debug_")).append(resource.getName()).toString()), false));
            pout.println(contentVersionXML);
            pout.close();
            }
            catch(Exception e2)
            {
            Logger.logConsole((new StringBuilder("Error e2:")).append(e2.getMessage()).toString());
            }
            */
        }
    return attributeValueMap;
}

From source file:org.infoglue.igide.cms.InfoglueCMS.java

License:Open Source License

public static String getAttributeValue(String versionValue) {
    String value = null;/*from  w  w  w .jav  a 2 s.c  o  m*/
    if (versionValue != null)
        try {
            Document doc = DocumentHelper.parseText(versionValue);
            Element root = doc.getRootElement();
            Element attributesElement = root.element("attributes");
            List attributeElementList = attributesElement.elements();
            Iterator attributeElementListIterator = attributeElementList.iterator();
            if (attributeElementListIterator.hasNext()) {
                Element attributeElement = (Element) attributeElementListIterator.next();
                String name = attributeElement.getName();
                value = attributeElement.getText();
            }
        } catch (Exception e) {
            Logger.logConsole((new StringBuilder("Error parsing xml:")).append(e.getMessage()).toString());
        }
    return value;
}