<commit_before>package xmlcomponents.autoparse;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import xmlcomponents.Converter;
import xmlcomponents.Jattr;
import xmlcomponents.Jode;
import xmlcomponents.autoparse.annotation.XmlProperty;
import xmlcomponents.autoparse.annotation.XmlProperty.XmlPropertyType;

/**
 * Class for automatically parsing simple nodes with some attributes.
 * 
 * This parser is not smart.
 * 
 * @author <NAME>
 */
public class AutoParser {

   /**
    * Parses a node into the given class type. with the same name. Use the XmlProperty annotation to customize how this
    * parses your file.
    * 
    * @param j
    *           the Jode to parse
    * @param clazz
    *           the type to parse the Jode (j) into
    * @return the object represented by the xml structure
    */
   public static <T> T parse(Jode j, Class<T> clazz) {
      T ret;
      try {
         ret = clazz.newInstance();
         for (Field f : clazz.getDeclaredFields()) {
            f.setAccessible(true);
            Object fieldValue = resolveField(f, j);
            f.set(ret, fieldValue);
         }
         return ret;
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
   }

   private static Object resolveField(Field f, Jode j) throws IllegalAccessException, InstantiationException {
      Class<?> type = f.getType();
      XmlProperty anno = f.getAnnotation(XmlProperty.class);
      String fieldName = getValueName(f);
      if (anno != null && anno.type() == XmlPropertyType.TEXT_NODE) {
         return j.v;
      } else if (j.hasAttribute(fieldName)) {
         Jattr attr = j.attribute(fieldName);
         Converter<String, Object> converter = determineConverter(type);
         return attr.value(converter);
      } else if (j.hasSingleChild(fieldName) && !type.isInstance((Collection<?>) null)) {
         Object complex = type.newInstance();
         for (Field subF : complex.getClass().getDeclaredFields()) {
            subF.setAccessible(true);
            subF.set(complex, resolveField(subF, j.single(fieldName)));
         }
         return complex;
      } else if (type.isInstance((List) (new ArrayList()))) {
         ArrayList<Object> values = new ArrayList<Object>();
         ParameterizedType genericType = (ParameterizedType) f.getGenericType();
         Class realGenericType_finally = (Class) genericType.getActualTypeArguments()[0];
         for (Jode subJ : j.children(fieldName)) {
            values.add(parse(subJ, realGenericType_finally));
         }
         return values;
      }
      if (anno == null || !anno.optional()) {
         throw new IllegalArgumentException("Could not parse field named '" + fieldName + "' for jode named '" + j.n
               + "'");
      }
      // By here, we know that the item we are trying to parse has not been able to be resolved, but it is optional, 
      // so we just return null.
      return null;
   }

   private static String getValueName(Field f) {
      XmlProperty anno = f.getAnnotation(XmlProperty.class);
      if (anno != null && !anno.valueName().equals("")) {
         return anno.valueName();
      } else {
         return f.getName();
      }
   }

   private static String cammelToPascal(String s) {
      return (s.charAt(0) + "").toUpperCase() + s.substring(1);
   }

   private static boolean empty(String s) {
      return s == null || s.equals("");
   }<commit_msg>Fixed another bug in autoparser<commit_after>package xmlcomponents.autoparse;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import xmlcomponents.Converter;
import xmlcomponents.Jattr;
import xmlcomponents.Jode;
import xmlcomponents.autoparse.annotation.XmlProperty;
import xmlcomponents.autoparse.annotation.XmlProperty.XmlPropertyType;

/**
 * Class for automatically parsing simple nodes with some attributes.
 * 
 * This parser is not smart.
 * 
 * @author <NAME>
 */
public class AutoParser {

   /**
    * Parses a node into the given class type. with the same name. Use the XmlProperty annotation to customize how this
    * parses your file.
    * 
    * @param j
    *           the Jode to parse
    * @param clazz
    *           the type to parse the Jode (j) into
    * @return the object represented by the xml structure
    */
   public static <T> T parse(Jode j, Class<T> clazz) {
      T ret;
      try {
         ret = clazz.newInstance();
         for (Field f : clazz.getDeclaredFields()) {
            f.setAccessible(true);
            Object fieldValue = resolveField(f, j);
            f.set(ret, fieldValue);
         }
         return ret;
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
   }

   private static Object resolveField(Field f, Jode j) throws IllegalAccessException, InstantiationException {
      Class<?> type = f.getType();
      XmlProperty anno = f.getAnnotation(XmlProperty.class);
      String fieldName = getValueName(f);
      if (anno != null && anno.type() == XmlPropertyType.TEXT_NODE) {
         return j.v;
      } else if (j.hasAttribute(fieldName)) {
         Jattr attr = j.attribute(fieldName);
         Converter<String, Object> converter = determineConverter(type);
         return attr.value(converter);
      } else if (j.hasSingleChild(fieldName) && !type.isInstance((Collection<?>) null)) {
         Object complex = type.newInstance();
         for (Field subF : complex.getClass().getDeclaredFields()) {
            subF.setAccessible(true);
            Object resolvedResult = resolveField(subF, j.single(fieldName));
            if (resolvedResult != null || !subF.getType().isPrimitive()) {
               subF.set(complex, resolvedResult);
            }
         }
         return complex;
      } else if (type.isInstance((List) (new ArrayList()))) {
         ArrayList<Object> values = new ArrayList<Object>();
         ParameterizedType genericType = (ParameterizedType) f.getGenericType();
         Class realGenericType_finally = (Class) genericType.getActualTypeArguments()[0];
         for (Jode subJ : j.children(fieldName)) {
            values.add(parse(subJ, realGenericType_finally));
         }
         return values;
      }
      if (anno == null || !anno.optional()) {
         throw new IllegalArgumentException("Could not parse field named '" + fieldName + "' for jode named '" + j.n
               + "'");
      }
      // By here, we know that the item we are trying to parse has not been able to be resolved, but it is optional,
      // so we just return null.
      return null;
   }

   private static String getValueName(Field f) {
      XmlProperty anno = f.getAnnotation(XmlProperty.class);
      if (anno != null && !anno.valueName().equals("")) {
         return anno.valueName();
      } else {
         return f.getName();
      }
   }

   private static String cammelToPascal(String s) {
      return (s.charAt(0) + "").toUpperCase() + s.substring(1);
   }

   private static boolean empty(String s) {
      return s == null || s.equals("");
   }
{'fraction_non_alphanumeric': 0.112348865643255, 'fraction_numerical': 0.0008151066431191414, 'mean_word_length': 2.60352422907489, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '2756238', 'n_tokens_mistral': 2078, 'n_tokens_neox': 2048, 'n_words': 583}