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);
}
}
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);
}
}
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()));
}
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);
}
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);
}
Aggregations