Home arrow Java arrow Page 4 - Entities, Handlers and SAX
JAVA

Entities, Handlers and SAX


Picking up from where we left off yesterday, we'll take a look at entities and handlers in SAX. This article is excerpted from chapter four of the book Java and XML, Third Edition, written by Brett McLaughlin and Justin Edelson (O'Reilly, 2006; ISBN: 059610149X). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 8
July 12, 2007
TABLE OF CONTENTS:
  1. · Entities, Handlers and SAX
  2. · Notations and Unparsed Entities
  3. · The DefaultHandler Class
  4. · LexicalHandler

print this article
SEARCH DEVARTICLES

Entities, Handlers and SAX - LexicalHandler
(Page 4 of 4 )

 

The first of these handlers is probably the most useful: org.xml.sax.ext.LexicalHandler. This handler provides methods that can receive notification of several lexical events in an XML document, such as comments, entity declarations, DTD declarations, and CDATA sections. In ContentHandler , these lexical events are essentially ignored, and you just get the data and declarations without notification of when or how they were provided.

This is not really a general-use handler, as most applications donít need to know if text was in a CDATA section or not. However, if you are working with an XML editor, serializer, or other component that must know the exact format of the input documentóand not just its contentsóthen the LexicalHandler can really help you out.

To see this guy in action, you first need to add an import statement for org.xml.sax. ext.LexicalHandler to your SAXTreeViewer.java source file. Once thatís done, you can add LexicalHandler to the implements clause in the nonpublic class JTreeContentHandler in that source file:

  class JTreeHandler implements ContentHandler, ErrorHandler, LexicalHandler { 

To get started, look at the first lexical event that might happen in processing an XML document: the start and end of a DTD reference or declaration. That triggers the startDTD() and endDTD() callbacks (Iíve coded up versions appropriate for SAXTreeViewer here):

  public void startDTD(String name, String publicID ,
                       S
tring systemID)
    throws SAXException {

    DefaultMutableTreeNode dtdReference =
      new DefaultMutableTreeNode("DTD for '" + name + "'");
    if (publicID != null) {
      DefaultMutableTreeNode publicIDNode =
        new DefaultMutableTreeNode("Public ID: '" + publicID + "'");
     
dtdReference.add(publicIDNode);
    }
    if (systemID != null) {
      DefaultMutableTreeNode systemIDNode =
        new DefaultMutableTreeNode("System ID: '" + systemID + "'");
      dtdReference.add(systemIDNode);
    }
   
current.add(dtdReference);
  }

  public void endDTD( ) throws SAXException {
    // No action needed here
  }

This adds a visual cue when a DTD is encountered, and notes the system ID and public ID of the DTD. Continuing on, there is a pair of similar methods for entity references, startEntity() and endEntity() . These are triggered before and after pro cessing entity references. You can add a visual cue for this event as well:

  public void startEntity(String name) throws SAXException {
    DefaultMutableTreeNode entity =
     
new DefaultMutableTreeNode("Entity: '" + name + "'");
    current.add(entity);
    current = entity;
 
}

  public void endEntity(String name) throws SAXException {
    // Walk back up the tree
    current = (DefaultMutableTreeNode)current.getParent( );
 
}

This ensures that the content of, for example, the usage-terms entity reference is included within an ďEntityĒ tree node. Simple enough.

Next are two events for CDATA sections:

  public void startCDATA( ) throws SAXException {
    DefaultMutableTreeNode cdata =
     
new DefaultMutableTreeNode("CDATA Section");
    current.add(cdata);
    current = cdata;
 
}

  public void endCDATA( ) throws SAXException {
    // Walk back up the tree
    current = (DefaultMutableTreeNode)current.getParent( );
 
}

This is old hat by now; the title elementís content now appears as the child of a CDATA node. And with that, only one method is left, which receives comment notification:

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

    String comment = new String(ch, start, length);
    DefaultMutableTreeNode commentNode =
      new DefaultMutableTreeNode("Comment: '" + comment + "'");
    current.add(commentNode);
  }

This method behaves just like the characters() and ignorableWhitespace() meth ods. Keep in mind that only the text of the comment is reported to this method, not the surrounding <!-- and --> delimiters.

Finally, register this handler with your XMLReader . Since the reader isnít required to support LexicalHandler , you canít just call setLexicalHandler() ; instead, use setProperty() :

  // Register lexical handle r
  reader.setProperty("http://xml.org/sax/properties/lexical-handler",
                    jTreeHandler);

With these changes in place, you can compile the example program and run it. You should get output similar to that shown in Figure 4-5.

Be sure you actually have some of these lexical events in your docu ment before trying this out. Iíve added an entity reference and comment in the as_you-with_entity.xml file, included in the downloadable examples for this book (see http://www.oreilly.com/catalog/ 9780596101497).


Figure 4-5.  The LexicalHandler implementation reports a DTD (in addition to an entity reference for that DTD), as well as a comment and the usage-terms entity 

Please check back next week for the conclusion to this article.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials