Index: src/main/java/org/castor/jaxb/JAXBContext.java =================================================================== --- src/main/java/org/castor/jaxb/JAXBContext.java (revision 7948) +++ src/main/java/org/castor/jaxb/JAXBContext.java (revision ) @@ -55,16 +55,23 @@ * @version $Id$ */ public final class JAXBContext extends javax.xml.bind.JAXBContext { - /** Logger to use. */ + + /** + * Logger to use. + */ private static final Log LOG = LogFactory.getLog(JAXBContext.class); - /** The Castor XML context. */ + + /** + * The Castor XML context. + */ private XMLContext _xmlContext; /** - * A private constructor so no one outside can instantiate this class. + * Creates new instance of {@link JAXBContext} class. + *

+ * Private constructor prevents from instantiation outside this class. */ private JAXBContext() { - super(); LOG.debug("Creating JAXBContext"); _xmlContext = new XMLContext(); @@ -111,7 +118,7 @@ } /** - * Creating a new instance of JAXBContext for a specific Java package. Be + * Creates a new instance of JAXBContext for a specific Java package. Be * aware that this kind of instance requires that this package was before * generated by schema compiler or is at least similar to the output of * schema compiler. A file jaxb.index has to exist. @@ -137,7 +144,7 @@ } /** - * Creating a new instance of JAXBContext for a specific Java package. Be + * Creates a new instance of JAXBContext for a specific Java package. Be * aware that this kind of instance requires that this package was before * generated by schema compiler or is at least similar to the output of * schema compiler. A file jaxb.index has to exist. @@ -167,7 +174,7 @@ } /** - * Creating a new instance of JAXBContext for a specific Java package. Be + * Creates a new instance of JAXBContext for a specific Java package. Be * aware that this kind of instance requires that this package was before * generated by schema compiler or is at least similar to the output of * schema compiler. A file jaxb.index has to exist. @@ -180,7 +187,6 @@ * the properties to use * @return the JAXBContext initialized for the given package * @throws JAXBException - * @throws JAXBException * creating the new context failed */ public static javax.xml.bind.JAXBContext newInstance( @@ -201,7 +207,7 @@ } /** - * Creating a new instance of JAXBContext for a specific list of classes. + * Creates a new instance of JAXBContext for a specific list of classes. * The given classes will be checked for JAXB specific annotations and the * mapping information found will be available for subsequent * (un-)marshalling actions. @@ -236,7 +242,7 @@ } /** - * A new instance of JAXBContext initialized with an array of classes and a + * Creates a new instance of JAXBContext initialized with an array of classes and a * Map of properties. * * @param classesToBeBound @@ -273,7 +279,7 @@ } /** - * To set the class loader to use with this context instance. + * Sets the class loader to use with this context instance. * * @param contextPathCL * the class loader to use @@ -346,8 +352,12 @@ * a Map of properties */ private void setProperties(final Map properties) { - // _xmlContext.setProperties(properties); + + // TODO does JAXB has it own properties that will need to be map by castor? + for(Map.Entry property : properties.entrySet()) { + _xmlContext.setProperty(property.getKey(), property.getValue()); - } + } + } /** * {@inheritDoc} @@ -356,12 +366,11 @@ */ @Override public Marshaller createMarshaller() throws JAXBException { - org.exolab.castor.xml.Marshaller castorMarshaller = _xmlContext - .createMarshaller(); - org.castor.jaxb.Marshaller m = new org.castor.jaxb.Marshaller( - castorMarshaller); - m.setInternalContext(_xmlContext.getInternalContext()); - return m; + + org.castor.jaxb.Marshaller marshaller = new org.castor.jaxb.Marshaller(_xmlContext + .createMarshaller()); + marshaller.setInternalContext(_xmlContext.getInternalContext()); + return marshaller; } /** @@ -371,12 +380,11 @@ */ @Override public Unmarshaller createUnmarshaller() throws JAXBException { - org.exolab.castor.xml.Unmarshaller castorUnmarshaller = _xmlContext - .createUnmarshaller(); - org.castor.jaxb.Unmarshaller u = new org.castor.jaxb.Unmarshaller( - castorUnmarshaller); - u.setInternalContext(_xmlContext.getInternalContext()); - return u; + + org.castor.jaxb.Unmarshaller unmarshaller = new org.castor.jaxb.Unmarshaller(_xmlContext + .createUnmarshaller()); + unmarshaller.setInternalContext(_xmlContext.getInternalContext()); + return unmarshaller; } /** @@ -386,6 +394,7 @@ */ @Override public Validator createValidator() throws JAXBException { + // TODO implement throw new UnsupportedOperationException(); } @@ -395,9 +404,9 @@ * @see javax.xml.bind.JAXBContext#createJAXBIntrospector() */ public JAXBIntrospector createJAXBIntrospector() { - org.castor.jaxb.JAXBIntrospector ji = new org.castor.jaxb.JAXBIntrospector(); - // ji.setClassDescriptorResolver(_classDescriptorResolver); - return ji; + org.castor.jaxb.JAXBIntrospector introspector = new org.castor.jaxb.JAXBIntrospector(); + introspector.setClassDescriptorResolver(_xmlContext.getInternalContext().getXMLClassDescriptorResolver()); + return introspector; } /** @@ -406,6 +415,7 @@ * @see javax.xml.bind.JAXBContext#createBinder(java.lang.Class) */ public Binder createBinder(final Class domType) { + // TODO implement throw new UnsupportedOperationException(); } @@ -415,6 +425,7 @@ * @see javax.xml.bind.JAXBContext#createBinder() */ public Binder createBinder() { + // TODO implement throw new UnsupportedOperationException(); } @@ -424,7 +435,7 @@ * @see javax.xml.bind.JAXBContext#generateSchema(javax.xml.bind.SchemaOutputResolver) */ public void generateSchema(final SchemaOutputResolver schemaOutputResolver) { + // TODO implement throw new UnsupportedOperationException(); } - } Index: src/main/java/org/castor/jaxb/Unmarshaller.java =================================================================== --- src/main/java/org/castor/jaxb/Unmarshaller.java (revision 7441) +++ src/main/java/org/castor/jaxb/Unmarshaller.java (revision ) @@ -30,6 +30,7 @@ import javax.xml.bind.ValidationEventHandler; import javax.xml.bind.annotation.adapters.XmlAdapter; import javax.xml.bind.attachment.AttachmentUnmarshaller; +import javax.xml.namespace.QName; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLStreamReader; import javax.xml.transform.Source; @@ -56,25 +57,43 @@ * @version $Id$ */ public class Unmarshaller implements javax.xml.bind.Unmarshaller { - /** Logger to use. */ + + /** + * Logger to use. + */ private static final Log LOG = LogFactory.getLog(Unmarshaller.class); - /** The Castor unmarshaller doing the work ;-) . */ + + /** + * The Castor unmarshaller doing the work. + */ private org.exolab.castor.xml.Unmarshaller _castorUnmarshaller; - /** UnmarshalListener to use. */ + + /** + * UnmarshalListener to use. + */ private UnmarshalListenerAdapter _unmarshalListener = null; - /** The Castor XML context to use. */ + + /** + * The Castor XML context to use. + */ private InternalContext _internalContext; /** - * The Unmarshaller is meant to be instantiated by JAXBContext only! + * Creates new instance of {@link Unmarshaller}. + *

+ * Constructor with package scope prevents from instantiation outside this component. + * * @param castorUnmarshaller the Castor Unmarshaller to use + * + * @see JAXBContext#createUnmarshaller() */ - protected Unmarshaller(final org.exolab.castor.xml.Unmarshaller castorUnmarshaller) { + Unmarshaller(final org.exolab.castor.xml.Unmarshaller castorUnmarshaller) { _castorUnmarshaller = castorUnmarshaller; } /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#getAdapter(java.lang.Class) */ public < A extends XmlAdapter > A getAdapter(final Class < A > arg0) { @@ -83,6 +102,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#getAttachmentUnmarshaller() */ public AttachmentUnmarshaller getAttachmentUnmarshaller() { @@ -91,6 +111,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#getEventHandler() */ public ValidationEventHandler getEventHandler() throws JAXBException { @@ -99,6 +120,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#getListener() */ public Listener getListener() { @@ -107,6 +129,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#getProperty(java.lang.String) */ public Object getProperty(final String property) throws PropertyException { @@ -115,6 +138,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#getSchema() */ public Schema getSchema() { @@ -123,6 +147,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#getUnmarshallerHandler() */ public UnmarshallerHandler getUnmarshallerHandler() { @@ -131,6 +156,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#isValidating() */ public boolean isValidating() throws JAXBException { @@ -139,6 +165,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#setAdapter(javax.xml.bind.annotation.adapters.XmlAdapter) */ public void setAdapter(final XmlAdapter xmlAdapter) { @@ -147,6 +174,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#setAdapter(java.lang.Class, javax.xml.bind.annotation.adapters.XmlAdapter) */ public < A extends XmlAdapter > void setAdapter(final Class < A > type, final A xmlAdapter) { @@ -155,6 +183,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#setAttachmentUnmarshaller(javax.xml.bind.attachment.AttachmentUnmarshaller) */ public void setAttachmentUnmarshaller(final AttachmentUnmarshaller attachmentUnmarshaller) { @@ -163,6 +192,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#setEventHandler(javax.xml.bind.ValidationEventHandler) */ public void setEventHandler(final ValidationEventHandler validationEventHandler) @@ -172,6 +202,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#setListener(javax.xml.bind.Unmarshaller.Listener) */ public void setListener(final Listener listener) { @@ -184,6 +215,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#setProperty(java.lang.String, java.lang.Object) */ public void setProperty(final String property, final Object value) throws PropertyException { @@ -192,6 +224,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#setSchema(javax.xml.validation.Schema) */ public void setSchema(final Schema schema) { @@ -200,6 +233,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#setValidating(boolean) */ public void setValidating(final boolean validate) throws JAXBException { @@ -212,18 +246,20 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(java.io.File) */ public Object unmarshal(final File file) throws JAXBException { try { return unmarshal(new InputSource(new FileInputStream(file))); } catch (FileNotFoundException e) { - throw new JAXBException("Problem unmarshalling from file " + file.getAbsolutePath(), e); + throw Utils.convertToJAXBException(LOG, "Problem unmarshalling from file " + file.getAbsolutePath(), e); } } /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(java.io.InputStream) */ public Object unmarshal(final InputStream inputStream) throws JAXBException { @@ -232,6 +268,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(java.io.Reader) */ public Object unmarshal(final Reader reader) throws JAXBException { @@ -240,130 +277,160 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(java.net.URL) */ public Object unmarshal(final URL url) throws JAXBException { try { return unmarshal(new InputSource(url.openStream())); } catch (IOException e) { - throw new JAXBException("Problem unmarshalling from URL " + url.toExternalForm(), e); + throw Utils.convertToJAXBException(LOG, "Problem unmarshalling from URL " + url.toExternalForm(), e); } } /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(org.xml.sax.InputSource) */ public Object unmarshal(final InputSource inputSource) throws JAXBException { try { return _castorUnmarshaller.unmarshal(inputSource); } catch (MarshalException e) { - String message = "Problem unmarshalling from InputSource " - + inputSource.toString() + " exception: " + e; - LOG.warn(message, e); - throw new JAXBException(message, e); + throw Utils.convertToJAXBException(LOG, "Problem occurred when unmarshalling from InputSource.", e); } catch (ValidationException e) { - String message = "Problem unmarshalling from InputSource " - + inputSource.toString() + " exception: " + e; - LOG.warn(message, e); - throw new JAXBException(message, e); + throw Utils.convertToJAXBException(LOG, "Problem occurred when unmarshalling from InputSource.", e); } } /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(org.w3c.dom.Node) */ public Object unmarshal(final Node node) throws JAXBException { try { return _castorUnmarshaller.unmarshal(node); } catch (MarshalException e) { - String message = "Problem unmarshalling from Node " - + node.toString() + " exception: " + e; - LOG.warn(message); - throw new JAXBException(message, e); + throw Utils.convertToJAXBException(LOG, "Problem occurred when unmarshalling from Node.", e); } catch (ValidationException e) { - String message = "Problem unmarshalling from Node " - + node.toString() + " exception: " + e; - LOG.warn(message); - throw new JAXBException(message, e); + throw Utils.convertToJAXBException(LOG, "Problem occurred when unmarshalling from Node.", e); } } /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(javax.xml.transform.Source) */ public Object unmarshal(final Source source) throws JAXBException { + // TODO starting from the Castor 1.3.3 this can be replaced if (source instanceof StreamSource) { StreamSource streamSource = (StreamSource) source; + if(streamSource.getReader() != null) { + // delegates to unmarshal method + return unmarshal(streamSource.getReader()); + } else if(streamSource.getInputStream() != null) { + // delegates to unmarshal method - return unmarshal(streamSource.getInputStream()); + return unmarshal(streamSource.getInputStream()); + } } else if (source instanceof DOMSource) { DOMSource domSource = (DOMSource) source; + if(domSource.getNode() != null) { + // delegates to unmarshal method - return unmarshal(domSource.getNode()); + return unmarshal(domSource.getNode()); + } } else if (source instanceof SAXSource) { SAXSource saxSource = (SAXSource) source; + if(saxSource.getInputSource() != null) { + // delegates to unmarshal method - return unmarshal(saxSource.getInputSource()); + return unmarshal(saxSource.getInputSource()); - } else { - throw new UnsupportedOperationException(); - } - } + } + } + throw new JAXBException("Could not unmarshall the passed Source object."); + } + /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(javax.xml.stream.XMLStreamReader) */ public Object unmarshal(final XMLStreamReader xmlStreamReader) throws JAXBException { - throw new UnsupportedOperationException(); + try { + return _castorUnmarshaller.unmarshal(xmlStreamReader); + } catch (MarshalException e) { + throw Utils.convertToJAXBException(LOG, "Error occurred when unmarshalling from XMLStreamReader.", e); + } catch (ValidationException e) { + throw Utils.convertToJAXBException(LOG, "Error occurred when unmarshalling from XMLStreamReader.", e); - } + } + } /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(javax.xml.stream.XMLEventReader) */ public Object unmarshal(final XMLEventReader xmlEventReader) throws JAXBException { - throw new UnsupportedOperationException(); + try { + return _castorUnmarshaller.unmarshal(xmlEventReader); + } catch (MarshalException e) { + throw Utils.convertToJAXBException(LOG, "Error occurred when unmarshalling from XMLEventReader.", e); + } catch (ValidationException e) { + throw Utils.convertToJAXBException(LOG, "Error occurred when unmarshalling from XMLEventReader.", e); - } + } + } /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(org.w3c.dom.Node, java.lang.Class) */ + @SuppressWarnings("unchecked") - public < T > JAXBElement < T > unmarshal( + public JAXBElement unmarshal( - final Node node, final Class < T > type) throws JAXBException { + final Node node, final Class type) throws JAXBException { - throw new UnsupportedOperationException(); + + return createJAXBElement(type, (T) unmarshal(node)); } /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(javax.xml.transform.Source, java.lang.Class) */ + @SuppressWarnings("unchecked") public < T > JAXBElement < T > unmarshal( - final Source node, final Class < T > type) throws JAXBException { - throw new UnsupportedOperationException(); + final Source source, final Class < T > type) throws JAXBException { + + return createJAXBElement(type, (T) unmarshal(source)); } /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(javax.xml.stream.XMLStreamReader, java.lang.Class) */ + @SuppressWarnings("unchecked") public < T > JAXBElement < T > unmarshal( final XMLStreamReader xmlStreamReader, final Class < T > type) throws JAXBException { - throw new UnsupportedOperationException(); + + return createJAXBElement(type, (T) unmarshal(xmlStreamReader)); } /** * {@inheritDoc} + * * @see javax.xml.bind.Unmarshaller#unmarshal(javax.xml.stream.XMLEventReader, java.lang.Class) */ + @SuppressWarnings("unchecked") public < T > JAXBElement < T > unmarshal( - final XMLEventReader xmlEventReader, final Class < T > type) - throws JAXBException { - throw new UnsupportedOperationException(); + final XMLEventReader xmlEventReader, final Class < T > type) throws JAXBException { + + return createJAXBElement(type, (T) unmarshal(xmlEventReader)); } /** - * To set the Castor XML context to use. + * Sets the Castor XML context to use. * @param internalContext the Castor XML Context to use */ protected void setInternalContext(final InternalContext internalContext) { @@ -371,4 +438,17 @@ _castorUnmarshaller.setInternalContext(_internalContext); } + /** + * Creates a instance of {@link JAXBElement} that wraps the passed object.

+ * + * @param clazz the class of the wrapped object + * @param obj the object to wrap + * @param the type of the wrapped object + * @return the newly created instance of {@link JAXBElement} that wrapps the passed object + */ + private JAXBElement createJAXBElement(Class clazz, T obj) { + // TODO currently it is impossible to enquire the name of the unmarshalled object + // this will be probably possible with help of JAXBIntrospector + return new JAXBElement(new QName(""), clazz , obj); -} + } +} Index: src/main/java/org/castor/jaxb/JAXBIntrospector.java =================================================================== --- src/main/java/org/castor/jaxb/JAXBIntrospector.java (revision 7345) +++ src/main/java/org/castor/jaxb/JAXBIntrospector.java (revision ) @@ -32,45 +32,58 @@ * @version $Id$ */ public class JAXBIntrospector extends javax.xml.bind.JAXBIntrospector { - /** Logger instance to use. */ + + /** + * Logger instance to use. + */ private static final Log LOG = LogFactory.getLog(JAXBIntrospector.class); + /** - * A link to the Castor internal context to look for - * known descriptors. + * A instance of the Castor internal context to look for known descriptors. */ private InternalContext _internalContext; + /** + * A instance of {@link XMLClassDescriptorResolver} used to lookup the class descriptors. + *

* The class descriptor resolver is required to either read * the descriptor form the cache or create a new descriptor. */ private XMLClassDescriptorResolver _classDescriptorResolver; /** - * JAXBIntrospector is only allowed to be instantiated through - * JAXBContext! + * Creates new instance of {@link JAXBIntrospector}. + *

+ * Constructor with package scope prevents from instantiation outside this component. + * + * @see JAXBContext#createJAXBIntrospector() */ - protected JAXBIntrospector() { - super(); + JAXBIntrospector() { + // empty constructor } /** - * To set the InternalContext. - * @param internalContext the InternalContext + * Sets the {@link InternalContext} instance to be used by this class. + * + * @param internalContext the {@link InternalContext} to be used by this class */ protected void setInternalContext(final InternalContext internalContext) { + // TODO this setter is never used _internalContext = internalContext; } /** - * To set the ClassDescriptorResolver. - * @param classDescriptorResolver the ClassdescriptorResolver + * Sets the {@link XMLClassDescriptorResolver} to be used by this class. + * + * @param classDescriptorResolver the {@link XMLClassDescriptorResolver} to be used by this class */ - protected void setClassDescriptorResolver( - final XMLClassDescriptorResolver classDescriptorResolver) { + protected void setClassDescriptorResolver(final XMLClassDescriptorResolver classDescriptorResolver) { _classDescriptorResolver = classDescriptorResolver; } /** + * Gets the xml element fully qualified name for the given object. + * * @param obj the object to get the element name for * @return the element name (String) for the obj * @see javax.xml.bind.JAXBIntrospector#getElementName(java.lang.Object) @@ -84,12 +97,15 @@ try { cd = (XMLClassDescriptor) _classDescriptorResolver.resolve(obj.getClass()); } catch (ResolverException e) { - LOG.info("Ingoring exception: " + e + " at resolve of: " + obj.getClass().getName()); + LOG.error(String.format("Exception occurred in JAXBIntrospector#getElementName when resolving %s object.", + obj.getClass().getName()), e); } return new QName(cd.getNameSpaceURI(), cd.getXMLName(), cd.getNameSpacePrefix()); } /** + * Returns whether the given object is a known element. + * * @param obj the object to check if it is a known element * @return true if obj is a known element * @see javax.xml.bind.JAXBIntrospector#isElement(java.lang.Object) @@ -103,7 +119,8 @@ try { cd = _classDescriptorResolver.resolve(obj.getClass().getName()); } catch (ResolverException e) { - LOG.info("Ingoring exception: " + e + " at resolve of: " + obj.getClass().getName()); + LOG.error(String.format("Exception occurred in JAXBIntrospector#isElement when resolving %s object.", + obj.getClass().getName()), e); } return (cd != null); } Index: src/main/java/org/castor/jaxb/Marshaller.java =================================================================== --- src/main/java/org/castor/jaxb/Marshaller.java (revision 7441) +++ src/main/java/org/castor/jaxb/Marshaller.java (revision ) @@ -56,7 +56,10 @@ * @version $Id$ */ public class Marshaller implements javax.xml.bind.Marshaller { - /** Logger to use. */ + + /** + * Logger to use. + */ private static final Log LOG = LogFactory.getLog(Marshaller.class); /** @@ -91,15 +94,21 @@ private InternalContext _internalContext; /** - * Only JAXBCopntext is allowed to instantiate Marshaller. + * Creates new instance of {@link JAXBContext}. + *

+ * Constructor with package scope prevents from instantiation outside this component. + * * @param castorMarshaller the Castor Marshaller to use underneath + * + * @see JAXBContext#createMarshaller() */ - protected Marshaller(final org.exolab.castor.xml.Marshaller castorMarshaller) { + Marshaller(final org.exolab.castor.xml.Marshaller castorMarshaller) { _castorMarshaller = castorMarshaller; } /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#getAdapter(java.lang.Class) */ public < A extends XmlAdapter > A getAdapter(final Class < A > xmlAdapter) { @@ -108,6 +117,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#getAttachmentMarshaller() */ public AttachmentMarshaller getAttachmentMarshaller() { @@ -116,6 +126,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#getEventHandler() */ public ValidationEventHandler getEventHandler() throws JAXBException { @@ -123,7 +134,7 @@ } /** - * Returns the listener used... if no listener was previously set null + * Returns the listener, if no listener was previously set null * is returned. * @return the listner set or null * @see javax.xml.bind.Marshaller#getListener() @@ -134,6 +145,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#getNode(java.lang.Object) */ public Node getNode(final Object node) throws JAXBException { @@ -143,6 +155,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#getProperty(java.lang.String) */ public Object getProperty(final String propertyName) throws PropertyException { @@ -151,6 +164,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#getSchema() */ public Schema getSchema() { @@ -159,90 +173,89 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#marshal(java.lang.Object, javax.xml.transform.Result) */ public void marshal( - final Object object, final Result result) - throws JAXBException { - if (result == null) { - String message = "Argument Result must not be null!"; - LOG.warn(message + " Throwing IllegalArgumentException."); - throw new IllegalArgumentException(message); - } + final Object object, final Result result) throws JAXBException { + // TODO starting from the Castor 1.3.3 this can be replaced + Utils.checkNotNull(LOG, result, "stream"); + if (result instanceof SAXResult) { SAXResult saxResult = (SAXResult) result; + + if(saxResult.getHandler() != null) { - marshal(object, saxResult.getHandler()); + marshal(object, saxResult.getHandler()); + } } else if (result instanceof DOMResult) { DOMResult domResult = (DOMResult) result; + + if(domResult.getNode() != null) { - marshal(object, domResult.getNode()); + marshal(object, domResult.getNode()); + return; + } } else if (result instanceof StreamResult) { StreamResult streamResult = (StreamResult) result; - // TODO: if (getWriter != null) { + + if(streamResult.getWriter() != null) { - marshal(object, streamResult.getWriter()); + marshal(object, streamResult.getWriter()); - // TODO: if (getOutputStream() != null) { - } else { - throw new IllegalArgumentException( - "Illegal Result instance. Not soppurted by Castor"); + return; + } else if(streamResult.getOutputStream() != null) { + marshal(object, streamResult.getWriter()); + return; - } - } + } + } + throw new JAXBException("Could not unmarshall the passed Result object."); + } + /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#marshal(java.lang.Object, java.io.OutputStream) */ public void marshal( - final Object object, final OutputStream stream) - throws JAXBException { - if (stream == null) { - String message = "OutputStream must not be null!"; - LOG.warn(message + " Throwing IllegalArgumentException."); - throw new IllegalArgumentException(message); - } + final Object object, final OutputStream stream) throws JAXBException { + Utils.checkNotNull(LOG, stream, "stream"); + try { _castorMarshaller.setWriter(new OutputStreamWriter(stream)); marshal(object); validation(); } catch (IOException e) { - String message = "Failed to wrap OutputStream into OutputStreamWriter with: " + e; - LOG.warn(message); - throw new JAXBException(message, e); + throw Utils.convertToJAXBException(LOG, + "Error occurred when marshalling object from OutputStream.", e); } } /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#marshal(java.lang.Object, java.io.Writer) */ public void marshal( - final Object object, final Writer writer) - throws JAXBException { - if (writer == null) { - String message = "Writer for marshalling must not be null!"; - LOG.warn(message + " Throwing IllegalArgumentException."); - throw new IllegalArgumentException(message); - } + final Object object, final Writer writer) throws JAXBException { + Utils.checkNotNull(LOG, writer, "writer"); + try { _castorMarshaller.setWriter(writer); marshal(object); validation(); } catch (IOException e) { - throw new JAXBException( - "Problem opening the java.util.Writer instance for marshalling " + object); + throw Utils.convertToJAXBException(LOG, + "Error occurred when marshalling object from java.util.Writter.", e); } } /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#marshal(java.lang.Object, org.xml.sax.ContentHandler) */ public void marshal( - final Object object, final ContentHandler contentHandler) - throws JAXBException { - if (contentHandler == null) { - String message = "ContentHandler for marshalling must not be null!"; - LOG.warn(message + " Throwing IllegalArgumentException."); - throw new IllegalArgumentException(message); - } + final Object object, final ContentHandler contentHandler) throws JAXBException { + Utils.checkNotNull(LOG, contentHandler, "contentHandler"); + _castorMarshaller.setContentHandler(contentHandler); marshal(object); validation(); @@ -250,55 +263,49 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#marshal(java.lang.Object, org.w3c.dom.Node) */ public void marshal( final Object object, - final Node node) - throws JAXBException { - if (node == null) { - String message = "Argument 'node' is null."; - LOG.warn(message + " Throwing IllegalArgumentException."); - throw new IllegalArgumentException(message); - } - ContentHandler contentHandler = new DocumentHandlerAdapter(new SAX2DOMHandler(node)); - _castorMarshaller.setContentHandler(contentHandler); + final Node node) throws JAXBException { + Utils.checkNotNull(LOG, node, "node"); + _castorMarshaller.setContentHandler(new DocumentHandlerAdapter(new SAX2DOMHandler(node))); marshal(object); validation(); } /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#marshal(java.lang.Object, javax.xml.stream.XMLStreamWriter) */ public void marshal( - final Object object, final XMLStreamWriter xmlStreamWriter) - throws JAXBException { + final Object object, final XMLStreamWriter xmlStreamWriter) throws JAXBException { + // TODO Castor 1.3.3 will support this out of the box throw new UnsupportedOperationException(); } /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#marshal(java.lang.Object, javax.xml.stream.XMLEventWriter) */ public void marshal( - final Object object, final XMLEventWriter xmlEventWriter) - throws JAXBException { + final Object object, final XMLEventWriter xmlEventWriter) throws JAXBException { + // TODO Castor 1.3.3 will support this out of the box throw new UnsupportedOperationException(); } /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#marshal(java.lang.Object, java.io.File) */ public void marshal( - final Object object, final File file) - throws JAXBException { - if (file == null) { - String message = "Argument 'file' is null."; - LOG.warn(message + " Throwing IllegalArgumentException."); - throw new IllegalArgumentException(message); - } + final Object object, final File file) throws JAXBException { + Utils.checkNotNull(LOG, file, "file"); + try { _castorMarshaller.setWriter(new FileWriter(file)); marshal(object); @@ -309,7 +316,7 @@ throw new JAXBException(message, e); } } - + /** * A private marshal method to have the marshalling exception handling only once. * @param object the object to marshal @@ -322,13 +329,9 @@ } _castorMarshaller.marshal(object); } catch (MarshalException e) { - String message = "Failed to marshal object: " + object + " with exception: " + e; - LOG.warn(message); - throw new JAXBException(message, e); + throw Utils.convertToJAXBException(LOG, "Error occurred when marshalling object.", e); } catch (ValidationException e) { - String message = "Failed to marshal object: " + object + " with exception: " + e; - LOG.warn(message); - throw new JAXBException(message, e); + throw Utils.convertToJAXBException(LOG, "Error occurred when marshalling object.", e); } } @@ -346,6 +349,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#setAdapter(javax.xml.bind.annotation.adapters.XmlAdapter) */ public void setAdapter(final XmlAdapter arg0) { @@ -354,6 +358,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#setAdapter(java.lang.Class, javax.xml.bind.annotation.adapters.XmlAdapter) */ public < A extends XmlAdapter > void setAdapter( @@ -363,6 +368,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#setAttachmentMarshaller(javax.xml.bind.attachment.AttachmentMarshaller) */ public void setAttachmentMarshaller(final AttachmentMarshaller arg0) { @@ -371,6 +377,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#setEventHandler(javax.xml.bind.ValidationEventHandler) */ public void setEventHandler(final ValidationEventHandler validationEventHandler) @@ -380,6 +387,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#setListener(javax.xml.bind.Marshaller.Listener) */ public void setListener(final Listener listener) { @@ -392,6 +400,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#setProperty(java.lang.String, java.lang.Object) */ public void setProperty(final String property, final Object value) @@ -401,6 +410,7 @@ /** * {@inheritDoc} + * * @see javax.xml.bind.Marshaller#setSchema(javax.xml.validation.Schema) */ public void setSchema(final Schema schema) { @@ -408,11 +418,13 @@ } /** - * To set the Castor XML context to use. + * Sets the Castor XML context to use. + * * @param internalContext the Castor XML Context to use */ protected void setInternalContext(final InternalContext internalContext) { _internalContext = internalContext; + // TODO doesn't seam reason why this should be set once again _castorMarshaller.setInternalContext(_internalContext); } } Index: src/main/java/org/castor/jaxb/Utils.java =================================================================== --- src/main/java/org/castor/jaxb/Utils.java (revision ) +++ src/main/java/org/castor/jaxb/Utils.java (revision ) @@ -0,0 +1,76 @@ +package org.castor.jaxb; + +import org.apache.commons.logging.Log; + +import javax.xml.bind.JAXBException; + +/** + * A utility class that defines helper method uses in this component. + * + * @author Jakub Narloch, jmnarloch AT gmail DOT com + * @version $Id$ + */ +class Utils { + + /** + * Checks if passed parameter is not null, if it is then {@link IllegalArgumentException} is being thrown. + * + * @param log the log to use + * @param param the parameter to check + * @param paramName the parameter name + * + * @throws IllegalArgumentException if param is null + */ + static void checkNotNull(Log log, Object param, String paramName) { + + if (param == null) { + + if (log != null) { + log.error(String.format("Argument '%s' can not be null.", paramName)); + } + + throw new IllegalArgumentException(String.format("Argument '%s' can not be null.", paramName)); + } + } + + /** + * Checks if passed parameter is not null and not empty string, if it is then {@link IllegalArgumentException} is being thrown. + * + * @param log the log to use + * @param param the parameter to check + * @param paramName the parameter name + * + * @throws IllegalArgumentException if param is null or empty + */ + static void checkNotEmpty(Log log, String param, String paramName) { + + if (param.trim().length() == 0) { + + if (log != null) { + log.error(String.format("Argument '%s' can not be empty string.", paramName)); + } + + throw new IllegalArgumentException(String.format("Argument '%s' can not be empty string.", paramName)); + } + } + + /** + * Coverts the passed exception into a new instance of {@link javax.xml.bind.JAXBException}. + * + * @param log the log to use + * @param msg the error message + * @param e the exception which was caused of the error + * + * @return JAXBException newly created {@link JAXBException} which wrapps the passed object + */ + static JAXBException convertToJAXBException(Log log, String msg, Throwable e) { + + // log error message + if (log != null) { + log.error(msg, e); + } + + // returns the newly created exception + return new JAXBException(msg, e); + } +}