1 22 23 package org.xquark.util; 24 25 import java.util.Hashtable ; 26 import java.util.Stack ; 27 28 import org.xquark.schema.validation.ElementPSVInfoset; 29 import org.xquark.schema.validation.PSVInfoset; 30 31 39 public class TagTracker 40 { 41 private static final String RCSRevision = "$Revision: 1.2 $"; 42 private static final String RCSName = "$Name: $"; 43 44 private Hashtable trackers = new Hashtable (); 51 52 private static SkippingTagTracker skip = new SkippingTagTracker(); 55 56 public TagTracker() 58 {} 59 60 68 public void track(String tagName, TagTracker tracker) 69 { 70 trackAny(tagName, tracker); 71 } 72 80 public void trackAttribute(String attName, AttributeTracker attHandler) 81 { 82 trackAny(attName, attHandler); 83 } 84 85 private void trackAny(String tagName, Object o) 86 { 87 88 int slashOffset = tagName.indexOf("/"); 89 90 if (slashOffset < 0) 91 { 92 trackers.put(tagName, o); 95 96 } 97 else if (slashOffset == 0) 98 { 99 trackAny(tagName.substring(1), o); 102 103 } 104 else 105 { 106 String topTagName = tagName.substring(0, slashOffset); 109 String remainderOfTagName = tagName.substring(slashOffset + 1); 110 TagTracker child = (TagTracker) trackers.get(topTagName); 111 if (child == null) 112 { 113 child = new TagTracker(); 116 trackers.put(topTagName, child); 117 } 118 child.trackAny(remainderOfTagName, o); 119 120 } 121 122 } 123 124 public void startElement(ElementPSVInfoset infoSet, Stack tagStack) 132 { 133 TagTracker tracker = (TagTracker) trackers.get( 140 getExpandedName(infoSet.getNamespaceURI(), infoSet.getLocalName(), false)); 141 142 if (tracker == null) 146 { 147 tagStack.push(skip); 151 } 152 else 153 { 154 155 160 onDeactivate(); 162 163 tracker.onStart(infoSet.getNamespaceURI(), 166 infoSet.getLocalName()); 167 168 int nbAtts = infoSet.getAttributeCount(); 170 AttributeTracker attHandler; 171 PSVInfoset attInfoset; 172 173 for (int i = 0; i < nbAtts; i++) 174 { 175 attInfoset = infoSet.getAttributePSVInfoset(i); 176 attHandler = 177 (AttributeTracker) tracker.trackers.get( 178 getExpandedName( 179 attInfoset.getNamespaceURI(), 180 attInfoset.getLocalName(), 181 true)); 182 if (attHandler != null) 183 attHandler.onAttributeEncountered( 184 attInfoset.getNamespaceURI(), 185 attInfoset.getLocalName(), 186 attInfoset.getActualValue(), 187 attInfoset.getSpecified() == PSVInfoset.SCHEMA ? true : false); 188 } 189 190 tagStack.push(tracker); 191 192 } 193 194 } 195 196 public void endElement( 204 ElementPSVInfoset infoSet, 205 Stack tagStack) 206 { 207 208 onEnd(infoSet.getNamespaceURI(), 210 infoSet.getLocalName(), 211 infoSet.getActualValue()); 212 213 tagStack.pop(); 215 216 TagTracker activeTracker = (TagTracker) tagStack.peek(); 218 if (activeTracker != null) 219 { 220 activeTracker.onReactivate(); 221 } 222 223 } 224 225 230 public void onStart( 231 String namespaceURI, 232 String localName) 233 { 234 235 } 237 238 public void onDeactivate() 239 { 240 241 } 243 244 public void onEnd( 245 String namespaceURI, 246 String localName, 247 Object actualValue) 248 { 249 250 } 252 253 public void onReactivate() 254 { 255 256 } 258 259 260 private String getExpandedName( 261 String namespace, 262 String localName, 263 boolean att) 264 { 265 StringBuffer sb = new StringBuffer (); 266 267 if (att) 268 sb.append('@'); 269 if (namespace != null && !namespace.equals("")) 270 { 271 sb.append('{'); 272 sb.append(namespace); 273 sb.append('}'); 274 } 275 sb.append(localName); 276 return sb.toString(); 277 } 278 279 } 280 class SkippingTagTracker extends TagTracker 281 { 282 private static final String RCSRevision = "$Revision: 1.2 $"; 283 private static final String RCSName = "$Name: $"; 284 285 public void startElement(ElementPSVInfoset infoSet, Stack tagStack) 304 { 305 306 tagStack.push(this); 312 313 } 314 315 public void endElement( 326 ElementPSVInfoset infoSet, 327 Stack tagStack) 328 { 329 330 tagStack.pop(); 332 333 } 334 } 335 | Popular Tags |