<commit_before>package com.google.sitebricks;

import com.google.inject.Inject;
import com.google.inject.Provider;
import net.jcip.annotations.Immutable;

import javax.servlet.ServletContext;
import java.io.*;
import java.net.URL;

/**
 * @author <NAME> (<EMAIL>)
 */
@Immutable
class TemplateLoader {
  private final Provider<ServletContext> context;

  private final String[] fileNameTemplates = new String[] { "%s.html", "%s.xhtml", "%s.xml",
      "%s.txt", "%s.fml", "%s.mvel" };

  @Inject
  public TemplateLoader(Provider<ServletContext> context) {
    this.context = context;
  }

  public Template load(Class<?> pageClass) {
    Show show = pageClass.getAnnotation(Show.class);
    String template;

    //annotation not present, resolve by name
    if (null == show) {
      template = resolve(pageClass);
    } else {
      template = show.value();
    }

    String text;
    try {
      InputStream stream = null;
      //first look in class neighborhood for template
      if (null != template) {
        stream = pageClass.getResourceAsStream(template);
      }

      //look on the webapp resource path if not in classpath
      if (null == stream) {

        final ServletContext servletContext = context.get();
        if (null != template)
          stream = open(template, servletContext);

        //resolve again, but this time on the webapp resource path
        if (null == stream) {
          final ResolvedTemplate resolvedTemplate = resolve(pageClass, servletContext);
          if (null != resolvedTemplate) {
            template = resolvedTemplate.templateName;
            stream = resolvedTemplate.resource;
          }
        }

        //if there's still no template, then error out
        if (null == stream) {
          throw new MissingTemplateException(String.format("Could not find a suitable template for %s, " +
              "did you remember to place an @Show? None of [" +
              fileNameTemplates[0] +
              "] could be found in either package [%s], in the root of the resource dir OR in WEB-INF/.",
              pageClass.getName(), pageClass.getSimpleName(),
              pageClass.getPackage().getName()));
        }
      }

      text = read(stream);
    } catch (IOException e) {
      throw new TemplateLoadingException("Could not load template for (i/o error): " + pageClass, e);
    }

    return new Template(Template.Kind.kindOf(template), text);
  }

  private ResolvedTemplate resolve(Class<?> pageClass, ServletContext context) {
    //first resolve using url conversion
    for (String nameTemplate : fileNameTemplates) {
      String templateName = String.format(nameTemplate, pageClass.getSimpleName());
      InputStream resource = open(templateName, context);

      if (null != resource) {
        return new ResolvedTemplate(templateName, resource);
      }

      resource = openWebInf(templateName, context);

      if (null != resource) {
        return new ResolvedTemplate(templateName, resource);
      }
    }

    //resolve again using servlet context if that fails
    for (String nameTemplate : fileNameTemplates) {
      String templateName = String.format(nameTemplate, pageClass.getSimpleName());
      InputStream resource = context.getResourceAsStream(templateName);

      if (null != resource) {
        return new ResolvedTemplate(templateName, resource);
      }
    }

    return null;
  }

  private static class ResolvedTemplate {
    private final InputStream resource;
    private final String templateName;

    private ResolvedTemplate(String templateName, InputStream resource) {
      this.templateName = templateName;
      this.resource = resource;
    }
  }

  private static InputStream open(String file, ServletContext context) {
    try {
      return new FileInputStream(new File(context.getRealPath(file)));
    } catch (FileNotFoundException e) {
      return null;
    }
  }
  
  private static InputStream openWebInf(String file, ServletContext context) {
    return open("/WEB-INF/" + file, context);
  }

  //resolves a location for this page class's template (assuming @Show is not present)
  private String resolve(Class<?> pageClass) {
    for (String nameTemplate : fileNameTemplates) {
      String name = String.format(nameTemplate, pageClass.getSimpleName());
      URL resource = pageClass.getResource(name);

      if (null != resource) {
        return name;
      }
    }

    return null;
  }

  private static String read(InputStream stream) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
<commit_msg>Prevent NPE if realpaths do not resolve for tempaltes. Thanks to JF Arcand for this patch!<commit_after>package com.google.sitebricks;

import com.google.inject.Inject;
import com.google.inject.Provider;
import net.jcip.annotations.Immutable;

import javax.servlet.ServletContext;
import java.io.*;
import java.net.URL;

/**
 * @author <NAME> (<EMAIL>)
 */
@Immutable
class TemplateLoader {
  private final Provider<ServletContext> context;

  private final String[] fileNameTemplates = new String[] { "%s.html", "%s.xhtml", "%s.xml",
      "%s.txt", "%s.fml", "%s.mvel" };

  @Inject
  public TemplateLoader(Provider<ServletContext> context) {
    this.context = context;
  }

  public Template load(Class<?> pageClass) {
    Show show = pageClass.getAnnotation(Show.class);
    String template;

    //annotation not present, resolve by name
    if (null == show) {
      template = resolve(pageClass);
    } else {
      template = show.value();
    }

    String text;
    try {
      InputStream stream = null;
      //first look in class neighborhood for template
      if (null != template) {
        stream = pageClass.getResourceAsStream(template);
      }

      //look on the webapp resource path if not in classpath
      if (null == stream) {

        final ServletContext servletContext = context.get();
        if (null != template)
          stream = open(template, servletContext);

        //resolve again, but this time on the webapp resource path
        if (null == stream) {
          final ResolvedTemplate resolvedTemplate = resolve(pageClass, servletContext);
          if (null != resolvedTemplate) {
            template = resolvedTemplate.templateName;
            stream = resolvedTemplate.resource;
          }
        }

        //if there's still no template, then error out
        if (null == stream) {
          throw new MissingTemplateException(String.format("Could not find a suitable template for %s, " +
              "did you remember to place an @Show? None of [" +
              fileNameTemplates[0] +
              "] could be found in either package [%s], in the root of the resource dir OR in WEB-INF/.",
              pageClass.getName(), pageClass.getSimpleName(),
              pageClass.getPackage().getName()));
        }
      }

      text = read(stream);
    } catch (IOException e) {
      throw new TemplateLoadingException("Could not load template for (i/o error): " + pageClass, e);
    }

    return new Template(Template.Kind.kindOf(template), text);
  }

  private ResolvedTemplate resolve(Class<?> pageClass, ServletContext context) {
    //first resolve using url conversion
    for (String nameTemplate : fileNameTemplates) {
      String templateName = String.format(nameTemplate, pageClass.getSimpleName());
      InputStream resource = open(templateName, context);

      if (null != resource) {
        return new ResolvedTemplate(templateName, resource);
      }

      resource = openWebInf(templateName, context);

      if (null != resource) {
        return new ResolvedTemplate(templateName, resource);
      }
    }

    //resolve again using servlet context if that fails
    for (String nameTemplate : fileNameTemplates) {
      String templateName = String.format(nameTemplate, pageClass.getSimpleName());
      InputStream resource = context.getResourceAsStream(templateName);

      if (null != resource) {
        return new ResolvedTemplate(templateName, resource);
      }
    }

    return null;
  }

  private static class ResolvedTemplate {
    private final InputStream resource;
    private final String templateName;

    private ResolvedTemplate(String templateName, InputStream resource) {
      this.templateName = templateName;
      this.resource = resource;
    }
  }

  private static InputStream open(String file, ServletContext context) {
    try {
      String path = context.getRealPath(file);
      return path == null ? null : new FileInputStream(new File(path));
    } catch (FileNotFoundException e) {
      return null;
    }
  }
  
  private static InputStream openWebInf(String file, ServletContext context) {
    return open("/WEB-INF/" + file, context);
  }

  //resolves a location for this page class's template (assuming @Show is not present)
  private String resolve(Class<?> pageClass) {
    for (String nameTemplate : fileNameTemplates) {
      String name = String.format(nameTemplate, pageClass.getSimpleName());
      URL resource = pageClass.getResource(name);

      if (null != resource) {
        return name;
      }
    }

    return null;
  }

  private static String read(InputStream stream) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));

{'fraction_non_alphanumeric': 0.10033229713795691, 'fraction_numerical': 0.0004287705005895594, 'mean_word_length': 2.911949685534591, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3835153', 'n_tokens_mistral': 2492, 'n_tokens_neox': 2437, 'n_words': 755}