Search in sources :

Example 1 with XmlSnapshotIdResult

use of org.eclipse.kura.core.configuration.XmlSnapshotIdResult in project kura by eclipse.

the class XmlUtilTest method testXmlSnapshotIdResultMarshalling.

@TestTarget(targetPlatforms = { TestTarget.PLATFORM_ALL })
@Test
public void testXmlSnapshotIdResultMarshalling() {
    try {
        XmlSnapshotIdResult xmlSnapshotIdResult = getSampleXmlSnapshotIdResultObject();
        String marshalledString = org.eclipse.kura.core.configuration.util.XmlUtil.marshal(xmlSnapshotIdResult);
        for (Long value : xmlSnapshotIdResult.getSnapshotIds()) {
            Assert.assertTrue(String.format(missingItemsMessage, value), marshalledString.contains(Long.toString(value)));
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : XmlSnapshotIdResult(org.eclipse.kura.core.configuration.XmlSnapshotIdResult) TestTarget(org.eclipse.kura.test.annotation.TestTarget) Test(org.junit.Test)

Example 2 with XmlSnapshotIdResult

use of org.eclipse.kura.core.configuration.XmlSnapshotIdResult in project kura by eclipse.

the class XmlUtilTest method getSampleXmlSnapshotIdResultObject.

private static XmlSnapshotIdResult getSampleXmlSnapshotIdResultObject() {
    List<Long> snapshotIds = new ArrayList<Long>();
    snapshotIds.add(102540L);
    snapshotIds.add(27848415L);
    snapshotIds.add(378485484848L);
    XmlSnapshotIdResult xmlSnapshotIdResult = new XmlSnapshotIdResult();
    xmlSnapshotIdResult.setSnapshotIds(snapshotIds);
    return xmlSnapshotIdResult;
}
Also used : ArrayList(java.util.ArrayList) XmlSnapshotIdResult(org.eclipse.kura.core.configuration.XmlSnapshotIdResult)

Example 3 with XmlSnapshotIdResult

use of org.eclipse.kura.core.configuration.XmlSnapshotIdResult in project kura by eclipse.

the class CoreTestXmlUtil method unmarshalXmlSnapshotIdResult.

// unmarshal XmlSnapshotIdResult
@SuppressWarnings("unchecked")
public static <T> T unmarshalXmlSnapshotIdResult(Document doc) throws Exception {
    XmlSnapshotIdResult xmlSnapshotIdResult = new XmlSnapshotIdResult();
    List<Long> idList = new ArrayList<Long>();
    // XML elements
    Element snapshotIds = doc.getDocumentElement();
    NodeList snapshotIdsChildren = snapshotIds.getChildNodes();
    // Collect all bundles
    for (int propIndex = 0; propIndex < snapshotIdsChildren.getLength(); propIndex++) {
        Node currentNode = snapshotIdsChildren.item(propIndex);
        if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
            Element el = (Element) currentNode;
            if (el.getNodeName().equals("esf:snapshotIds")) {
                // Add ID to list
                idList.add(Long.parseLong(el.getTextContent()));
            }
        }
    }
    // Add IDs
    xmlSnapshotIdResult.setSnapshotIds(idList);
    return (T) xmlSnapshotIdResult;
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) XmlSnapshotIdResult(org.eclipse.kura.core.configuration.XmlSnapshotIdResult)

Example 4 with XmlSnapshotIdResult

use of org.eclipse.kura.core.configuration.XmlSnapshotIdResult in project kura by eclipse.

the class XmlUtil method marshal.

public static void marshal(Object object, Writer w) throws Exception {
    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        // root elements
        Document doc = docBuilder.newDocument();
        doc.setXmlStandalone(true);
        if (object instanceof XmlSnapshotIdResult) {
            // Resulting xml:
            // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
            // <esf:snapshot-ids xmlns:ocd="http://www.osgi.org/xmlns/metatype/v1.2.0"
            // xmlns:esf="http://eurotech.com/esf/2.0">
            // <esf:snapshotIds>1434122113492</esf:snapshotIds>
            // <esf:snapshotIds>1434122124387</esf:snapshotIds>
            // </esf:snapshot-ids>
            new XmlJavaSnapshotIdResultMapper().marshal(doc, object);
        } else if (object instanceof XmlComponentConfigurations) {
            new XmlJavaComponentConfigurationsMapper().marshal(doc, object);
        }
        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        DOMSource source = new DOMSource(doc);
        // System.out
        StreamResult result = new StreamResult(w);
        transformer.transform(source, result);
    } catch (ParserConfigurationException pce) {
        s_logger.warn("Parser Exception", pce);
    } catch (TransformerException tfe) {
        s_logger.warn("Transformer Exception", tfe);
    }
}
Also used : XmlComponentConfigurations(org.eclipse.kura.core.configuration.XmlComponentConfigurations) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) XmlJavaComponentConfigurationsMapper(org.eclipse.kura.core.configuration.util.serializers.XmlJavaComponentConfigurationsMapper) StreamResult(javax.xml.transform.stream.StreamResult) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XmlSnapshotIdResult(org.eclipse.kura.core.configuration.XmlSnapshotIdResult) TransformerException(javax.xml.transform.TransformerException) XmlJavaSnapshotIdResultMapper(org.eclipse.kura.core.configuration.util.serializers.XmlJavaSnapshotIdResultMapper)

Example 5 with XmlSnapshotIdResult

use of org.eclipse.kura.core.configuration.XmlSnapshotIdResult in project kura by eclipse.

the class XmlJavaSnapshotIdResultMapper method marshal.

@Override
public Element marshal(Document doc, Object object) {
    Element snapshotIDs = doc.createElement(ESF_NAMESPACE + ":" + SNAPSHOT_IDS);
    // TODO:
    snapshotIDs.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:esf", "http://eurotech.com/esf/2.0");
    // add
    // xml
    // schema
    // to
    // EUROTECH
    // site
    snapshotIDs.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ocd", "http://www.osgi.org/xmlns/metatype/v1.2.0");
    doc.appendChild(snapshotIDs);
    XmlSnapshotIdResult xmlSnapshotIdResult = (XmlSnapshotIdResult) object;
    List<Long> snapshotIdVals = xmlSnapshotIdResult.getSnapshotIds();
    if (snapshotIdVals != null) {
        for (Long snapId : snapshotIdVals) {
            Element snapshotIds = doc.createElement(ESF_NAMESPACE + ":" + SNAPSHOTIDS);
            snapshotIds.setTextContent(snapId.toString());
            snapshotIDs.appendChild(snapshotIds);
        }
    }
    return snapshotIDs;
}
Also used : Element(org.w3c.dom.Element) XmlSnapshotIdResult(org.eclipse.kura.core.configuration.XmlSnapshotIdResult)

Aggregations

XmlSnapshotIdResult (org.eclipse.kura.core.configuration.XmlSnapshotIdResult)6 ArrayList (java.util.ArrayList)3 XmlComponentConfigurations (org.eclipse.kura.core.configuration.XmlComponentConfigurations)2 TestTarget (org.eclipse.kura.test.annotation.TestTarget)2 Test (org.junit.Test)2 Element (org.w3c.dom.Element)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Hashtable (java.util.Hashtable)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 ComponentConfigurationImpl (org.eclipse.kura.core.configuration.ComponentConfigurationImpl)1 XmlJavaComponentConfigurationsMapper (org.eclipse.kura.core.configuration.util.serializers.XmlJavaComponentConfigurationsMapper)1 XmlJavaSnapshotIdResultMapper (org.eclipse.kura.core.configuration.util.serializers.XmlJavaSnapshotIdResultMapper)1