Search in sources :

Example 1 with XMLStringSource

use of de.pdark.decentxml.XMLStringSource in project fabric8 by jboss-fuse.

the class RouteXml method marshalToDoc.

/**
 * Marshals the model to XML and updates the model's doc property to contain the
 * new marshalled model
 *
 * @param model
 */
public void marshalToDoc(XmlModel model) throws JAXBException {
    Marshaller marshaller = jaxbContext().createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, java.lang.Boolean.TRUE);
    try {
        marshaller.setProperty("com.sun.xml.bind.indentString", "  ");
    } catch (Exception e) {
        LOG.debug("Property not supported: {}", e);
    }
    Object value = model.marshalRootElement();
    Document doc = model.getDoc();
    Element docElem = doc.getRootElement();
    // JAXB only seems to do nice whitespace/namespace stuff when writing to stream
    // rather than DOM directly
    // marshaller.marshal(value, docElem);
    StringWriter buffer = new StringWriter();
    marshaller.marshal(value, buffer);
    // now lets parse the XML and insert the root element into the doc
    String xml = buffer.toString();
    if (!model.getNs().equals(springNS)) {
        // !!!
        xml = xml.replaceAll(springNS, model.getNs());
    }
    Document camelDoc = parse(new XMLStringSource(xml));
    Node camelElem = camelDoc.getRootElement();
    if (model.isRoutesContext() && camelDoc.getRootElement().getName().equals("camelContext")) {
        camelDoc.getRootElement().setName("routeContext");
    }
    if (model.isJustRoutes()) {
        replaceChild(doc, camelElem, docElem);
    } else {
        if (model.getNode() != null) {
            replaceCamelElement(docElem, camelElem, model.getNode());
        } else {
            docElem.addNode(camelElem);
        }
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) XMLStringSource(de.pdark.decentxml.XMLStringSource) StringWriter(java.io.StringWriter) Element(de.pdark.decentxml.Element) Node(de.pdark.decentxml.Node) Document(de.pdark.decentxml.Document) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

Document (de.pdark.decentxml.Document)1 Element (de.pdark.decentxml.Element)1 Node (de.pdark.decentxml.Node)1 XMLStringSource (de.pdark.decentxml.XMLStringSource)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1