Search in sources :

Example 1 with Value

use of ddf.catalog.transformer.xml.binding.StringxmlElement.Value in project ddf by codice.

the class StringxmlAdapter method marshalFrom.

/**
     * @param attribute
     * @return JAXB representable attribute
     * @throws CatalogTransformerException
     */
public static StringxmlElement marshalFrom(Attribute attribute) throws CatalogTransformerException {
    StringxmlElement element = new StringxmlElement();
    element.setName(attribute.getName());
    if (attribute.getValue() != null) {
        for (Serializable value : attribute.getValues()) {
            if (!(value instanceof String)) {
                continue;
            }
            String xmlString = (String) value;
            Element anyElement = null;
            DocumentBuilder builder = null;
            try {
                synchronized (FACTORY) {
                    builder = FACTORY.newDocumentBuilder();
                    builder.setErrorHandler(null);
                }
                anyElement = builder.parse(new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8))).getDocumentElement();
            } catch (ParserConfigurationException | SAXException | IOException e) {
                throw new CatalogTransformerException(TRANSFORMATION_FAILED_ERROR_MESSAGE, e);
            }
            Value anyValue = new StringxmlElement.Value();
            anyValue.setAny(anyElement);
            element.getValue().add(anyValue);
        }
    }
    return element;
}
Also used : Serializable(java.io.Serializable) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) StringxmlElement(ddf.catalog.transformer.xml.binding.StringxmlElement) Element(org.w3c.dom.Element) Value(ddf.catalog.transformer.xml.binding.StringxmlElement.Value) StringxmlElement(ddf.catalog.transformer.xml.binding.StringxmlElement) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 2 with Value

use of ddf.catalog.transformer.xml.binding.StringxmlElement.Value in project ddf by codice.

the class StringxmlAdapter method unmarshalFrom.

public static Attribute unmarshalFrom(StringxmlElement element) throws CatalogTransformerException, TransformerException, JAXBException {
    AttributeImpl attribute = null;
    if (templates == null) {
        throw new CatalogTransformerException("Could not transform XML due to internal configuration error.");
    }
    for (Value xmlValue : element.getValue()) {
        String xmlString = "";
        Element anyNode = xmlValue.getAny();
        StringWriter buffer = new StringWriter();
        Transformer transformer = templates.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.transform(new DOMSource(anyNode), new StreamResult(buffer));
        xmlString = buffer.toString();
        if (attribute == null) {
            attribute = new AttributeImpl(element.getName(), xmlString);
        } else {
            attribute.addValue(xmlString);
        }
    }
    return attribute;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) StringxmlElement(ddf.catalog.transformer.xml.binding.StringxmlElement) Element(org.w3c.dom.Element) Value(ddf.catalog.transformer.xml.binding.StringxmlElement.Value) CatalogTransformerException(ddf.catalog.transform.CatalogTransformerException)

Aggregations

CatalogTransformerException (ddf.catalog.transform.CatalogTransformerException)2 StringxmlElement (ddf.catalog.transformer.xml.binding.StringxmlElement)2 Value (ddf.catalog.transformer.xml.binding.StringxmlElement.Value)2 Element (org.w3c.dom.Element)2 AttributeImpl (ddf.catalog.data.impl.AttributeImpl)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 StringWriter (java.io.StringWriter)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Transformer (javax.xml.transform.Transformer)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 SAXException (org.xml.sax.SAXException)1