FastParser

http://www.japisoft.com

v1.2



Features
:
FastParser is an XML parser for non validating XML processing, it is based on a Turing finite state automaton.


Benchmark parsing result with JDK1.4 usage :

File FastParser
Xerces 2
JDK 1.4 parser
test.xml (3296 bytes)
80 ms
414 ms
472 ms
test2.xml (16089 bytes)
112 ms
450 ms
493 ms


FastParser is provided with several package level independently working : You may have to use only one or several packages. For example,
applet usage should not use the DOM support and could work only with fp-parser.jar.

Package
Role
fp-parser.jar
Core parser
fp-sax.jar, fp-parser.jar
SAX parsing
fp-dom.jar, fp-parser.jar, dom.jar
DOM support
fp-tool.jar
Swing node

FastParser is a shareware, it is free to try for 30 days, else you must register the full version at : http://www.japisoft.com/buy.html

I. Parsing simple usage
II. Parsing with Node for Swing JTree
III. Navigate easily with Walker
IV. Sax usage
V.
DOM Usage
VI. Parsing mode

I. Parsing simple usage

Target package : fp-parser.jar

import java.io.*;

import java.util.*;

import com.japisoft.fastparser.*;
import com.japisoft.fastparser.node.*;
import com.japisoft.fastparser.walker.*;

/**
 * Simple sample of parsing
 * @author (c) 2002-2003 JAPISOFT
 * @version 1.0
 * @since 1.0
 */
public class Demo {

    public static void main( String[] args ) throws Throwable {
        Parser p = new Parser();
        p.setInputStream( new FileInputStream( args[ 0 ] ) );
        System.out.println( "Parsing " + args[ 0 ] );
        p.parse();
        SimpleNode root = (SimpleNode)p.getDocument().getRoot();
        System.out.println( "Parsing root result = " + root );
    }
}

This is a demonstration without SAX and DOM from the FastParser core package (fp-parser.jar). SimpleNode contains a lot
of facilities like (cloning, navigating, attributes, mutation....). Note that this node can be easily replaced thanks to the NodeFactory class.

II. Parsing with Node for Swing JTree

Target package : fp-parser.jar, fp-tool.jar

Here a sample of Node change with Swing JTree node support. Get the full sources from the distribution.


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.tree.*;

import com.japisoft.fastparser.*;
import com.japisoft.fastparser.document.*;

/**
* Demonstration of parsing with a Jtree
 * @author (c) 2002-2003 JAPISOFT
 * @version 1.0
 */
public class Demo extends JFrame implements ActionListener {

private JTree tree;

    public Demo() {
        super();
        JButton b = new JButton( "Click
to Select an XML file (use /xml-data)" );
        b.addActionListener( this );
        getContentPane().add( BorderLayout.NORTH, b );
        getContentPane().add( BorderLayout.CENTER,new JScrollPane( tree = new JTree() ) );
        setSize( 300, 400 );
        setTitle( "Swing demo" );
    }

    public void actionPerformed( ActionEvent e ) {
        // XML Selection
        JFileChooser chooser = new JFileChooser();
        int returnVal = chooser.showOpenDialog( this );
        if ( returnVal == JFileChooser.APPROVE_OPTION ) {
            File f = chooser.getSelectedFile();
            try {
             Parser p = new Parser();
            p.setNodeFactory( new SwingNodeFactory() );
p.setInputStream( new FileInputStream( f ) );
            p.parse();
Document d = p.getDocument();  
tree.setModel( new DefaultTreeModel( (TreeNode)d.getRoot() ) );
            } catch( Throwable th ) {
               th.printStackTrace();
               JOptionPane.showMessageDialog( null, "Error", th.getMessage(),JOptionPane.ERROR_MESSAGE );
            }
        }
    }


    public static void main( String[] a ) {
        Demo d = new Demo();
        d.setVisible( true );
    }
}

In this sample we provide to the parser a custom NodeFactory (SwingNodeFactory) that generated Node supporting the javax.swing.tree.TreeNode interface.

III. Navigate easily with Walker

Target package : fp-parser.jar

Walker is a toolkit for navigating between tag, it supports a deeply and non deeply mode.


import java.io.*;

import java.util.*;

import com.japisoft.fastparser.*;
import com.japisoft.fastparser.node.*;
import com.japisoft.fastparser.walker.*;

/**
 * Simple of the <code>TreeWalker</code> for navigating in the parsed tree
 *
 * @author (c) 2002-2003 JAPISOFT
 * @version 1.0
 * @since 1.0
 */
public class Demo {

    public static void main( String[] args ) throws Throwable {
        Parser p = new Parser();
        p.setInputStream( new FileInputStream( args[ 0 ] ) );
        p.parse();
        SimpleNode root = (SimpleNode)p.getDocument().getRoot();
        TreeWalker t = new TreeWalker( root );
        Enumeration enum = t.getTagNodeByName( "loc", true );
        System.out.println( "Show loc tag" );
        while ( enum.hasMoreElements() ) {
            System.out.println( "Match node :" + enum.nextElement() );
        }
    }
}



This sample parses a document and prints all "loc" tag node.

IV. SAX usage

Target packages : fp-parser.jar, fp-sax.jar, sax2.jar

FastParser is Sax level 1 and 2 compliant.

The SaxParser class is for SAX level 1, The SaxParser2 class is for SAX level 2.


import org.xml.sax.*;
import org.xml.sax.helpers.*;
import com.japisoft.fastparser.*;

import java.io.*;

/**
 * Sample of Sax usage inside the Xerces API. This is
 * a case of <code>FastParser</code> integration without changing
 * your DOM API usage.
 *
 * This class shows all SAX event during parsing.
 *
 * @author (c) 2002-2003 JAPISOFT
 * @version 1.0
 * @since 1.0
 */
public class Demo implements DocumentHandler {

    public void setDocumentLocator (Locator locator) {
    }

    public void startDocument()
        throws SAXException {
        System.out.println( "- start document" );
    }

    public void endDocument()
        throws SAXException {
        System.out.println( "- end document" );
    }

    public void startElement(String name, AttributeList atts)
        throws SAXException {
        System.out.println( "* start tag " + name + " / " + printAttributes( atts ) );
    }

    public void endElement(String name)
        throws SAXException {
        System.out.println( "* end tag" + name );
    }

    public void characters(char ch[], int start, int length)
        throws SAXException {
        System.out.println( "+ text [" + new String( ch ) + "]" );
    }

public void ignorableWhitespace(char ch[], int start,int length)
        throws SAXException {
    }

    public void processingInstruction(String target, String data)
        throws SAXException {
        System.out.println( "! instruction " + target + " " + data );
    }

    private String printAttributes( AttributeList atts ) {
        StringBuffer s = new StringBuffer();
        if ( atts != null ) {
            for ( int i = 0; i < atts.getLength(); i++ ) {
s.append( atts.getName( i ) + "=" + atts.getValue( i ) ).append( " /" );
            }
        }
        return s.toString();
    }

    public static void main( String[] args ) throws Throwable {
        System.out.println( "SAX usage sample" );
        SaxParser p = new SaxParser();
        p.setDocumentHandler( new Demo() );
        p.parse( new InputSource( new FileInputStream( args[ 0 ] ) ) );
    }
}

This sample prints all SAX events.

V. DOM Usage

Target packages : fp-parser.jar, fp-dom.jar, dom.jar

DOM API used the FastParser NodeFactory for producing adapted node.


import com.japisoft.fastparser.*;
import com.japisoft.fastparser.document.Document;
import com.japisoft.fastparser.dom.*;


import java.io.*;

import org.w3c.dom.*;

/**
 * Here a sample of DOM usage. This sample parse and creates a DOM tree,it walks
 * through it and writes the tree structure.
 *
 * @author (c) 2002-2003 JAPISOFT
 * @version 1.0
 */
public class Demo {

public Demo() {
        super();
    }

static void walk( Node n ) {
        if ( n instanceof Text ) {
            System.out.println( "TEXT" );
        } else
            
if ( n instanceof Comment ) {
              System.out.println( "COMMENT" );
} else           
if ( n instanceof Element ) {
System.out.println( ( (Element)n ).getTagName() );
NodeList l = ( (Element)n ).getChildNodes();       
for ( int i = 0; i < l.getLength(); i++ ) {           
walk( l.item( i ) );
}   
}
    }

    public static void main( String[] args ) throws Throwable {
        Parser p  = new Parser();
        p.setNodeFactory( new DomNodeFactory() );
        // Parse the first argument file
        System.out.println( "Parse " + args[ 0 ] );
        p.setInputStream( new FileInputStream( args[ 0 ] ) );
        p.parse();
        Document d = p.getDocument();
        // Extract the DOM root
        Element e = ( Element )d.getRoot();
        System.out.println( "SHOW FOUND TAGS" );
        System.out.println( "ROOT = "+ e );
        walk( e );
    }

}

This sample navigates through DOM API node parsing result

VI. Parsing mode

FastParser offers the following parsing mode :
These modes are available using the setParserMode method on the Parser class.


(c) 2002-2003 Alexandre Brillant / JAPISOFT