PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/sitebricks-client/src/main/java/com/google/sitebricks/client/transport/XStreamXmlTransport.java

http://github.com/dhanji/sitebricks
Java | 35 lines | 25 code | 7 blank | 3 comment | 0 complexity | f2ccfee1671c9a2bb6fdf04456b6aca3 MD5 | raw file
 1package com.google.sitebricks.client.transport;
 2
 3import com.google.inject.Inject;
 4import com.google.inject.TypeLiteral;
 5import com.thoughtworks.xstream.XStream;
 6
 7import java.io.IOException;
 8import java.io.InputStream;
 9import java.io.OutputStream;
10
11/**
12 * @author Dhanji R. Prasanna (dhanji@gmail.com)
13 */
14class XStreamXmlTransport extends Xml {
15  private final XStream xStream;
16
17  @Inject
18  public XStreamXmlTransport(XStream xStream) {
19    this.xStream = xStream;
20  }
21
22  public <T> T in(InputStream in, Class<T> type) throws IOException {
23    return type.cast(xStream.fromXML(in));
24  }
25
26  @Override
27  @SuppressWarnings("unchecked")
28  public <T> T in(InputStream in, TypeLiteral<T> type) throws IOException {
29    return (T)xStream.fromXML(in);
30  }
31
32  public <T> void out(OutputStream out, Class<T> type, T data) {
33    xStream.toXML(data, out);
34  }
35}