001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.schema;
011:
012: import org.xml.sax.Attributes;
013: import org.xml.sax.SAXException;
014:
015: /**
016: * AttributeDescriptionHandler
017: *
018: */
019: public class SchemaAttributeHandler extends BaseSchemaHandler {
020:
021: private String fElementName;
022:
023: private String fAttributeName;
024:
025: private String fTargetElementName;
026:
027: private String fTargetAttributeName;
028:
029: private StringBuffer fDescription;
030:
031: private final static String[] DESC_NESTED_ELEM = { "documentation", //$NON-NLS-1$
032: "annotation", "attribute", "complexType", "element" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
033:
034: private final static String NAME_ATTR = "name"; //$NON-NLS-1$
035:
036: /**
037: *
038: */
039: public SchemaAttributeHandler(String targetElementName,
040: String targetAttributeName) {
041: super ();
042: setTargetElementName(targetElementName);
043: setTargetAttributeName(targetAttributeName);
044: }
045:
046: public void setTargetElementName(String targetElementName) {
047: fTargetElementName = targetElementName;
048: }
049:
050: public void setTargetAttributeName(String targetAttributeName) {
051: fTargetAttributeName = targetAttributeName;
052: }
053:
054: protected void reset() {
055: super .reset();
056: fDescription = new StringBuffer();
057: fElementName = null;
058: fAttributeName = null;
059: }
060:
061: public void startElement(String uri, String localName,
062: String qName, Attributes attributes) throws SAXException {
063: super .startElement(uri, localName, qName, attributes);
064: if (qName.compareTo(DESC_NESTED_ELEM[4]) == 0) {
065: // Element
066: // Remember the last element that had a name attribute
067: // Element references are ignored
068: if ((attributes != null)
069: && (attributes.getValue(NAME_ATTR) != null)) {
070: fElementName = attributes.getValue(NAME_ATTR);
071: }
072: } else if (qName.compareTo(DESC_NESTED_ELEM[2]) == 0) {
073: // Attribute
074: // Remember the last attribute
075: if (attributes != null) {
076: fAttributeName = attributes.getValue(NAME_ATTR);
077: }
078: }
079: }
080:
081: public void characters(char[] ch, int start, int length)
082: throws SAXException {
083:
084: if (onTarget()) {
085: for (int i = 0; i < length; i++) {
086: fDescription.append(ch[start + i]);
087: }
088: }
089: }
090:
091: protected boolean onTarget() {
092: if (fElementList.size() >= DESC_NESTED_ELEM.length) {
093: for (int i = 0; i < DESC_NESTED_ELEM.length; i++) {
094: String currentElement = (String) fElementList.get(i);
095: if (currentElement.compareTo(DESC_NESTED_ELEM[i]) != 0) {
096: return false;
097: }
098: }
099: if ((fElementName == null)
100: || (fElementName.compareTo(fTargetElementName) != 0)) {
101: return false;
102: }
103: if ((fAttributeName == null)
104: || (fAttributeName.compareTo(fTargetAttributeName) != 0)) {
105: return false;
106: }
107: return true;
108: }
109: return false;
110: }
111:
112: public String getDescription() {
113: return fDescription.toString();
114: }
115:
116: }
|