Browse Source

Removed unused XStreamUtils class; direct access to the xstream field in XStreamMarshaller

pull/230/head
Juergen Hoeller 12 years ago
parent
commit
9e9cdf5f13
  1. 52
      spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java
  2. 63
      spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamUtils.java

52
spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamMarshaller.java

@ -1,5 +1,5 @@ @@ -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 @@ -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 @@ -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 @@ -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 @@ -155,9 +155,8 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
*/
public void setAliases(Map<String, ?> aliases) throws ClassNotFoundException {
Map<String, Class<?>> classMap = toClassMap(aliases);
for (Map.Entry<String, Class<?>> 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 @@ -169,15 +168,13 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
*/
public void setAliasesByType(Map<String, ?> aliases) throws ClassNotFoundException {
Map<String, Class<?>> classMap = toClassMap(aliases);
for (Map.Entry<String, Class<?>> entry : classMap.entrySet()) {
this.getXStream().aliasType(entry.getKey(), entry.getValue());
this.xstream.aliasType(entry.getKey(), entry.getValue());
}
}
private Map<String, Class<?>> toClassMap(Map<String, ?> map) throws ClassNotFoundException {
Map<String, Class<?>> result = new LinkedHashMap<String, Class<?>>(map.size());
for (Map.Entry<String, ?> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
@ -198,10 +195,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin @@ -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<String, String> aliases) throws ClassNotFoundException, NoSuchFieldException {
@ -213,8 +207,9 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin @@ -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 @@ -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 @@ -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 @@ -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 @@ -286,7 +281,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
for (Map.Entry<Class<?>, 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 @@ -300,7 +295,7 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
for (Map.Entry<Class<?>, 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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -574,4 +569,5 @@ public class XStreamMarshaller extends AbstractMarshaller implements Initializin
return new UncategorizedMappingException("Unknown XStream exception", ex);
}
}
}

63
spring-oxm/src/main/java/org/springframework/oxm/xstream/XStreamUtils.java

@ -1,63 +0,0 @@ @@ -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.
* <p>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);
}
}
}
Loading…
Cancel
Save