XMLValidator

DTD Edition
Contact : http://www.japisoft.com/feedback.html
Home : http://www.japisoft.com

v1.0



Features
:
XMLValidator (DTD Edition) is ideal for adding validating support to your products or parsers. XMLValidator 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. Validation

We parse a DTD with the following instruction :
Parser p = new Parser();
p.parse( new FileInputStream( args[ 0 ] ) );

The parsing processing produces a root node (it shouldn't be confused with the root node of an XML document). This root node gives you access to all the DTD content. You can use it with the getDTDElement() method from the RootDTDNode.

Features of the root node : The Parser offers you some shorcuts like :
XMLValidator v = p.getXMLValidator();
XMLGenerator g = p.getXMLGenerator();

Here a complete sample that parses a DTD and try to validate several case from a test file. The
test file has this format :
A nodeName attributeName
E parentNodeName previousNodeName nodeName occurrence

In the first case we test if an attribute is valid, in the second line we check for a node in a tree.

Example :

<E>
    <A/>
    <B>
        ...
    </B>
</E>

We declare in the DTD

<!ELEMENT E (A, B)+>

So B is a part of E and must be after the A node. So if we validate with the instruction
E E A B 1 : this is ok!
E E A A 1 : this is not ok! (A is not before A)
E E A B 0 : this is not ok! (A should appears once)

import java.io.*;
import java.util.*;
import com.japisoft.dtdparser.*;

/**
* Sample of validating processing. This is
* a facility for testing cases.
*
* @author (c) 2002-2003 JAPISOFT
* @version 1.0
* @since 1.0
*/
public class Demo {

public static void main( String[] args ) throws Throwable {
// Parse it

Parser p = new Parser();
p.parse( new FileInputStream( args[ 0 ] ) );
XMLValidator v = p.getXMLValidator();

// List of case

BufferedReader br = new BufferedReader(
new FileReader( args[ 1 ] ) );
String line;
while ( ( line = br.readLine() ) != null ) {
if ( line.startsWith( "A" ) ) { // Attribute test
StringTokenizer st = new StringTokenizer( line );
st.nextToken();
System.out.println( "Test:" + line + ":" +
v.isAttributeValid( st.nextToken(), st.nextToken() ) );

} else
if ( line.startsWith( "E" ) ) { // Element test
StringTokenizer st = new StringTokenizer( line );
st.nextToken();
System.out.println( "Test:" + line + ":" +
v.isNodeValid( t( st.nextToken() ), t( st.nextToken() ), t( st.nextToken() ), Integer.parseInt( st.nextToken() ) ) );
}
}

br.close();

}

public static String t( String t ) {
if ( "[]".equals( t ) )
return null;
return t;
}


II. DTD content

Here we parse a DTD and write it.

import java.io.*;
import com.japisoft.dtdparser.Parser;
import com.japisoft.dtdparser.node.RootDTDNode;

/**
* Here a demonstration of the DTD writing support.
* This program parses a DTD and writes it after.
*
* @author (c) 2002 - 2003 JAPISOFT
* @version 1.0 */
public class Demo {
public Demo() {
super();
}

public static void main( String[] args ) throws Throwable {
if ( args.length != 1 ) {
System.err.println( "Invalid argument : need a dtd file " );
System.exit( 1 );
}

Parser p = new Parser();
p.parse( new FileInputStream( args[ 0 ] ) );
p.getDTDElement().writeDTD( new PrintWriter( System.out ) );
System.exit( 0 );
}

}


III. XML Document Building

Parsing a DTD a producing a minimal valid XML document is made by this sample :
Parser p = new Parser();
p.parse( new FileInputStream( f ) );
StringWriter sw = new StringWriter();
XMLGenerator xg = p.getXMLGenerator();
String root = p.getDTDElement().getFirstElementName();
String uri = f.toString();
xg.writeDocument( new PrintWriter( sw ), root, uri );

Note that we get the root document node by retreiving the first element declaration
thanks to the getFirstElementName method.

IV. DTD Document builder

Building a DTD tree is based on a similar SAX system. Such system can be used
to convert DTD to schema easily as sample.

A DTD document receives parsing event and uses it for building the root DTD node. Here
a sample of Document that writes all the parse events.



package com.japisoft.dtdparser.document;

/**
* Trace the DTD process building
* @author (c) 2002-2003 JAPISOFT
* @version 1.0
*/
public class TraceDTDDocumentBuilder extends AbstractDTDDocumentBuilder {
public TraceDTDDocumentBuilder() {
super();
}

/** Start the DTD definition */
public void notifyStartDTD() {
System.out.println( "* start the DTD" );
}

/** Stop the DTD definition */
public void notifyStopDTD() {
System.out.println( "* stop the DTD" );
}

public void notifyComment( String comment ) {
System.out.println( "* comment :" + comment );
}

public void notifyEntity( String entity, boolean reference, int type, String value ) {
System.out.println( "* entity : [" + entity + "] = [" + value + "]" );
}

public void notifyStartElement( String e ) {
System.out.println( "* start element [" + e + "]" );
}

public void notifyStopElement() {
System.out.println( "* stop element" );
}

/** Item equals element name or #PCDATA */
public void notifyElementChoiceItem( String item ) {
System.out.println( "* item choice element [" + item + "]" );
}

/** Item equals element name or EMPTY or ANY or #PCDATA */
public void notifyElementIncludeItem( String item ) {
System.out.println( "* item include element [" + item + "]" );
}

/** Notify '(' meet for the element declaration */
public void notifyStartElementChildren() {
System.out.println( "* notify start element children" );
}

/** Notify operator '+' or '*' or '?' */
public void notifyOperator( char operator ) {
System.out.println( "* notify operator : " + operator );
}

/** Notify ')' meet for the element declaration */
public void notifyStopElementChildren() {
System.out.println( "* notify stop element children" );
}

/**
* @param element Element tag
* @param id Attribute id
* @param valueType ID, IDREF, ENTITY, ENTITIES, NMTOKEN, NMTOKENS or CDATA
* @param enum a String[] value
* @param attDec REQUIRED, IMPLIED or FIXED
* @param def a String value or ""
*/
public void notifyAttribute(
String element,
String id,
int valueType,
String[] enum,
int attDec,
String def ) {
System.out.println( " notify attribute [" + id + "] for [" + element + "] /" + valueType + "/" + enum.
length + "/" + attDec + " [" + def + "]" );
}

}



(c) 2002-2003 Alexandre Brillant / JAPISOFT