<commit_before>package com.thaiopensource.relaxng.output;

import com.thaiopensource.xml.util.EncodingMap;
import com.thaiopensource.xml.out.CharRepertoire;

import java.io.Writer;
import java.io.IOException;
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.HashMap;

public class LocalOutputDirectory implements OutputDirectory {
  private final File mainOutputFile;
  private final String lineSeparator;
  private final String outputExtension;
  private String defaultEncoding;
  private boolean alwaysUseDefaultEncoding;
  private final int lineLength;
  // maps URIs to filenames
  private final Map uriMap = new HashMap();
  private final String mainInputExtension;

  public LocalOutputDirectory(String mainSourceUri, File mainOutputFile, String extension,
                              String encoding, int lineLength) {
    this.mainOutputFile = mainOutputFile;
    this.outputExtension = extension;
    this.defaultEncoding = encoding;
    this.lineSeparator = System.getProperty("line.separator");
    this.lineLength = lineLength;
    this.uriMap.put(mainSourceUri, mainOutputFile.getName());
    int off = mainSourceUri.lastIndexOf('/') + 1;
    off = mainSourceUri.lastIndexOf('.', off);
    this.mainInputExtension = off < 0 ? "" : mainSourceUri.substring(off);
  }

  public void setEncoding(String encoding) {
    defaultEncoding = encoding;
    alwaysUseDefaultEncoding = true;
  }

  public Stream open(String sourceUri, String encoding) throws IOException {
    if (encoding == null || alwaysUseDefaultEncoding)
      encoding = defaultEncoding;
    String javaEncoding = EncodingMap.getJavaName(encoding);
    File file = new File(mainOutputFile.getParentFile(), mapFilename(sourceUri));
    return new Stream(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file)), javaEncoding),
                      encoding,
                      CharRepertoire.getInstance(javaEncoding));
  }

  public String reference(String fromSourceUri, String toSourceUri) {
    return mapFilename(toSourceUri);
  }

  private String mapFilename(String sourceUri) {
    String filename = (String)uriMap.get(sourceUri);
    if (filename == null) {
      filename = chooseFilename(sourceUri);
      uriMap.put(sourceUri, filename);<commit_msg>Fix bug in computing mainInputExtension<commit_after>package com.thaiopensource.relaxng.output;

import com.thaiopensource.xml.util.EncodingMap;
import com.thaiopensource.xml.out.CharRepertoire;

import java.io.Writer;
import java.io.IOException;
import java.io.File;
import java.io.OutputStreamWriter;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.HashMap;

public class LocalOutputDirectory implements OutputDirectory {
  private final File mainOutputFile;
  private final String lineSeparator;
  private final String outputExtension;
  private String defaultEncoding;
  private boolean alwaysUseDefaultEncoding;
  private final int lineLength;
  // maps URIs to filenames
  private final Map uriMap = new HashMap();
  private final String mainInputExtension;

  public LocalOutputDirectory(String mainSourceUri, File mainOutputFile, String extension,
                              String encoding, int lineLength) {
    this.mainOutputFile = mainOutputFile;
    this.outputExtension = extension;
    this.defaultEncoding = encoding;
    this.lineSeparator = System.getProperty("line.separator");
    this.lineLength = lineLength;
    this.uriMap.put(mainSourceUri, mainOutputFile.getName());
    int slashOff = mainSourceUri.lastIndexOf('/');
    int dotOff = mainSourceUri.lastIndexOf('.');
    this.mainInputExtension = dotOff > 0 && dotOff > slashOff ? mainSourceUri.substring(dotOff) : "";
  }

  public void setEncoding(String encoding) {
    defaultEncoding = encoding;
    alwaysUseDefaultEncoding = true;
  }

  public Stream open(String sourceUri, String encoding) throws IOException {
    if (encoding == null || alwaysUseDefaultEncoding)
      encoding = defaultEncoding;
    String javaEncoding = EncodingMap.getJavaName(encoding);
    File file = new File(mainOutputFile.getParentFile(), mapFilename(sourceUri));
    return new Stream(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file)), javaEncoding),
                      encoding,
                      CharRepertoire.getInstance(javaEncoding));
  }

  public String reference(String fromSourceUri, String toSourceUri) {
    return mapFilename(toSourceUri);
  }

  private String mapFilename(String sourceUri) {
    String filename = (String)uriMap.get(sourceUri);
    if (filename == null) {
      filename = chooseFilename(sourceUri);
      uriMap.put(sourceUri, filename);
{'fraction_non_alphanumeric': 0.08548252911813643, 'fraction_numerical': 0.0006239600665557404, 'mean_word_length': 4.29042904290429, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '2654496', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1253, 'n_words': 320}