1 package org.sapia.util.xml.parser; 2 3 4 import org.xml.sax.Attributes ; 7 import org.xml.sax.SAXException ; 8 9 import java.util.HashMap ; 12 import java.util.LinkedList ; 13 import java.util.Map ; 14 15 16 20 34 public class DefaultHandlerContext implements HandlerContextIF { 35 39 40 41 46 47 private HandlerStateIF _theCurrentState; 48 49 50 private LinkedList _theHandlerStates; 51 52 53 private Map _theNamespaceMapping; 54 55 56 private Map _theResults; 57 58 62 68 public DefaultHandlerContext(HandlerStateIF anInitialState) { 69 if (anInitialState == null) { 70 throw new IllegalArgumentException ( 71 "The initial handler state passes in is null."); 72 } 73 74 _theCurrentState = anInitialState; 75 _theHandlerStates = new LinkedList (); 76 _theNamespaceMapping = new HashMap (); 77 _theResults = new HashMap (); 78 _theHandlerStates.addFirst(anInitialState); 79 } 80 81 85 90 public synchronized HandlerStateIF getCurrentState() { 91 return _theCurrentState; 92 } 93 94 109 public synchronized void setCurrentState(HandlerStateIF aHandlerState, 110 String anUri, String aLocalName, String aQualifiedName, 111 Attributes someAttributes) throws SAXException { 112 _theCurrentState = aHandlerState; 113 _theHandlerStates.addFirst(aHandlerState); 114 115 try { 116 _theCurrentState.startElement(this, anUri, aLocalName, aQualifiedName, 118 someAttributes); 119 } catch (SAXException se) { 120 String aMessage = 121 "Error when notifying the child handler state of the start of the element " + 122 aLocalName; 123 124 128 throw new SAXException (aMessage, se); 129 } 130 } 131 132 145 public synchronized void removeCurrentState(String anUri, String aLocalName, 146 String aQualifiedName) throws SAXException { 147 if (_theHandlerStates.size() == 1) { 149 throw new IllegalStateException ( 150 "Could not remove the root handler state."); 151 } 152 153 _theHandlerStates.removeFirst(); 155 156 _theCurrentState = (HandlerStateIF) _theHandlerStates.getFirst(); 158 159 try { 160 _theCurrentState.endElement(this, anUri, aLocalName, aQualifiedName); 162 } catch (SAXException se) { 163 String aMessage = 164 "Error when notifying the parent handler state of the end of the element " + 165 aLocalName; 166 167 171 throw new SAXException (aMessage, se); 172 } 173 } 174 175 181 public void startPrefixMapping(String aPrefix, String anUri) { 182 LinkedList aNamespaceList = (LinkedList ) _theNamespaceMapping.get(aPrefix); 184 185 if (aNamespaceList == null) { 187 aNamespaceList = new LinkedList (); 188 189 _theNamespaceMapping.put(aPrefix, aNamespaceList); 191 } 192 193 aNamespaceList.addFirst(anUri); 195 } 196 197 202 public void endPrefixMapping(String aPrefix) { 203 LinkedList aNamespaceList = (LinkedList ) _theNamespaceMapping.get(aPrefix); 205 206 if (aNamespaceList != null) { 207 aNamespaceList.removeFirst(); 208 } else { 209 throw new IllegalStateException ( 210 "The prefix mapping is not found in the map."); 211 } 212 } 213 214 219 public String getNamespaceURIFor(String aPrefix) { 220 String aNamespace = null; 221 222 LinkedList aNamespaceList = (LinkedList ) _theNamespaceMapping.get(aPrefix); 224 225 if ((aNamespaceList != null) && !aNamespaceList.isEmpty()) { 227 aNamespace = (String ) aNamespaceList.getFirst(); 228 } 229 230 return aNamespace; 231 } 232 233 239 public void setResultFor(HandlerStateIF aHandlerState, Object aResult) { 240 _theResults.put(aHandlerState, aResult); 241 } 242 243 250 public Object getResultFor(HandlerStateIF aHandlerState) { 251 return _theResults.get(aHandlerState); 252 } 253 } 254 | Popular Tags |