<commit_before>/*
 * Copyright (c) 2010-2012. Axon Framework
 *
 * 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.axonframework.serializer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.MapConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
import org.axonframework.commandhandling.GenericCommandMessage;
import org.axonframework.common.SerializationException;
import org.axonframework.domain.GenericDomainEventMessage;
import org.axonframework.domain.GenericEventMessage;
import org.axonframework.domain.MetaData;
import org.joda.time.DateTime;

import java.lang.reflect.Constructor;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Abstract implementation for XStream based serializers. It provides some helper methods and configuration features
 * independent of the actual format used to marshal to.
 *
 * @author <NAME>
 * @since 2.0
 */
public abstract class AbstractXStreamSerializer implements Serializer {

    private static final Charset DEFAULT_CHARSET_NAME = Charset.forName("UTF-8");

    private final XStream xStream;
    private final Charset charset;
    private final RevisionResolver revisionResolver;
    private final ConverterFactory converterFactory;

    /**
     * Initialize a generic serializer using the UTF-8 character set. The provided XStream instance  is used to perform
     * the serialization.
     * <p/>
     * An {@link AnnotationRevisionResolver} is used to resolve the revision for serialized objects.
     *
     * @param xStream XStream instance to use
     */
    protected AbstractXStreamSerializer(XStream xStream) {
        this(xStream, new AnnotationRevisionResolver());
    }

    /**
     * Initialize a generic serializer using the UTF-8 character set. The provided XStream instance  is used to perform
     * the serialization.
     *
     * @param xStream          XStream instance to use
     * @param revisionResolver The strategy to use to resolve the revision of an object
     */
    protected AbstractXStreamSerializer(XStream xStream, RevisionResolver revisionResolver) {
        this(DEFAULT_CHARSET_NAME, xStream, revisionResolver);
    }

    /**
     * Initialize the serializer using the given <code>charset</code> and <code>xStream</code> instance. The
     * <code>xStream</code> instance is configured with several converters for the most common types in Axon.
     * <p/>
     * An {@link AnnotationRevisionResolver} is used to resolve revision for serialized objects.
     *
     * @param charset The character set to use
     * @param xStream The XStream instance to use
     */
    protected AbstractXStreamSerializer(Charset charset, XStream xStream) {
        this(charset, xStream, new AnnotationRevisionResolver(), new ChainingConverterFactory());
    }

    /**
     * Initialize the serializer using the given <code>charset</code> and <code>xStream</code> instance. The
     * <code>xStream</code> instance is configured with several converters for the most common types in Axon.
     *
     * @param charset          The character set to use
     * @param xStream          The XStream instance to use
     * @param revisionResolver The strategy to use to resolve the revision of an object
     */
    protected AbstractXStreamSerializer(Charset charset, XStream xStream, RevisionResolver revisionResolver) {
        this(charset, xStream, revisionResolver, new ChainingConverterFactory());
    }

    /**
     * Initialize the serializer using the given <code>charset</code>, <code>xStream</code> instance,
     * <code>revisionResolver</code> and <code>converterFactory</code>. The <code>xStream</code> instance is configured
     * with several converters for the most common types in Axon.
     *
     * @param charset          The character set to use
     * @param xStream          The XStream instance to use
     * @param revisionResolver The strategy to use to resolve the revision of an object
     * @param converterFactory The ConverterFactory providing the necessary content converters
     */
    protected AbstractXStreamSerializer(Charset charset, XStream xStream, RevisionResolver revisionResolver,
                                        ConverterFactory converterFactory) {
        this.charset = charset;
        this.xStream = xStream;
        this.converterFactory = converterFactory;
        this.revisionResolver = revisionResolver;
        if (converterFactory instanceof ChainingConverterFactory) {
            registerConverters((ChainingConverterFactory) converterFactory);
        }
        xStream.registerConverter(new JodaTimeConverter());
        xStream.addImmutableType(UUID.class);
        xStream.aliasPackage("axon.domain", "org.axonframework.domain");
        xStream.aliasPackage("axon.es", "org.axonframework.eventsourcing");

        xStream.alias("domain-event", GenericDomainEventMessage.class);
        xStream.alias("event", GenericEventMessage.class);
        xStream.alias("command", GenericCommandMessage.class);

        // for backward compatibility
        xStream.alias("localDateTime", DateTime.class);
        xStream.alias("dateTime", DateTime.class);
        xStream.alias("uuid", UUID.class);

        xStream.alias("meta-data", MetaData.class);
        xStream.registerConverter(new MetaDataConverter(xStream.getMapper()));
    }

    /**
     * Registers any converters that are specific to the type of content written by this serializer.
     *
     * @param converterFactory the ConverterFactory to register the converters with
     */
    protected abstract void registerConverters(ChainingConverterFactory converterFactory);

    @Override
    public <T> boolean canSerializeTo(Class<T> expectedRepresentation) {
        return converterFactory.hasConverter(byte[].class, expectedRepresentation);
    }
<commit_msg>Added Saga related aliases to XStream configuration<commit_after>/*
 * Copyright (c) 2010-2012. Axon Framework
 *
 * 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.axonframework.serializer;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.MapConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.mapper.Mapper;
import org.axonframework.commandhandling.GenericCommandMessage;
import org.axonframework.common.SerializationException;
import org.axonframework.domain.GenericDomainEventMessage;
import org.axonframework.domain.GenericEventMessage;
import org.axonframework.domain.MetaData;
import org.axonframework.saga.AssociationValue;
import org.axonframework.saga.AssociationValues;
import org.axonframework.saga.annotation.AbstractAnnotatedSaga;
import org.axonframework.saga.annotation.AssociationValuesImpl;
import org.joda.time.DateTime;

import java.lang.reflect.Constructor;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Abstract implementation for XStream based serializers. It provides some helper methods and configuration features
 * independent of the actual format used to marshal to.
 *
 * @author <NAME>
 * @since 2.0
 */
public abstract class AbstractXStreamSerializer implements Serializer {

    private static final Charset DEFAULT_CHARSET_NAME = Charset.forName("UTF-8");

    private final XStream xStream;
    private final Charset charset;
    private final RevisionResolver revisionResolver;
    private final ConverterFactory converterFactory;

    /**
     * Initialize a generic serializer using the UTF-8 character set. The provided XStream instance  is used to perform
     * the serialization.
     * <p/>
     * An {@link AnnotationRevisionResolver} is used to resolve the revision for serialized objects.
     *
     * @param xStream XStream instance to use
     */
    protected AbstractXStreamSerializer(XStream xStream) {
        this(xStream, new AnnotationRevisionResolver());
    }

    /**
     * Initialize a generic serializer using the UTF-8 character set. The provided XStream instance  is used to perform
     * the serialization.
     *
     * @param xStream          XStream instance to use
     * @param revisionResolver The strategy to use to resolve the revision of an object
     */
    protected AbstractXStreamSerializer(XStream xStream, RevisionResolver revisionResolver) {
        this(DEFAULT_CHARSET_NAME, xStream, revisionResolver);
    }

    /**
     * Initialize the serializer using the given <code>charset</code> and <code>xStream</code> instance. The
     * <code>xStream</code> instance is configured with several converters for the most common types in Axon.
     * <p/>
     * An {@link AnnotationRevisionResolver} is used to resolve revision for serialized objects.
     *
     * @param charset The character set to use
     * @param xStream The XStream instance to use
     */
    protected AbstractXStreamSerializer(Charset charset, XStream xStream) {
        this(charset, xStream, new AnnotationRevisionResolver(), new ChainingConverterFactory());
    }

    /**
     * Initialize the serializer using the given <code>charset</code> and <code>xStream</code> instance. The
     * <code>xStream</code> instance is configured with several converters for the most common types in Axon.
     *
     * @param charset          The character set to use
     * @param xStream          The XStream instance to use
     * @param revisionResolver The strategy to use to resolve the revision of an object
     */
    protected AbstractXStreamSerializer(Charset charset, XStream xStream, RevisionResolver revisionResolver) {
        this(charset, xStream, revisionResolver, new ChainingConverterFactory());
    }

    /**
     * Initialize the serializer using the given <code>charset</code>, <code>xStream</code> instance,
     * <code>revisionResolver</code> and <code>converterFactory</code>. The <code>xStream</code> instance is configured
     * with several converters for the most common types in Axon.
     *
     * @param charset          The character set to use
     * @param xStream          The XStream instance to use
     * @param revisionResolver The strategy to use to resolve the revision of an object
     * @param converterFactory The ConverterFactory providing the necessary content converters
     */
    protected AbstractXStreamSerializer(Charset charset, XStream xStream, RevisionResolver revisionResolver,
                                        ConverterFactory converterFactory) {
        this.charset = charset;
        this.xStream = xStream;
        this.converterFactory = converterFactory;
        this.revisionResolver = revisionResolver;
        if (converterFactory instanceof ChainingConverterFactory) {
            registerConverters((ChainingConverterFactory) converterFactory);
        }
        xStream.registerConverter(new JodaTimeConverter());
        xStream.addImmutableType(UUID.class);
        xStream.aliasPackage("axon.domain", "org.axonframework.domain");
        xStream.aliasPackage("axon.es", "org.axonframework.eventsourcing");

        // Message serialization
        xStream.alias("domain-event", GenericDomainEventMessage.class);
        xStream.alias("event", GenericEventMessage.class);
        xStream.alias("command", GenericCommandMessage.class);

        // Configuration to enhance Saga serialization
        xStream.addDefaultImplementation(AssociationValuesImpl.class, AssociationValues.class);
        xStream.aliasField("associations", AbstractAnnotatedSaga.class, "associationValues");
        xStream.alias("association", AssociationValue.class);
        xStream.aliasField("key", AssociationValue.class, "propertyKey");
        xStream.aliasField("value", AssociationValue.class, "propertyValue");

        // for backward compatibility
        xStream.alias("localDateTime", DateTime.class);
        xStream.alias("dateTime", DateTime.class);
        xStream.alias("uuid", UUID.class);

        xStream.alias("meta-data", MetaData.class);
        xStream.registerConverter(new MetaDataConverter(xStream.getMapper()));
    }

    /**
     * Registers any converters that are specific to the type of content written by this serializer.
     *
     * @param converterFactory the ConverterFactory to register the converters with
     */
    protected abstract void registerConverters(ChainingConverterFactory converterFactory);

    @Override
    public <T> boolean canSerializeTo(Class<T> expectedRepresentation) {
        return converterFactory.hasConverter(byte[].class, expectedRepresentation);
    }

{'fraction_non_alphanumeric': 0.07907370799209262, 'fraction_numerical': 0.0024004518497599547, 'mean_word_length': 3.7358742895352726, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4156886', 'n_tokens_mistral': 3643, 'n_tokens_neox': 3511, 'n_words': 1124}