Java
  Home arrow Java arrow Page 4 - Entities, Handlers and SAX
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Entities, Handlers and SAX
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2007-07-12

    Table of Contents:
  • Entities, Handlers and SAX
  • Notations and Unparsed Entities
  • The DefaultHandler Class
  • LexicalHandler

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

       · This article is an excerpt from the book "Java and XML, Third Edition," published by...
     

    Buy this book now. 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). Check it out today at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway