Save This Page
Home » Xerces-J-src.2.9.1 » dom » events » [javadoc | source]
    1   /* $Id: Test.java,v 1.1.1.1 2000/10/07 00:26:17 markd Exp $ */
    2   /*
    3    * The Apache Software License, Version 1.1
    4    * 
    5    * Copyright (c) 2000 The Apache Software Foundation.  All rights 
    6    * reserved.
    7    * 
    8    * Redistribution and use in source and binary forms, with or without
    9    * modification, are permitted provided that the following conditions
   10    * are met:
   11    * 
   12    * 1. Redistributions of source code must retain the above copyright
   13    *    notice, this list of conditions and the following disclaimer. 
   14    * 
   15    * 2. Redistributions in binary form must reproduce the above copyright
   16    *    notice, this list of conditions and the following disclaimer in
   17    *    the documentation and/or other materials provided with the
   18    *    distribution.
   19    * 
   20    * 3. The end-user documentation included with the redistribution,
   21    *    if any, must include the following acknowledgment:  
   22    *       "This product includes software developed by the
   23    *        Apache Software Foundation (http://www.apache.org/)."
   24    *    Alternately, this acknowledgment may appear in the software itself,
   25    *    if and wherever such third-party acknowledgments normally appear.
   26    * 
   27    * 4. The names "Xerces" and "Apache Software Foundation" must
   28    *    not be used to endorse or promote products derived from this
   29    *    software without prior written permission. For written 
   30    *    permission, please contact apache\@apache.org.
   31    * 
   32    * 5. Products derived from this software may not be called "Apache",
   33    *    nor may "Apache" appear in their name, without prior written
   34    *    permission of the Apache Software Foundation.
   35    * 
   36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   47    * SUCH DAMAGE.
   48    * ====================================================================
   49    * 
   50    * This software consists of voluntary contributions made by many
   51    * individuals on behalf of the Apache Software Foundation, and was
   52    * originally based on software copyright (c) 1999, International
   53    * Business Machines, Inc., http://www.ibm.com .  For more information
   54    * on the Apache Software Foundation, please see
   55    * <http://www.apache.org/>.
   56    */
   57   
   58   package dom.events;
   59   
   60   import org.w3c.dom;
   61   import org.w3c.dom.events;
   62   
   63   public class Test
   64   {
   65       EventReporter sharedReporter=new EventReporter();
   66       
   67       public static void main(String[] args)
   68       {
   69           Test met=new Test();
   70           met.runTest();
   71       }
   72   
   73       void runTest()
   74       {
   75           Document doc=new org.apache.xerces.dom.DocumentImpl();
   76           reportAllMutations(doc);
   77           
   78           Element root=addNoisyElement(doc,doc,0);
   79           Element e=null;
   80           int i;
   81           
   82           // Individual nodes
   83           e=addNoisyElement(doc,root,0);
   84           Attr a=addNoisyAttr(doc,e,0);
   85           a.setNodeValue("Updated A0 of E0, prepare to be acidulated.");
   86           NamedNodeMap nnm=e.getAttributes();
   87           nnm.removeNamedItem(a.getName());
   88           nnm.setNamedItem(a);
   89           
   90           // InsertedInto/RemovedFrom tests.
   91           // ***** These do not currently cross the Attr/Element barrier.
   92           // DOM spec is pretty clear on that, but this may not be the intent.
   93           System.out.println("\nAdd/remove a preconstructed tree; tests AddedToDocument\n");
   94           sharedReporter.off();
   95           Element lateAdd=doc.createElement("lateAdd");
   96           reportAllMutations(lateAdd);
   97           e=lateAdd;
   98           for(i=0;i<2;++i)
   99           {
  100               e=addNoisyElement(doc,e,i);
  101               addNoisyAttr(doc,e,i);
  102           }
  103           sharedReporter.on();
  104           root.appendChild(lateAdd);
  105           root.removeChild(lateAdd);
  106   
  107           System.out.println("\nReplace a preconstructed tree; tests AddedToDocument\n");
  108   
  109           sharedReporter.off();
  110           Node e0=root.replaceChild(lateAdd,root.getFirstChild());
  111           sharedReporter.on();
  112           root.replaceChild(e0,lateAdd);
  113           
  114   
  115           System.out.println("Done");
  116       }
  117       
  118       Element addNoisyElement(Document doc,Node parent,int index)
  119       {
  120           String nodeName="Root";
  121           if(parent!=doc)
  122               nodeName=parent.getNodeName()+"_E"+index;
  123           Element e=doc.createElement(nodeName);
  124           reportAllMutations(e);
  125           parent.appendChild(e);
  126           return e;
  127       }
  128   
  129       Attr addNoisyAttr(Document doc,Element parent,int index)
  130       {
  131           String attrName=parent.getNodeName()+"_A"+index;
  132           Attr a=doc.createAttribute(attrName);
  133           reportAllMutations(a);
  134           a.setNodeValue("Initialized A"+index+" of "+parent.getNodeName());
  135           parent.setAttributeNode(a);
  136           return a;
  137       }
  138       
  139       void reportAllMutations(Node n)
  140       {
  141           String[] evtNames={
  142               "DOMSubtreeModified","DOMAttrModified","DOMCharacterDataModified",
  143               "DOMNodeInserted","DOMNodeRemoved",
  144               "DOMNodeInsertedIntoDocument","DOMNodeRemovedFromDocument",
  145               };
  146               
  147           EventTarget t=(EventTarget)n;
  148           
  149           for(int i=evtNames.length-1;
  150               i>=0;
  151               --i)
  152           {
  153               t.addEventListener(evtNames[i], sharedReporter, true);
  154               t.addEventListener(evtNames[i], sharedReporter, false);
  155           }
  156   
  157       }
  158   }
  159   
  160   class EventReporter implements EventListener
  161   {
  162       boolean silent=false; // Toggle this to mask reports you don't care about
  163       int count=0;
  164       String[] phasename={"?","BUBBLING","CAPTURING","AT_TARGET","?"};
  165       
  166       public void on()
  167       {
  168           System.out.println("\nEventReporter awakened:\n");
  169           silent=false;
  170       }
  171       public void off()
  172       {
  173           System.out.println("\nEventReporter muted\n");
  174           silent=true;
  175       }
  176       
  177       public void handleEvent(Event evt)
  178       {
  179           ++count;
  180           if(silent)
  181               return;
  182               
  183           System.out.print("EVT "+count+": '"+
  184               evt.getType()+
  185               "' listener '"+(evt.getCurrentNode()).getNodeName()+
  186               "' target '"+((Node)evt.getTarget()).getNodeName()+
  187               "' while "+phasename[evt.getEventPhase()] +
  188               "... ");
  189           if(evt.getBubbles()) System.out.print("will bubble");
  190           if(evt.getCancelable()) System.out.print("can cancel");
  191           System.out.print("\n");
  192           if(evt instanceof MutationEvent)
  193           {
  194               MutationEvent me=(MutationEvent)evt;
  195               System.out.print("\t");
  196               if(me.getRelatedNode()!=null)
  197                   System.out.print(" relatedNode='"+me.getRelatedNode()+"'");
  198               if(me.getAttrName()!=null)
  199                   System.out.print(" attrName='"+me.getAttrName()+"'");
  200               System.out.print("\n");
  201               if(me.getPrevValue()!=null)
  202                   System.out.println("\t prevValue='"+me.getPrevValue()+"'");
  203               if(me.getNewValue()!=null)
  204                   System.out.println("\t newValue='"+me.getNewValue()+"'");
  205           }
  206       }
  207   }

Save This Page
Home » Xerces-J-src.2.9.1 » dom » events » [javadoc | source]