users@fi.java.net

[Fwd: Bug in DocumentSerialier sample code]

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 16 Feb 2005 09:00:04 +0100

Hi,

Lei has found a problem with the sample code.

Joe, can you work out what is wrong.

Paul.

-------- Original Message --------
Subject: Bug in DocumentSerialier sample code
Date: Tue, 15 Feb 2005 18:45:18 -0800
From: Lei Liu <Lei.Liu_at_Sun.COM>

Hi:

I have found the bug in DocumentSerializer.java sample code. It
generated FI document in size 0.
After I have changed the code, it works (I can read the FI document ).
However, it created very
large size document compared with SAX serializer. Why the file get
bigger using STAX ?
The changed code is listed below:


/*
* DocumentSerializer.java
*
* Created on February 15, 2005, 5:27 PM
*/

/**
*
* @author Administrator
*/
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.ByteArrayOutputStream;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import javax.xml.transform.sax.SAXResult;

import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import com.sun.xml.fastinfoset.stax.StAXInputFactory;
import com.sun.xml.fastinfoset.stax.SAX2StAXWriter;

/** <p>FI StAX document serializer</p>
* Demonstrates the use of StAXDocumentSerializer to transform
* an XML file into a FastInfoset document
*/
public class DocumentSerializer {
    XMLOutputFactory factory;
    String _xmlFile;
    Transformer _transformer;
    DocumentBuilder _docBuilder;
    DOMSource _source = null;
    SAXResult _result = null;
    ByteArrayOutputStream _baos;

    /** Creates a new instance of DocumentSerializer */
    public DocumentSerializer() {
        try {
            _transformer =
TransformerFactory.newInstance().newTransformer();
            _docBuilder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
            factory = XMLOutputFactory.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

     /** Construct a DOMSource with a file.
     *
     * @param input the XML file input
     */
    void getDOMSource(File input) {
        try {
            FileInputStream fis = new FileInputStream(input);
            Document document = _docBuilder.parse(fis);
            fis.close();
            _source = new DOMSource(document);
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

    /** Initialize a SAXResult and set its handers.
     *
     * @param output FI document output
     */
    void getSAXResult(File output) {
        try {
            BufferedOutputStream fos = new BufferedOutputStream(new
FileOutputStream(output)); ------ uncommented
            //_baos = new
ByteArrayOutputStream();

------- commented
            //XMLStreamWriter serializer =
factory.createXMLStreamWriter(_baos);
--------- commented
            XMLStreamWriter serializer =
factory.createXMLStreamWriter(fos);

---------- add new line of code
            SAX2StAXWriter saxTostax = new SAX2StAXWriter(serializer);

            _result = new SAXResult();
            _result.setHandler(saxTostax);
            _result.setLexicalHandler(saxTostax);

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** Transform an XML file into a FI document.
     *
     * @param input an XML file input
     * @param output the FI document output
     */
    public void write(File input, File output) {
        getDOMSource(input);
        getSAXResult(output);
        for (int i=1; i<6; i++) {
           // _baos.reset();
            if (_source != null && _result != null) {
                try {
                    System.out.println("Transforming "+input.getName()+
" into " + output.getName());
                    _transformer.transform(_source, _result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("\ndone.");
        } else {
                System.out.println("Source or Result could not be null.");
            }
        }
        try {
           // BufferedOutputStream fos = new BufferedOutputStream(new
FileOutputStream(output)); -------- commented

//_baos.writeTo(fos);

-------- commented

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}



-- 
| ? + ? = To question
----------------\
    Paul Sandoz
         x38109
+33-4-76188109