<commit_before>/**
 * Copyright (C) 2015 BITPlan GmbH
 *
 * Pater-Delp-Str. 1
 * D-47877 Willich-Schiefbahn
 *
 * http://www.bitplan.com
 * 
 * This source is part of
 * https://github.com/WolfgangFahl/Mediawiki-Japi
 * and the license for Mediawiki-Japi applies
 * 
 */
package com.bitplan.mediawiki.japi.jaxb;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.logging.Level;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.eclipse.persistence.jaxb.MarshallerProperties;

/**
 * gerneric JaxbFactory
 * 
 * @author wf
 *
 * @param <T>
 */
public class JaxbFactory<T> implements JaxbFactoryApi<T> {
	final Class<T> classOfT;
	
	protected static java.util.logging.Logger LOGGER = java.util.logging.Logger
      .getLogger("com.bitplan.mediawiki.japi.jaxb");
	
	/**
	 * construct me for the given T class - workaround for java generics type
	 * erasure
	 * 
	 * @param pClassOfT
	 */
	public JaxbFactory(Class<T> pClassOfT) {
		classOfT = pClassOfT;
	}

	/**
	 * get an instance of T for the given xml string
	 * 
	 * @param xml
	 *          - the xml representation of the <T> instance
	 * @return T
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	public T fromXML(String xml) throws Exception {
		// unmarshal the xml message to an Mediawiki Api Java object
		JAXBContext context = JAXBContext.newInstance(classOfT);
		Unmarshaller u = context.createUnmarshaller();
		StringReader xmlReader = new StringReader(xml);
		T result = null;
		// this step will convert from xml text to Java Object
		try {
			Object unmarshalResult = u.unmarshal(xmlReader);
			if (classOfT.isInstance(unmarshalResult)) {
				result = (T) unmarshalResult;
			}
		} catch (JAXBException jex) {
			String msg="JAXBException: "+jex.getMessage();
			LOGGER.log(Level.SEVERE,msg);
			LOGGER.log(Level.SEVERE,xml);
			throw (new Exception(msg,jex));
		}
		return result;
	}

	/**
	 * get an instance of T for the given json string
	 * 
	 * @param json
	 *          - the json representation of the <T> instance
	 * @return T
	 * @throws Exception 
	 */
	public T fromJson(String json) throws Exception {
		return fromXML(json);
	}

	/**
	 * get a marshaller for the given <T> instance
	 * 
	 * @param instance
	 *          - the instance to get a marshaller for
	 * @return a marshaller for <T>
	 * @throws JAXBException
	 */
	public Marshaller getMarshaller(T instance) throws JAXBException {
		JAXBContext context = JAXBContext.newInstance(instance.getClass());
		Marshaller marshaller = context.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		return marshaller;
	}

	/**
	 * get the string representation of the given marshaller
	 * 
	 * @param marshaller
	 * @param instance 
	 * @return the string representation for the given marshaller
	 * @throws JAXBException
	 */
	public String getString(Marshaller marshaller, T instance) throws JAXBException {
		StringWriter sw = new StringWriter();
		marshaller.marshal(instance, sw);
		String result = sw.toString();
		return result;
	}

	/**
	 * create a Json representation for the given <T> instance
	 * 
	 * @param instance
	 *          - the instance to convert to json
	 * @return a Json representation of the given <T>
	 * @throws JAXBException
	 */
	public String asJson(T instance) throws JAXBException {
		Marshaller marshaller = getMarshaller(instance);
		marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
		marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
		String result = getString(marshaller,instance);
		return result;
	}

	/**
	 * create an xml representation for the given <T> instance
	 * 
	 * @param instance
	 *          - the instance to convert to xml
	 * @return a xml representation of the given <T> instance
	 * @throws JAXBException
	 */
	public String asXml(T instance) throws JAXBException {
		Marshaller marshaller = getMarshaller(instance);
		marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
		String result = getString(marshaller,instance);
		return result;
	}
}<commit_msg>fixes to and fromJson<commit_after>/**
 * Copyright (C) 2015 BITPlan GmbH
 *
 * Pater-Delp-Str. 1
 * D-47877 Willich-Schiefbahn
 *
 * http://www.bitplan.com
 * 
 * This source is part of
 * https://github.com/WolfgangFahl/Mediawiki-Japi
 * and the license for Mediawiki-Japi applies
 * 
 */
package com.bitplan.mediawiki.japi.jaxb;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.logging.Level;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.eclipse.persistence.jaxb.MarshallerProperties;

/**
 * gerneric JaxbFactory
 * 
 * @author wf
 *
 * @param <T>
 */
public class JaxbFactory<T> implements JaxbFactoryApi<T> {
  final Class<T> classOfT;

  protected static java.util.logging.Logger LOGGER = java.util.logging.Logger
      .getLogger("com.bitplan.mediawiki.japi.jaxb");

  /**
   * construct me for the given T class - workaround for java generics type
   * erasure
   * 
   * @param pClassOfT
   */
  public JaxbFactory(Class<T> pClassOfT) {
    classOfT = pClassOfT;
  }

  /**
   * get a fitting Unmarshaller
   * 
   * @return - the Unmarshaller for the classOfT set
   * @throws JAXBException
   */
  public Unmarshaller getUnmarshaller() throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(classOfT);
    Unmarshaller u = context.createUnmarshaller();
    return u;
  }

  @SuppressWarnings("unchecked")
  public T fromString(Unmarshaller u, String s) throws Exception {
    // unmarshal the string to a Java object of type <T> (classOfT has the runtime type)
    StringReader stringReader = new StringReader(s);
    T result = null;
    // this step will convert from xml text to Java Object
    try {
      Object unmarshalResult = u.unmarshal(stringReader);
      if (classOfT.isInstance(unmarshalResult)) {
        result = (T) unmarshalResult;
      } else {
        throw new Exception("unmarshalling returned "+unmarshalResult.getClass().getName()+" but "+classOfT.getName()+" was expected");
      }
    } catch (JAXBException jex) {
      String msg = "JAXBException: " + jex.getMessage();
      LOGGER.log(Level.SEVERE, msg);
      LOGGER.log(Level.SEVERE, s);
      throw (new Exception(msg, jex));
    }
    return result;
  }

  /**
   * get an instance of T for the given xml string
   * 
   * @param xml
   *          - the xml representation of the <T> instance
   * @return T
   * @throws Exception
   */
  public T fromXML(String xml) throws Exception {
    Unmarshaller u=this.getUnmarshaller();
    u.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
    T result=this.fromString(u, xml);
    return result;
  }

  /**
   * get an instance of T for the given json string
   * 
   * @param json
   *          - the json representation of the <T> instance
   * @return T
   * @throws Exception
   */
  public T fromJson(String json) throws Exception {
    Unmarshaller u=this.getUnmarshaller();
    u.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
    T result=this.fromString(u, json);
    return result;
  }

  /**
   * get a marshaller for the given <T> instance
   * 
   * @param instance
   *          - the instance to get a marshaller for
   * @return a marshaller for <T>
   * @throws JAXBException
   */
  public Marshaller getMarshaller(T instance) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(instance.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    return marshaller;
  }

  /**
   * get the string representation of the given marshaller
   * 
   * @param marshaller
   * @param instance
   * @return the string representation for the given marshaller
   * @throws JAXBException
   */
  public String getString(Marshaller marshaller, T instance)
      throws JAXBException {
    StringWriter sw = new StringWriter();
    marshaller.marshal(instance, sw);
    String result = sw.toString();
    return result;
  }

  /**
   * create a Json representation for the given <T> instance
   * 
   * @param instance
   *          - the instance to convert to json
   * @return a Json representation of the given <T>
   * @throws JAXBException
   */
  public String asJson(T instance) throws JAXBException {
    Marshaller marshaller = getMarshaller(instance);
    marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
    marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
    String result = getString(marshaller, instance);
    return result;
  }

  /**
   * create an xml representation for the given <T> instance
   * 
   * @param instance
   *          - the instance to convert to xml
   * @return a xml representation of the given <T> instance
   * @throws JAXBException
   */
  public String asXml(T instance) throws JAXBException {
    Marshaller marshaller = getMarshaller(instance);
    marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/xml");
    String result = getString(marshaller, instance);
    return result;
  }
}
{'fraction_non_alphanumeric': 0.10004289084280506, 'fraction_numerical': 0.002144542140253056, 'mean_word_length': 3.4287749287749287, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 33}, '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': '256463', 'n_tokens_mistral': 2983, 'n_tokens_neox': 2911, 'n_words': 802}