Example usage for org.dom4j.io DOMWriter DOMWriter

List of usage examples for org.dom4j.io DOMWriter DOMWriter

Introduction

In this page you can find the example usage for org.dom4j.io DOMWriter DOMWriter.

Prototype

public DOMWriter() 

Source Link

Usage

From source file:com.anite.zebra.ext.xmlloader.XMLLoadProcess.java

License:Apache License

/**
 * Read in a file of XML, strip out the pretty printing via some juggling of objects and then return a
 * org.w3.document DOM object.//w w  w  .  ja  v a2  s.  c  o  m
 * 
 * @param xmlFile
 *            The file to be read in.
 * @return A ready to use org.w3.Document with pretty printing stripped out.
 * @throws DocumentException
 * @throws MalformedURLException
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
private Document readDocument(File xmlFile)
        throws DocumentException, MalformedURLException, UnsupportedEncodingException, IOException {
    SAXReader xmlReader = new SAXReader();
    xmlReader.setStripWhitespaceText(true);

    org.dom4j.Document dom4jDocument = xmlReader.read(xmlFile);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    OutputFormat format = OutputFormat.createCompactFormat();
    XMLWriter writer = new XMLWriter(baos, format);

    writer.write(dom4jDocument);

    dom4jDocument = DocumentHelper.parseText(baos.toString());

    DOMWriter domWriter = new DOMWriter();
    Document doc = domWriter.write(dom4jDocument);
    return doc;
}

From source file:com.noterik.bart.fs.legacy.tools.XmlHelper.java

License:Open Source License

/**
 * Converts dom4j Document to org.w3c Document
 *
 * @param doc1//from  w w w  .  j a  v  a  2s. c om
 * @return
 */

public static org.w3c.dom.Document convert(Document doc1) {
    if (doc1 == null) {
        return null;
    }
    DOMWriter writer = new DOMWriter();
    org.w3c.dom.Document doc2 = null;
    try {
        doc2 = writer.write(doc1);
    } catch (DocumentException e) {
    }
    return doc2;
}

From source file:de.innovationgate.webgate.api.jdbc.WGDocumentImpl.java

License:Open Source License

protected static Object readItemValue(WGDatabaseImpl db, WGDocumentImpl doc, Item item) throws WGAPIException {
    switch (item.getType()) {

    case ITEMTYPE_SERIALIZED_XSTREAM:
        try {/*  w  w w . j a v  a  2s.c  o  m*/

            DeserialisationCache cache = item.getDeserialisationCache();
            int serializedHashCode = item.getText().hashCode();
            if (cache != null && cache.getSerializedHashCode() == serializedHashCode) {
                return cache.getDeserializedObject();
            }

            Object obj = XSTREAM_SERIALIZER.fromXML(item.getText());
            cache = new DeserialisationCache(serializedHashCode, obj);
            item.setDeserialisationCache(cache);
            return obj;

        } catch (Exception e) {
            throw new WGIllegalDataException(
                    "Error deserializing itemvalue of type 'ITEMTYPE_SERIALIZED_XSTREAM'.", e);
        }

    case ITEMTYPE_SERIALIZED_CASTOR:
        Unmarshaller unmarshaller = new Unmarshaller();
        try {

            // Workaround for castor bug. Primitive wrappers for
            // boolean, character, long do not contain xsi:type
            Document domDoc = DocumentHelper.parseText(item.getText());
            Element root = domDoc.getRootElement();
            if (root.getName().equals("boolean")) {
                root.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                root.addAttribute("xsi:type", "java:java.lang.Boolean");
            } else if (root.getName().equals("character")) {
                root.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                root.addAttribute("xsi:type", "java:java.lang.Boolean");
            } else if (root.getName().equals("long")) {
                root.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                root.addAttribute("xsi:type", "java:java.lang.Long");
            } else if (root.getName().equals("array-list")) {
                root.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                root.addAttribute("xsi:type", "java:java.util.ArrayList");
            }

            DOMWriter domWriter = new DOMWriter();
            return unmarshaller.unmarshal(domWriter.write(domDoc));
            // return unmarshaller.unmarshal(new
            // StringReader(item.getText()));
        } catch (MarshalException e) {
            throw new WGIllegalDataException("Error unmarshaling serialized object", e);
        } catch (ValidationException e) {
            throw new WGIllegalDataException("Error unmarshaling serialized object", e);
        } catch (DocumentException e) {
            throw new WGIllegalDataException("Error unmarshaling serialized object", e);
        }

    case ITEMTYPE_JSON:
        return new JsonParser().parse(item.getText());

    case ITEMTYPE_STRING:
        return item.getText();

    case ITEMTYPE_NUMBER:
        return item.getNumber();

    case ITEMTYPE_DATE:
        return new Date(item.getDate().getTime());

    case ITEMTYPE_BOOLEAN:
        return new Boolean(item.getNumber().doubleValue() == 1);

    case ITEMTYPE_BINARY:
        if (item instanceof ExtensionData) {
            return db.getFileHandling().readBinaryExtensionData(doc, (ExtensionData) item);
        } else {
            throw new WGSystemException("Unsupported itemtype '" + item.getType() + "' for the current entity");
        }

    default:
        throw new WGSystemException("Unsupported itemtype '" + item.getType() + "'.");

    }
}

From source file:de.innovationgate.wgpublisher.WebTMLDebugger.java

License:Open Source License

private void showTMLModules(HttpServletRequest request, HttpServletResponse response, HttpSession session)
        throws HttpErrorException, IOException {
    String urlStr = request.getParameter("url");
    String indexStr = request.getParameter("index");

    if (urlStr != null) {
        urlStr = _dispatcher.getCore().getURLEncoder().decode(urlStr);
        List<Document> debugDocuments = WGACore.getDebugDocumentsList(session);
        Document debugDoc;//from www .j  ava2  s  . c  o m
        for (int idx = 0; idx < debugDocuments.size(); idx++) {
            debugDoc = (Document) debugDocuments.get(idx);
            if (debugDoc.getRootElement().attributeValue("url", "").equals(urlStr)) {
                indexStr = String.valueOf(idx);
                break;
            }
        }
    }

    response.setContentType("text/html");

    if (indexStr != null) {
        int index = Integer.valueOf(indexStr).intValue();
        List<Document> debugDocuments = WGACore.getDebugDocumentsList(session);
        if (index == -1) {
            index = debugDocuments.size() - 1;
        }
        if (index >= debugDocuments.size()) {
            throw new HttpErrorException(HttpServletResponse.SC_BAD_REQUEST,
                    "Index out of range: " + index + " where maximum index is " + (debugDocuments.size() - 1),
                    null);
        } else {
            Document doc = (Document) debugDocuments.get(index);
            doc.getRootElement().addAttribute("index", String.valueOf(index));

            try {
                DOMWriter domWriter = new DOMWriter();
                org.w3c.dom.Document domDocument = domWriter.write(doc);

                Transformer trans = getDebugModulesTransformer(request.getParameter("throwAway") != null);
                trans.transform(new DOMSource(domDocument), new StreamResult(response.getOutputStream()));
            } catch (TransformerConfigurationException e) {
                response.sendError(500, e.getMessageAndLocation());
                e.printStackTrace();
            } catch (TransformerFactoryConfigurationError e) {
                response.sendError(500, e.getMessage());
                e.printStackTrace();
            } catch (TransformerException e) {
                response.sendError(500, e.getMessageAndLocation());
                e.printStackTrace();
            } catch (IOException e) {
                response.sendError(500, e.getMessage());
                e.printStackTrace();
            } catch (DocumentException e) {
                response.sendError(500, e.getMessage());
                e.printStackTrace();
            }
        }
    } else {
        throw new HttpErrorException(HttpServletResponse.SC_BAD_REQUEST,
                "You must include either parameter index or url to address the debug document to show", null);
    }
}

From source file:de.innovationgate.wgpublisher.WebTMLDebugger.java

License:Open Source License

private void showTMLTags(HttpServletRequest request, HttpServletResponse response, HttpSession session)
        throws HttpErrorException, IOException {
    String urlStr = request.getParameter("url");
    String indexStr = request.getParameter("index");

    if (urlStr != null) {
        urlStr = _dispatcher.getCore().getURLEncoder().decode(urlStr);
        List<Document> debugDocuments = WGACore.getDebugDocumentsList(session);
        Document debugDoc;/*from   ww w  . ja  v  a2  s . c  om*/
        for (int idx = 0; idx < debugDocuments.size(); idx++) {
            debugDoc = (Document) debugDocuments.get(idx);
            if (debugDoc.getRootElement().attributeValue("url", "").equals(urlStr)) {
                indexStr = String.valueOf(idx);
                break;
            }
        }
    }

    response.setContentType("text/html");

    if (indexStr != null) {
        int index = Integer.valueOf(indexStr).intValue();
        List<Document> debugDocuments = WGACore.getDebugDocumentsList(session);
        if (index == -1) {
            index = debugDocuments.size() - 1;
        }
        if (index >= debugDocuments.size()) {
            throw new HttpErrorException(HttpServletResponse.SC_BAD_REQUEST,
                    "Index out of range: " + index + " where maximum index is " + (debugDocuments.size() - 1),
                    null);
        } else {
            Document doc = (Document) debugDocuments.get(index);
            Element element = (Element) doc.selectSingleNode(request.getParameter("root"));
            doc = DocumentFactory.getInstance().createDocument(element.createCopy());
            doc.getRootElement().addAttribute("index", String.valueOf(index));

            try {
                DOMWriter domWriter = new DOMWriter();
                org.w3c.dom.Document domDocument = domWriter.write(doc);

                Transformer trans = getDebugTagsTransformer(request.getParameter("throwAway") != null);
                trans.transform(new DOMSource(domDocument), new StreamResult(response.getOutputStream()));
            } catch (TransformerConfigurationException e) {
                response.sendError(500, e.getMessageAndLocation());
                e.printStackTrace();
            } catch (TransformerFactoryConfigurationError e) {
                response.sendError(500, e.getMessage());
                e.printStackTrace();
            } catch (TransformerException e) {
                response.sendError(500, e.getMessageAndLocation());
                e.printStackTrace();
            } catch (IOException e) {
                response.sendError(500, e.getMessage());
                e.printStackTrace();
            } catch (DocumentException e) {
                response.sendError(500, e.getMessage());
                e.printStackTrace();
            }
        }
    } else {
        throw new HttpErrorException(HttpServletResponse.SC_BAD_REQUEST,
                "You must include either parameter index or url to address the debug document to show", null);
    }
}

From source file:de.innovationgate.wgpublisher.WebTMLDebugger.java

License:Open Source License

private void showTMLPortlets(HttpServletRequest request, HttpServletResponse response, HttpSession session)
        throws HttpErrorException, IOException {
    String urlStr = request.getParameter("url");
    String indexStr = request.getParameter("index");

    if (urlStr != null) {
        urlStr = _dispatcher.getCore().getURLEncoder().decode(urlStr);
        List<Document> debugDocuments = WGACore.getDebugDocumentsList(session);
        Document debugDoc;/*  w w w.  j  a  va2s . co m*/
        for (int idx = 0; idx < debugDocuments.size(); idx++) {
            debugDoc = (Document) debugDocuments.get(idx);
            if (debugDoc.getRootElement().attributeValue("url", "").equals(urlStr)) {
                indexStr = String.valueOf(idx);
                break;
            }
        }
    }

    response.setContentType("text/html");

    if (indexStr != null) {
        int index = Integer.valueOf(indexStr).intValue();
        List<Document> debugDocuments = WGACore.getDebugDocumentsList(session);
        if (index == -1) {
            index = debugDocuments.size() - 1;
        }
        if (index >= debugDocuments.size()) {
            throw new HttpErrorException(HttpServletResponse.SC_BAD_REQUEST,
                    "Index out of range: " + index + " where maximum index is " + (debugDocuments.size() - 1),
                    null);
        } else {
            Document doc = (Document) debugDocuments.get(index);
            doc.getRootElement().addAttribute("index", String.valueOf(index));

            try {
                DOMWriter domWriter = new DOMWriter();
                org.w3c.dom.Document domDocument = domWriter.write(doc);

                Transformer trans = getDebugPortletsTransformer(request.getParameter("throwAway") != null);
                trans.transform(new DOMSource(domDocument), new StreamResult(response.getOutputStream()));
            } catch (TransformerConfigurationException e) {
                response.sendError(500, e.getMessageAndLocation());
                e.printStackTrace();
            } catch (TransformerFactoryConfigurationError e) {
                response.sendError(500, e.getMessage());
                e.printStackTrace();
            } catch (TransformerException e) {
                response.sendError(500, e.getMessageAndLocation());
                e.printStackTrace();
            } catch (IOException e) {
                response.sendError(500, e.getMessage());
                e.printStackTrace();
            } catch (DocumentException e) {
                response.sendError(500, e.getMessage());
                e.printStackTrace();
            }
        }
    } else {
        throw new HttpErrorException(HttpServletResponse.SC_BAD_REQUEST,
                "You must include either parameter index or url to address the debug document to show", null);
    }
}

From source file:edu.wustl.common.hibernate.HibernateUtil.java

License:BSD License

/**
 * This method adds configuration file to Hibernate Configuration.
 * //from   www.j  a v a 2  s . c  o  m
 * @param fileName name of the file that needs to be added
 * @param cfg Configuration to which this file is added.
 */
private static void addConfigurationFile(String fileName, Configuration cfg) {
    try {
        InputStream inputStream = HibernateUtil.class.getClassLoader().getResourceAsStream(fileName);
        List errors = new ArrayList();
        // hibernate api to read configuration file and convert it to
        // Document(dom4j) object.
        XMLHelper xmlHelper = new XMLHelper();
        Document document = xmlHelper.createSAXReader(fileName, errors, entityResolver)
                .read(new InputSource(inputStream));
        // convert to w3c Document object.
        DOMWriter writer = new DOMWriter();
        org.w3c.dom.Document doc = writer.write(document);
        // configure
        cfg.configure(doc);
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    } catch (HibernateException e) {
        throw new RuntimeException(e);
    }
}

From source file:edu.wustl.common.util.dbManager.DBUtil.java

License:BSD License

/**
 * This method adds configuration file to Hibernate Configuration. 
 * @param fileName name of the file that needs to be added
 * @param cfg Configuration to which this file is added.
 *//*ww  w  .j a  va 2 s.  c o  m*/
private static void addConfigurationFile(String fileName, Configuration cfg) {

    try {
        InputStream inputStream = DBUtil.class.getClassLoader().getResourceAsStream(fileName);
        List errors = new ArrayList();
        //hibernate api to read configuration file and convert it to Document(dom4j) object.
        Document document = XMLHelper.createSAXReader(fileName, errors).read(new InputSource(inputStream));
        //convert to w3c Document object.
        DOMWriter writer = new DOMWriter();
        org.w3c.dom.Document doc = writer.write(document);
        //configure
        cfg.configure(doc);
    } catch (DocumentException e) {
        throw new RuntimeException(e.getMessage());
    } catch (HibernateException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:edu.wustl.dao.daofactory.DAOFactory.java

License:BSD License

/**
 * This method adds configuration file to Hibernate Configuration.
 * It will parse the configuration file and creates the configuration.
 * @param configurationfile name of the file that needs to be added
 * @return Configuration :Configuration object.
* @throws DAOException :Generic DAOException.
 *///  w  w w.j a va 2s  .c o m
private Configuration setConfiguration(String configurationfile) throws DAOException {
    try {

        Configuration configuration = new Configuration();
        //InputStream inputStream = DAOFactory.class.getClassLoader().getResourceAsStream(configurationfile);
        InputStream inputStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream(configurationfile);
        List<Object> errors = new ArrayList<Object>();
        // hibernate api to read configuration file and convert it to
        // Document(dom4j) object.
        XMLHelper xmlHelper = new XMLHelper();
        Document document = xmlHelper.createSAXReader(configurationfile, errors, entityResolver)
                .read(new InputSource(inputStream));
        // convert to w3c Document object.
        DOMWriter writer = new DOMWriter();
        org.w3c.dom.Document doc = writer.write(document);
        // configure
        configuration.configure(doc);
        return configuration;
    } catch (Exception exp) {
        logger.fatal(exp.getMessage());
        ErrorKey errorKey = ErrorKey.getErrorKey("db.operation.error");
        throw new DAOException(errorKey, exp, "DAOFactory.java :" + DAOConstants.CONFIG_FILE_PARSE_ERROR);
    }

}

From source file:eu.eexcess.partnerrecommender.reference.PartnerConnectorBase.java

License:Open Source License

protected Document transformJSON2XML(String jsonData) throws EEXCESSDataTransformationException {
    XMLSerializer serializer = new XMLSerializer();
    JSON json = JSONSerializer.toJSON(jsonData);
    serializer.setTypeHintsEnabled(false);

    String xmlString = serializer.write(json);
    try {/*from  ww w.ja v  a 2  s. c o m*/
        SAXReader reader = new SAXReader();
        org.dom4j.Document dom4jDoc = reader.read(new StringReader(xmlString));

        DOMWriter writer = new DOMWriter();
        org.w3c.dom.Document w3cDoc = writer.write(dom4jDoc);

        return w3cDoc;
    } catch (Exception e) {
        e.printStackTrace();
        throw new EEXCESSDataTransformationException(e);
    }
}