1 17 package org.apache.ws.jaxme.impl; 18 19 import java.io.File ; 20 import java.io.FileInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.Reader ; 24 import java.net.URL ; 25 26 import javax.xml.bind.JAXBException; 27 import javax.xml.bind.UnmarshalException; 28 import javax.xml.bind.UnmarshallerHandler; 29 import javax.xml.parsers.ParserConfigurationException ; 30 import javax.xml.parsers.SAXParser ; 31 import javax.xml.parsers.SAXParserFactory ; 32 import javax.xml.transform.Source ; 33 import javax.xml.transform.dom.DOMSource ; 34 import javax.xml.transform.sax.SAXSource ; 35 import javax.xml.transform.stream.StreamSource ; 36 37 import org.apache.ws.jaxme.JMUnmarshaller; 38 import org.apache.ws.jaxme.util.DOMSerializer; 39 import org.w3c.dom.Node ; 40 import org.xml.sax.InputSource ; 41 import org.xml.sax.SAXException ; 42 import org.xml.sax.XMLReader ; 43 44 45 47 public class JMUnmarshallerImpl extends JMControllerImpl implements JMUnmarshaller { 48 private static final SAXParserFactory spf; 49 static { 50 spf = SAXParserFactory.newInstance(); 51 spf.setValidating(false); 52 spf.setNamespaceAware(true); 53 } 54 55 private boolean validating; 56 57 public boolean isValidating() { return validating; } 58 public void setValidating(boolean pValidating) { validating = pValidating; } 59 60 public Object unmarshal(URL pURL) throws JAXBException { 61 InputSource isource; 62 try { 63 isource = new InputSource (pURL.openStream()); 64 isource.setSystemId(pURL.toString()); 65 } catch (IOException e) { 66 throw new UnmarshalException("Failed to open URL " + pURL, e); 67 } 68 return unmarshal(isource); 69 } 70 71 public Object unmarshal(File pFile) throws JAXBException { 72 InputSource isource; 73 try { 74 isource = new InputSource (new FileInputStream (pFile)); 75 isource.setSystemId(pFile.toURL().toString()); 76 } catch (IOException e) { 77 throw new UnmarshalException("Failed to open file " + pFile, e); 78 } 79 return unmarshal(isource); 80 } 81 82 public Object unmarshal(InputStream pStream) throws JAXBException { 83 return unmarshal(new InputSource (pStream)); 84 } 85 86 public Object unmarshal(InputSource pSource) throws JAXBException { 87 UnmarshallerHandler uh = getUnmarshallerHandler(); 88 try { 89 SAXParser sp = spf.newSAXParser(); 90 XMLReader xr = sp.getXMLReader(); 91 xr.setContentHandler(uh); 92 xr.parse(pSource); 93 } catch (SAXException e) { 94 if (e.getException() != null) { 95 throw new UnmarshalException(e.getException()); 96 } else { 97 throw new UnmarshalException(e); 98 } 99 } catch (IOException e) { 100 throw new UnmarshalException(e); 101 } catch (ParserConfigurationException e) { 102 throw new UnmarshalException(e); 103 } 104 return uh.getResult(); 105 } 106 107 public Object unmarshal(Node pNode) throws JAXBException { 108 UnmarshallerHandler uh = getUnmarshallerHandler(); 109 DOMSerializer ds = new DOMSerializer(); 110 try { 111 ds.serialize(pNode, uh); 112 } catch (SAXException e) { 113 if (e.getException() != null) { 114 throw new UnmarshalException(e.getException()); 115 } else { 116 throw new UnmarshalException(e); 117 } 118 } 119 return uh.getResult(); 120 } 121 122 public Object unmarshal(Source pSource) throws JAXBException { 123 if (pSource instanceof SAXSource ) { 124 SAXSource ss = (SAXSource ) pSource; 125 InputSource is = ss.getInputSource(); 126 if (is == null) { 127 throw new UnmarshalException("The SAXResult doesn't have its InputSource set."); 128 } 129 XMLReader xr = ss.getXMLReader(); 130 if (xr == null) { 131 return unmarshal(is); 132 } else { 133 UnmarshallerHandler uh = getUnmarshallerHandler(); 134 xr.setContentHandler(uh); 135 try { 136 xr.parse(is); 137 } catch (IOException e) { 138 throw new JAXBException(e); 139 } catch (SAXException e) { 140 if (e.getException() != null) { 141 throw new JAXBException(e.getException()); 142 } else { 143 throw new JAXBException(e); 144 } 145 } 146 return uh.getResult(); 147 } 148 } else if (pSource instanceof StreamSource ) { 149 StreamSource ss = (StreamSource ) pSource; 150 InputSource iSource = new InputSource (); 151 iSource.setPublicId(ss.getPublicId()); 152 iSource.setSystemId(ss.getSystemId()); 153 Reader r = ss.getReader(); 154 if (r == null) { 155 InputStream is = ss.getInputStream(); 156 if (is == null) { 157 throw new IllegalArgumentException ("The StreamSource doesn't have its Reader or InputStream set."); 158 } else { 159 iSource.setByteStream(is); 160 } 161 } else { 162 iSource.setCharacterStream(r); 163 } 164 return unmarshal(iSource); 165 } else if (pSource instanceof DOMSource ) { 166 Node node = ((DOMSource ) pSource).getNode(); 167 if (node == null) { 168 throw new IllegalArgumentException ("The DOMSource doesn't have its Node set."); 169 } 170 return unmarshal(node); 171 } else { 172 throw new IllegalArgumentException ("Unknown type of Source: " + pSource.getClass().getName() + 173 ", only SAXSource, StreamSource and DOMSource are supported."); 174 } 175 } 176 177 public UnmarshallerHandler getUnmarshallerHandler() { 178 return new JMUnmarshallerHandlerImpl(this); 179 } 180 } 181 | Popular Tags |