Help with XMLFilters

Help with XMLFilters

Post by Bruce Mcdougal » Fri, 04 Oct 2002 05:54:10



I am having trouble transforming a parsed XML document.  I have set up a
servlet with two XML Filters the first to parse the XML document, and the
second to apply the stylesheet.  However, when I run the following servlet,
the output from the XSLT has the contents of the entire XML doc, not the
parsed version.  I'm not sure what I'm doing wrong.

I had the xmlParser() working before as a handler to an XML Reader just to
see it reduce the size of my original XML document, but now I do not see it
ever being executed.

Thanks for your help.

---------------------------------------
SOURCE CODE:
--------------------------------------

package tstparse;

// java classes
import java.io.*;
import java.lang.*;
import java.util.*;

// Servlet Classes
import javax.servlet.*;
import javax.servlet.http.*;

// JAXP
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;

// SAX
// import org.xml.sax.AttributeList; deprecated
import org.xml.sax.Attributes;
// import org.xml.sax.HandlerBase; deprecated
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLFilter;
import org.xml.sax.helpers.XMLFilterImpl;
import org.xml.sax.ContentHandler;
import org.xml.sax.ErrorHandler;

// TraX imports
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;

// XALAN Classes
import org.apache.xalan.serialize.Serializer;
import org.apache.xalan.serialize.SerializerFactory;
import org.apache.xalan.templates.OutputProperties;
import org.xml.sax.helpers.XMLReaderFactory;

public class tstParse extends HttpServlet {

    public void doGet (HttpServletRequest req, HttpServletResponse res)
 throws ServletException,IOException
    {

  try{

    TransformerFactory tFactory = TransformerFactory.newInstance();

    if (tFactory.getFeature(SAXSource.FEATURE) &&
tFactory.getFeature(SAXResult.FEATURE))
    {

      XMLReader reader = XMLReaderFactory.createXMLReader();

      // Cast the TransformerFactory to SAXTransformerFactory.
      SAXTransformerFactory saxTFactory = ((SAXTransformerFactory)
tFactory);

      // Create an XMLFilter to parse the xml
      XMLFilter xmlFilter1 = new xmlParse(reader);

      // Create an XMLFilter for the stylesheet
      XMLFilter xmlFilter2 =
               saxTFactory.newXMLFilter(new
StreamSource("http://localhost/taymac.nsf/dynitmact.xsl"));

      // xmlFilter1 uses the XMLReader as its reader.
      xmlFilter1.setParent(reader);

      // xmlFilter2 uses the XMLFilter1 as its reader.
      xmlFilter2.setParent(xmlFilter1);

      // xmlFilter2 outputs SAX events to the serializer.
      Serializer serializer = SerializerFactory.getSerializer

(OutputProperties.getDefaultMethodProperties("html"));

      serializer.setOutputStream(System.out);

      xmlFilter2.setContentHandler(serializer.asContentHandler());

      xmlFilter2.parse(new
InputSource("http://localhost/cgi-bin/xmlbldr.pl"));
    }

}

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

}
}

-------------------------------
Parsing Filter
-------------------------------

public class xmlParse extends XMLFilterImpl      //  was DefaultHandler
{

    String col;
    String row;
    String drvr;
    String root;
    String node;

    PrintWriter pw = new PrintWriter(System.out,true);

    static boolean  rootFlag = false;
    static boolean  drvrFlag = false;
    static boolean  colFlag = false;
    static boolean  rowFlag = false;

    static int drvrCount = 0;
    static int colCount = 0;
    static int rowCount = 0;

    static int colStart = 85;
    static int rowStart = 0;

    static int colLimit = 13;
    static int rowLimit = 25;
    static int drvrLimit = 3;

    public xmlParse (XMLReader reader)
    {
 super(reader);
    }

    ////////////////////////////////////////////////////////////////////
    // Event handlers.
    ////////////////////////////////////////////////////////////////////

    public void startDocument ()
    {
     pw.flush();
         pw.println("content-type: text/xml");
  pw.println("<?xml version=\"1.0\" ?>");
    }

    public void endDocument ()
    {
         //pw.println("\n");
       pw.flush();
    }

    public void startElement (String uri, String name,
         String qName, Attributes atts)
                              throws SAXException
    {
        node = qName;

        if ( qName.equals( "ITMACT" ) ) {
             rootFlag = true;
             rowFlag = false;
             colFlag = false;
             drvrFlag = false;
             root = qName;
             pw.println("<" + qName + ">");
       }

        if ( qName.equals( "DRIVER" ) ) {
             drvrCount++;
             rowCount=0;
             rowFlag = false;
             colFlag = false;
             if (drvrCount == drvrLimit){
                drvrFlag = true;
                drvr = qName;
                pw.println("<" + qName + ">");
             }else{
                 drvrFlag = false;
             }
        }

        if ( drvrFlag && qName.equals("DRVRKEY") )  {
             pw.print("<" + qName + ">");
       }

        if (qName.equals("ROW") && drvrFlag){
           rowCount++;
           colCount = 0;
           colFlag = false;
           if ((rowCount >= rowStart) &&
               (rowCount <= rowStart + rowLimit)){
                rowFlag = true;
                row = qName;
                pw.println("<" + qName + ">");
             }else{
               rowFlag = false;
             }

        }

        if ( qName.equals("ROWKEY") && rowFlag ) {
                pw.print("<" + qName + ">");
          }

        if (qName.equals("COLUMN") && rowFlag){
           colCount++;
           if ((colCount >= colStart) &&
               (colCount <= colStart + colLimit)){
                colFlag = true;
                col = qName;
                pw.println("<" + qName + ">");
           }else{
               colFlag = false;
           }
        }

        if (( (qName.equals("COLKEY")) ||
              (qName.equals("CDATA1")) ||
              (qName.equals("CDATA2")) ||
              (qName.equals("CDATA3")) ) && colFlag ) {
              pw.print("<" + qName + ">");
        }

    }

///////////////////////////////////////////////////////////////
    public void endElement (String uri, String name, String qName)
       throws SAXException
    {

       if ( qName.equals("ITMACT") ) {
                pw.println("</" + qName + ">");
       }

       if ( drvrFlag && ( (qName.equals("DRIVER"))||
(qName.equals("DRVRKEY")) )) {
             pw.println("</" + qName + ">");
 //            pw.println("</ITMACT>");
       }

        if ( ((qName.equals("ROW")) || (qName.equals("ROWKEY"))) &&
rowFlag ) {
                pw.println("</" + qName + ">");
          }

        if (( (qName.equals("COLUMN")) ||
              (qName.equals("COLKEY")) ||
              (qName.equals("CDATA1")) ||
              (qName.equals("CDATA2")) ||
              (qName.equals("CDATA3"))) && colFlag ) {
                    pw.println("</" + qName + ">");
        }

 //if ("".equals (uri))
 //    System.out.println("End element: " + qName);
 //else
 //    System.out.println("End element:   {" + uri + "}" + name);

    }

    public void characters (char ch[], int start, int length)
    {
        boolean wFlag = false;

        if(drvrFlag && ( node.equals("DRIVER") || node.equals("DRVRKEY")))
{ wFlag = true;};

        if(rowFlag && ( node.equals("ROW") || node.equals("ROWKEY"))) {wFlag
= true;};

        if(colFlag){ wFlag=true; }

        if(wFlag) {
 for (int i = start; i < start + length; i++) {
     switch (ch[i]) {
     case '\\':
         pw.print("\\\\");
  break;
     case '"':
         pw.print("\\\"");
  break;
     case '\n':
  //System.out.print("\\n");
  break;
     case '\r':
         pw.print("\\r");
  break;
     case '\t':
        pw.print("\\t");
  break;
     default:
  pw.print(ch[i]);
  break;
     }
 }

     }
  }
  }

 
 
 

1. SAXResult with XMLFilter chain

Hi everyone,

I'm trying to transform an XML document via XSLT where the result of the
tranformation gets postprocessed by an XMLFilter. I am using the dom4j
and JAXP APIs. I do this by pushing the result of the transformation
into a SAXResult that gets instantiated with the filter as argument. The
actual filter is an object of class XMLWriter that writes its output
into a StringWriter.
My problem: everything works fine when I use one XMLWriter to filter the
SAX events. But I would like to chain filters to provide further
processing of the transformation result. I tried to do this by calling
the #setParent method with no success. I tried some other variations
with no luck. No more than the filter is called that is passed
in the SAXResult constructor.

Any ideas?

- Knut

--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG

2. Visio2000 not displaying correctly on Windows 2000

3. XMLFilter with SAX

4. Workstation drive sharing to SBS Admin

5. UTF-8, XML, XSL, ASP help :) please - HELP HELP HELP

6. Kyocera Users! Your Opinion Please

7. Help, Help, Help

8. Who uses MPC8XX?

9. HELP HELP HELP !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

10. Dissertation Help - Please HELP!!!

11. How can XML help me, pls help?

12. HELP, HELP, NEWBIE QUESTION

13. Please Help to create this xml file - Please help me to solve this problem Please