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: */
18: package org.apache.ivy.util;
19:
20: import java.util.Iterator;
21: import java.util.Stack;
22:
23: import org.xml.sax.Attributes;
24: import org.xml.sax.SAXException;
25: import org.xml.sax.helpers.DefaultHandler;
26:
27: public class ContextualSAXHandler extends DefaultHandler {
28:
29: private Stack contextStack = new Stack();
30:
31: private StringBuffer buffer = new StringBuffer();
32:
33: public void characters(char[] ch, int start, int length)
34: throws SAXException {
35: buffer.append(ch, start, length);
36: }
37:
38: public void startElement(String uri, String localName,
39: String qName, Attributes attributes) throws SAXException {
40: contextStack.push(qName);
41: buffer.setLength(0);
42: }
43:
44: public void endElement(String uri, String localName, String qName)
45: throws SAXException {
46: contextStack.pop();
47: buffer.setLength(0);
48: }
49:
50: protected String getContext() {
51: StringBuffer buf = new StringBuffer();
52: for (Iterator iter = contextStack.iterator(); iter.hasNext();) {
53: String ctx = (String) iter.next();
54: buf.append(ctx).append("/");
55: }
56: if (buf.length() > 0) {
57: buf.setLength(buf.length() - 1);
58: }
59: return buf.toString();
60: }
61:
62: protected String getText() {
63: return buffer.toString();
64: }
65:
66: }
|