<commit_before>package foam.core;

import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.io.StringReader;
import java.io.File;
import java.io.Writer;
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, Document doc) {
    Iterator i = objList.iterator();
    Element rootElement = doc.createElement("objects");
    doc.appendChild(rootElement);

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

  public static void toXML(FObject obj, Document doc) {
    Element objElement = doc.createElement("object");
    objElement.setAttribute("class", obj.getClass().toString().replaceAll("class ", ""));
    if ( doc.hasChildNodes() ) {
      Element root = doc.getDocumentElement();
      root.appendChild(objElement);
    } else {
      doc.appendChild(objElement);
    }
    writeToXML(obj, doc, objElement);
  }

  // Write properties from given FObject
  public static void writeToXML(FObject obj, Document doc, Element objElement) {
    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, doc, objElement);
    }
  }

  // Returns XML string as partial XML string with only object tags<commit_msg>Writing to XML file from Document obj<commit_after>package foam.core;

import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.io.StringReader;
import java.io.File;
import java.io.Writer;
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, Document doc) {
    Iterator i = objList.iterator();
    Element rootElement = doc.createElement("objects");
    doc.appendChild(rootElement);

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

  public static void toXML(FObject obj, Document doc) {
    Element objElement = doc.createElement("object");
    objElement.setAttribute("class", obj.getClass().toString().replaceAll("class ", ""));
    if ( doc.hasChildNodes() ) {
      Element root = doc.getDocumentElement();
      root.appendChild(objElement);
    } else {
      doc.appendChild(objElement);
    }
    writeToXML(obj, doc, objElement);
  }

  // Write properties from given FObject
  public static void writeToXML(FObject obj, Document doc, Element objElement) {
    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, doc, objElement);
    }
  }

  public static void toXMLFile (Document doc, String fileName) {
    try {
      TransformerFactory transformerFactory = TransformerFactory.newInstance();
      Transformer transformer = transformerFactory.newTransformer();
      transformer.setOutputProperty(OutputKeys.INDENT, "yes");
      transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
      DOMSource source = new DOMSource(doc);
      StreamResult result = new StreamResult(new File(fileName));
      transformer.transform(source, result);
    } catch (TransformerConfigurationException ex) {
    } catch (TransformerException ex ) {
    }
  }

  // Returns XML string as partial XML string with only object tags
{'fraction_non_alphanumeric': 0.10914355718899654, 'fraction_numerical': 0.0005568548836173293, 'mean_word_length': 2.7092110698058653, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 41}, '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': '4866506', 'n_tokens_mistral': 2598, 'n_tokens_neox': 2583, 'n_words': 625}