<commit_before>/*
 * @author - dushyant
 */
package com.dushyant.yml;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.helpers.DefaultValidationEventHandler;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

public class Yml<T> implements AutoCloseable {
  YmlContext context;
  Marshaller marshaller;
  Unmarshaller unmarshaller;
  T object;

  public Yml(T object) throws YmlException {
    this.object = object;
    JAXBContext newInstance = null;
    try {
      newInstance = JAXBContext.newInstance(object.getClass());
    } catch (JAXBException e) {
      throw new YmlException(e);
    }
    context = new YmlContext(newInstance);
    marshaller = getMarshaller();
    unmarshaller = getUnmarshaller();
  }

  @Override
  public void close() {
    context.close();
  }

  public Marshaller getMarshaller() {
    return context.createMarshaller();
  }

  public static Marshaller getMarshaller(JAXBContext context) throws YmlException {
    try {
      return context.createMarshaller();
    } catch (JAXBException e) {
      throw new YmlException(e);
    }
  }

  public Unmarshaller getUnmarshaller() throws YmlException {
    try {
      final Unmarshaller createUnmarshaller = context.createUnmarshaller();
      createUnmarshaller.setSchema(validate(createUnmarshaller));
      return createUnmarshaller;
    } catch (JAXBException e) {
      throw new YmlException(e);
    }
  }

  /**
   * @param createUnmarshaller
   * @return
   * @throws YmlException
   * @throws JAXBException
   */
  private Schema validate(final Unmarshaller createUnmarshaller) throws YmlException {
    Schema schema = null;
    try {
      createUnmarshaller.setEventHandler(new DefaultValidationEventHandler());
      SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
      schema = sf.newSchema(new File("Schema.xsd"));
    } catch (JAXBException | SAXException e) {
      throw new YmlException(e);
    }
    return schema;
  }

  public boolean validate() throws YmlException {
    Schema schema = null;
    try {
      getUnmarshaller().setEventHandler(new DefaultValidationEventHandler());
      SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
      schema = sf.newSchema(new File("Schema.xsd"));
    } catch (JAXBException | YmlException | SAXException e) {
      throw new YmlException(e);
    }
    return schema != null ? true : false;
  }

  public static Unmarshaller getUnmarshaller(JAXBContext context) throws YmlException {
    try {
      return context.createUnmarshaller();
    } catch (JAXBException e) {
      throw new YmlException(e);
    }
  }

  public void marshalToFile(String fileName) throws YmlException {<commit_msg>refactor<commit_after>package com.dushyant.yml;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.helpers.DefaultValidationEventHandler;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.xml.sax.SAXException;

public class Yml<T> implements AutoCloseable {
  YmlContext context;
  Marshaller marshaller;
  Unmarshaller unmarshaller;
  T object;

  public Yml(T object) throws YmlException {
    this.object = object;
    JAXBContext newInstance = null;
    try {
      newInstance = JAXBContext.newInstance(object.getClass());
    } catch (JAXBException e) {
      throw new YmlException(e);
    }
    context = new YmlContext(newInstance);
    marshaller = getMarshaller();
    unmarshaller = getUnmarshaller();
  }

  @Override
  public void close() {
    context.close();
  }

  public Marshaller getMarshaller() {
    return context.createMarshaller();
  }

  public static Marshaller getMarshaller(JAXBContext context) throws YmlException {
    try {
      return context.createMarshaller();
    } catch (JAXBException e) {
      throw new YmlException(e);
    }
  }

  public Unmarshaller getUnmarshaller() throws YmlException {
    try {
      final Unmarshaller createUnmarshaller = context.createUnmarshaller();
      createUnmarshaller.setSchema(validate(createUnmarshaller));
      return createUnmarshaller;
    } catch (JAXBException e) {
      throw new YmlException(e);
    }
  }


  private Schema validate(final Unmarshaller createUnmarshaller) throws YmlException {
    Schema schema = null;
    try {
      createUnmarshaller.setEventHandler(new DefaultValidationEventHandler());
      SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
      schema = sf.newSchema(new File("myschema.xsd"));
    } catch (JAXBException | SAXException e) {
      throw new YmlException(e);
    }
    return schema;
  }

  public boolean validate() throws YmlException {
    Schema schema = null;
    try {
      getUnmarshaller().setEventHandler(new DefaultValidationEventHandler());
      SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
      schema = sf.newSchema(new File("myschema.xsd"));
    } catch (JAXBException | YmlException | SAXException e) {
      throw new YmlException(e);
    }
    return schema != null ? true : false;
  }

  public static Unmarshaller getUnmarshaller(JAXBContext context) throws YmlException {
    try {
      return context.createUnmarshaller();
    } catch (JAXBException e) {
      throw new YmlException(e);
    }
  }

  public void marshalToFile(String fileName) throws YmlException {
{'fraction_non_alphanumeric': 0.09310227658851512, 'fraction_numerical': 0.0006795786612300374, 'mean_word_length': 3.8135731807031887, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, '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': '641046', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1834, 'n_words': 390}