1 16 package org.apache.cocoon.portal.transformation; 17 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.parameters.Parameters; 23 import org.apache.cocoon.ProcessingException; 24 import org.apache.cocoon.caching.CacheableProcessingComponent; 25 import org.apache.cocoon.environment.SourceResolver; 26 import org.apache.cocoon.transformation.AbstractTransformer; 27 import org.apache.cocoon.xml.XMLUtils; 28 import org.apache.commons.lang.BooleanUtils; 29 import org.apache.excalibur.source.SourceValidity; 30 import org.apache.excalibur.source.impl.validity.NOPValidity; 31 import org.xml.sax.Attributes ; 32 import org.xml.sax.SAXException ; 33 34 50 public class HTMLRootTransformer 51 extends AbstractTransformer 52 implements CacheableProcessingComponent{ 53 54 55 protected boolean addMode; 56 57 58 protected boolean ignoreRootElement; 59 protected int ignoreRootElementCount; 60 61 protected boolean insideBodyTag; 62 63 66 public void setup(SourceResolver resolver, 67 Map objectModel, 68 String src, 69 Parameters par) 70 throws ProcessingException, SAXException , IOException { 71 this.addMode = par.getParameterAsBoolean("add-mode", true); 72 this.ignoreRootElement = par.getParameterAsBoolean("ignore-root", false); 73 this.ignoreRootElementCount = 0; 74 this.insideBodyTag = false; 75 } 76 77 80 public void endDocument() throws SAXException { 81 if ( this.addMode ) { 82 XMLUtils.endElement(this.xmlConsumer, "body"); 83 XMLUtils.endElement(this.xmlConsumer, "html"); 84 } 85 super.endDocument(); 86 } 87 88 91 public void startDocument() throws SAXException { 92 super.startDocument(); 93 if ( this.addMode ) { 94 XMLUtils.startElement(this.xmlConsumer, "html"); 95 XMLUtils.startElement(this.xmlConsumer, "body"); 96 } 97 } 98 99 102 public void startElement(String uri, String local, String qName, Attributes attr) throws SAXException { 103 if ( !this.ignoreRootElement || this.ignoreRootElementCount > 0) { 104 if ( this.addMode || this.insideBodyTag ) { 105 this.contentHandler.startElement(uri,local,qName,attr); 106 } 107 } 108 if ( "body".equals(local) ) { 109 this.insideBodyTag = true; 110 } 111 this.ignoreRootElementCount++; 112 } 113 114 117 public void endElement(String uri, String local, String qName) throws SAXException { 118 if ( "body".equals(local) ) { 119 this.insideBodyTag = false; 120 } 121 this.ignoreRootElementCount--; 122 if (!this.ignoreRootElement || this.ignoreRootElementCount > 0) { 123 if ( this.addMode || this.insideBodyTag ) { 124 this.contentHandler.endElement(uri, local, qName); 125 } 126 } 127 } 128 129 132 public void characters(char[] c, int start, int len) throws SAXException { 133 if ( this.addMode || this.insideBodyTag ) { 134 super.characters(c, start, len); 135 } 136 } 137 138 141 public Serializable getKey() { 142 return new Boolean [] { BooleanUtils.toBooleanObject(this.addMode), BooleanUtils.toBooleanObject(this.ignoreRootElement)}; 143 } 144 145 148 public SourceValidity getValidity() { 149 return NOPValidity.SHARED_INSTANCE; 150 } 151 } 152 | Popular Tags |