1 package com.thoughtworks.xstream; 2 3 import com.thoughtworks.xstream.alias.ClassMapper; 4 import com.thoughtworks.xstream.converters.Converter; 5 import com.thoughtworks.xstream.converters.ConverterLookup; 6 import com.thoughtworks.xstream.converters.reflection.ReflectionProvider; 7 import com.thoughtworks.xstream.core.*; 8 import com.thoughtworks.xstream.io.HierarchicalStreamDriver; 9 import com.thoughtworks.xstream.io.HierarchicalStreamReader; 10 import com.thoughtworks.xstream.io.HierarchicalStreamWriter; 11 import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; 12 import com.thoughtworks.xstream.io.xml.XppDriver; 13 14 import java.io.Reader ; 15 import java.io.StringReader ; 16 import java.io.StringWriter ; 17 import java.io.Writer ; 18 19 91 public class XStream { 92 93 private HierarchicalStreamDriver hierarchicalStreamDriver; 94 private MarshallingStrategy marshallingStrategy; 95 private ClassMapper classMapper; 96 private DefaultConverterLookup converterLookup; 97 98 public static final int NO_REFERENCES = 1001; 99 public static final int ID_REFERENCES = 1002; 100 public static final int XPATH_REFERENCES = 1003; 101 102 public XStream() { 103 this(JVM.bestReflectionProvider(), new DefaultClassMapper(), new XppDriver()); 104 } 105 106 public XStream(HierarchicalStreamDriver hierarchicalStreamDriver) { 107 this(JVM.bestReflectionProvider(), new DefaultClassMapper(), hierarchicalStreamDriver); 108 } 109 110 public XStream(ReflectionProvider reflectionProvider) { 111 this(reflectionProvider, new DefaultClassMapper(), new XppDriver()); 112 } 113 114 public XStream(ReflectionProvider reflectionProvider, HierarchicalStreamDriver hierarchicalStreamDriver) { 115 this(reflectionProvider, new DefaultClassMapper(), hierarchicalStreamDriver); 116 } 117 118 public XStream(ReflectionProvider reflectionProvider, ClassMapper classMapper, HierarchicalStreamDriver driver) { 119 this(reflectionProvider, classMapper, driver, "class"); 120 } 121 122 public XStream(ReflectionProvider reflectionProvider, ClassMapper classMapper, HierarchicalStreamDriver driver, String classAttributeIdentifier) { 123 this.classMapper = classMapper; 124 this.hierarchicalStreamDriver = driver; 125 setMode(XPATH_REFERENCES); 126 converterLookup = new DefaultConverterLookup(reflectionProvider, classMapper, classAttributeIdentifier); 127 converterLookup.setupDefaults(); 128 } 129 130 public void setMarshallingStrategy(MarshallingStrategy marshallingStrategy) { 131 this.marshallingStrategy = marshallingStrategy; 132 } 133 134 137 public String toXML(Object obj) { 138 Writer stringWriter = new StringWriter (); 139 HierarchicalStreamWriter writer = new PrettyPrintWriter(stringWriter); 140 marshal(obj, writer); 141 return stringWriter.toString(); 142 } 143 144 147 public void toXML(Object obj, Writer writer) { 148 marshal(obj, new PrettyPrintWriter(writer)); 149 } 150 151 154 public void marshal(Object obj, HierarchicalStreamWriter writer) { 155 marshallingStrategy.marshal(writer, obj, converterLookup, classMapper); 156 } 157 158 161 public Object fromXML(String xml) { 162 return fromXML(new StringReader (xml)); 163 } 164 165 168 public Object fromXML(Reader xml) { 169 return unmarshal(hierarchicalStreamDriver.createReader(xml), null); 170 } 171 172 175 public Object unmarshal(HierarchicalStreamReader reader) { 176 return unmarshal(reader, null); 177 } 178 179 184 public Object unmarshal(HierarchicalStreamReader reader, Object root) { 185 return marshallingStrategy.unmarshal(root, reader, converterLookup, classMapper); 186 } 187 188 194 public void alias(String elementName, Class type) { 195 converterLookup.alias(elementName, type, type); 196 } 197 198 205 public void alias(String elementName, Class type, Class defaultImplementation) { 206 converterLookup.alias(elementName, type, defaultImplementation); 207 } 208 209 public void registerConverter(Converter converter) { 210 converterLookup.registerConverter(converter); 211 } 212 213 public ClassMapper getClassMapper() { 214 return classMapper; 215 } 216 217 public ConverterLookup getConverterLookup() { 218 return converterLookup; 219 } 220 221 230 public void setMode(int mode) { 231 switch (mode) { 232 case NO_REFERENCES: 233 setMarshallingStrategy(new TreeMarshallingStrategy()); 234 break; 235 case ID_REFERENCES: 236 setMarshallingStrategy(new ReferenceByIdMarshallingStrategy()); 237 break; 238 case XPATH_REFERENCES: 239 setMarshallingStrategy(new ReferenceByXPathMarshallingStrategy()); 240 break; 241 default: 242 throw new IllegalArgumentException ("Unknown mode : " + mode); 243 } 244 } 245 } 246 | Popular Tags |