You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Evert Hoff <ev...@pixie.co.za> on 2001/05/19 04:38:58 UTC
DTD Grammar Document is empty
Hi,
I am new to this mailing list and the archives are down, so I couldn't
check whether this has already been discussed.
I would like to access the Grammar Document for a DTD, but I just
receive an empty document. It works well for a Schema. Below is the code
with which I am trying to do this.
This is the only way that I could think of to get access to the Grammar
Document. Is there a right way that I am missing?
I don't understand the Xerces code well - although I've tried! So, any
help will be appreciated.
Thanks,
Evert
-----------------------------------------------------
import org.apache.xerces.parsers.DOMParser;
import org.apache.xerces.validators.common.*;
import org.apache.xerces.validators.dtd.DTDGrammar;
import java.io.*;
import org.w3c.dom.*;
import java.util.*;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXException;
import org.apache.xerces.utils.QName;
public class GrammarAccess extends DOMParser
{
public GrammarResolver getGrammarResolver()
{
return fGrammarResolver;
}
public static void main( String[] args )
{
if ( args.length != 1 )
{
System.out.println( "Usage: uri" );
System.exit( 0 );
}
String uri = args[0];
GrammarAccess parser = new GrammarAccess();
try {
parser.setFeature("http://apache.org/xml/features/domx/grammar-access",
true); }
catch (Exception e) { System.out.println("warning: unable to set
feature."); }
try
{
parser.setValidation( true );
parser.setErrorHandler( new MyErrorHandler() );
System.out.println( "Validation On: " + parser.getValidation() );
parser.parse( uri );
Document doc = parser.getDocument();
if ( doc == null )
System.out.println( "Document is null." );
else
System.out.println( "Document is not null." );
//print( doc );
GrammarResolver resolver = parser.getGrammarResolver();
if ( resolver == null )
System.out.println( "GrammerResolver is null." );
else
System.out.println( "GrammarResolver is not null." );
Enumeration nameSpaces = resolver.nameSpaceKeys();
while ( nameSpaces.hasMoreElements() )
{
String nameSpace = (String)nameSpaces.nextElement();
System.out.println( "NameSpace: " + nameSpace );
//Grammar grammar = resolver.getGrammar( nameSpace );
//if ( grammar == null )
// System.out.println( "Grammar is null." );
//else
// System.out.println( "Grammar is not null. " );
}
Grammar grammar = resolver.getGrammar( "" );
if ( grammar == null )
System.out.println( "Grammar is null." );
else
System.out.println( "Grammar is not null. " );
if ( grammar instanceof DTDGrammar )
System.out.println( "DTDGrammar" );
Document grammarDocument = grammar.getGrammarDocument();
if ( grammarDocument == null )
System.out.println( "Grammar document is null." );
else
System.out.println( "Grammar document is not null. " );
print( grammarDocument );
System.out.println( "Grammar document: " + grammarDocument );
XMLContentModel model = grammar.getElementContentModel( 1 );
if ( model == null )
System.out.println( "Content model is null." );
else
System.out.println( "Content model is not null. " );
XMLElementDecl elem = new XMLElementDecl();
boolean found = grammar.getElementDecl( 0, elem );
System.out.println( "Found element declaration: " + found );
System.out.println( "Element " + elem.name + " type: " +
elem.type );
}
catch ( Exception ex )
{
System.out.println( ex );
ex.printStackTrace();
}
}
/** Prints the specified node, recursively. */
public static void print(Node node) {
// is there anything to do?
if ( node == null ) {
return;
}
int type = node.getNodeType();
switch ( type ) {
// print document
case Node.DOCUMENT_NODE: {
NodeList children = node.getChildNodes();
for ( int iChild = 0; iChild < children.getLength();
iChild++ ) {
print(children.item(iChild));
}
System.out.flush();
break;
}
// print element with attributes
case Node.ELEMENT_NODE: {
System.out.print('<');
System.out.print(node.getNodeName());
Attr attrs[] = sortAttributes(node.getAttributes());
for ( int i = 0; i < attrs.length; i++ ) {
Attr attr = attrs[i];
System.out.print(' ');
System.out.print(attr.getNodeName());
System.out.print("=\"");
System.out.print(attr.getNodeValue());
System.out.print('"');
}
System.out.print('>');
NodeList children = node.getChildNodes();
if ( children != null ) {
int len = children.getLength();
for ( int i = 0; i < len; i++ ) {
print(children.item(i));
}
}
break;
}
// handle entity reference nodes
case Node.ENTITY_REFERENCE_NODE: {
System.out.print('&');
System.out.print(node.getNodeName());
System.out.print(';');
break;
}
// print cdata sections
case Node.CDATA_SECTION_NODE: {
System.out.print("<![CDATA[");
System.out.print(node.getNodeValue());
System.out.print("]]>");
break;
}
// print text
case Node.TEXT_NODE: {
System.out.print(node.getNodeValue());
break;
}
// print processing instruction
case Node.PROCESSING_INSTRUCTION_NODE: {
System.out.print("<?");
System.out.print(node.getNodeName());
String data = node.getNodeValue();
if ( data != null && data.length() > 0 ) {
System.out.print(' ');
System.out.print(data);
}
System.out.println("?>");
break;
}
}
if ( type == Node.ELEMENT_NODE ) {
System.out.print("</");
System.out.print(node.getNodeName());
System.out.print('>');
}
System.out.flush();
} // print(Node)
/** Returns a sorted list of attributes. */
protected static Attr[] sortAttributes(NamedNodeMap attrs) {
int len = (attrs != null) ? attrs.getLength() : 0;
Attr array[] = new Attr[len];
for ( int i = 0; i < len; i++ ) {
array[i] = (Attr)attrs.item(i);
}
for ( int i = 0; i < len - 1; i++ ) {
String name = array[i].getNodeName();
int index = i;
for ( int j = i + 1; j < len; j++ ) {
String curName = array[j].getNodeName();
if ( curName.compareTo(name) < 0 ) {
name = curName;
index = j;
}
}
if ( index != i ) {
Attr temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
return(array);
} // sortAttributes(NamedNodeMap):Attr[]
}
class MyErrorHandler implements ErrorHandler
{
public void warning (SAXParseException exception)
throws SAXException
{
System.out.println( "Warning: " + exception );
}
public void error (SAXParseException exception)
throws SAXException
{
System.out.println( "Error: " + exception );
}
public void fatalError (SAXParseException exception)
throws SAXException
{
System.out.println( "Fatal: " + exception );
}
}
-------------------------------------------
Output:
Document is not null.
GrammarResolver is not null.
NameSpace:
Grammar is not null.
DTDGrammar
Grammar document is not null.
Grammar document: [#document: null]
Content model is not null.
Found element declaration: true
Element prefix: -1, localpart: 39, rawname: 39, uri: 0 type: 4
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-dev-help@xml.apache.org