/*
 * This file is part of the DITA Open Toolkit project hosted on
 * Sourceforge.net. See the accompanying license.txt file for
 * applicable licenses.
 */
/*
 * (c) Copyright IBM Corp. 2011 All Rights Reserved.
 */
package org.dita.dost.writer;

import static org.dita.dost.util.Constants.FILE_EXTENSION_TEMP;

import java.io.File;
import java.util.Properties;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLFilterImpl;

import org.dita.dost.exception.DITAOTException;
import org.dita.dost.log.DITAOTLogger;
import org.dita.dost.log.MessageUtils;
import org.dita.dost.module.Content;
import org.dita.dost.util.StringUtils;

/**
 * Base for XML filters.
 */
abstract class AbstractXMLFilter extends XMLFilterImpl implements AbstractWriter {

    protected DITAOTLogger logger;

    public abstract void setContent(Content content);

    public void write(final String filename) throws DITAOTException {
        final File inputFile = new File(filename);
        final File outputFile = new File(filename + FILE_EXTENSION_TEMP);
        try {
            final Transformer transformer = TransformerFactory.newInstance().newTransformer();
            final XMLReader reader = StringUtils.getXMLReader();
            //reader.setErrorHandler(new DITAOTXMLErrorHandler(filename));
            setParent(reader);
            final Source source = new SAXSource(this, new InputSource(inputFile.toURI().toString()));
            final Result result = new StreamResult(outputFile.toURI().toString());
            
            transformer.transform(source, result);

            // replace original file
            if (!inputFile.delete()) {
                final Properties prop = new Properties();
                prop.put("%1", inputFile.getPath());
                prop.put("%2", outputFile.getPath());
                logger.logError(MessageUtils.getMessage("DOTJ009E", prop).toString());
            }
            if (!outputFile.renameTo(inputFile)) {
                final Properties prop = new Properties();
                prop.put("%1", inputFile.getPath());
                prop.put("%2", outputFile.getPath());
                logger.logError(MessageUtils.getMessage("DOTJ009E", prop).toString());
            }
        } catch (final Exception e) {
            logger.logException(e);
        }
    }
    
	public final void setLogger(final DITAOTLogger logger) {
        this.logger = logger;
    }
	
}

@@ -327,16 +327,239 @@
 o.File;%0A
+import java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0Aimport java.io.BufferedInputStream;%0Aimport java.io.BufferedOutputStream;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0A
 import j
@@ -1165,16 +1165,35 @@
 ilters.%0A
+ *%0A * @since 1.5.4%0A
  */%0Aabst
@@ -1558,16 +1558,80 @@
 _TEMP);%0A
+        InputStream in = null;%0A        OutputStream out = null;%0A
         
@@ -1902,16 +1902,359 @@
 eader);%0A
+            // ContentHandler must be reset so e.g. Saxon 9.1 will reassign ContentHandler%0A            // when reusing filter with multiple Transformers.%0A            setContentHandler(null);%0A            in = new BufferedInputStream(new FileInputStream(inputFile));%0A            out = new BufferedOutputStream(new FileOutputStream(outputFile));%0A
         
@@ -2321,34 +2321,8 @@
 e(in
-putFile.toURI().toString()
 ));%0A
@@ -2379,34 +2379,8 @@
 (out
-putFile.toURI().toString()
 );%0A 
@@ -2442,21 +2442,526 @@
 esult);%0A
-%0A
     
+    %7D catch (final Exception e) %7B%0A            logger.logException(e);%0A        %7D finally %7B%0A            if (in != null) %7B%0A                try %7B%0A                    in.close();%0A                %7D catch (IOException e) %7B%0A                    logger.logException(e);%0A                %7D%0A            %7D%0A            if (out != null) %7B%0A                try %7B%0A                    out.close();%0A                %7D catch (IOException e) %7B%0A                    logger.logException(e);%0A                %7D%0A            %7D%0A        %7D%0A
         
@@ -2985,16 +2985,30 @@
 al file%0A
+        try %7B%0A
         

Fix AbstractXMLFilter reuse and rename bug - ID: 3343562