Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You 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.apache.camel.dataformat.xstream;
 
 import java.util.List;
 import java.util.Map;
 
 
 
An abstract class which implement data format (org.apache.camel.spi.DataFormat) interface which leverage the XStream library for XML or JSON's marshaling and unmarshaling

Version:
 
 public abstract class AbstractXStreamWrapper implements DataFormat {
     
     private XStream xstream;
     private StaxConverter staxConverter;
     private List<Stringconverters;
     private Map<StringStringaliases;
     private Map<StringString[]> omitFields;
     private Map<StringString[]> implicitCollections;
 
     public AbstractXStreamWrapper() {
     }
     
     public AbstractXStreamWrapper(XStream xstream) {
         this. = xstream;
     }
     
     public XStream getXStream(ClassResolver resolver) {
         if ( == null) {
              = createXStream(resolver);
         }
         return ;
     }
 
     public void setXStream(XStream xstream) {
         this. = xstream;
     }
 
     protected XStream createXStream(ClassResolver resolver) {
         if ( != null) {
              = new XStream();
         } else {
              = new XStream();
         }
 
         try {
             if (this. != null) {
                 for (Entry<StringString[]> entry : this..entrySet()) {
                     for (String name : entry.getValue()) {
                         .addImplicitCollection(resolver.resolveMandatoryClass(entry.getKey()), name);
                     }
                 }
             }
 
             if (this. != null) {
                 for (Entry<StringStringentry : this..entrySet()) {
                     .alias(entry.getKey(), resolver.resolveMandatoryClass(entry.getValue()));
                     // It can turn the auto-detection mode off
                     .processAnnotations(resolver.resolveMandatoryClass(entry.getValue()));
                 }
             }
 
             if (this. != null) {
                for (Entry<StringString[]> entry : this..entrySet()) {
                    for (String name : entry.getValue()) {
                        .omitField(resolver.resolveMandatoryClass(entry.getKey()), name);
                    }
                }
            }
            if (this. != null) {
                for (String name : this.) {
                    Class<ConverterconverterClass = resolver.resolveMandatoryClass(nameConverter.class);
                    Converter converter;
                    Constructor<Convertercon = null;
                    try {
                        con = converterClass.getDeclaredConstructor(new Class[] {XStream.class});
                    } catch (Exception e) {
                         //swallow as we null check in a moment.
                    }
                    if (con != null) {
                        converter = con.newInstance();
                    } else {
                        converter = converterClass.newInstance();
                        try { 
                            Method method = converterClass.getMethod("setXStream"new Class[] {XStream.class});
                            if (method != null) {
                                ObjectHelper.invokeMethod(methodconverter);
                            }
                        } catch (Throwable e) {
                            // swallow, as it just means the user never add an XStream setter, which is optional
                        }
                    }
                    .registerConverter(converter);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("Unable to build XStream instance"e);
        }
        return ;
    }    
    public StaxConverter getStaxConverter() {
        if ( == null) {
             = new StaxConverter();
        }
        return ;
    }
    public void setStaxConverter(StaxConverter staxConverter) {
        this. = staxConverter;
    }
    public List<StringgetConverters() {
        return ;
    }
    public void setConverters(List<Stringconverters) {
        this. = converters;
    }
    public Map<StringStringgetAliases() {
        return ;
    }
    public void setAliases(Map<StringStringaliases) {
        this. = aliases;
    }
    public Map<StringString[]> getImplicitCollections() {
        return ;
    }
    public void setImplicitCollections(Map<StringString[]> implicitCollections) {
        this. = implicitCollections;
    }
    public Map<StringString[]> getOmitFields() {
        return ;
    }
    public void setOmitFields(Map<StringString[]> omitFields) {
        this. = omitFields;
    }
        return ;
    }
    public void setXstreamDriver(HierarchicalStreamDriver xstreamDriver) {
        this. = xstreamDriver;
    }
    public XStream getXstream() {
        return ;
    }
    public void setXstream(XStream xstream) {
        this. = xstream;
    }
    public void marshal(Exchange exchangeObject bodyOutputStream streamthrows Exception {
        HierarchicalStreamWriter writer = createHierarchicalStreamWriter(exchangebodystream);
        try {
            getXStream(exchange.getContext().getClassResolver()).marshal(bodywriter);
        } finally {
            writer.close();
        }
    }
    public Object unmarshal(Exchange exchangeInputStream streamthrows Exception {
        HierarchicalStreamReader reader = createHierarchicalStreamReader(exchangestream);
        try {
            return getXStream(exchange.getContext().getClassResolver()).unmarshal(reader);
        } finally {
            reader.close();
        }
    }
            Exchange exchangeObject bodyOutputStream streamthrows XMLStreamException;
            Exchange exchangeInputStream streamthrows XMLStreamException;
New to GrepCode? Check out our FAQ X