All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.carrot2.util.simplexml.PersisterHelpers Maven / Gradle / Ivy

Go to download

Carrot2 search results clustering framework. Minimal functional subset (core algorithms and infrastructure, no document sources).

There is a newer version: 3.16.3
Show newest version

/*
 * Carrot2 project.
 *
 * Copyright (C) 2002-2016, Dawid Weiss, Stanisław Osiński.
 * All rights reserved.
 *
 * Refer to the full license file "carrot2.LICENSE"
 * in the root folder of the repository checkout or at:
 * http://www.carrot2.org/carrot2.LICENSE
 */

package org.carrot2.util.simplexml;

import java.io.*;
import java.util.Map;

import org.carrot2.util.CloseableUtils;
import org.carrot2.util.resource.IResource;
import org.carrot2.util.resource.ResourceLookup;
import org.simpleframework.xml.convert.AnnotationStrategy;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.strategy.Strategy;

import org.carrot2.shaded.guava.common.collect.ImmutableMap;

/**
 * Simple XML session context helpers.
 */
public final class PersisterHelpers
{
    /**
     * Resource lookup key in the serialization/ deserialization session.
     */
    private static final String RESOURCE_LOOKUP_KEY = ResourceLookup.class.getName();

    /**
     * Acquire {@link ResourceLookup} from a serialization/ deserialization session. 
     */
    public static ResourceLookup getResourceLookup(Map session)
    {
        ResourceLookup resourceLookup = (ResourceLookup) session.get(RESOURCE_LOOKUP_KEY);
        if (resourceLookup == null)
        {
            throw new RuntimeException("Session does not carry resource lookup context.");
        }
        return resourceLookup;
    }

    /**
     * Create a persister with the given {@link ResourceLookup} key.
     */
    public static Persister createPersister(
        final ResourceLookup resourceLookup, Strategy strategy)
    {
        return createPersister(
            ImmutableMap. of(RESOURCE_LOOKUP_KEY, resourceLookup),
            strategy);
    }

    /**
     * Read and deserialize an XML resource of class clazz.
     * 
     * @param  Class to be deserialized.
     * @param required If true, missing resources will throw an IOException.
     * 
     * @return Returns the deserialized resource or null if required
     * is false.
     */
    public static  T read(ResourceLookup resourceLookup, 
        String resource, Class clazz, boolean required)
        throws IOException
    {
        IResource res = resourceLookup.getFirst(resource);
        if (res == null)
        {
            if (required) throw new IOException("Required resource not found: " + resource);
            return null;
        }

        InputStream inputStream = null;
        try
        {
            inputStream = new BufferedInputStream(res.open());
            try
            {
                T read = PersisterHelpers.createPersister(resourceLookup, new AnnotationStrategy()).read(clazz, inputStream);
                if (read instanceof ISourceLocationAware)
                {
                    ((ISourceLocationAware) read).setSource(res.toString());
                }
                return read;
            }
            catch (IOException e)
            {
                throw e;
            }
            catch (Exception e)
            {
                throw new IOException(e);
            }
        }
        finally
        {
            CloseableUtils.close(inputStream);
        }
    }
    
    /**
     * Create a persister with an arbitrary session map and deserialization strategy.
     */
    private static Persister createPersister(
        Map attributes, Strategy strategy)
    {
        return new Persister(new SessionInitStrategy(strategy, attributes));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy