// Fig 8.10 : ReplaceText.java
// Reads intro.xml and replaces a text node.

import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import com.sun.xml.tree.XmlDocument;
import org.xml.sax.*;

public class ReplaceText {
   private Document document; 

   public ReplaceText()
   {
      try {

         // obtain the default parser
         DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();

         // set the parser to validating           
         factory.setValidating( true );
     
         DocumentBuilder builder = factory.newDocumentBuilder();

         // set error handler for validation errors
         builder.setErrorHandler( new MyErrorHandler() );

         // obtain document object from XML document
         document = builder.parse( new File( "intro.xml" ) );

         // fetch the root node
         Node root = document.getDocumentElement();

         if ( root.getNodeType() == Node.ELEMENT_NODE ) {
            Element myMessageNode = ( Element ) root;
            NodeList messageNodes = 
               myMessageNode.getElementsByTagName( "message" );

            if ( messageNodes.getLength() != 0 ) {
               Node message = messageNodes.item( 0 );

               // create a text node
               Text newText = document.createTextNode(
                                "New Changed Message!!" );

               // get the old text node
               Text oldText =
                  ( Text ) message.getChildNodes().item( 0 );  

               // replace the text 
               message.replaceChild( newText, oldText );
            }
         }            
        
         ( (XmlDocument) document).write( new FileOutputStream(
             "intro1.xml" ) );  
      } 
      catch ( SAXParseException spe ) {
         System.err.println( "Parse error: " + 
            spe.getMessage() );
         System.exit( 1 );
      }
      catch ( SAXException se ) {
         se.printStackTrace();         
      }
      catch ( FileNotFoundException fne ) {
         System.err.println( "File \'intro.xml\' not found. " );
         System.exit( 1 );
      }
      catch ( Exception e ) {
         e.printStackTrace();
      }
   }
   
   public static void main( String args[] )
   {
      ReplaceText d = new ReplaceText();    
   }
}

/*
 **************************************************************************
 * (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall.     *
 * All Rights Reserved.                                                   *
 *                                                                        *
 * DISCLAIMER: The authors and publisher of this book have used their     *
 * best efforts in preparing the book. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these books. The authors *
 * and publisher shall not be liable in any event for incidental or       *
 * consequential damages in connection with, or arising out of, the       *
 * furnishing, performance, or use of these programs.                     *
 **************************************************************************
*/
