FileDocCategorySizeDatePackage
DOMAddLines.javaAPI DocApache Xerces 3.0.19738Fri Sep 14 20:33:58 BST 2007dom

DOMAddLines

public class DOMAddLines extends org.apache.xerces.parsers.DOMParser
A sample of Adding lines to the DOM Node. This sample program illustrates: - How to override methods from DocumentHandler ( XMLDocumentHandler) - How to turn off ignorable white spaces by overriding ignorableWhiteSpace - How to use the SAX Locator to return row position ( line number of DOM element). - How to attach user defined Objects to Nodes using method setUserData This example relies on the following: - Turning off the "fast" DOM so we can use set expansion to FULL
version
$Id: DOMAddLines.java 447683 2006-09-19 02:36:31Z mrglavas $

Fields Summary
private PrintWriter
out
Print writer.
private static boolean
NotIncludeIgnorableWhiteSpaces
private org.apache.xerces.xni.XMLLocator
locator
Constructors Summary
public DOMAddLines(String inputName)

 


        
      //fNodeExpansion = FULL; // faster than: this.setFeature("http://apache.org/xml/features/defer-node-expansion", false);

      try {                        
         this.setFeature( "http://apache.org/xml/features/dom/defer-node-expansion", false ); 
         this.parse( inputName );
         out = new PrintWriter(new OutputStreamWriter(System.out, "UTF8"));
      } catch ( IOException e ) {
         System.err.println( "except" + e );
      } catch ( org.xml.sax.SAXException e ) {
         System.err.println( "except" + e );
      }
   
Methods Summary
public voidignorableWhitespace(org.apache.xerces.xni.XMLString text, org.apache.xerces.xni.Augmentations augs)

    if(! NotIncludeIgnorableWhiteSpaces )
       super.ignorableWhitespace( text, augs);
    else
       ;// Ignore ignorable white spaces
    
public static voidmain(java.lang.String[] argv)
Main program entry point.

      // is there anything to do?
      if ( argv.length == 0 ) {
         printUsage();
         System.exit(1);
      }
      // check parameters

      for ( int i = 0; i < argv.length; i++ ) {
         String arg = argv[i];

         // options
         if ( arg.startsWith("-") ) {
            if ( arg.equals("-h") ) {
               printUsage();
               System.exit(1);
            }
            if (arg.equals("-i")) {
                   NotIncludeIgnorableWhiteSpaces = true;
                   continue;
               }
            
         }
      // DOMAddLine parse and print

      DOMAddLines domAddExample = new DOMAddLines( arg );
      Document doc             = domAddExample.getDocument();
      domAddExample.print( doc );

     }
   
public voidprint(org.w3c.dom.Node node)
Prints the specified node, recursively.

      // is there anything to do?
      if ( node == null ) {
         return;
      }

      String lineRowColumn = (String ) ((Node) node).getUserData("startLine");

      int type = node.getNodeType();
      switch ( type ) {
         // print document
         case Node.DOCUMENT_NODE: {
               out.println(  lineRowColumn + ":" + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
               print( ((Document)node).getDocumentElement());
               out.flush();
               break;
            }

            // print element with attributes
         case Node.ELEMENT_NODE: {
               out.print( lineRowColumn + ":" + '<");
               out.print(node.getNodeName());
               Attr attrs[] = sortAttributes(node.getAttributes());
               for ( int i = 0; i < attrs.length; i++ ) {
                  Attr attr = attrs[i];
                  out.print(' ");
                  out.print(attr.getNodeName());
                  out.print("=\"");
                  out.print( attr.getNodeValue());
                  out.print('"");
               }
               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: {
               out.print('&");
               out.print(node.getNodeName());
               out.print(';");
               break;
            }

            // print cdata sections
         case Node.CDATA_SECTION_NODE: {
               out.print("<![CDATA[");
               out.print(node.getNodeValue());
               out.print("]]>");
               break;
            }

            // print text
         case Node.TEXT_NODE: {
               out.print(  node.getNodeValue());
               break;
            }

            // print processing instruction
         case Node.PROCESSING_INSTRUCTION_NODE: {
               out.print("<?");
               out.print(node.getNodeName());
               String data = node.getNodeValue();
               if ( data != null && data.length() > 0 ) {
                  out.print(' ");
                  out.print(data);
               }
               out.print("?>");
               break;
            }
      }

      if ( type == Node.ELEMENT_NODE ) {
         out.print("</");
         out.print(node.getNodeName());
         out.print('>");
      }

      out.flush();

   
private static voidprintUsage()
Prints the usage.

      System.err.println("usage: jre dom.DOMAddLines (options) uri ...");
      System.err.println();
      System.err.println("  -h       Display help screen.");
      System.err.println("  -i       Don't print ignorable white spaces.");

   
private org.w3c.dom.Attr[]sortAttributes(org.w3c.dom.NamedNodeMap attrs)
Returns a sorted list of attributes.


      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);

   
public voidstartDocument(org.apache.xerces.xni.XMLLocator locator, java.lang.String encoding, org.apache.xerces.xni.NamespaceContext namespaceContext, org.apache.xerces.xni.Augmentations augs)

     super.startDocument(locator, encoding, namespaceContext, augs);
     this.locator = locator;
     Node node = null ;
      try {
      node = (Node) this.getProperty( "http://apache.org/xml/properties/dom/current-element-node" );
      }
     catch( org.xml.sax.SAXException ex )
      {
        System.err.println( "except" + ex );;
      }
     
     if( node != null )
          node.setUserData( "startLine", String.valueOf( locator.getLineNumber() ), null ); // Save location String into node
  
public voidstartElement(org.apache.xerces.xni.QName elementQName, org.apache.xerces.xni.XMLAttributes attrList, org.apache.xerces.xni.Augmentations augs)

      super.startElement(elementQName, attrList, augs);

      Node node = null;
      try {
      node = (Node) this.getProperty( "http://apache.org/xml/properties/dom/current-element-node" );
      //System.out.println( "The node = " + node );  TODO JEFF
      }
      catch( org.xml.sax.SAXException ex )
      {
          System.err.println( "except" + ex );;
      }
      if( node != null )
          node.setUserData( "startLine", String.valueOf( locator.getLineNumber() ), null ); // Save location String into node