package com.thaiopensource.xml.dtd.app;

import java.io.IOException;

import com.thaiopensource.xml.dtd.om.Dtd;
import com.thaiopensource.xml.dtd.parse.DtdParserImpl;

public class Driver implements ErrorMessageHandler {
  
  public void message(ErrorMessage message) {
    switch (message.getSeverity()) {
    case ErrorMessage.WARNING:
      System.err.print("Warning:");
      break;
    case ErrorMessage.ERROR:
      System.err.print("Error:");
      break;
    }
    System.err.println(message.getMessage());
  }

  public static void main (String args[]) throws IOException {
    Dtd dtd = new DtdParserImpl().parse(args[0], new FileEntityManager());
    ExtensionMapper mapper = new ExtensionMapper(".dtd", ".rng");
    DirectoryOutputCollection out
      = new DirectoryOutputCollection(dtd.getUri(), args[1], mapper);
    RelaxNgWriter w = new RelaxNgWriter(out);
    w.setErrorMessageHandler(new Driver());
    w.writeDtd(dtd);
  }
}

@@ -61,16 +61,37 @@
 ception;
+%0Aimport java.io.File;
 %0A%0Aimport
@@ -185,28 +185,175 @@
 pl;%0A
-%0Apublic class Driver
+import com.thaiopensource.xml.util.Localizer;%0A%0Apublic class Driver %7B%0A%0A  private static final int FAILURE_EXIT_CODE = 1;%0A%0A  private static class ErrorMessageHandlerImpl
  imp
@@ -384,19 +384,51 @@
 ler %7B%0A  
-%0A
+  private int errorCount = 0;%0A%0A 
   
+ 
 public v
@@ -467,16 +467,18 @@
 ) %7B%0A    
+  
 switch (
@@ -498,24 +498,26 @@
 verity()) %7B%0A
+  
     case Err
@@ -539,57 +539,63 @@
 NG:%0A
-      System.err.print(%22Warning:%22);%0A      
+%09errorCount++;%0A%09error(message.getMessage());%0A%09
 break;%0A
+  
     
@@ -623,104 +623,255 @@
 OR:%0A
-      System.err.print(%22Error:%22);%0A      break;%0A    %7D%0A    System.err.println(message.getMessage()
+%09warning(message.getMessage());%0A%09break;%0A      %7D%0A    %7D%0A  %7D%0A%0A  public static void main(String%5B%5D args) %7B%0A    try %7B%0A      if (doMain(args))%0A%09return;%0A    %7D%0A    catch (IOException e) %7B%0A      error(e.getMessage());%0A    %7D%0A    System.exit(FAILURE_EXIT_CODE
 );%0A 
@@ -894,18 +894,22 @@
 tic 
-void m
+boolean doM
 ain
- 
 (Str
@@ -941,16 +941,317 @@
 ption %7B%0A
+    if (args.length != 2) %7B%0A      error(localizer().message(%22MISSING_ARGUMENT%22));%0A      print(localizer().message(%22USAGE%22));%0A      return false;%0A    %7D%0A    File dir = new File(args%5B1%5D);%0A    if (!dir.isDirectory()) %7B%0A      error(localizer().message(%22NOT_DIRECTORY%22, args%5B1%5D));%0A      return false;%0A    %7D%0A
     Dtd 
@@ -1321,25 +1321,20 @@
 ));%0A    
-Extension
+Name
 Mapper m
@@ -1464,23 +1464,19 @@
 tUri(), 
-args%5B1%5D
+dir
 , mapper
@@ -1532,66 +1532,504 @@
     
-w.setErrorMessageHandler(new Driver());%0A    w.writeDtd(dtd
+ErrorMessageHandlerImpl emh = new ErrorMessageHandlerImpl();%0A    w.setErrorMessageHandler(emh);%0A    w.writeDtd(dtd);%0A    return emh.errorCount == 0;%0A  %7D%0A%0A  private static Localizer localizer() %7B%0A    return RelaxNgWriter.localizer;%0A  %7D%0A%0A  private static void error(String str) %7B%0A    print(localizer().message(%22ERROR%22, str));%0A  %7D%0A%0A  private static void warning(String str) %7B%0A    print(localizer().message(%22WARNING%22, str));%0A  %7D%0A  %0A  private static void print(String str) %7B%0A    System.err.println(str
 );%0A 

Improve error handling.