The Notation Interface


The Notation interface represents a notation declared in the DTD; it is not part of the tree. A Notation object has no parent and no children. The only way to access a document's notations is through the getNotations() method of the DocumentType object.

Note

Notations are very uncommon in practice. You may want to skip this section unless you discover a specific need for it.


Example 11.24 summarizes the Notation interface. This interface has methods to get the system ID and public ID for the notation, either of which may be null. As usual, Notation objects also have all the methods of the Node superinterface such as getNodeName() and getNodeValue() .

Example 11.24 The Notation Interface
 package org.w3c.dom; public interface Notation extends Node {   public String getPublicId();   public String getSystemId(); } 

Example 11.25 is a simple program that lists all of the notations declared in a document's DTD. To determine whether these were actually used anywhere in the document, you would need to compare them to all of the processing instruction targets, notation type attributes, and unparsed entities to see if they matched up anywhere .

Example 11.25 Listing the Notations Declared in a DTD
 import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.SAXException; import java.io.IOException; public class NotationLister {   // No recursion for a change. We don't need to walk the tree.   public static void listNotations(Document doc) {     DocumentType doctype   = doc.getDoctype();     NamedNodeMap notations = doctype.getNotations();     for (int i = 0; i < notations.getLength(); i++) {       Notation notation = (Notation) notations.item(i);       String name       = notation.getNodeName();       String publicID   = notation.getPublicId();       String systemID   = notation.getSystemId();       System.out.print(name + ": ");       if (publicID != null) System.out.print(publicID + " ");       if (systemID != null) System.out.print(systemID + " ");       System.out.println();     }   }   public static void main(String[] args) {     if (args.length <= 0) {       System.out.println("Usage: java NotationLister URL");       return;     }     String url = args[0];     try {       DocumentBuilderFactory factory        = DocumentBuilderFactory.newInstance();       DocumentBuilder parser = factory.newDocumentBuilder();       // Read the document       Document document = parser.parse(url);       // Process the document       listNotations(document);     }     catch (SAXException e) {       System.out.println(url + " is not well-formed.");     }     catch (IOException e) {       System.out.println(        "Due to an IOException, the parser could not check " + url       );     }     catch (FactoryConfigurationError e) {       System.out.println("Could not locate a factory class");     }     catch (ParserConfigurationException e) {       System.out.println("Could not locate a JAXP parser");     }   } // end main } 

DocBook is the only XML application I'm aware of that even declares any notations (and that's really only because of legacy compatibility issues with DocBook's earlier SGML incarnation). This book is written in DocBook, and so I ran NotationLister across one of its chapters. Here's the output:

 D:\books\XMLJAVA>  java NotationLister masterbook.xml  BMP: +//ISBN 0-7923-9432-1::Graphic Notation//NOTATION Microsoft  Windows bitmap//EN CGM-BINARY: ISO 8632/3//NOTATION Binary encoding//EN CGM-CHAR: ISO 8632/2//NOTATION Character encoding//EN CGM-CLEAR: ISO 8632/4//NOTATION Clear text encoding//EN DITROFF: DITROFF DVI: DVI EPS: +//ISBN 0-201-18127-4::Adobe//NOTATION PostScript Language  Ref. Manual//EN EQN: EQN FAX: -//USA-DOD//NOTATION CCITT Group 4 Facsimile Type 1  Untiled Raster//EN GIF: GIF GIF87a: -//CompuServe//NOTATION Graphics Interchange Format  87a//EN GIF89a: -//CompuServe//NOTATION Graphics Interchange Format  89a//EN IGES: -//USA-DOD//NOTATION (ASME/ANSI Y14.26M-1987) Initial  Graphics Exchange Specification//EN JPEG: JPG JPG: JPG PCX: +//ISBN 0-7923-9432-1::Graphic Notation//NOTATION ZSoft PCX  bitmap//EN PIC: PIC PNG: http://www.w3.org/TR/REC-png PS: PS SGML: ISO 8879:1986//NOTATION Standard Generalized Markup  Language//EN TBL: TBL TEX: +//ISBN 0-201-13448-9::Knuth//NOTATION The TeXbook//EN TIFF: TIFF WMF: +//ISBN 0-7923-9432-1::Graphic Notation//NOTATION Microsoft Windows Metafile//EN WPG: WPG linespecific: linespecific 

I had to add a few line breaks to fit the output on the page, but otherwise the result would be the same given any DocBook document because the notations come from the DTD, not the instance document.



Processing XML with Java. A Guide to SAX, DOM, JDOM, JAXP, and TrAX
Processing XML with Javaв„ў: A Guide to SAX, DOM, JDOM, JAXP, and TrAX
ISBN: 0201771861
EAN: 2147483647
Year: 2001
Pages: 191

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net