/Users/detro/workspace-nb/JAEBI/src/java/org/jaebi/server/service/xml/JAXBMarshallerWrapperImpl.java

Vai alla documentazione di questo file.
00001 /*
00002  * JAXBMarshallerWrapperImpl.java
00003  *
00004  * Created on May 29, 2005, 7:02 PM
00005  * Created by Detro - 566/2145
00006  */
00007 
00008 package org.jaebi.server.service.xml;
00009 
00010 import java.io.File;
00011 import java.io.InputStream;
00012 import java.io.StringReader;
00013 import java.io.StringWriter;
00014 import java.net.URL;
00015 
00016 import javax.xml.bind.JAXBContext;
00017 import javax.xml.bind.JAXBException;
00018 import javax.xml.bind.Marshaller;
00019 import javax.xml.bind.Unmarshaller;
00020 import javax.xml.transform.Source;
00021 import javax.xml.transform.stream.StreamSource;
00022 
00023 import org.jaebi.server.service.xml.exception.JAXBMarshallerWrapperInitException;
00024 import org.jaebi.server.service.xml.exception.JAXBMarshallerWrapperMarshallException;
00025 import org.jaebi.server.service.xml.exception.JAXBMarshallerWrapperUnmarshallException;
00026 
00037 public class JAXBMarshallerWrapperImpl implements JAXBMarshallerWrapper {
00039     private final JAXBContext context;
00041     private final Marshaller marshaller;
00043     private final Unmarshaller unmarshaller;
00045     private boolean formattedMarshalling;
00047     private static String exceptionPrefix = "[JAXBMarshallerWrapperImpl Exception]: ";
00048         
00058     public JAXBMarshallerWrapperImpl(String packageName) 
00059         throws JAXBMarshallerWrapperInitException {
00060         
00061         try {
00062             // JAXBContext
00063             context = JAXBContext.newInstance(packageName);
00064             // Marshaller
00065             marshaller = context.createMarshaller();
00066             // Marshaller
00067             unmarshaller = context.createUnmarshaller();
00068         } catch (JAXBException e) {
00069             throw new JAXBMarshallerWrapperInitException(exceptionPrefix + e);
00070         } 
00071     }
00072     
00073     public String marshall(Object marshallable) 
00074         throws JAXBMarshallerWrapperMarshallException {
00075         
00076         StringWriter writer = new StringWriter();
00077         
00078         try {
00079             marshaller.marshal(marshallable, writer);
00080         } catch (JAXBException e) {
00081             throw new JAXBMarshallerWrapperMarshallException(exceptionPrefix + e);
00082         }
00083         
00084         return writer.toString();
00085     }
00086    
00087     public Object unmarshall(URL url) throws JAXBMarshallerWrapperUnmarshallException {
00088         Object xmlElement = null; 
00089         
00090         try {
00091             xmlElement = unmarshaller.unmarshal(url);
00092         } catch (JAXBException e) {
00093             throw new JAXBMarshallerWrapperUnmarshallException(exceptionPrefix + e);
00094         }
00095         
00096         return xmlElement;
00097     }
00098     
00099     public Object unmarshall(File file) throws JAXBMarshallerWrapperUnmarshallException {
00100         Object xmlElement = null; 
00101         
00102         try {
00103             xmlElement = unmarshaller.unmarshal(file);
00104         } catch (JAXBException e) {
00105             throw new JAXBMarshallerWrapperUnmarshallException(exceptionPrefix + e);
00106         }
00107         
00108         return xmlElement;
00109     }
00110     
00111     public Object unmarshall(InputStream inputStream) 
00112         throws JAXBMarshallerWrapperUnmarshallException {
00113         
00114         Object xmlElement = null; 
00115         
00116         try {
00117             xmlElement = unmarshaller.unmarshal(inputStream);
00118         } catch (JAXBException e) {
00119             throw new JAXBMarshallerWrapperUnmarshallException(exceptionPrefix + e);
00120         }
00121         
00122         return xmlElement;
00123     }
00124     
00125     public Object unmarshall(Source source) 
00126         throws JAXBMarshallerWrapperUnmarshallException {
00127         
00128         Object xmlElement = null; 
00129         
00130         try {
00131             xmlElement = unmarshaller.unmarshal(source);
00132         } catch (JAXBException e) {
00133             throw new JAXBMarshallerWrapperUnmarshallException(exceptionPrefix + e);
00134         }
00135         
00136         return xmlElement;
00137     }
00138     
00139     public Object unmarshall(String xmlString) 
00140         throws JAXBMarshallerWrapperUnmarshallException {
00141         
00142         /* Creazione StringBuffer che useremo nell'unmarshalling.
00143          * L'interfaccia della classe Unmashaller non prevede un metodo
00144          * che prenda in input, direttamente, un oggetto String */
00145         StringBuffer xmlStr = new StringBuffer( xmlString );
00146         
00147         return unmarshall( new StreamSource( new StringReader( xmlStr.toString() ) ) );
00148     }
00149     
00150     public void setFormattedMarshalling(boolean value) 
00151         throws JAXBMarshallerWrapperMarshallException {
00152         
00153         formattedMarshalling = value;
00154         try {
00155             if ( formattedMarshalling ) {
00156                 /* XML Formatting Property Setting */
00157                 marshaller.setProperty(
00158                         Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean("true"));
00159             } else {
00160                 /* XML Formatting Property Setting */
00161                 marshaller.setProperty(
00162                         Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean("false"));
00163             }
00164         } catch ( JAXBException e ) {
00165             throw new JAXBMarshallerWrapperMarshallException(exceptionPrefix + e);
00166         }
00167     }
00168     
00169     public boolean isFormattedMarshalling() {
00170         return formattedMarshalling;
00171     }
00172 }

Generato il Mon Jun 27 22:50:38 2005 per JAEBI - JAva Enterprice BackEnd Interfacement da  doxygen 1.4.3