KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > generation > SelectionListFilter


1 /*
2  * Copyright 2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.forms.generation;
17
18 import org.apache.cocoon.forms.FormsConstants;
19 import org.apache.cocoon.xml.AbstractXMLPipe;
20 import org.xml.sax.Attributes JavaDoc;
21 import org.xml.sax.ContentHandler JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.helpers.DefaultHandler JavaDoc;
24
25 /**
26  * A filter for selection lists, that keeps only those items that start with a given filter value.
27  *
28  * @since 2.1.8
29  * @version $Id: SelectionListFilter.java 326838 2005-10-20 06:26:53Z sylvain $
30  */

31 public class SelectionListFilter extends AbstractXMLPipe {
32     
33     private ContentHandler JavaDoc next;
34     private int filterDepth = 0;
35     private int depth = 0;
36     private String JavaDoc filterValue;
37     private static final ContentHandler JavaDoc NULL_HANDLER = new DefaultHandler JavaDoc();
38
39     public SelectionListFilter(String JavaDoc filterValue, ContentHandler JavaDoc next) {
40         this.next = next;
41         this.setContentHandler(next);
42         this.filterValue = filterValue;
43     }
44     
45     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a) throws SAXException JavaDoc {
46         depth++;
47
48         if (uri.equals(FormsConstants.INSTANCE_NS) && loc.equals("item")) {
49             String JavaDoc value = a.getValue("value");
50             if (!value.startsWith(this.filterValue)) {
51                 filterDepth = depth;
52                 setContentHandler(NULL_HANDLER);
53             }
54         }
55
56         super.startElement(uri, loc, raw, a);
57     }
58     
59     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) throws SAXException JavaDoc {
60         super.endElement(uri, loc, raw);
61         
62         if (depth == filterDepth) {
63             filterDepth = 0;
64             setContentHandler(this.next);
65         }
66
67         depth--;
68     }
69 }
70
Popular Tags