01: /*
02: * Copyright 2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.springframework.oxm.xmlbeans;
17:
18: import org.apache.xmlbeans.XMLStreamValidationException;
19: import org.apache.xmlbeans.XmlException;
20: import org.springframework.oxm.XmlMappingException;
21: import org.xml.sax.SAXException;
22:
23: /**
24: * Generic utility methods for working with XMLBeans. Mainly for internal use within the framework.
25: *
26: * @author Arjen Poutsma
27: * @since 1.0.0
28: */
29: public class XmlBeansUtils {
30:
31: /**
32: * Converts the given XMLBeans exception to an appropriate exception from the <code>org.springframework.oxm</code>
33: * hierarchy.
34: * <p/>
35: * A boolean flag is used to indicate whether this exception occurs during marshalling or unmarshalling, since
36: * XMLBeans itself does not make this distinction in its exception hierarchy.
37: *
38: * @param ex XMLBeans Exception that occured
39: * @param marshalling indicates whether the exception occurs during marshalling (<code>true</code>), or
40: * unmarshalling (<code>false</code>)
41: * @return the corresponding <code>XmlMappingException</code>
42: */
43: public static XmlMappingException convertXmlBeansException(
44: Exception ex, boolean marshalling) {
45: if (ex instanceof XMLStreamValidationException) {
46: return new XmlBeansValidationFailureException(
47: (XMLStreamValidationException) ex);
48: } else if (ex instanceof XmlException) {
49: XmlException xmlException = (XmlException) ex;
50: if (marshalling) {
51: return new XmlBeansMarshallingFailureException(
52: xmlException);
53: } else {
54: return new XmlBeansUnmarshallingFailureException(
55: xmlException);
56: }
57: } else if (ex instanceof SAXException) {
58: SAXException saxException = (SAXException) ex;
59: if (marshalling) {
60: return new XmlBeansMarshallingFailureException(
61: saxException);
62: } else {
63: return new XmlBeansUnmarshallingFailureException(
64: saxException);
65: }
66: }
67: // fallback
68: return new XmlBeansSystemException(ex);
69: }
70:
71: }
|