NekoDTD comes with everything that you need to generate XML representations of DTDs. Your application can use the included NekoDTD parsers to parse a DTD and access the information using standard APIs such as the Document Object Model (DOM) or the Simple API for XML (SAX). Instead of writing custom code to access a DTD's information, the application can simply traverse a DOM tree representation of the grammar or pull the required information from the callbacks to the registered SAX handlers.
In addition, by using XSLT, many grammar transformations are possible without the need for custom code. To this end, NekoDTD includes stylesheets that allow you to transform the XML representation of your favorite DTD to a "flattened" DTD or to an equivalent XML Schema or Relax NG grammar. [Note: Other grammar formats may be supported in future releases. If you write a new stylesheet and want it included with NekoDTD, please contact me.]
The format of the generated document conforms to the "DTD for
XML" (DTDx) grammar defined by the author. This format maps
directly to the XNI callbacks of the XMLDTDHandler
and XMLDTDContentModel
interfaces and provides all
of the information that is available through these interfaces.
The grammar for DTDx, in
DTD format, is included with the NekoDTD package.
Note: The NekoDTD parser configuration is designed to be used with the Xerces2 reference implementation of the XNI framework. Therefore, it is assumed that you have a version of Xerces version 2.0.0 (or higher) installed on your system. If you do not already have Xerces2, you can download the latest version from Apache.
DOM and SAX parser classes are included in the
org.cyberneko.dtd.parsers
package for convenience. Both
parsers use the DTDConfiguration
class to be able to
parse DTD documents. The following example shows how to use the
NekoDTD DOMParser
directly:
package sample; import org.cyberneko.dtd.parsers.DOMParser; import org.w3c.dom.Document; import org.w3c.dom.Node; public class TestDTDDOM { public static void main(String[] argv) throws Exception { DOMParser parser = new DOMParser(); for (int i = 0; i < argv.length; i++) { parser.parse(argv[i]); print(parser.getDocument(), ""); } } public static void print(Node node, String indent) { System.out.println(indent+node.getClass().getName()); Node child = node.getFirstChild(); while (child != null) { print(child, indent+" "); child = child.getNextSibling(); } } }
Alternatively, you can construct any XNI-based parser class
using the DTDConfiguration
parser configuration class
found in the org.cyberneko.dtd
package. The following
example shows how to extend the abstract SAX parser provided with
the Xerces2 implementation by passing the NekoDTD parser
configuration to the base class in the constructor.
package sample; import org.apache.xerces.parsers.AbstractSAXParser; import org.cyberneko.dtd.DTDConfiguration; public class DTDSAXParser extends AbstractSAXParser { public DTDSAXParser() { super(new DTDConfiguration()); } }