package com.thaiopensource.validate.schematron;

import com.thaiopensource.util.PropertyMap;
import com.thaiopensource.validate.ValidateProperty;
import com.thaiopensource.validate.Validator;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.ErrorHandler;

import javax.xml.transform.Templates;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;

class ValidatorImpl implements Validator {
  private final Templates templates;
  private final SAXTransformerFactory factory;
  private final ContentHandler outputHandler;
  private TransformerHandler transformerHandler;

  ValidatorImpl(Templates templates, SAXTransformerFactory factory, PropertyMap properties) {
    this.templates = templates;
    this.factory = factory;
    ErrorHandler eh = ValidateProperty.ERROR_HANDLER.get(properties);
    outputHandler = new OutputHandler(eh);
    initTransformerHandler();
  }

  public ContentHandler getContentHandler() {
    return transformerHandler;
  }

  public DTDHandler getDTDHandler() {
    return transformerHandler;
  }

  public void reset() {
    initTransformerHandler();
  }

  private void initTransformerHandler() {
    try {
      transformerHandler = factory.newTransformerHandler(templates);
      // XXX set up transformer with a resolver that uses the resolver
      // XXX and XMLReaderCreator from properties
      // XXX set up transformer with an ErrorListener that just throws
      // XXX (what about errors from document() calls?)
    }
    catch (TransformerConfigurationException e) {
      throw new RuntimeException("could not create transformer");
    }
    transformerHandler.setResult(new SAXResult(outputHandler));
  }
}

@@ -86,16 +86,68 @@
 rtyMap;%0A
+import com.thaiopensource.validate.ResolverFactory;%0A
 import c
@@ -191,16 +191,16 @@
 operty;%0A
-
 import c
@@ -237,16 +237,60 @@
 idator;%0A
+import com.thaiopensource.xml.sax.Resolver;%0A
 import o
@@ -481,16 +481,56 @@
 eption;%0A
+import javax.xml.transform.URIResolver;%0A
 import j
@@ -890,16 +890,51 @@
 Handler;
+%0A  private final Resolver resolver;
 %0A%0A  Vali
@@ -1194,16 +1194,75 @@
 er(eh);%0A
+    resolver = ResolverFactory.createResolver(properties);%0A
     init
@@ -1634,122 +1634,164 @@
     
-// XXX set up transformer with a resolver that uses the resolver%0A      // XXX and XMLReaderCreator from properties
+URIResolver uriResolver = resolver.getUriResolver();%0A      if (uriResolver != null)%0A        transformerHandler.getTransformer().setURIResolver(uriResolver);
 %0A   

Set a URIResolver on the schematron transformerHandler.