001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: DispatchHandler.java,v 1.2 2002/02/01 10:55:25 jstrachan Exp $ 008 */ 009 010package org.dom4j.io; 011 012import java.util.ArrayList; 013import java.util.HashMap; 014 015import org.dom4j.Element; 016import org.dom4j.ElementHandler; 017import org.dom4j.ElementPath; 018 019/** <p><code>DispatchHandler</code> implements the <code>ElementHandler</code> 020 * interface and provides a means to register multiple <code>ElementHandler</code> 021 * instances to be used by an event based processor. This is a special 022 * <code>ElementHandler</code> in that it's <b>onStart</b> and <b>onEnd</b> 023 * implementation methods are called for every element encountered during 024 * the parse. It then delegates to other <code>ElementHandler</code> instances 025 * registered with it to process the elements encountered. 026 * 027 * @author <a href="mailto:dwhite@equipecom.com">Dave White</a> 028 * @version $Revision: 1.2 $ 029 */ 030 031class DispatchHandler implements ElementHandler 032{ 033 /** Whether the parser is at the root element or not */ 034 private boolean atRoot; 035 036 /** The current path in the XML tree (i.e. /a/b/c) */ 037 private String path; 038 039 /** maintains a stack of previously encountered paths */ 040 private ArrayList pathStack; 041 042 /** maintains a stack of previously encountered handlers */ 043 private ArrayList handlerStack; 044 045 /** <code>HashMap</code> maintains the mapping between element paths and handlers */ 046 private HashMap handlers; 047 048 /** <code>ElementHandler</code> to use by default for element paths with no handlers registered */ 049 private ElementHandler defaultHandler; 050 051 public DispatchHandler() 052 { 053 atRoot = true; 054 path = "/"; 055 pathStack = new ArrayList(); 056 handlerStack = new ArrayList(); 057 handlers = new HashMap(); 058 } 059 060 /** Adds the <code>ElementHandler</code> to be called when the 061 * specified path is encounted. 062 * 063 * @param path is the path to be handled 064 * @param handler is the <code>ElementHandler</code> to be called 065 * by the event based processor. 066 */ 067 public void addHandler(String path, ElementHandler handler) 068 { 069 handlers.put(path, handler); 070 } 071 072 /** Removes the <code>ElementHandler</code> from the event based 073 * processor, for the specified path. 074 * 075 * @param path is the path to remove the <code>ElementHandler</code> for. 076 */ 077 public ElementHandler removeHandler(String path) 078 { 079 return (ElementHandler)handlers.remove(path); 080 } 081 082 /** When multiple <code>ElementHandler</code> instances have been 083 * registered, this will set a default <code>ElementHandler</code> 084 * to be called for any path which does <b>NOT</b> have a handler 085 * registered. 086 * @param handler is the <code>ElementHandler</code> to be called 087 * by the event based processor. 088 */ 089 public void setDefaultHandler(ElementHandler handler) 090 { 091 defaultHandler = handler; 092 } 093 094 /** @return the current path for the parse */ 095 public String getPath() { return path; } 096 097 // The following methods implement the ElementHandler interface 098 099 public void onStart(ElementPath elementPath) 100 { 101 Element element = elementPath.getCurrent(); 102 // Save the location of the last (i.e. parent) path 103 pathStack.add(path); 104 // Calculate the new path 105 if (atRoot) 106 { 107 path = path + element.getName(); 108 atRoot = false; 109 } 110 else 111 { 112 path = path + "/" + element.getName(); 113 } 114 115 if ((handlers != null) && (handlers.containsKey(path))) 116 { 117 // The current node has a handler associated with it. 118 // Find the handler and save it on the handler stack. 119 ElementHandler handler = (ElementHandler)handlers.get(path); 120 handlerStack.add(handler); 121 // Call the handlers onStart method. 122 handler.onStart(elementPath); 123 } 124 else 125 { 126 // No handler is associated with this node, so use the 127 // defaultHandler it it exists. 128 if (handlerStack.isEmpty() && (defaultHandler != null)) 129 { 130 defaultHandler.onStart(elementPath); 131 } 132 } 133 } 134 135 public void onEnd(ElementPath elementPath) 136 { 137 if ((handlers != null) && (handlers.containsKey(path))) 138 { 139 // This node has a handler associated with it. 140 // Find the handler and pop it from the handler stack. 141 ElementHandler handler = (ElementHandler)handlers.get(path); 142 handlerStack.remove( handlerStack.size() - 1 ); 143 // Call the handlers onEnd method 144 handler.onEnd(elementPath); 145 } 146 else 147 { 148 // No handler is associated with this node, so use the 149 // defaultHandler it it exists. 150 if (handlerStack.isEmpty() && (defaultHandler != null)) 151 { 152 defaultHandler.onEnd(elementPath); 153 } 154 } 155 // Set path back to its parent 156 path = (String)pathStack.remove( pathStack.size() - 1 ); 157 } 158} 159 160 161 162 163/* 164 * Redistribution and use of this software and associated documentation 165 * ("Software"), with or without modification, are permitted provided 166 * that the following conditions are met: 167 * 168 * 1. Redistributions of source code must retain copyright 169 * statements and notices. Redistributions must also contain a 170 * copy of this document. 171 * 172 * 2. Redistributions in binary form must reproduce the 173 * above copyright notice, this list of conditions and the 174 * following disclaimer in the documentation and/or other 175 * materials provided with the distribution. 176 * 177 * 3. The name "DOM4J" must not be used to endorse or promote 178 * products derived from this Software without prior written 179 * permission of MetaStuff, Ltd. For written permission, 180 * please contact dom4j-info@metastuff.com. 181 * 182 * 4. Products derived from this Software may not be called "DOM4J" 183 * nor may "DOM4J" appear in their names without prior written 184 * permission of MetaStuff, Ltd. DOM4J is a registered 185 * trademark of MetaStuff, Ltd. 186 * 187 * 5. Due credit should be given to the DOM4J Project 188 * (http://dom4j.org/). 189 * 190 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 191 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 192 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 193 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 194 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 195 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 196 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 197 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 198 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 199 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 200 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 201 * OF THE POSSIBILITY OF SUCH DAMAGE. 202 * 203 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 204 * 205 * $Id: DispatchHandler.java,v 1.2 2002/02/01 10:55:25 jstrachan Exp $ 206 */