KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > schema > SchemaAttributeHandler


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.core.schema;
13
14 import org.xml.sax.Attributes JavaDoc;
15 import org.xml.sax.SAXException JavaDoc;
16
17 /**
18  * AttributeDescriptionHandler
19  *
20  */

21 public class SchemaAttributeHandler extends BaseSchemaHandler {
22
23     private String JavaDoc fElementName;
24     
25     private String JavaDoc fAttributeName;
26     
27     private String JavaDoc fTargetElementName;
28     
29     private String JavaDoc fTargetAttributeName;
30     
31     private StringBuffer JavaDoc fDescription;
32     
33     private final static String JavaDoc[] DESC_NESTED_ELEM = { "documentation", //$NON-NLS-1$
34
"annotation", "attribute", "complexType", "element" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
35

36     private final static String JavaDoc NAME_ATTR = "name"; //$NON-NLS-1$
37

38     /**
39      *
40      */

41     public SchemaAttributeHandler(String JavaDoc targetElementName, String JavaDoc targetAttributeName) {
42         super();
43         setTargetElementName(targetElementName);
44         setTargetAttributeName(targetAttributeName);
45     }
46
47     public void setTargetElementName(String JavaDoc targetElementName) {
48         fTargetElementName = targetElementName;
49     }
50
51     public void setTargetAttributeName(String JavaDoc targetAttributeName) {
52         fTargetAttributeName = targetAttributeName;
53     }
54     
55     protected void reset() {
56         super.reset();
57         fDescription = new StringBuffer JavaDoc();
58         fElementName = null;
59         fAttributeName = null;
60     }
61     
62     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
63         super.startElement(uri, localName, qName, attributes);
64         if (qName.compareTo(DESC_NESTED_ELEM[4]) == 0) {
65             // Element
66
// Remember the last element that had a name attribute
67
// Element references are ignored
68
if ((attributes != null) &&
69                     (attributes.getValue(NAME_ATTR) != null)) {
70                 fElementName = attributes.getValue(NAME_ATTR);
71             }
72         } else if (qName.compareTo(DESC_NESTED_ELEM[2]) == 0) {
73             // Attribute
74
// Remember the last attribute
75
if (attributes != null) {
76                 fAttributeName = attributes.getValue(NAME_ATTR);
77             }
78         }
79     }
80
81     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
82         
83         if (onTarget()) {
84             for (int i = 0; i < length; i++) {
85                 fDescription.append(ch[start + i]);
86             }
87         }
88     }
89     
90     protected boolean onTarget() {
91         if (fElementList.size() >= DESC_NESTED_ELEM.length) {
92             for (int i = 0; i < DESC_NESTED_ELEM.length; i++) {
93                 String JavaDoc currentElement = (String JavaDoc)fElementList.get(i);
94                 if (currentElement.compareTo(DESC_NESTED_ELEM[i]) != 0) {
95                     return false;
96                 }
97             }
98             if ((fElementName == null) ||
99                     (fElementName.compareTo(fTargetElementName) != 0)) {
100                 return false;
101             }
102             if ((fAttributeName == null) ||
103                     (fAttributeName.compareTo(fTargetAttributeName) != 0)) {
104                 return false;
105             }
106             return true;
107         }
108         return false;
109     }
110     
111     public String JavaDoc getDescription() {
112         return fDescription.toString();
113     }
114     
115 }
116
Popular Tags