1 16 package org.apache.cocoon.transformation; 17 18 import java.io.IOException ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 23 import org.apache.avalon.framework.parameters.Parameters; 24 25 import org.apache.cocoon.ProcessingException; 26 import org.apache.cocoon.environment.SourceResolver; 27 import org.apache.cocoon.xml.dom.DOMBuilder; 28 29 import org.w3c.dom.Document ; 30 import org.xml.sax.Attributes ; 31 import org.xml.sax.SAXException ; 32 33 34 42 abstract public class AbstractExtractionTransformer extends AbstractTransformer { 43 44 protected DOMBuilder currentBuilder; 45 46 private Map prefixMap; 47 48 protected int extractLevel; 49 50 51 52 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters parameters) 53 throws ProcessingException, SAXException , IOException { 54 extractLevel = 0; 55 prefixMap = new HashMap (); 56 } 57 58 public void recycle() { 59 this.extractLevel = 0; 60 this.currentBuilder = null; 61 this.prefixMap = null; 62 super.recycle(); 63 } 64 65 66 72 public void startPrefixMapping(String prefix, String uri) 73 throws SAXException { 74 if (extractLevel == 0) { 75 super.startPrefixMapping(prefix,uri); 76 prefixMap.put(prefix,uri); 77 } else { 78 this.currentBuilder.startPrefixMapping(prefix,uri); 79 } 80 } 81 82 87 public void endPrefixMapping(String prefix) 88 throws SAXException { 89 if (extractLevel == 0) { 90 super.endPrefixMapping(prefix); 91 prefixMap.remove(prefix); 92 } else { 93 this.currentBuilder.endPrefixMapping(prefix); 94 } 95 } 96 97 98 115 public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException { 116 if (!startExtracting(uri, loc, raw, a)) { 117 118 if (extractLevel == 0) { 119 super.startElement(uri,loc,raw,a); 120 } else { 121 this.currentBuilder.startElement(uri,loc,raw,a); 122 } 123 124 } else { 125 126 extractLevel++; 127 if (this.getLogger().isDebugEnabled()) { 128 getLogger().debug("extractLevel now " + extractLevel + "."); 129 } 130 131 if (extractLevel != 1) { 132 this.currentBuilder.startElement(uri,loc,raw,a); 133 } else { 134 135 this.currentBuilder = new DOMBuilder(); 137 this.currentBuilder.startDocument(); 138 Iterator itt = prefixMap.entrySet().iterator(); 140 while (itt.hasNext()) { 141 Map.Entry entry = (Map.Entry )itt.next(); 142 this.currentBuilder.startPrefixMapping( 143 (String )entry.getKey(), 144 (String )entry.getValue() 145 ); 146 } 147 startExtractingDocument(uri, loc, raw, a); 149 150 } 151 152 } 153 } 154 155 156 169 public void endElement(String uri, String loc, String raw) 170 throws SAXException { 171 if (extractLevel == 0) { 172 super.endElement(uri,loc,raw); 173 } else { 174 if (endExtracting(uri, loc, raw)) { 175 extractLevel--; 176 if (this.getLogger().isDebugEnabled()) { 177 getLogger().debug("extractLevel now " + extractLevel + "."); 178 } 179 180 if (extractLevel != 0) { 181 this.currentBuilder.endElement(uri,loc,raw); 182 } else { 183 184 endExtractingDocument(uri, loc, raw); 186 Iterator itt = prefixMap.entrySet().iterator(); 188 while (itt.hasNext()) { 189 Map.Entry entry = (Map.Entry ) itt.next(); 190 this.currentBuilder.endPrefixMapping( 191 (String )entry.getKey() 192 ); 193 } 194 this.currentBuilder.endDocument(); 195 196 handleExtractedDocument(this.currentBuilder.getDocument()); 197 198 if (this.getLogger().isDebugEnabled()) { 199 getLogger().debug("Stored document."); 200 } 201 202 } 203 } else { 204 this.currentBuilder.endElement(uri, loc, raw); 205 } 206 } 207 } 208 209 216 public void characters(char c[], int start, int len) 217 throws SAXException { 218 if (extractLevel == 0) { 219 super.characters(c,start,len); 220 } else { 221 this.currentBuilder.characters(c,start,len); 222 } 223 } 224 225 232 public void ignorableWhitespace(char c[], int start, int len) 233 throws SAXException { 234 if (extractLevel == 0) { 235 super.ignorableWhitespace(c,start,len); 236 } else { 237 this.currentBuilder.ignorableWhitespace(c,start,len); 238 } 239 } 240 241 248 public void processingInstruction(String target, String data) 249 throws SAXException { 250 if (extractLevel == 0) { 251 super.processingInstruction(target,data); 252 } else { 253 this.currentBuilder.processingInstruction(target,data); 254 } 255 } 256 257 263 public void skippedEntity(String name) 264 throws SAXException { 265 if (extractLevel == 0) { 266 super.skippedEntity(name); 267 } else { 268 this.currentBuilder.skippedEntity(name); 269 } 270 } 271 272 281 public void startDTD(String name, String publicId, String systemId) 282 throws SAXException { 283 if (extractLevel == 0) { 284 super.startDTD(name,publicId,systemId); 285 } else { 286 throw new SAXException ( 287 "Recieved startDTD after beginning fragment extraction process." 288 ); 289 } 290 } 291 292 295 public void endDTD() 296 throws SAXException { 297 if (extractLevel == 0) { 298 super.endDTD(); 299 } else { 300 throw new SAXException ( 301 "Recieved endDTD after beginning fragment extraction process." 302 ); 303 } 304 } 305 306 312 public void startEntity(String name) 313 throws SAXException { 314 if (extractLevel == 0) { 315 super.startEntity(name); 316 } else { 317 this.currentBuilder.startEntity(name); 318 } 319 } 320 321 326 public void endEntity(String name) 327 throws SAXException { 328 if (extractLevel == 0) { 329 super.endEntity(name); 330 } else { 331 this.currentBuilder.endEntity(name); 332 } 333 } 334 335 338 public void startCDATA() 339 throws SAXException { 340 if (extractLevel == 0) { 341 super.startCDATA(); 342 } else { 343 this.currentBuilder.startCDATA(); 344 } 345 } 346 347 350 public void endCDATA() 351 throws SAXException { 352 if (extractLevel == 0) { 353 super.endCDATA(); 354 } else { 355 this.currentBuilder.endCDATA(); 356 } 357 } 358 359 366 public void comment(char ch[], int start, int len) 367 throws SAXException { 368 if (extractLevel == 0) { 369 super.comment(ch,start,len); 370 } else { 371 this.currentBuilder.comment(ch,start,len); 372 } 373 } 374 375 376 377 391 abstract boolean startExtracting(String uri, String loc, String raw, Attributes a); 392 393 408 public void startExtractingDocument(String uri, String loc, String raw, Attributes a) throws SAXException { 409 this.currentBuilder.startElement(uri,loc,raw,a); 410 } 411 412 423 abstract boolean endExtracting(String uri, String loc, String raw); 424 425 440 public void endExtractingDocument(String uri, String loc, String raw) throws SAXException { 441 this.currentBuilder.endElement(uri,loc,raw); 442 } 443 444 449 abstract void handleExtractedDocument(Document doc); 450 451 452 } 453 | Popular Tags |