From cocoon-dev-return-14357-apmail-xml-cocoon-dev-archive=xml.apache.org@xml.apache.org Mon Jun 18 14:22:35 2001 Return-Path: Delivered-To: apmail-xml-cocoon-dev-archive@xml.apache.org Received: (qmail 76699 invoked by uid 500); 18 Jun 2001 14:22:17 -0000 Mailing-List: contact cocoon-dev-help@xml.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: list-post: Reply-To: cocoon-dev@xml.apache.org Delivered-To: mailing list cocoon-dev@xml.apache.org Received: (qmail 76652 invoked from network); 18 Jun 2001 14:22:08 -0000 Message-ID: <703B15C33AA4D411A69000508B09061D0C8C41@BYRON> From: Beauprez Sven To: "'cocoon-dev@xml.apache.org'" Subject: C2: new FilterTransformer Date: Mon, 18 Jun 2001 10:29:59 +0200 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2650.21) Content-Type: multipart/mixed; boundary="----_=_NextPart_000_01C0F7D0.DC813F50" X-Spam-Rating: h31.sny.collab.net 1.6.2 0/1000/N ------_=_NextPart_000_01C0F7D0.DC813F50 Content-Type: text/plain; charset="iso-8859-1" Hi, In addition to the SQLTransformer, this one can be very handy... Feel free to add it to C2 Sven ------_=_NextPart_000_01C0F7D0.DC813F50 Content-Type: application/octet-stream; name="FilterTransformer.java" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="FilterTransformer.java" package org.apache.cocoon.transformation; import java.util.Map; import java.io.IOException; import org.apache.cocoon.ProcessingException; import org.apache.cocoon.transformation.AbstractTransformer; import org.apache.cocoon.environment.SourceResolver; import org.apache.avalon.framework.parameters.Parameters; import org.apache.avalon.excalibur.pool.Poolable; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; /** * The filter transformer can be used to let only an amount of elements = through in=20 * a given block. * * Usage in sitemap * <map:transform type=3D"filter"> * <map:parameter name=3D"element-name" value=3D"row"/> * <map:parameter name=3D"count" value=3D"5"/> * <map:parameter name=3D"blocknr" value=3D"3"/> * </map:transform> * * Only the 3th block will be shown, containing only 5 row elements. * * @author Sven = Beauprez * @version CVS $Revision: 1.11 $ $Date: 2001/06/18 08:30:52 $ $Author: = sbeaupre $ */ public class FilterTransformer extends AbstractTransformer implements = Poolable { private static final String ELEMENT =3D "element-name"; private static final String COUNT =3D "count"; private static final String BLOCKNR =3D "blocknr"; private static final String BLOCK =3D "block"; private static final String BLOCKID =3D "id"; private static final int DEFAULT_COUNT =3D 10; private static final int DEFAULT_BLOCK =3D 1; private int counter; private int count; private int blocknr; private int currentBlocknr; private String elementName; private String parentName; boolean skip; boolean foundIt; /** BEGIN SitemapComponent methods **/ public void setup(SourceResolver resolver, Map objectModel, String = source, Parameters parameters) throws ProcessingException, SAXException, IOException { counter=3D0; currentBlocknr=3D0; skip=3Dfalse; foundIt=3Dfalse; parentName=3Dnull; elementName =3D = parameters.getParameter(FilterTransformer.ELEMENT,null); String tmpCount =3D = parameters.getParameter(FilterTransformer.COUNT,""+FilterTransformer.DEF= AULT_COUNT); String tmpBlockNr =3D = parameters.getParameter(FilterTransformer.BLOCKNR,""+FilterTransformer.D= EFAULT_BLOCK); if (elementName=3D=3Dnull || tmpCount=3D=3Dnull) { getLogger().error("FilterTransformer: both "+ = FilterTransformer.ELEMENT + " and " + FilterTransformer.COUNT + " parameters need to = be specified"); }else { try { count =3D Integer.parseInt(tmpCount); getLogger().debug("FilterTransformer: " + = FilterTransformer.COUNT + "=3D" + count); } catch (NumberFormatException e) { //set default values so we can move on count=3DFilterTransformer.DEFAULT_COUNT; =20 getLogger().error("FilterTransformer: " + = FilterTransformer.COUNT +=20 " not valid, using default value of = "+FilterTransformer.DEFAULT_COUNT); } try { blocknr =3D Integer.parseInt(tmpBlockNr); getLogger().debug("FilterTransformer: " + = FilterTransformer.BLOCKNR + "=3D" + blocknr); } catch (NumberFormatException e) { //set default values so we can move on blocknr=3DFilterTransformer.DEFAULT_BLOCK; getLogger().error("FilterTransformer: " + = FilterTransformer.BLOCKNR +=20 " not valid, using default value of "+ = FilterTransformer.DEFAULT_BLOCK); } } } /** END SitemapComponent methods **/ /** BEGIN SAX ContentHandler handlers **/ public void startElement(String uri, String name, String raw, = Attributes attributes) throws SAXException { if (name.equalsIgnoreCase(elementName)) { foundIt=3Dtrue; counter++; if (counter <=3D (count*(blocknr)) && counter > = (count*(blocknr-1))) { skip=3Dfalse; } else { skip=3Dtrue; } if (currentBlocknr !=3D (int)Math.ceil((float)counter/count)) { currentBlocknr =3D (int)Math.ceil((float)counter/count); AttributesImpl attr =3D new AttributesImpl(); = attr.addAttribute(uri,FilterTransformer.BLOCKID,FilterTransformer.BLOCKI= D,"CDATA",String.valueOf(currentBlocknr)); if (counter < count) { = super.contentHandler.startElement(uri,FilterTransformer.BLOCK,FilterTran= sformer.BLOCK,attr); } else { = super.contentHandler.endElement(uri,FilterTransformer.BLOCK,FilterTransf= ormer.BLOCK); = super.contentHandler.startElement(uri,FilterTransformer.BLOCK,FilterTran= sformer.BLOCK,attr); } } } else if (!foundIt) { parentName =3D name; } if (!skip) { super.contentHandler.startElement(uri,name,raw,attributes); } } public void endElement(String uri,String name,String raw) throws SAXException { if (name.equalsIgnoreCase(parentName) && foundIt) { =20 = super.contentHandler.endElement(uri,FilterTransformer.BLOCK,FilterTransf= ormer.BLOCK); super.contentHandler.endElement(uri,name,raw); foundIt=3Dfalse; skip=3Dfalse; } else if (!skip) { super.contentHandler.endElement(uri,name,raw); } } public void characters(char c[], int start, int len) throws SAXException { if (!skip) { super.contentHandler.characters(c,start,len); } } public void processingInstruction(String target, String data) throws SAXException { if (!skip) { super.contentHandler.processingInstruction(target, data); } } public void startEntity(String name) throws SAXException { if (!skip) { super.lexicalHandler.startEntity(name); } } public void endEntity(String name) throws SAXException { if (!skip) { super.lexicalHandler.endEntity( name); } } public void startCDATA() throws SAXException { if (!skip) { super.lexicalHandler.startCDATA(); } } public void endCDATA() throws SAXException { if (!skip) { super.lexicalHandler.endCDATA(); } } public void comment(char ch[], int start, int len) throws SAXException { if (!skip) { super.lexicalHandler.comment(ch, start, len); } } /** END SAX ContentHandler handlers **/ } ------_=_NextPart_000_01C0F7D0.DC813F50 Content-Type: text/plain; charset=us-ascii --------------------------------------------------------------------- To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org For additional commands, email: cocoon-dev-help@xml.apache.org ------_=_NextPart_000_01C0F7D0.DC813F50--