1 5 package javax.xml.bind.util; 6 7 import org.xml.sax.ContentHandler ; 8 import org.xml.sax.DTDHandler ; 9 import org.xml.sax.EntityResolver ; 10 import org.xml.sax.ErrorHandler ; 11 import org.xml.sax.InputSource ; 12 import org.xml.sax.SAXException ; 13 import org.xml.sax.SAXNotRecognizedException ; 14 import org.xml.sax.SAXParseException ; 15 import org.xml.sax.XMLReader ; 16 import org.xml.sax.ext.LexicalHandler ; 17 import org.xml.sax.helpers.XMLFilterImpl ; 18 19 import javax.xml.bind.JAXBContext; 20 import javax.xml.bind.JAXBException; 21 import javax.xml.bind.Marshaller; 22 import javax.xml.transform.sax.SAXSource ; 23 24 69 public class JAXBSource extends SAXSource { 70 71 85 public JAXBSource( JAXBContext context, Object contentObject ) 86 throws JAXBException { 87 88 this( 89 ( context == null ) ? 90 assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTEXT ) ) : 91 context.createMarshaller(), 92 93 ( contentObject == null ) ? 94 assertionFailed( Messages.format( Messages.SOURCE_NULL_CONTENT ) ) : 95 contentObject); 96 } 97 98 113 public JAXBSource( Marshaller marshaller, Object contentObject ) 114 throws JAXBException { 115 116 if( marshaller == null ) 117 throw new JAXBException( 118 Messages.format( Messages.SOURCE_NULL_MARSHALLER ) ); 119 120 if( contentObject == null ) 121 throw new JAXBException( 122 Messages.format( Messages.SOURCE_NULL_CONTENT ) ); 123 124 this.marshaller = marshaller; 125 this.contentObject = contentObject; 126 127 super.setXMLReader(pseudoParser); 128 super.setInputSource(new InputSource ()); 130 } 131 132 private final Marshaller marshaller; 133 private final Object contentObject; 134 135 private final XMLReader pseudoParser = new XMLReader () { 139 public boolean getFeature(String name) throws SAXNotRecognizedException { 140 if(name.equals("http://xml.org/sax/features/namespaces")) 141 return true; 142 if(name.equals("http://xml.org/sax/features/namespace-prefixes")) 143 return false; 144 throw new SAXNotRecognizedException (name); 145 } 146 147 public void setFeature(String name, boolean value) throws SAXNotRecognizedException { 148 if(name.equals("http://xml.org/sax/features/namespaces") && value) 149 return; 150 if(name.equals("http://xml.org/sax/features/namespace-prefixes") && !value) 151 return; 152 throw new SAXNotRecognizedException (name); 153 } 154 155 public Object getProperty(String name) throws SAXNotRecognizedException { 156 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) { 157 return lexicalHandler; 158 } 159 throw new SAXNotRecognizedException (name); 160 } 161 162 public void setProperty(String name, Object value) throws SAXNotRecognizedException { 163 if( "http://xml.org/sax/properties/lexical-handler".equals(name) ) { 164 this.lexicalHandler = (LexicalHandler )value; 165 return; 166 } 167 throw new SAXNotRecognizedException (name); 168 } 169 170 private LexicalHandler lexicalHandler; 171 172 private EntityResolver entityResolver; 174 public void setEntityResolver(EntityResolver resolver) { 175 this.entityResolver = resolver; 176 } 177 public EntityResolver getEntityResolver() { 178 return entityResolver; 179 } 180 181 private DTDHandler dtdHandler; 182 public void setDTDHandler(DTDHandler handler) { 183 this.dtdHandler = handler; 184 } 185 public DTDHandler getDTDHandler() { 186 return dtdHandler; 187 } 188 189 private XMLFilterImpl repeater = new XMLFilterImpl (); 193 194 public void setContentHandler(ContentHandler handler) { 195 repeater.setContentHandler(handler); 196 } 197 public ContentHandler getContentHandler() { 198 return repeater.getContentHandler(); 199 } 200 201 private ErrorHandler errorHandler; 202 public void setErrorHandler(ErrorHandler handler) { 203 this.errorHandler = handler; 204 } 205 public ErrorHandler getErrorHandler() { 206 return errorHandler; 207 } 208 209 public void parse(InputSource input) throws SAXException { 210 parse(); 211 } 212 213 public void parse(String systemId) throws SAXException { 214 parse(); 215 } 216 217 public void parse() throws SAXException { 218 try { 222 marshaller.marshal( contentObject, repeater ); 223 } catch( JAXBException e ) { 224 SAXParseException se = 226 new SAXParseException ( e.getMessage(), 227 null, null, -1, -1, e ); 228 229 if(errorHandler!=null) 232 errorHandler.fatalError(se); 233 234 throw se; 237 } 238 } 239 }; 240 241 245 private static Marshaller assertionFailed( String message ) 246 throws JAXBException { 247 248 throw new JAXBException( message ); 249 } 250 } 251 | Popular Tags |