<commit_before>package foam.core;

import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter;
import foam.dao.ListSink;
import foam.dao.XMLDAO;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.xml.stream.*;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;

public class XMLSupport {

  // TODO: Planning to add support for other date formats in the future

  public static List<FObject> fromXML(X x, XMLStreamReader xmlr) {
    List<FObject> objList = new ArrayList<FObject>();
    try {
      int eventType;
      Object clsInstance = null;
      while ( xmlr.hasNext() ) {
        eventType = xmlr.next();
        switch ( eventType ) {
          case XMLStreamConstants.START_DOCUMENT:
            break;
          case XMLStreamConstants.START_ELEMENT:
            if ( xmlr.getLocalName().equals("object") ) {
              // Create new fObject
              String objClass = xmlr.getAttributeValue(null, "class");
              Class cls;

              try {
                cls = Class.forName(objClass);
                clsInstance = x.create(cls);
              } catch (ClassNotFoundException ex) {

              }
              // Object properties
              copyFromXML((FObject) clsInstance, xmlr);
              if ( clsInstance != null ) {
                objList.add((FObject) clsInstance);
              }
            }
            break;
          case XMLStreamConstants.END_ELEMENT:
            break;
          case XMLStreamConstants.END_DOCUMENT:
            break;
        }
      }
      xmlr.close();
    } catch (XMLStreamException ex) {
    }
    return objList;
  }

  public static List<FObject> fromXML(X x, String fileName) throws IOException {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    XMLStreamReader xmlr = null;
    try {
      xmlr = xmlInputFactory.createXMLStreamReader(new FileReader(fileName));
    } catch (IOException ex) {
      throw new IOException("Could not create/file with given fileName");
    } catch (XMLStreamException ex) {

    }
    return fromXML(x, xmlr);
  }

  public static void copyFromXML(FObject obj, XMLStreamReader reader) throws XMLStreamException {
    try {
      PropertyInfo prop = null;
      while ( reader.hasNext() ) {
        int eventType;
        eventType = reader.next();
        switch ( eventType ) {
          case XMLStreamConstants.START_ELEMENT:
            ClassInfo cInfo = obj.getClassInfo();
            prop = (PropertyInfo) cInfo.getAxiomByName(reader.getLocalName());
            if ( prop == null ) {
              throw new XMLStreamException("Could not find property " + reader.getLocalName());
            }
            break;
          case XMLStreamConstants.CHARACTERS:
            if ( prop != null ) {
              prop.setFromString(obj, reader.getText());
              prop = null;
            }
            break;
          case XMLStreamConstants.END_ELEMENT:
            if ( reader.getLocalName().equals("object") ) {
              return;
            }
            break;
        }
      }
    } catch (XMLStreamException ex) {
      throw new XMLStreamException("Premature end of xml file");
    }
  }

  public static void toXML(List<FObject> objList, XMLStreamWriter xmlStreamWriter) {
    try {
      xmlStreamWriter = new IndentingXMLStreamWriter(xmlStreamWriter);
      Iterator i = objList.iterator();
      xmlStreamWriter.writeStartDocument();
      //Root tag
      xmlStreamWriter.writeStartElement("objects");

      while ( i.hasNext() ) {
        toXML((FObject) i.next(), xmlStreamWriter);
      }

      xmlStreamWriter.writeEndElement();
      xmlStreamWriter.writeEndDocument();

    } catch (XMLStreamException ex) {
    }
  }

  public static void toXML(FObject obj, XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
    try {
      xmlStreamWriter.writeStartElement("object");
      xmlStreamWriter.writeAttribute("class", obj.getClass().toString().replaceAll("class ", ""));
      writeToXML(obj, xmlStreamWriter);
      xmlStreamWriter.writeEndElement();
    } catch (XMLStreamException ex) {
      throw new XMLStreamException("Error while writing xml file");
    }
  }

  // Write properties from given FObject
  public static void writeToXML(FObject obj, XMLStreamWriter writer) {
    ClassInfo cInfo = obj.getClassInfo();
    List props = cInfo.getAxiomsByClass(PropertyInfo.class);
    Iterator propItr = props.iterator();

    try {
      while ( propItr.hasNext() ) {
        PropertyInfo prop = (PropertyInfo) propItr.next();
        Object value = prop.f(obj);
        if (prop.getTransient() ) continue;
        if ( value != null && value != "" ) {
          writer.writeStartElement(prop.getName());
          // Case for date
          if ( value instanceof java.util.Date ) {
            String s = value.toString();
            writer.writeCharacters(s);
          } else {
            writer.writeCharacters(value.toString());
          }
          writer.writeEndElement();
        }
      }
    } catch (XMLStreamException ex) {
    }
  }

  // Returns XML string as partial XML string with only object tags
  public static String toXMLString(FObject obj) {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    StringWriter sw = new StringWriter();
    try {
      XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
      writer = new IndentingXMLStreamWriter(writer);
      toXML(obj, writer);
    } catch (XMLStreamException ex) {
    }
    return sw.toString();
  }

  // Returns XML string as full XML document string with document tags
  public static String toXMLString(List<FObject> objArray) {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    StringWriter sw = new StringWriter();
    try {
      XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
      writer = new IndentingXMLStreamWriter(writer);
      writer.writeStartDocument();
      Iterator i = objArray.iterator();
      while ( i.hasNext() ) {
        toXML((FObject) i.next(), writer);
      }
      writer.writeEndDocument();
    } catch (XMLStreamException ex) {
    }
    return sw.toString();
  }
}<commit_msg>Modified XMLSupport for PropertyInfo Changes<commit_after>package foam.core;

import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.*;

public class XMLSupport {

  public static List<FObject> fromXML(X x, XMLStreamReader xmlr) {
    List<FObject> objList = new ArrayList<FObject>();
    try {
      int eventType;
      while ( xmlr.hasNext() ) {
        eventType = xmlr.next();
        switch ( eventType ) {
          case XMLStreamConstants.START_ELEMENT:
            if ( xmlr.getLocalName().equals("object") ) {
              FObject obj = createObj(x, xmlr);
              if ( obj != null ) {
                objList.add(obj);
              }
            }
            break;
        }
      }
      xmlr.close();
    } catch (XMLStreamException ex) {
    }
    return objList;
  }

  public static FObject createObj ( X x, XMLStreamReader xmlr ) {
    Object clsInstance = null;
    try {
      // Create new fObject
      String objClass = xmlr.getAttributeValue(null, "class");
      Class cls = Class.forName(objClass);
      clsInstance = x.create(cls);
      // Object properties
      copyFromXML(x, (FObject) clsInstance, xmlr);
    } catch (ClassNotFoundException ex) {

    } catch (XMLStreamException ex ) {

    }
    return (FObject) clsInstance;
  }

  public static List<FObject> fromXML(X x, String fileName) throws IOException {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    XMLStreamReader xmlr = null;
    try {
      xmlr = xmlInputFactory.createXMLStreamReader(new FileReader(fileName));
    } catch (IOException ex) {
      throw new IOException("Could not create/file with given fileName");
    } catch (XMLStreamException ex) {

    }
    return fromXML(x, xmlr);
  }

  public static void copyFromXML(X x, FObject obj, XMLStreamReader reader) throws XMLStreamException {
    try {
      PropertyInfo prop = null;
      while ( reader.hasNext() ) {
        int eventType;
        eventType = reader.next();
        switch ( eventType ) {
          case XMLStreamConstants.START_ELEMENT:
            ClassInfo cInfo = obj.getClassInfo();
            prop = (PropertyInfo) cInfo.getAxiomByName(reader.getLocalName());
            if ( prop != null ) {
              prop.set(obj, prop.fromXML(x, reader));
              prop = null;
            }
            break;
          case XMLStreamConstants.END_ELEMENT:
            if ( reader.getLocalName().equals("object") ) {
              return;
            }
            break;
        }
      }
    } catch (XMLStreamException ex) {
      throw new XMLStreamException("Premature end of xml file");
    }
  }

  public static void toXML(List<FObject> objList, XMLStreamWriter xmlStreamWriter) {
    try {
      xmlStreamWriter = new IndentingXMLStreamWriter(xmlStreamWriter);
      Iterator i = objList.iterator();
      xmlStreamWriter.writeStartDocument();
      //Root tag
      xmlStreamWriter.writeStartElement("objects");

      while ( i.hasNext() ) {
        toXML((FObject) i.next(), xmlStreamWriter);
      }

      xmlStreamWriter.writeEndElement();
      xmlStreamWriter.writeEndDocument();

    } catch (XMLStreamException ex) {
    }
  }

  public static void toXML(FObject obj, XMLStreamWriter xmlStreamWriter) throws XMLStreamException {
    try {
      xmlStreamWriter.writeStartElement("object");
      xmlStreamWriter.writeAttribute("class", obj.getClass().toString().replaceAll("class ", ""));
      writeToXML(obj, xmlStreamWriter);
      xmlStreamWriter.writeEndElement();
    } catch (XMLStreamException ex) {
      throw new XMLStreamException("Error while writing xml file");
    }
  }

  // Write properties from given FObject
  public static void writeToXML(FObject obj, XMLStreamWriter writer) {
    ClassInfo cInfo = obj.getClassInfo();
    List props = cInfo.getAxiomsByClass(PropertyInfo.class);
    Iterator propItr = props.iterator();

    while ( propItr.hasNext() ) {
      PropertyInfo prop = (PropertyInfo) propItr.next();
      prop.toXML(obj, writer);
    }
  }

  // Returns XML string as partial XML string with only object tags
  public static String toXMLString(FObject obj) {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    StringWriter sw = new StringWriter();
    try {
      XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
      writer = new IndentingXMLStreamWriter(writer);
      toXML(obj, writer);
    } catch (XMLStreamException ex) {
    }
    return sw.toString();
  }

  // Returns XML string as full XML document string with document tags
  public static String toXMLString(List<FObject> objArray) {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    StringWriter sw = new StringWriter();
    try {
      XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
      writer = new IndentingXMLStreamWriter(writer);
      writer.writeStartDocument();
      Iterator i = objArray.iterator();
      writer.writeStartElement("objects");
      while ( i.hasNext() ) {
        toXML((FObject) i.next(), writer);
      }
      writer.writeEndElement();
      writer.writeEndDocument();
    } catch (XMLStreamException ex) {
    }
    return sw.toString();
  }
}
{'fraction_non_alphanumeric': 0.10103800435292148, 'fraction_numerical': 0.00016742005692281934, 'mean_word_length': 2.6071859903381642, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 64}, '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': '4486578', 'n_tokens_mistral': 3313, 'n_tokens_neox': 3260, 'n_words': 783}