1 package com.thoughtworks.xstream.io.xml; 2 3 import com.thoughtworks.xstream.XStream; 4 import org.xml.sax.InputSource ; 5 import org.xml.sax.SAXException ; 6 import org.xml.sax.XMLFilter ; 7 import org.xml.sax.XMLReader ; 8 9 import javax.xml.transform.sax.SAXSource ; 10 import java.util.ArrayList ; 11 import java.util.List ; 12 13 40 public class TraxSource extends SAXSource { 41 42 53 public final static String XSTREAM_FEATURE = 54 "http://com.thoughtworks.xstream/XStreamSource/feature"; 55 56 62 private XMLReader xmlReader = null; 63 64 67 private XStream xstream = null; 68 69 72 private List source = null; 73 74 75 79 82 public TraxSource() { 83 super(new InputSource ()); 84 } 85 86 95 public TraxSource(Object source) { 96 super(new InputSource ()); 97 98 this.setSource(source); 99 } 100 101 112 public TraxSource(Object source, XStream xstream) { 113 super(new InputSource ()); 114 115 this.setSource(source); 116 this.setXStream(xstream); 117 } 118 119 128 public TraxSource(List source) { 129 super(new InputSource ()); 130 131 this.setSourceAsList(source); 132 } 133 134 146 public TraxSource(List source, XStream xstream) { 147 super(new InputSource ()); 148 149 this.setSourceAsList(source); 150 this.setXStream(xstream); 151 } 152 153 157 168 public void setInputSource(InputSource inputSource) { 169 throw new UnsupportedOperationException (); 170 } 171 172 188 public void setXMLReader(XMLReader reader) { 189 this.createXMLReader(reader); 190 } 191 192 203 public XMLReader getXMLReader() { 204 if (this.xmlReader == null) { 205 this.createXMLReader(null); 206 } 207 return this.xmlReader; 208 } 209 210 211 215 222 public void setXStream(XStream xstream) { 223 if (xstream == null) { 224 throw new IllegalArgumentException ("xstream"); 225 } 226 this.xstream = xstream; 227 228 this.configureXMLReader(); 229 } 230 231 238 public void setSource(Object obj) { 239 if (obj == null) { 240 throw new IllegalArgumentException ("obj"); 241 } 242 List list = new ArrayList (1); 243 list.add(obj); 244 245 this.setSourceAsList(list); 246 } 247 248 264 public void setSourceAsList(List list) { 265 if ((list == null) || (list.isEmpty())) { 266 throw new IllegalArgumentException ("list"); 267 } 268 this.source = list; 269 270 this.configureXMLReader(); 271 } 272 273 private void createXMLReader(XMLReader filterChain) { 274 if (filterChain == null) { 275 this.xmlReader = new SaxWriter(); 276 } else { 277 if (filterChain instanceof XMLFilter ) { 278 XMLFilter filter = (XMLFilter ) filterChain; 280 while (filter.getParent() instanceof XMLFilter ) { 281 filter = (XMLFilter ) (filter.getParent()); 282 } 283 filter.setParent(new SaxWriter()); 284 285 this.xmlReader = filterChain; 287 } else { 288 throw new UnsupportedOperationException (); 289 } 290 } 291 this.configureXMLReader(); 292 } 293 294 private void configureXMLReader() { 295 if (this.xmlReader != null) { 296 try { 297 if (this.xstream != null) { 298 this.xmlReader.setProperty(SaxWriter.CONFIGURED_XSTREAM_PROPERTY, this.xstream); 299 } 300 if (this.source != null) { 301 this.xmlReader.setProperty(SaxWriter.SOURCE_OBJECT_LIST_PROPERTY, this.source); 302 } 303 } catch (SAXException e) { 304 throw new IllegalArgumentException (e.getMessage()); 305 } 306 } 307 } 308 } 309 310 | Popular Tags |