<commit_before>
/*
 * Copyright (C) 2011 <NAME>. All rights reserved.
 *
 * $Id$
 */

package org.dellroad.stuff.jibx;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.util.concurrent.Callable;

import org.dellroad.stuff.java.IdGenerator;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IMarshallable;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;

/**
 * Some simplified API methods for JiBX XML encoding/decoding.
 */
public final class JiBXUtil {

    public static final String XML_ENCODING = "UTF-8";

    private JiBXUtil() {
    }

    /**
     * Read in an object encoded as XML.
     * This method assumes there is exactly one binding for the given class.
     *
     * <p>
     * This method runs within a new invocation of {@link IdGenerator#run(Callable) IdGenerator.run()} to support object references
     * (see {@link IdMapper}).
     *
     * <p>
     * The {@code input} is not closed by this method.
     *
     * @param targetClass target class
     * @param input source for the XML document
     * @throws JiBXException if there is a JiBX parse error
     * @throws IOException if an error occurs reading from {@code input}
     */
    public static <T> T readObject(Class<T> targetClass, InputStream input) throws JiBXException, IOException {
        return JiBXUtil.readObject(targetClass, null, input);
    }

    /**
     * Read in an object encoded as XML.
     *
     * <p>
     * This method runs within a new invocation of {@link IdGenerator#run(Callable) IdGenerator.run()} to support object references
     * (see {@link IdMapper}).
     *
     * <p>
     * The {@code input} is not closed by this method.
     *
     * @param targetClass target class
     * @param bindingName binding name, or null to choose the only one
     * @param input source for the XML document
     * @throws JiBXException if there is a JiBX parse error
     * @throws IOException if an error occurs reading from {@code input}
     */
    public static <T> T readObject(final Class<T> targetClass, String bindingName, final InputStream input)
      throws JiBXException, IOException {
        IBindingFactory bindingFactory = bindingName != null ?
          BindingDirectory.getFactory(bindingName, targetClass) : BindingDirectory.getFactory(targetClass);
        final IUnmarshallingContext unmarshallingContext = bindingFactory.createUnmarshallingContext();
        try {
            return IdGenerator.run(new Callable<T>() {
                @Override
                public T call() throws Exception {
                    return targetClass.cast(unmarshallingContext.unmarshalDocument(input, null));
                }
            });
        } catch (Exception e) {
            JiBXUtil.unwrapException(e);
            return null;            // not reached
        }
    }
<commit_msg>Add JiBXUtil.readObject() variants that take a Reader as input.<commit_after>
/*
 * Copyright (C) 2011 <NAME>. All rights reserved.
 *
 * $Id$
 */

package org.dellroad.stuff.jibx;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.util.concurrent.Callable;

import org.dellroad.stuff.java.IdGenerator;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IMarshallable;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;

/**
 * Some simplified API methods for JiBX XML encoding/decoding.
 */
public final class JiBXUtil {

    public static final String XML_ENCODING = "UTF-8";

    private JiBXUtil() {
    }

    /**
     * Read in an object encoded as XML.
     * This method assumes there is exactly one binding for the given class.
     *
     * <p>
     * This method runs within a new invocation of {@link IdGenerator#run(Callable) IdGenerator.run()} to support object references
     * (see {@link IdMapper}).
     *
     * <p>
     * The {@code input} is not closed by this method.
     *
     * @param targetClass target class
     * @param input source for the XML document
     * @throws JiBXException if there is a JiBX parse error
     * @throws IOException if an error occurs reading from {@code input}
     */
    public static <T> T readObject(Class<T> targetClass, InputStream input) throws JiBXException, IOException {
        return JiBXUtil.readObject(targetClass, null, input);
    }

    /**
     * Read in an object encoded as XML.
     *
     * <p>
     * This method runs within a new invocation of {@link IdGenerator#run(Callable) IdGenerator.run()} to support object references
     * (see {@link IdMapper}).
     *
     * <p>
     * The {@code input} is not closed by this method.
     *
     * @param targetClass target class
     * @param bindingName binding name, or null to choose the only one
     * @param input source for the XML document
     * @throws JiBXException if there is a JiBX parse error
     * @throws IOException if an error occurs reading from {@code input}
     */
    public static <T> T readObject(final Class<T> targetClass, String bindingName, final InputStream input)
      throws JiBXException, IOException {
        IBindingFactory bindingFactory = bindingName != null ?
          BindingDirectory.getFactory(bindingName, targetClass) : BindingDirectory.getFactory(targetClass);
        final IUnmarshallingContext unmarshallingContext = bindingFactory.createUnmarshallingContext();
        try {
            return IdGenerator.run(new Callable<T>() {
                @Override
                public T call() throws Exception {
                    return targetClass.cast(unmarshallingContext.unmarshalDocument(input, null));
                }
            });
        } catch (Exception e) {
            JiBXUtil.unwrapException(e);
            return null;            // not reached
        }
    }

    /**
     * Read in an object encoded as XML.
     * This method assumes there is exactly one binding for the given class.
     *
     * <p>
     * This method runs within a new invocation of {@link IdGenerator#run(Callable) IdGenerator.run()} to support object references
     * (see {@link IdMapper}).
     *
     * <p>
     * The {@code input} is not closed by this method.
     *
     * @param targetClass target class
     * @param input source for the XML document
     * @throws JiBXException if there is a JiBX parse error
     * @throws IOException if an error occurs reading from {@code input}
     */
    public static <T> T readObject(Class<T> targetClass, Reader input) throws JiBXException, IOException {
        return JiBXUtil.readObject(targetClass, null, input);
    }

    /**
     * Read in an object encoded as XML.
     *
     * <p>
     * This method runs within a new invocation of {@link IdGenerator#run(Callable) IdGenerator.run()} to support object references
     * (see {@link IdMapper}).
     *
     * <p>
     * The {@code input} is not closed by this method.
     *
     * @param targetClass target class
     * @param bindingName binding name, or null to choose the only one
     * @param input source for the XML document
     * @throws JiBXException if there is a JiBX parse error
     * @throws IOException if an error occurs reading from {@code input}
     */
    public static <T> T readObject(final Class<T> targetClass, String bindingName, final Reader input)
      throws JiBXException, IOException {
        IBindingFactory bindingFactory = bindingName != null ?
          BindingDirectory.getFactory(bindingName, targetClass) : BindingDirectory.getFactory(targetClass);
        final IUnmarshallingContext unmarshallingContext = bindingFactory.createUnmarshallingContext();
        try {
            return IdGenerator.run(new Callable<T>() {
                @Override
                public T call() throws Exception {
                    return targetClass.cast(unmarshallingContext.unmarshalDocument(input));
                }
            });
        } catch (Exception e) {
            JiBXUtil.unwrapException(e);
            return null;            // not reached
        }
    }

{'fraction_non_alphanumeric': 0.08776844070961717, 'fraction_numerical': 0.0011671335200746965, 'mean_word_length': 2.743556138051551, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4432909', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2334, 'n_words': 786}