/*--- formatted by Jindent 2.1, (www.c-lab.de/~jindent) ---*/

// copyright => http://www.jxml.com/mdsax/License.txt

package com.jxml.quick;



import java.io.*;
import java.text.*;
import java.util.*;


/**
 * Class declaration
 *
 *
 * <p>author Hannu Pääkkö
 * <p>updated for compatibility with 1.1a1 QExpressFactory--Quick 1.1a2
 */
public class QPrintWriterExpressFactory extends QExpressFactory
{
    PrintWriter pw;


    /**
     * Method declaration
     *
     *
     *
     * @throws QE
     *
     */
    protected QFilter newFilter() throws QE
    {
        return new QPrintWriterExpress(pw);
    }


    public QPrintWriterExpressFactory(PrintWriter pw)
    {
       this.pw=pw;
    }


    /**
     * Class declaration
     *
     *
     * @author Hannu Pääkkö
     */
    public class QPrintWriterExpress extends QExpress
    {

        PrintWriter pw=null;
        /**
         * Constructor declaration
         *
         *
         */
        private QPrintWriterExpress() throws QE
        {
           this(new PrintWriter(System.err, true));
           //System.err.println("QPrintWriterExpress default!");
        }



        /**
         * Constructor declaration
         *
         *
         */
        public QPrintWriterExpress(PrintWriter pw) throws QE
        {
           this.pw=pw;
        }


        private void flush()
        {
           if(b.length()>32000)
           {
               forced_flush();
           }
        }


        private void forced_flush()
        {
           pw.print(b.toString());
           b.setLength(0);
        }


        /**
         * Method declaration
         *
         *
         * @throws QPE
         *
         */
        public void endDocument() throws QPE
        {
            //System.err.println("PW endDocument");
            forced_flush();
            context.doc.setRoot(null);
        }

        /**
         * Method declaration
         *
         */
        public void newline()
        {
            flush();
            super.newline();
        }
    }
}
