01: /*
02: * Copyright 2006 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:
17: package org.springframework.oxm.xstream;
18:
19: import com.thoughtworks.xstream.alias.CannotResolveClassException;
20: import com.thoughtworks.xstream.converters.ConversionException;
21: import com.thoughtworks.xstream.io.StreamException;
22: import org.springframework.oxm.XmlMappingException;
23:
24: /**
25: * Generic utility methods for working with XStream. Mainly for internal use within the framework.
26: *
27: * @author Arjen Poutsma
28: * @since 1.0.0
29: */
30: public abstract class XStreamUtils {
31:
32: /**
33: * Converts the given XStream exception to an appropriate exception from the <code>org.springframework.oxm</code>
34: * hierarchy.
35: * <p/>
36: * A boolean flag is used to indicate whether this exception occurs during marshalling or unmarshalling, since
37: * XStream itself does not make this distinction in its exception hierarchy.
38: *
39: * @param ex XStream exception that occured
40: * @param marshalling indicates whether the exception occurs during marshalling (<code>true</code>), or
41: * unmarshalling (<code>false</code>)
42: * @return the corresponding <code>XmlMappingException</code>
43: */
44: public static XmlMappingException convertXStreamException(
45: Exception ex, boolean marshalling) {
46: if (ex instanceof StreamException) {
47: if (marshalling) {
48: return new XStreamMarshallingFailureException(
49: (StreamException) ex);
50: } else {
51: return new XStreamUnmarshallingFailureException(
52: (StreamException) ex);
53: }
54: } else if (ex instanceof CannotResolveClassException) {
55: if (marshalling) {
56: return new XStreamMarshallingFailureException(
57: (CannotResolveClassException) ex);
58: } else {
59: return new XStreamUnmarshallingFailureException(
60: (CannotResolveClassException) ex);
61: }
62: } else if (ex instanceof ConversionException) {
63: if (marshalling) {
64: return new XStreamMarshallingFailureException(
65: (ConversionException) ex);
66: } else {
67: return new XStreamUnmarshallingFailureException(
68: (ConversionException) ex);
69: }
70: }
71: // fallback
72: return new XStreamSystemException("Unknown XStream exception: "
73: + ex.getMessage(), ex);
74: }
75:
76: }
|