schnawel007
schnawel007

Reputation: 4020

XStream Java display progress

is there a possibility to display the progress of the serialization/deserialization process? Because I have a huge amount of Objects (200.000) and it takes 10 to 20 seconds, so I want to show the user a progress bar. Or is there a better solution/library?

Upvotes: 1

Views: 187

Answers (2)

Martin Frank
Martin Frank

Reputation: 3454

you can create a custom Converter...

i assume that you have a collection in wich resides your data...

ArrayList<String> stringList = new ArrayList<String>();  //example list
stringList.add("abc"); //example data
stringList.add("abc"); //example data
stringList.add("abc"); //example data
stringList.add("abc"); //example data

XStream xstream = new XStream();
xstream.alias("string", String.class ); //example aliasing

Mapper mapper = xstream.getMapper();

CollectionConverter aColCon = new CollectionConverter(mapper) {

    @Override
    protected void writeItem(Object item, MarshallingContext context, HierarchicalStreamWriter writer){
        super.writeItem(item, context, writer);
        System.out.println("write object item"+item);
        //TODO your progress bar here!

    }
};

xstream.registerConverter(aColCon);
String asXml = xstream.toXML(stringList);
System.out.println(asXml);

this is the output:

write object itemabc
write object itemabc
write object itemabc
write object itemabc
<list>
  <string>abc</string>
  <string>abc</string>
  <string>abc</string>
  <string>abc</string>
</list>

have a look at http://x-stream.github.io/javadoc/com/thoughtworks/xstream/converters/collections/AbstractCollectionConverter.html#readItem%28com.thoughtworks.xstream.io.HierarchicalStreamReader,%20com.thoughtworks.xstream.converters.UnmarshallingContext,%20java.lang.Object%29 there you can find you best converter...

Upvotes: 1

Vertex
Vertex

Reputation: 2712

Or you can extend a HierarchicalStreamWriter and add some progress information to startNode

@XStreamAlias("entity")
private static class Entity {
    String foo;
    String bar;
}

private static class ProgressIndicationWriter extends PrettyPrintWriter {
    public ProgressIndicationWriter(Writer writer, NameCoder nameCoder) {
        super(writer, nameCoder);
    }

    @Override
    public void startNode(String name) {
        System.out.print("writing node " + name + ".. ");
        super.startNode(name);
        System.out.println("OK");
    }
}

private static class ProgressIndicationDriver extends XppDriver {
    @Override
    public HierarchicalStreamWriter createWriter(Writer out) {
        return new ProgressIndicationWriter(out, getNameCoder());
    }
}

public static void main(String[] args) {
    final XStream xstream = new XStream(new ProgressIndicationDriver());
    xstream.autodetectAnnotations(true);

    final Entity myEntity = new Entity();
    myEntity.foo = "Lorem";
    myEntity.bar = "Ipsum";

    final String xml = xstream.toXML(myEntity);
    System.out.println();
    System.out.println(xml);
}

And the output is

writing node entity.. OK
writing node foo.. OK
writing node bar.. OK

<entity>
  <foo>Lorem</foo>
  <bar>Ipsum</bar>
</entity>

This should be more generic than the approach to write custom converter.

Upvotes: 0

Related Questions