01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.forms.generation;
18:
19: import org.apache.cocoon.forms.FormsConstants;
20: import org.apache.cocoon.xml.AbstractXMLPipe;
21: import org.xml.sax.Attributes;
22: import org.xml.sax.ContentHandler;
23: import org.xml.sax.SAXException;
24: import org.xml.sax.helpers.DefaultHandler;
25:
26: /**
27: * A filter for selection lists, that keeps only those items that start with a given filter value.
28: *
29: * @since 2.1.8
30: * @version $Id: SelectionListFilter.java 449149 2006-09-23 03:58:05Z crossley $
31: */
32: public class SelectionListFilter extends AbstractXMLPipe {
33:
34: private ContentHandler next;
35: private int filterDepth = 0;
36: private int depth = 0;
37: private String filterValue;
38: private static final ContentHandler NULL_HANDLER = new DefaultHandler();
39:
40: public SelectionListFilter(String filterValue, ContentHandler next) {
41: this .next = next;
42: this .setContentHandler(next);
43: this .filterValue = filterValue;
44: }
45:
46: public void startElement(String uri, String loc, String raw,
47: Attributes a) throws SAXException {
48: depth++;
49:
50: if (uri.equals(FormsConstants.INSTANCE_NS)
51: && loc.equals("item")) {
52: String value = a.getValue("value");
53: if (!value.startsWith(this .filterValue)) {
54: filterDepth = depth;
55: setContentHandler(NULL_HANDLER);
56: }
57: }
58:
59: super .startElement(uri, loc, raw, a);
60: }
61:
62: public void endElement(String uri, String loc, String raw)
63: throws SAXException {
64: super .endElement(uri, loc, raw);
65:
66: if (depth == filterDepth) {
67: filterDepth = 0;
68: setContentHandler(this.next);
69: }
70:
71: depth--;
72: }
73: }
|