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);
    }
    return filename;
  }

  private String chooseFilename(String sourceUri) {
    String filename = sourceUri.substring(sourceUri.lastIndexOf('/') + 1);
    String base;
    if (filename.endsWith(mainInputExtension))
      base = filename.substring(0, filename.length() - mainInputExtension.length());
    else
      base = filename;
    filename = base + outputExtension;
    for (int i = 1; uriMap.containsValue(filename); i++)
      filename = base + Integer.toString(i) + outputExtension;
    return filename;
  }

  public String getLineSeparator() {
    return lineSeparator;
  }

  public int getLineLength() {
    return lineLength;
  }
}

@@ -1206,17 +1206,22 @@
     int 
-o
+slashO
 ff = mai
@@ -1251,19 +1251,22 @@
 '/')
- + 1
 ;%0A    
-o
+int dotO
 ff =
@@ -1299,13 +1299,8 @@
 ('.'
-, off
 );%0A 
@@ -1332,22 +1332,41 @@
 n = 
-off %3C 0 ? %22%22 :
+dotOff %3E 0 && dotOff %3E slashOff ?
  mai
@@ -1390,12 +1390,20 @@
 ing(
-off)
+dotOff) : %22%22
 ;%0A  

Fix bug in computing mainInputExtension