📄 xstream.java
字号:
public void aliasField(String alias, Class type, String fieldName) { fieldAliasingMapper.addFieldAlias(alias, type, fieldName); } /** * Associate a default implementation of a class with an object. Whenever XStream encounters an instance of this * type, it will use the default implementation instead. * * For example, java.util.ArrayList is the default implementation of java.util.List. * @param defaultImplementation * @param ofType */ public void addDefaultImplementation(Class defaultImplementation, Class ofType) { defaultImplementationsMapper.addDefaultImplementation(defaultImplementation, ofType); } public void addImmutableType(Class type) { immutableTypesMapper.addImmutableType(type); } /** * @deprecated As of 1.1.1 you should register a converter with the appropriate priority. */ public void changeDefaultConverter(Converter defaultConverter) { registerConverter(defaultConverter, PRIORITY_VERY_LOW); } public void registerConverter(Converter converter) { registerConverter(converter, PRIORITY_NORMAL); } public void registerConverter(Converter converter, int priority) { converterLookup.registerConverter(converter, priority); } public ClassMapper getClassMapper() { return classMapper; } public ConverterLookup getConverterLookup() { return converterLookup; } /** * Change mode for dealing with duplicate references. * Valid valuse are <code>XStream.XPATH_REFERENCES</code>, * <code>XStream.ID_REFERENCES</code> and <code>XStream.NO_REFERENCES</code>. * * @see #XPATH_REFERENCES * @see #ID_REFERENCES * @see #NO_REFERENCES */ public void setMode(int mode) { switch (mode) { case NO_REFERENCES: setMarshallingStrategy(new TreeMarshallingStrategy()); break; case ID_REFERENCES: setMarshallingStrategy(new ReferenceByIdMarshallingStrategy()); break; case XPATH_REFERENCES: setMarshallingStrategy(new ReferenceByXPathMarshallingStrategy()); break; default: throw new IllegalArgumentException("Unknown mode : " + mode); } } /** * @deprecated Use addImplicitCollection() instead. */ public void addDefaultCollection(Class ownerType, String fieldName) { addImplicitCollection(ownerType, fieldName); } /** * Adds a default implicit collection which is used for any unmapped xml tag. * * @param ownerType class owning the implicit collection * @param fieldName name of the field in the ownerType. This field must be an <code>java.util.ArrayList</code>. */ public void addImplicitCollection(Class ownerType, String fieldName) { implicitCollectionMapper.add(ownerType, fieldName, null, Object.class); } /** * Adds implicit collection which is used for all items of the given itemType. * * @param ownerType class owning the implicit collection * @param fieldName name of the field in the ownerType. This field must be an <code>java.util.ArrayList</code>. * @param itemType type of the items to be part of this collection. */ public void addImplicitCollection(Class ownerType, String fieldName, Class itemType) { implicitCollectionMapper.add(ownerType, fieldName, null, itemType); } /** * Adds implicit collection which is used for all items of the given element name defined by itemFieldName. * * @param ownerType class owning the implicit collection * @param fieldName name of the field in the ownerType. This field must be an <code>java.util.ArrayList</code>. * @param itemFieldName element name of the implicit collection * @param itemType item type to be aliases be the itemFieldName */ public void addImplicitCollection(Class ownerType, String fieldName, String itemFieldName, Class itemType) { implicitCollectionMapper.add(ownerType, fieldName, itemFieldName, itemType); } public DataHolder newDataHolder() { return new MapBackedDataHolder(); } /** * Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream. * * <p>To change the name of the root element (from <object-stream>), use * {@link #createObjectOutputStream(java.io.Writer, String)}.</p> * * @see #createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String) * @see #createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader) * @since 1.0.3 */ public ObjectOutputStream createObjectOutputStream(Writer writer) throws IOException { return createObjectOutputStream(new PrettyPrintWriter(writer), "object-stream"); } /** * Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream. * * <p>To change the name of the root element (from <object-stream>), use * {@link #createObjectOutputStream(java.io.Writer, String)}.</p> * * @see #createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String) * @see #createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader) * @since 1.0.3 */ public ObjectOutputStream createObjectOutputStream(HierarchicalStreamWriter writer) throws IOException { return createObjectOutputStream(writer, "object-stream"); } /** * 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.0.3 */ public ObjectOutputStream createObjectOutputStream(Writer writer, String rootNodeName) throws IOException { return createObjectOutputStream(new PrettyPrintWriter(writer), rootNodeName); } /** * Creates an ObjectOutputStream that serializes a stream of objects to the writer using XStream. * * <p>Because an ObjectOutputStream can contain multiple items and XML only allows a single root node, the stream * must be written inside an enclosing node.</p> * * <p>It is necessary to call ObjectOutputStream.close() when done, otherwise the stream will be incomplete.</p> * * <h3>Example</h3> * <pre>ObjectOutputStream out = xstream.createObjectOutputStream(aWriter, "things"); * out.writeInt(123); * out.writeObject("Hello"); * out.writeObject(someObject) * out.close();</pre> * * @param writer The writer to serialize the objects to. * @param rootNodeName The name of the root node enclosing the stream of objects. * * @see #createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader) * @since 1.0.3 */ public ObjectOutputStream createObjectOutputStream(final HierarchicalStreamWriter writer, String rootNodeName) throws IOException { writer.startNode(rootNodeName); return new CustomObjectOutputStream(new CustomObjectOutputStream.StreamCallback() { public void writeToStream(Object object) { marshal(object, writer); } 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() { writer.flush(); } public void close() { writer.endNode(); writer.close(); } }); } /** * Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream. * * @see #createObjectInputStream(com.thoughtworks.xstream.io.HierarchicalStreamReader) * @see #createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String) * @since 1.0.3 */ public ObjectInputStream createObjectInputStream(Reader xmlReader) throws IOException { return createObjectInputStream(hierarchicalStreamDriver.createReader(xmlReader)); } /** * Creates an ObjectInputStream that deserializes a stream of objects from a reader using XStream. * * <h3>Example</h3> * <pre>ObjectInputStream in = xstream.createObjectOutputStream(aReader); * int a = out.readInt(); * Object b = out.readObject(); * Object c = out.readObject();</pre> * * @see #createObjectOutputStream(com.thoughtworks.xstream.io.HierarchicalStreamWriter, String) * @since 1.0.3 */ public ObjectInputStream createObjectInputStream(final HierarchicalStreamReader reader) throws IOException { return new CustomObjectInputStream(new CustomObjectInputStream.StreamCallback() { public Object readFromStream() throws EOFException { if (!reader.hasMoreChildren()) { throw new EOFException(); } reader.moveDown(); Object result = unmarshal(reader); 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(); } }); } /** * Change the ClassLoader XStream uses to load classes. * * @since 1.1.1 */ public void setClassLoader(ClassLoader classLoader) { classLoaderReference.setReference(classLoader); } /** * Change the ClassLoader XStream uses to load classes. * * @since 1.1.1 */ public ClassLoader getClassLoader() { return classLoaderReference.getReference(); } /** * Prevents a field from being serialized. * * @since 1.2 */ public void omitField(Class type, String fieldName) { fieldAliasingMapper.omitField(type, fieldName); } public static class InitializationException extends BaseException { public InitializationException(String message, Throwable cause) { super(message, cause); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -