Search in sources :

Example 1 with SerializationData

use of org.apache.sling.ide.serialization.SerializationData in project sling by apache.

the class SimpleXmlSerializationManager method buildSerializationData.

@Override
public SerializationData buildSerializationData(File contentSyncRoot, ResourceProxy resource) throws SerializationException {
    if (resource == null) {
        return null;
    }
    Map<String, Object> content = resource.getProperties();
    if (content == null || content.isEmpty()) {
        return null;
    }
    try {
        SAXTransformerFactory f = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        StreamResult sr = new StreamResult(result);
        TransformerHandler handler = f.newTransformerHandler();
        Transformer t = handler.getTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        handler.setResult(sr);
        handler.startDocument();
        startElement(handler, TAG_RESOURCE);
        Set<Entry<String, Object>> entrySet = new TreeMap<>(content).entrySet();
        for (Map.Entry<String, Object> property : entrySet) {
            Object value = property.getValue();
            if (value instanceof String) {
                String tagName = property.getKey();
                String tagValue = (String) value;
                AttributesImpl attributes = new AttributesImpl();
                attributes.addAttribute("", ATT_PROPERTY_NAME, ATT_PROPERTY_NAME, null, tagName);
                handler.startElement("", TAG_PROPERTY, TAG_PROPERTY, attributes);
                handler.characters(tagValue.toCharArray(), 0, tagValue.length());
                handler.endElement("", TAG_PROPERTY, TAG_PROPERTY);
            } else {
                // TODO multi-valued properties, other primitives
                System.err.println("Can't yet handle property " + property.getKey() + " of type " + value.getClass());
            }
        }
        endElement(handler, TAG_RESOURCE);
        handler.endDocument();
        // TODO - also add the serialization type
        return new SerializationData(resource.getPath(), CONTENT_XML, result.toByteArray(), null);
    } catch (TransformerConfigurationException | TransformerFactoryConfigurationError | SAXException e) {
        // TODO proper exception handling
        throw new RuntimeException(e);
    }
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SAXException(org.xml.sax.SAXException) Entry(java.util.Map.Entry) AttributesImpl(org.xml.sax.helpers.AttributesImpl) SerializationData(org.apache.sling.ide.serialization.SerializationData) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with SerializationData

use of org.apache.sling.ide.serialization.SerializationData in project sling by apache.

the class VltSerializationDataBuilder method buildSerializationData.

@Override
public SerializationData buildSerializationData(File contentSyncRoot, ResourceProxy resource) throws SerializationException {
    try {
        List<Aggregate> chain = findAggregateChain(resource);
        if (chain == null) {
            return null;
        }
        Aggregate aggregate = chain.get(chain.size() - 1);
        String fileOrFolderPathHint = calculateFileOrFolderPathHint(chain);
        String nameHint = PlatformNameFormat.getPlatformName(aggregate.getName());
        SerializationKind serializationKind = getSerializationKind(aggregate);
        if (resource.getPath().equals("/") || serializationKind == SerializationKind.METADATA_PARTIAL || serializationKind == SerializationKind.FILE || serializationKind == SerializationKind.FOLDER) {
            nameHint = Constants.DOT_CONTENT_XML;
        } else if (serializationKind == SerializationKind.METADATA_FULL) {
            nameHint += ".xml";
        }
        Activator.getDefault().getPluginLogger().trace("Got location {0} for path {1}", fileOrFolderPathHint, resource.getPath());
        if (!needsDir(aggregate)) {
            return SerializationData.empty(fileOrFolderPathHint, serializationKind);
        }
        DocViewSerializer s = new DocViewSerializer(aggregate);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        s.writeContent(out);
        byte[] result = out.toByteArray();
        return new SerializationData(fileOrFolderPathHint, nameHint, result, serializationKind);
    } catch (RepositoryException e) {
        throw new SerializationException(e);
    } catch (IOException e) {
        throw new SerializationException(e);
    }
}
Also used : SerializationException(org.apache.sling.ide.serialization.SerializationException) SerializationData(org.apache.sling.ide.serialization.SerializationData) SerializationKind(org.apache.sling.ide.serialization.SerializationKind) DocViewSerializer(org.apache.jackrabbit.vault.fs.impl.io.DocViewSerializer) RepositoryException(javax.jcr.RepositoryException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Aggregate(org.apache.jackrabbit.vault.fs.api.Aggregate)

Example 3 with SerializationData

use of org.apache.sling.ide.serialization.SerializationData in project sling by apache.

the class SimpleXmlSerializationManagerTest method nullSerializedData.

@Test
public void nullSerializedData() throws SerializationException, SAXException {
    SerializationData serializationData = sm.newBuilder(null, null).buildSerializationData(null, null);
    assertThat(serializationData, is(nullValue()));
}
Also used : SerializationData(org.apache.sling.ide.serialization.SerializationData) Test(org.junit.Test)

Example 4 with SerializationData

use of org.apache.sling.ide.serialization.SerializationData in project sling by apache.

the class SimpleXmlSerializationManagerTest method serializedDataIsEscaped.

@Test
public void serializedDataIsEscaped() throws SerializationException, SAXException, IOException {
    Map<String, Object> data = new HashMap<>();
    data.put("jcr:description", "<p class=\"active\">Welcome</p>");
    SerializationData serializationData = sm.newBuilder(null, null).buildSerializationData(null, newResourceWithProperties(data));
    String methodName = "serializedDataIsEscaped";
    assertXmlOutputIsEqualTo(serializationData.getContents(), methodName);
}
Also used : SerializationData(org.apache.sling.ide.serialization.SerializationData) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with SerializationData

use of org.apache.sling.ide.serialization.SerializationData in project sling by apache.

the class SimpleXmlSerializationManagerTest method stringSerializedData.

@Test
public void stringSerializedData() throws SerializationException, SAXException, IOException {
    Map<String, Object> data = new HashMap<>();
    data.put("jcr:createdBy", "admin");
    data.put("jcr:lastModifiedBy", "author");
    SerializationData serializationData = sm.newBuilder(null, null).buildSerializationData(null, newResourceWithProperties(data));
    String methodName = "stringSerializedData";
    assertXmlOutputIsEqualTo(serializationData.getContents(), methodName);
}
Also used : SerializationData(org.apache.sling.ide.serialization.SerializationData) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

SerializationData (org.apache.sling.ide.serialization.SerializationData)7 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 TreeMap (java.util.TreeMap)1 RepositoryException (javax.jcr.RepositoryException)1 Transformer (javax.xml.transform.Transformer)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 TransformerFactoryConfigurationError (javax.xml.transform.TransformerFactoryConfigurationError)1 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)1 TransformerHandler (javax.xml.transform.sax.TransformerHandler)1 StreamResult (javax.xml.transform.stream.StreamResult)1 Aggregate (org.apache.jackrabbit.vault.fs.api.Aggregate)1 DocViewSerializer (org.apache.jackrabbit.vault.fs.impl.io.DocViewSerializer)1 FilterResult (org.apache.sling.ide.filter.FilterResult)1