01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.core.schema;
11:
12: import org.xml.sax.Attributes;
13: import org.xml.sax.SAXException;
14:
15: /**
16: * ElementDescriptionHandler
17: *
18: */
19: public class SchemaElementHandler extends BaseSchemaHandler {
20:
21: private String fElementName;
22:
23: private String fTargetElementName;
24:
25: private StringBuffer fDescription;
26:
27: private final static String[] DESC_NESTED_ELEM = { "documentation", //$NON-NLS-1$
28: "annotation", "element" }; //$NON-NLS-1$ //$NON-NLS-2$
29:
30: private final static String NAME_ATTR = "name"; //$NON-NLS-1$
31:
32: /**
33: *
34: */
35: public SchemaElementHandler(String targetElementName) {
36: super ();
37: setTargetElementName(targetElementName);
38: }
39:
40: public void setTargetElementName(String targetElement) {
41: fTargetElementName = targetElement;
42: }
43:
44: protected void reset() {
45: super .reset();
46: fDescription = new StringBuffer();
47: fElementName = null;
48: }
49:
50: public void startElement(String uri, String localName,
51: String qName, Attributes attributes) throws SAXException {
52: super .startElement(uri, localName, qName, attributes);
53:
54: if (qName.compareTo(DESC_NESTED_ELEM[2]) == 0) {
55: // Element
56: // Note: No need to check if attributes object is null
57: // Parser returns empty attributes object if no attributes
58: // Remember the last element
59: fElementName = attributes.getValue(NAME_ATTR);
60: }
61: }
62:
63: public void characters(char[] ch, int start, int length)
64: throws SAXException {
65:
66: if (onTarget()) {
67: for (int i = 0; i < length; i++) {
68: fDescription.append(ch[start + i]);
69: }
70: }
71: }
72:
73: protected boolean onTarget() {
74: if (fElementList.size() >= DESC_NESTED_ELEM.length) {
75: for (int i = 0; i < DESC_NESTED_ELEM.length; i++) {
76: String currentElement = (String) fElementList.get(i);
77: if (currentElement.compareTo(DESC_NESTED_ELEM[i]) != 0) {
78: return false;
79: }
80: }
81: if ((fElementName == null)
82: || (fElementName.compareTo(fTargetElementName) != 0)) {
83: return false;
84: }
85: return true;
86: }
87: return false;
88: }
89:
90: public String getDescription() {
91: return fDescription.toString();
92: }
93: }
|