From 9e9cdf5f1312a1d80b2809e68c6ce7883c0a3d8d Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 10 Feb 2013 21:10:00 +0100 Subject: [PATCH] Removed unused XStreamUtils class; direct access to the xstream field in XStreamMarshaller --- .../oxm/xstream/XStreamMarshaller.java | 52 +++++++-------- .../oxm/xstream/XStreamUtils.java | 63 ------------------- 2 files changed, 24 insertions(+), 91 deletions(-) delete mode 100644 spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamUtils.java diff --git a/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java index b53c20a5b9..dd36d17b8b 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -114,7 +114,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin /** * Returns the XStream instance used by this marshaller. */ - public XStream getXStream() { + public final XStream getXStream() { return this.xstream; } @@ -125,7 +125,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin * @see XStream#NO_REFERENCES */ public void setMode(int mode) { - this.getXStream().setMode(mode); + this.xstream.setMode(mode); } /** @@ -137,10 +137,10 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin public void setConverters(ConverterMatcher[] converters) { for (int i = 0; i < converters.length; i++) { if (converters[i] instanceof Converter) { - this.getXStream().registerConverter((Converter) converters[i], i); + this.xstream.registerConverter((Converter) converters[i], i); } else if (converters[i] instanceof SingleValueConverter) { - this.getXStream().registerConverter((SingleValueConverter) converters[i], i); + this.xstream.registerConverter((SingleValueConverter) converters[i], i); } else { throw new IllegalArgumentException("Invalid ConverterMatcher [" + converters[i] + "]"); @@ -155,9 +155,8 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin */ public void setAliases(Map aliases) throws ClassNotFoundException { Map> classMap = toClassMap(aliases); - for (Map.Entry> entry : classMap.entrySet()) { - this.getXStream().alias(entry.getKey(), entry.getValue()); + this.xstream.alias(entry.getKey(), entry.getValue()); } } @@ -169,15 +168,13 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin */ public void setAliasesByType(Map aliases) throws ClassNotFoundException { Map> classMap = toClassMap(aliases); - for (Map.Entry> entry : classMap.entrySet()) { - this.getXStream().aliasType(entry.getKey(), entry.getValue()); + this.xstream.aliasType(entry.getKey(), entry.getValue()); } } private Map> toClassMap(Map map) throws ClassNotFoundException { Map> result = new LinkedHashMap>(map.size()); - for (Map.Entry entry : map.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); @@ -198,10 +195,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin } /** - * Sets a field alias/type map, consiting of field names - * @param aliases - * @throws ClassNotFoundException - * @throws NoSuchFieldException + * Set a field alias/type map, consiting of field names. * @see XStream#aliasField(String, Class, String) */ public void setFieldAliases(Map aliases) throws ClassNotFoundException, NoSuchFieldException { @@ -213,8 +207,9 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin String className = field.substring(0, idx); Class clazz = ClassUtils.forName(className, classLoader); String fieldName = field.substring(idx + 1); - this.getXStream().aliasField(alias, clazz, fieldName); - } else { + this.xstream.aliasField(alias, clazz, fieldName); + } + else { throw new IllegalArgumentException("Field name [" + field + "] does not contain '.'"); } } @@ -226,7 +221,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin */ public void setUseAttributeForTypes(Class[] types) { for (Class type : types) { - this.getXStream().useAttributeFor(type); + this.xstream.useAttributeFor(type); } } @@ -242,7 +237,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin for (Map.Entry entry : attributes.entrySet()) { if (entry.getKey() instanceof String) { if (entry.getValue() instanceof Class) { - this.getXStream().useAttributeFor((String) entry.getKey(), (Class) entry.getValue()); + this.xstream.useAttributeFor((String) entry.getKey(), (Class) entry.getValue()); } else { throw new IllegalArgumentException( @@ -253,14 +248,14 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin else if (entry.getKey() instanceof Class) { Class key = (Class) entry.getKey(); if (entry.getValue() instanceof String) { - this.getXStream().useAttributeFor(key, (String) entry.getValue()); + this.xstream.useAttributeFor(key, (String) entry.getValue()); } else if (entry.getValue() instanceof List) { List list = (List) entry.getValue(); for (Object o : list) { if (o instanceof String) { - this.getXStream().useAttributeFor(key, (String) o); + this.xstream.useAttributeFor(key, (String) o); } } } @@ -286,7 +281,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin for (Map.Entry, String> entry : implicitCollections.entrySet()) { String[] collectionFields = StringUtils.commaDelimitedListToStringArray(entry.getValue()); for (String collectionField : collectionFields) { - this.getXStream().addImplicitCollection(entry.getKey(), collectionField); + this.xstream.addImplicitCollection(entry.getKey(), collectionField); } } } @@ -300,7 +295,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin for (Map.Entry, String> entry : omittedFields.entrySet()) { String[] fields = StringUtils.commaDelimitedListToStringArray(entry.getValue()); for (String field : fields) { - this.getXStream().omitField(entry.getKey(), field); + this.xstream.omitField(entry.getKey(), field); } } } @@ -311,7 +306,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin */ public void setAnnotatedClass(Class annotatedClass) { Assert.notNull(annotatedClass, "'annotatedClass' must not be null"); - this.getXStream().processAnnotations(annotatedClass); + this.xstream.processAnnotations(annotatedClass); } /** @@ -320,7 +315,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin */ public void setAnnotatedClasses(Class[] annotatedClasses) { Assert.notEmpty(annotatedClasses, "'annotatedClasses' must not be empty"); - this.getXStream().processAnnotations(annotatedClasses); + this.xstream.processAnnotations(annotatedClasses); } /** @@ -330,7 +325,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin * @see XStream#autodetectAnnotations(boolean) */ public void setAutodetectAnnotations(boolean autodetectAnnotations) { - this.getXStream().autodetectAnnotations(autodetectAnnotations); + this.xstream.autodetectAnnotations(autodetectAnnotations); } /** @@ -363,7 +358,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin public final void afterPropertiesSet() throws Exception { - customizeXStream(getXStream()); + customizeXStream(this.xstream); } /** @@ -458,7 +453,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin */ private void marshal(Object graph, HierarchicalStreamWriter streamWriter) { try { - getXStream().marshal(graph, streamWriter); + this.xstream.marshal(graph, streamWriter); } catch (Exception ex) { throw convertXStreamException(ex, true); @@ -541,7 +536,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin */ private Object unmarshal(HierarchicalStreamReader streamReader) { try { - return getXStream().unmarshal(streamReader); + return this.xstream.unmarshal(streamReader); } catch (Exception ex) { throw convertXStreamException(ex, false); @@ -574,4 +569,5 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin return new UncategorizedMappingException("Unknown XStream exception", ex); } } + } diff --git a/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamUtils.java b/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamUtils.java deleted file mode 100644 index 04f53b6973..0000000000 --- a/spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamUtils.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2002-2012 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.oxm.xstream; - -import com.thoughtworks.xstream.converters.ConversionException; -import com.thoughtworks.xstream.io.StreamException; -import com.thoughtworks.xstream.mapper.CannotResolveClassException; - -import org.springframework.oxm.MarshallingFailureException; -import org.springframework.oxm.UncategorizedMappingException; -import org.springframework.oxm.UnmarshallingFailureException; -import org.springframework.oxm.XmlMappingException; - -/** - * Generic utility methods for working with XStream. Mainly for internal use within the framework. - * - * @author Arjen Poutsma - * @author Juergen Hoeller - * @since 3.0 - */ -abstract class XStreamUtils { - - /** - * Convert the given XStream exception to an appropriate exception from the - * {@code org.springframework.oxm} hierarchy. - *

A boolean flag is used to indicate whether this exception occurs during marshalling or - * unmarshalling, since XStream itself does not make this distinction in its exception hierarchy. - * @param ex XStream exception that occured - * @param marshalling indicates whether the exception occurs during marshalling ({@code true}), - * or unmarshalling ({@code false}) - * @return the corresponding {@code XmlMappingException} - */ - public static XmlMappingException convertXStreamException(Exception ex, boolean marshalling) { - if (ex instanceof StreamException || ex instanceof CannotResolveClassException || - ex instanceof ConversionException) { - if (marshalling) { - return new MarshallingFailureException("XStream marshalling exception", ex); - } - else { - return new UnmarshallingFailureException("XStream unmarshalling exception", ex); - } - } - else { - // fallback - return new UncategorizedMappingException("Unknown XStream exception", ex); - } - } - -}