KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ElementDescriptionHandler
19  *
20  */

21 public class SchemaElementHandler extends BaseSchemaHandler {
22
23     private String JavaDoc fElementName;
24     
25     private String JavaDoc fTargetElementName;
26     
27     private StringBuffer JavaDoc fDescription;
28     
29     private final static String JavaDoc[] DESC_NESTED_ELEM = { "documentation", //$NON-NLS-1$
30
"annotation", "element" }; //$NON-NLS-1$ //$NON-NLS-2$
31

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

34     /**
35      *
36      */

37     public SchemaElementHandler(String JavaDoc targetElementName) {
38         super();
39         setTargetElementName(targetElementName);
40     }
41
42     public void setTargetElementName(String JavaDoc targetElement) {
43         fTargetElementName = targetElement;
44     }
45     
46     protected void reset() {
47         super.reset();
48         fDescription = new StringBuffer JavaDoc();
49         fElementName = null;
50     }
51     
52     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
53         super.startElement(uri, localName, qName, attributes);
54
55         if (qName.compareTo(DESC_NESTED_ELEM[2]) == 0) {
56             // Element
57
// Note: No need to check if attributes object is null
58
// Parser returns empty attributes object if no attributes
59
// Remember the last element
60
fElementName = attributes.getValue(NAME_ATTR);
61         }
62     }
63
64     public void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
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 JavaDoc currentElement = (String JavaDoc)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 JavaDoc getDescription() {
91         return fDescription.toString();
92     }
93 }
94
Popular Tags