1 16 package org.apache.cocoon.transformation; 17 18 import org.apache.avalon.framework.parameters.Parameters; 19 import org.apache.cocoon.ProcessingException; 20 import org.apache.cocoon.caching.CacheableProcessingComponent; 21 import org.apache.cocoon.environment.SourceResolver; 22 import org.apache.excalibur.source.SourceValidity; 23 import org.apache.excalibur.source.impl.validity.NOPValidity; 24 import org.xml.sax.Attributes ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.helpers.AttributesImpl ; 27 28 import java.io.IOException ; 29 import java.util.Map ; 30 31 60 public class FilterTransformer 61 extends AbstractTransformer 62 implements CacheableProcessingComponent { 63 64 private static final String ELEMENT = "element-name"; 65 private static final String COUNT = "count"; 66 private static final String BLOCKNR = "blocknr"; 67 private static final String BLOCK = "block"; 68 private static final String BLOCKID = "id"; 69 private static final int DEFAULT_COUNT = 10; 70 private static final int DEFAULT_BLOCK = 1; 71 72 protected int counter; 73 protected int count; 74 protected int blocknr; 75 protected int currentBlocknr; 76 protected String elementName; 77 protected String parentName; 78 protected boolean skip; 79 protected boolean foundIt; 80 81 82 public void setup(SourceResolver resolver, 83 Map objectModel, 84 String source, 85 Parameters parameters) 86 throws ProcessingException, SAXException , IOException { 87 this.counter=0; 88 this.currentBlocknr=0; 89 this.skip=false; 90 this.foundIt=false; 91 this.parentName=null; 92 this.elementName = parameters.getParameter(ELEMENT, ""); 93 this.count = parameters.getParameterAsInteger(COUNT, DEFAULT_COUNT); 94 this.blocknr = parameters.getParameterAsInteger(BLOCKNR, DEFAULT_BLOCK); 95 if (this.elementName == null || this.elementName.equals("") || this.count == 0) { 96 throw new ProcessingException("FilterTransformer: both "+ ELEMENT + " and " + 97 COUNT + " parameters need to be specified"); 98 } 99 } 100 101 109 public java.io.Serializable getKey() { 110 return this.elementName + '<' + this.count + '>' + this.blocknr; 111 } 112 113 121 public SourceValidity getValidity() { 122 return NOPValidity.SHARED_INSTANCE; 123 } 124 125 126 public void startElement(String uri, String name, String raw, Attributes attributes) 127 throws SAXException { 128 if (name.equalsIgnoreCase(elementName)) { 129 this.foundIt = true; 130 this.counter++; 131 if (this.counter <= (this.count*(this.blocknr)) && this.counter > (this.count*(this.blocknr-1))) { 132 this.skip = false; 133 } else { 134 this.skip = true; 135 } 136 if (this.currentBlocknr != (int)Math.ceil((float)this.counter/this.count)) { 137 this.currentBlocknr = (int)Math.ceil((float)this.counter/this.count); 138 AttributesImpl attr = new AttributesImpl (); 139 attr.addAttribute("", BLOCKID, BLOCKID, "CDATA", String.valueOf(this.currentBlocknr)); 140 if (this.counter < this.count) { 141 super.contentHandler.startElement("", BLOCK, BLOCK, attr); 142 } else { 143 if (this.counter > 1) { 147 super.contentHandler.endElement("", BLOCK, BLOCK); 148 } 149 super.contentHandler.startElement("", BLOCK, BLOCK, attr); 150 } 151 } 152 } else if (!this.foundIt) { 153 this.parentName = name; 154 } 155 if (!this.skip) { 156 super.contentHandler.startElement(uri,name,raw,attributes); 157 } 158 } 159 160 public void endElement(String uri,String name,String raw) 161 throws SAXException { 162 if (this.foundIt && name.equals(this.parentName)) { 163 super.contentHandler.endElement("", BLOCK, BLOCK); 168 super.contentHandler.endElement(uri, name, raw); 169 this.foundIt = false; 170 this.skip = false; 171 } else if (!this.skip) { 172 super.contentHandler.endElement(uri,name,raw); 173 } 174 } 175 176 public void characters(char c[], int start, int len) 177 throws SAXException { 178 if (!this.skip) { 179 super.contentHandler.characters(c,start,len); 180 } 181 } 182 183 public void processingInstruction(String target, String data) 184 throws SAXException { 185 if (!this.skip) { 186 super.contentHandler.processingInstruction(target, data); 187 } 188 } 189 190 public void startEntity(String name) 191 throws SAXException { 192 if (!this.skip) { 193 super.lexicalHandler.startEntity(name); 194 } 195 } 196 197 public void endEntity(String name) 198 throws SAXException { 199 if (!this.skip) { 200 super.lexicalHandler.endEntity( name); 201 } 202 } 203 204 public void startCDATA() 205 throws SAXException { 206 if (!this.skip) { 207 super.lexicalHandler.startCDATA(); 208 } 209 } 210 211 public void endCDATA() 212 throws SAXException { 213 if (!this.skip) { 214 super.lexicalHandler.endCDATA(); 215 } 216 } 217 218 public void comment(char ch[], int start, int len) 219 throws SAXException { 220 if (!this.skip) { 221 super.lexicalHandler.comment(ch, start, len); 222 } 223 } 224 } 225 | Popular Tags |