diff options
4 files changed, 94 insertions, 2 deletions
diff --git a/src/com/isode/stroke/entity/GenericPayloadLoader.java b/src/com/isode/stroke/entity/GenericPayloadLoader.java new file mode 100644 index 0000000..67636bd --- /dev/null +++ b/src/com/isode/stroke/entity/GenericPayloadLoader.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2015, Isode Limited. + * All rights reserved. + */ +package com.isode.stroke.entity; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import com.isode.stroke.elements.Payload; +import com.isode.stroke.parser.AttributeMap; +import com.isode.stroke.parser.PayloadParser; +import com.isode.stroke.parser.PlatformXMLParserFactory; +import com.isode.stroke.parser.XMLParser; +import com.isode.stroke.parser.XMLParserClient; + +public class GenericPayloadLoader<PayloadType extends Payload, Parser extends PayloadParser> { + + private final Parser parser; + + public GenericPayloadLoader(Parser parser) { + this.parser = parser; + } + + @SuppressWarnings("unchecked") + public PayloadType loadPayload(final InputStream is) throws IOException { + if (is == null) return null; + final StringBuilder sb = new StringBuilder(2048); + final char[] read = new char[2048]; + final InputStreamReader ir = new InputStreamReader(is, "UTF-8"); + for (int i; -1 != (i = ir.read(read)); sb.append(read, 0, i)); + + final XMLParser xmlParser = PlatformXMLParserFactory.createXMLParser(new ParserClient(parser)); + xmlParser.parse(sb.toString()); + + return (PayloadType) parser.getPayload(); + } + + private static class ParserClient implements XMLParserClient { + private final PayloadParser parser; + + public ParserClient(PayloadParser parser) { + this.parser = parser; + } + + @Override + public void handleStartElement(String element, String ns, AttributeMap attributes) { + parser.handleStartElement(element, ns, attributes); + } + + @Override + public void handleEndElement(String element, String ns) { + parser.handleEndElement(element, ns); + } + + @Override + public void handleCharacterData(String data) { + parser.handleCharacterData(data); + } + + } + +} diff --git a/src/com/isode/stroke/entity/GenericPayloadPersister.java b/src/com/isode/stroke/entity/GenericPayloadPersister.java new file mode 100644 index 0000000..5cda8ec --- /dev/null +++ b/src/com/isode/stroke/entity/GenericPayloadPersister.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2015, Isode Limited. + * All rights reserved. + */ +package com.isode.stroke.entity; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; + +import com.isode.stroke.elements.Payload; +import com.isode.stroke.serializer.PayloadSerializer; + +public class GenericPayloadPersister<PayloadType extends Payload, Serializer extends PayloadSerializer> { + + private final Serializer serializer; + + public GenericPayloadPersister(Serializer serializer) { + this.serializer = serializer; + } + + public void savePayload(PayloadType payload, OutputStream os) throws IOException { + String s = serializer.serialize(payload); + OutputStreamWriter ow = new OutputStreamWriter(os, "UTF-8"); + ow.append(s); + ow.close(); + } +} diff --git a/src/com/isode/stroke/serializer/payloadserializers/DiscoInfoSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/DiscoInfoSerializer.java index 563eb53..616fadb 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/DiscoInfoSerializer.java +++ b/src/com/isode/stroke/serializer/payloadserializers/DiscoInfoSerializer.java @@ -15,7 +15,7 @@ import com.isode.stroke.serializer.GenericPayloadSerializer; import com.isode.stroke.serializer.xml.XMLElement; import com.isode.stroke.serializer.xml.XMLRawTextNode; -class DiscoInfoSerializer extends GenericPayloadSerializer<DiscoInfo>{ +public class DiscoInfoSerializer extends GenericPayloadSerializer<DiscoInfo>{ public DiscoInfoSerializer() { super(DiscoInfo.class); diff --git a/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java index 8a98f46..9d460fa 100644 --- a/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java +++ b/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java @@ -13,7 +13,7 @@ import com.isode.stroke.serializer.xml.XMLElement; import com.isode.stroke.serializer.xml.XMLRawTextNode; import com.isode.stroke.stringcodecs.Base64; -class VCardSerializer extends GenericPayloadSerializer<VCard>{ +public class VCardSerializer extends GenericPayloadSerializer<VCard>{ public VCardSerializer() { super(VCard.class); |