下面列出了java.io.ObjectInputValidation#com.thoughtworks.xstream.converters.DataHolder 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Deserialize an object from a hierarchical data structure (such as XML).
*
* @param root If present, the passed in object will have its fields populated, as opposed
* to XStream creating a new instance. Note, that this is a special use case!
* With the ReflectionConverter XStream will write directly into the raw memory
* area of the existing object. Use with care!
* @param dataHolder Extra data you can use to pass to your converters. Use this as you
* want. If not present, XStream shall create one lazily as needed.
* @throws XStreamException if the object cannot be deserialized
*/
public Object unmarshal(HierarchicalStreamReader reader, Object root, DataHolder dataHolder) {
try {
if (!securityInitialized && !securityWarningGiven) {
securityWarningGiven = true;
System.err.println("Security framework of XStream not initialized, XStream is probably vulnerable.");
}
return marshallingStrategy.unmarshal(
root, reader, dataHolder, converterLookup, mapper);
} catch (ConversionException e) {
Package pkg = getClass().getPackage();
String version = pkg != null ? pkg.getImplementationVersion() : null;
e.add("version", version != null ? version : "not available");
throw e;
}
}
public void marshalOutputStream(Object graph, OutputStream outputStream, @Nullable DataHolder dataHolder)
throws XmlMappingException, IOException {
if (this.streamDriver != null) {
doMarshal(graph, this.streamDriver.createWriter(outputStream), dataHolder);
}
else {
marshalWriter(graph, new OutputStreamWriter(outputStream, this.encoding), dataHolder);
}
}
public void marshalWriter(Object graph, Writer writer, @Nullable DataHolder dataHolder)
throws XmlMappingException, IOException {
if (this.streamDriver != null) {
doMarshal(graph, this.streamDriver.createWriter(writer), dataHolder);
}
else {
doMarshal(graph, new CompactWriter(writer), dataHolder);
}
}
public Object unmarshalInputStream(InputStream inputStream, @Nullable DataHolder dataHolder) throws XmlMappingException, IOException {
if (this.streamDriver != null) {
return doUnmarshal(this.streamDriver.createReader(inputStream), dataHolder);
}
else {
return unmarshalReader(new InputStreamReader(inputStream, this.encoding), dataHolder);
}
}
/**
* Unmarshals the given graph to the given XStream HierarchicalStreamWriter.
* Converts exceptions using {@link #convertXStreamException}.
*/
private Object doUnmarshal(HierarchicalStreamReader streamReader, @Nullable DataHolder dataHolder) {
try {
return getXStream().unmarshal(streamReader, null, dataHolder);
}
catch (Exception ex) {
throw convertXStreamException(ex, false);
}
}
public void marshalOutputStream(Object graph, OutputStream outputStream, @Nullable DataHolder dataHolder)
throws XmlMappingException, IOException {
if (this.streamDriver != null) {
doMarshal(graph, this.streamDriver.createWriter(outputStream), dataHolder);
}
else {
marshalWriter(graph, new OutputStreamWriter(outputStream, this.encoding), dataHolder);
}
}
public void marshalWriter(Object graph, Writer writer, @Nullable DataHolder dataHolder)
throws XmlMappingException, IOException {
if (this.streamDriver != null) {
doMarshal(graph, this.streamDriver.createWriter(writer), dataHolder);
}
else {
doMarshal(graph, new CompactWriter(writer), dataHolder);
}
}
public Object unmarshalInputStream(InputStream inputStream, @Nullable DataHolder dataHolder) throws XmlMappingException, IOException {
if (this.streamDriver != null) {
return doUnmarshal(this.streamDriver.createReader(inputStream), dataHolder);
}
else {
return unmarshalReader(new InputStreamReader(inputStream, this.encoding), dataHolder);
}
}
/**
* Unmarshals the given graph to the given XStream HierarchicalStreamWriter.
* Converts exceptions using {@link #convertXStreamException}.
*/
private Object doUnmarshal(HierarchicalStreamReader streamReader, @Nullable DataHolder dataHolder) {
try {
return getXStream().unmarshal(streamReader, null, dataHolder);
}
catch (Exception ex) {
throw convertXStreamException(ex, false);
}
}
/**
* Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream.
*
* @see #createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
* @see #createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
* @since 1.4.10
*/
public ObjectOutputStream createObjectOutputStream(final HierarchicalStreamWriter writer, final String rootNodeName,
final DataHolder dataHolder) throws IOException {
final StatefulWriter statefulWriter = new StatefulWriter(writer);
statefulWriter.startNode(rootNodeName, null);
return new CustomObjectOutputStream(new CustomObjectOutputStream.StreamCallback() {
public void writeToStream(final Object object) {
marshal(object, statefulWriter, dataHolder);
}
public void writeFieldsToStream(Map fields) throws NotActiveException {
throw new NotActiveException("not in call to writeObject");
}
public void defaultWriteObject() throws NotActiveException {
throw new NotActiveException("not in call to writeObject");
}
public void flush() {
statefulWriter.flush();
}
public void close() {
if (statefulWriter.state() != StatefulWriter.STATE_CLOSED) {
statefulWriter.endNode();
statefulWriter.close();
}
}
});
}
/**
* Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream.
*
* @see #createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String)
* @see #createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader)
* @since 1.4.10
*/
public ObjectInputStream createObjectInputStream(final HierarchicalStreamReader reader, final DataHolder dataHolder)
throws IOException {
return new CustomObjectInputStream(new CustomObjectInputStream.StreamCallback() {
public Object readFromStream() throws EOFException {
if (!reader.hasMoreChildren()) {
throw new EOFException();
}
reader.moveDown();
final Object result = unmarshal(reader, dataHolder);
reader.moveUp();
return result;
}
public Map readFieldsFromStream() throws IOException {
throw new NotActiveException("not in call to readObject");
}
public void defaultReadObject() throws NotActiveException {
throw new NotActiveException("not in call to readObject");
}
public void registerValidation(ObjectInputValidation validation, int priority)
throws NotActiveException {
throw new NotActiveException("stream inactive");
}
public void close() {
reader.close();
}
}, classLoaderReference);
}
public Object start(DataHolder dataHolder) {
this.dataHolder = dataHolder;
Class type = HierarchicalStreams.readClassType(reader, mapper);
Object result = convertAnother(null, type);
Iterator validations = validationList.iterator();
while (validations.hasNext()) {
Runnable runnable = (Runnable)validations.next();
runnable.run();
}
return result;
}
public void start(Object item, DataHolder dataHolder) {
this.dataHolder = dataHolder;
if (item == null) {
writer.startNode(mapper.serializedClass(null));
writer.endNode();
} else {
ExtendedHierarchicalStreamWriterHelper.startNode(writer, mapper
.serializedClass(item.getClass()), item.getClass());
convertAnother(item);
writer.endNode();
}
}
public void marshalOutputStream(Object graph, OutputStream outputStream, DataHolder dataHolder)
throws XmlMappingException, IOException {
if (this.streamDriver != null) {
doMarshal(graph, this.streamDriver.createWriter(outputStream), dataHolder);
}
else {
marshalWriter(graph, new OutputStreamWriter(outputStream, this.encoding), dataHolder);
}
}
public void marshalWriter(Object graph, Writer writer, DataHolder dataHolder)
throws XmlMappingException, IOException {
if (this.streamDriver != null) {
doMarshal(graph, this.streamDriver.createWriter(writer), dataHolder);
}
else {
doMarshal(graph, new CompactWriter(writer), dataHolder);
}
}
public Object unmarshalInputStream(InputStream inputStream, DataHolder dataHolder) throws XmlMappingException, IOException {
if (this.streamDriver != null) {
return doUnmarshal(this.streamDriver.createReader(inputStream), dataHolder);
}
else {
return unmarshalReader(new InputStreamReader(inputStream, this.encoding), dataHolder);
}
}
/**
* Unmarshals the given graph to the given XStream HierarchicalStreamWriter.
* Converts exceptions using {@link #convertXStreamException}.
*/
private Object doUnmarshal(HierarchicalStreamReader streamReader, DataHolder dataHolder) {
try {
return getXStream().unmarshal(streamReader, null, dataHolder);
}
catch (Exception ex) {
throw convertXStreamException(ex, false);
}
}
public Object unmarshalReader(Reader reader, @Nullable DataHolder dataHolder) throws XmlMappingException, IOException {
return doUnmarshal(getDefaultDriver().createReader(reader), dataHolder);
}
public Object unmarshalReader(Reader reader, @Nullable DataHolder dataHolder) throws XmlMappingException, IOException {
return doUnmarshal(getDefaultDriver().createReader(reader), dataHolder);
}
public Object unmarshal(Object root, HierarchicalStreamReader reader, DataHolder dataHolder, ConverterLookup converterLookup, Mapper mapper) {
TreeUnmarshaller context = createUnmarshallingContext(root, reader, converterLookup, mapper);
return context.start(dataHolder);
}
public void marshal(HierarchicalStreamWriter writer, Object obj, ConverterLookup converterLookup, Mapper mapper, DataHolder dataHolder) {
TreeMarshaller context = createMarshallingContext(writer, converterLookup, mapper);
context.start(obj, dataHolder);
}
/**
* @deprecated As of 1.4 use {@link #getInstance(DataHolder, StreamCallback, ClassLoader)}
*/
public static CustomObjectInputStream getInstance(DataHolder whereFrom, CustomObjectInputStream.StreamCallback callback) {
return getInstance(whereFrom, callback, (ClassLoader)null);
}
/**
* @deprecated As of 1.4.5 use {@link #getInstance(DataHolder, StreamCallback, ClassLoaderReference)}
*/
public static synchronized CustomObjectInputStream getInstance(DataHolder whereFrom, CustomObjectInputStream.StreamCallback callback, ClassLoader classLoader) {
return getInstance(whereFrom, callback, new ClassLoaderReference(classLoader));
}
public Object unmarshalReader(Reader reader, DataHolder dataHolder) throws XmlMappingException, IOException {
return doUnmarshal(getDefaultDriver().createReader(reader), dataHolder);
}
public static GraphBuilder contextGraphBuilder(DataHolder context) {
return (GraphBuilder) context.get(GraphBuilder.class);
}
/**
* Serialize and object to a hierarchical data structure (such as XML).
*
* @param dataHolder Extra data you can use to pass to your converters. Use this as you
* want. If not present, XStream shall create one lazily as needed.
* @throws XStreamException if the object cannot be serialized
*/
public void marshal(Object obj, HierarchicalStreamWriter writer, DataHolder dataHolder) {
marshallingStrategy.marshal(writer, obj, converterLookup, mapper, dataHolder);
}
/**
* Create a DataHolder that can be used to pass data to the converters. The DataHolder is provided with a call to
* {@link #marshal(Object, HierarchicalStreamWriter, DataHolder)},
* {@link #unmarshal(HierarchicalStreamReader, Object, DataHolder)},
* {@link #createObjectInputStream(HierarchicalStreamReader, DataHolder)} or
* {@link #createObjectOutputStream(HierarchicalStreamWriter, String, DataHolder)}.
*
* @return a new {@link DataHolder}
*/
public DataHolder newDataHolder() {
return new MapBackedDataHolder();
}
Object unmarshal(Object root, HierarchicalStreamReader reader, DataHolder dataHolder, ConverterLookup converterLookup, Mapper mapper);
void marshal(HierarchicalStreamWriter writer, Object obj, ConverterLookup converterLookup, Mapper mapper, DataHolder dataHolder);