1 29 30 package com.caucho.jaxb; 31 32 import com.caucho.jaxb.adapters.BeanAdapter; 33 import com.caucho.jaxb.skeleton.Skeleton; 34 import com.caucho.util.L10N; 35 import com.caucho.vfs.*; 36 import com.caucho.xml.stream.*; 37 38 import org.w3c.dom.*; 39 import org.xml.sax.*; 40 import org.xml.sax.*; 41 42 import javax.xml.bind.*; 43 import javax.xml.bind.annotation.adapters.*; 44 import javax.xml.bind.attachment.*; 45 import javax.xml.bind.helpers.*; 46 import javax.xml.namespace.QName ; 47 import javax.xml.stream.*; 48 import javax.xml.transform.*; 49 import javax.xml.validation.*; 50 51 import java.net.*; 52 import java.io.*; 53 import java.util.*; 54 55 public class UnmarshallerImpl implements Unmarshaller 56 { 57 private static final L10N L = new L10N(UnmarshallerImpl.class); 58 59 private JAXBContextImpl _context; 60 61 protected boolean validating; 62 63 private AttachmentUnmarshaller _attachmentUnmarshaller = null; 64 private ValidationEventHandler _validationEventHandler = null; 65 private Listener _listener = null; 66 private HashMap<String ,Object > _properties = new HashMap<String ,Object >(); 67 private Schema _schema = null; 68 private XMLReader _xmlreader = null; 69 private XmlAdapter _adapter = null; 70 private UnmarshallerHandler _unmarshallerHandler = null; 71 private HashMap<Class ,XmlAdapter> _adapters 72 = new HashMap<Class ,XmlAdapter>(); 73 74 UnmarshallerImpl(JAXBContextImpl context) 75 { 76 this._context = context; 77 } 78 79 83 86 public Object unmarshal(Node node) throws JAXBException 87 { 88 throw new UnsupportedOperationException (); 89 } 90 91 94 protected Object unmarshal(XMLReader reader, InputSource source) 95 throws JAXBException 96 { 97 throw new UnsupportedOperationException (); 98 } 99 100 103 public Object unmarshal(XMLEventReader reader) throws JAXBException 104 { 105 throw new UnsupportedOperationException (); 106 } 107 108 public <T> JAXBElement<T> unmarshal(XMLEventReader xmlEventReader, 109 Class <T> declaredType) 110 throws JAXBException 111 { 112 throw new UnsupportedOperationException (); 113 } 114 115 public Object unmarshal(XMLStreamReader reader) 116 throws JAXBException 117 { 118 try { 119 if (reader.nextTag() != XMLStreamReader.START_ELEMENT) 120 throw new JAXBException(L.l("Expected root element")); 121 122 Skeleton skel = _context.getRootElement(reader.getName()); 123 124 if (skel == null) 125 throw new JAXBException(L.l("'{0}' is an unknown root element", 126 reader.getName())); 127 128 return skel.read(this, reader); 129 } catch (JAXBException e) { 130 throw e; 131 } catch (Exception e) { 132 throw new RuntimeException (e); 133 } 134 } 135 136 139 public Object unmarshal(InputStream is) 140 throws JAXBException 141 { 142 try { 143 XMLStreamReader reader = new XMLStreamReaderImpl(is); 144 145 try { 146 if (reader.nextTag() != XMLStreamReader.START_ELEMENT) 147 throw new JAXBException(L.l("Expected root element")); 148 149 Skeleton skel = _context.getRootElement(reader.getName()); 150 151 if (skel == null) 152 throw new JAXBException(L.l("'{0}' is an unknown root element", 153 reader.getName())); 154 155 return skel.read(this, reader); 156 } finally { 157 reader.close(); 158 } 159 } catch (JAXBException e) { 160 throw e; 161 } catch (Exception e) { 162 throw new RuntimeException (e); 163 } 164 } 165 166 public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, 167 Class <T> declaredType) 168 throws JAXBException 169 { 170 try { 171 while (reader.getEventType() != XMLStreamReader.START_ELEMENT) 172 reader.next(); 173 174 QName name = reader.getName(); 175 T val = (T)_context.getSkeleton(declaredType).read(this, reader); 176 return new JAXBElement<T>(name, declaredType, val); 177 } 178 catch (Exception e) { 179 throw new RuntimeException (e); 180 } 181 } 182 183 187 public UnmarshallerHandler getUnmarshallerHandler() 188 { 189 return _unmarshallerHandler; 190 } 191 192 public void setUnmarshallerHandler(UnmarshallerHandler u) 193 { 194 _unmarshallerHandler = u; 195 } 196 197 protected UnmarshalException createUnmarshalException(SAXException e) 198 { 199 return new UnmarshalException(e); 200 } 201 202 public <A extends XmlAdapter> A getAdapter(Class <A> type) 203 { 204 A a = (A)_adapters.get(type); 205 206 if (a == null) 207 return (A)new BeanAdapter(); 208 209 return a; 210 } 211 212 public AttachmentUnmarshaller getAttachmentUnmarshaller() 213 { 214 return _attachmentUnmarshaller; 215 } 216 217 public ValidationEventHandler getEventHandler() throws JAXBException 218 { 219 return _validationEventHandler; 220 } 221 222 public Listener getListener() 223 { 224 return _listener; 225 } 226 227 public Object getProperty(String name) throws PropertyException 228 { 229 return _properties.get(name); 230 } 231 232 public Schema getSchema() 233 { 234 return _schema; 235 } 236 237 protected XMLReader getXMLReader() throws JAXBException 238 { 239 return _xmlreader; 240 } 241 242 public boolean isValidating() throws JAXBException 243 { 244 return validating; 245 } 246 247 public <A extends XmlAdapter> void setAdapter(Class <A> type, A adapter) 248 { 249 _adapters.put(type, adapter); 250 } 251 252 public void setAdapter(XmlAdapter adapter) 253 { 254 setAdapter((Class )adapter.getClass(), adapter); 255 } 256 257 public void setAttachmentUnmarshaller(AttachmentUnmarshaller au) 258 { 259 _attachmentUnmarshaller = au; 260 } 261 262 public void setEventHandler(ValidationEventHandler handler) 263 throws JAXBException 264 { 265 _validationEventHandler = handler; 266 } 267 268 public void setListener(Listener listener) 269 { 270 _listener = listener; 271 } 272 273 public void setProperty(String name, Object value) throws PropertyException 274 { 275 _properties.put(name, value); 276 } 277 278 public void setSchema(Schema schema) 279 { 280 _schema = schema; 281 } 282 283 public void setValidating(boolean validating) throws JAXBException 284 { 285 this.validating = validating; 286 } 287 288 public Object unmarshal(File f) throws JAXBException 289 { 290 FileInputStream fis = null; 291 try { 292 fis = new FileInputStream(f); 293 return unmarshal(fis); 294 } 295 catch (IOException e) { 296 throw new JAXBException(e); 297 } 298 finally { 299 try { 300 if (fis != null) 301 fis.close(); 302 } 303 catch (IOException e) { 304 throw new JAXBException(e); 305 } 306 } 307 } 308 309 public Object unmarshal(InputSource source) throws JAXBException 310 { 311 throw new UnsupportedOperationException ("subclasses must override this"); 312 } 313 314 public Object unmarshal(Reader reader) throws JAXBException 315 { 316 try { 317 XMLInputFactory factory = _context.getXMLInputFactory(); 318 319 return unmarshal(factory.createXMLStreamReader(reader)); 320 } 321 catch (XMLStreamException e) { 322 throw new JAXBException(e); 323 } 324 } 325 326 public Object unmarshal(Source source) throws JAXBException 327 { 328 try { 329 XMLInputFactory factory = _context.getXMLInputFactory(); 330 331 return unmarshal(factory.createXMLStreamReader(source)); 332 } 333 catch (XMLStreamException e) { 334 throw new JAXBException(e); 335 } 336 } 337 338 public <T> JAXBElement<T> unmarshal(Node node, Class <T> declaredType) 339 throws JAXBException 340 { 341 throw new UnsupportedOperationException ("subclasses must override this"); 342 } 343 344 public <T> JAXBElement<T> unmarshal(Source node, Class <T> declaredType) 345 throws JAXBException 346 { 347 try { 348 XMLInputFactory factory = XMLInputFactory.newInstance(); 349 return unmarshal(factory.createXMLStreamReader(node), declaredType); 350 } 351 catch (XMLStreamException e) { 352 throw new JAXBException(e); 353 } 354 } 355 356 public Object unmarshal(URL url) throws JAXBException 357 { 358 try { 359 InputStream is = url.openStream(); 360 361 try { 362 return unmarshal(is); 363 } finally { 364 is.close(); 365 } 366 } catch (IOException e) { 367 throw new JAXBException(e); 368 } 369 } 370 } 371 | Popular Tags |