KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > palette > PaletteItemHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.palette;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23 import org.openide.util.NbBundle;
24 import org.xml.sax.Attributes JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.helpers.DefaultHandler JavaDoc;
27
28
29 /**
30  *
31  * @author Libor Kotouc
32  */

33 public final class PaletteItemHandler extends DefaultHandler JavaDoc {
34
35     private static final String JavaDoc XML_ROOT = "editor_palette_item"; // NOI18N
36
private static final String JavaDoc ATTR_VERSION = "version"; // NOI18N
37
private static final String JavaDoc TAG_BODY = "body"; // NOI18N
38
private static final String JavaDoc TAG_CLASS = "class"; // NOI18N
39
private static final String JavaDoc ATTR_CLASSNAME = "name"; // NOI18N
40
private static final String JavaDoc TAG_CUSTOMIZER = "customizer"; // NOI18N
41
private static final String JavaDoc ATTR_CUSTNAME = "name"; // NOI18N
42
private static final String JavaDoc TAG_ICON16 = "icon16"; // NOI18N
43
private static final String JavaDoc ATTR_URL = "urlvalue"; // NOI18N
44
private static final String JavaDoc TAG_ICON32 = "icon32"; // NOI18N
45
private static final String JavaDoc TAG_DESCRIPTION = "description"; // NOI18N
46
private static final String JavaDoc TAG_INLINE_DESCRIPTION = "inline-description"; // NOI18N
47
private static final String JavaDoc TAG_DISPLAY_NAME = "display-name"; // NOI18N
48
private static final String JavaDoc TAG_TOOLTIP = "tooltip"; // NOI18N
49
private static final String JavaDoc ATTR_BUNDLE = "localizing-bundle"; // NOI18N
50
private static final String JavaDoc ATTR_DISPLAY_NAME_KEY = "display-name-key"; // NOI18N
51
private static final String JavaDoc ATTR_TOOLTIP_KEY = "tooltip-key"; // NOI18N
52

53     private LinkedList JavaDoc<String JavaDoc> bodyLines;
54     private boolean insideBody = false;
55     
56     //raw data read from the file
57
private String JavaDoc body;
58     private String JavaDoc className;
59     
60     private String JavaDoc icon16URL;
61     private String JavaDoc icon32URL;
62     private String JavaDoc bundleName;
63     private String JavaDoc displayNameKey;
64     private String JavaDoc tooltipKey;
65     private String JavaDoc displayName;
66     private String JavaDoc tooltip;
67     
68     private StringBuffer JavaDoc textBuffer;
69     
70     public String JavaDoc getBody() { return body; }
71     public String JavaDoc getClassName() { return className; }
72     
73     public String JavaDoc getIcon16URL() { return icon16URL; }
74     public String JavaDoc getIcon32URL() { return icon32URL; }
75     public String JavaDoc getBundleName() { return bundleName; }
76     public String JavaDoc getDisplayNameKey() { return displayNameKey; }
77     public String JavaDoc getTooltipKey() { return tooltipKey; }
78     public String JavaDoc getDisplayName() { return displayName; }
79     public String JavaDoc getTooltip() { return tooltip; }
80     
81     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes)
82         throws SAXException JavaDoc
83     {
84         if (XML_ROOT.equals(qName)) {
85             String JavaDoc version = attributes.getValue(ATTR_VERSION);
86             if (version == null) {
87                 String JavaDoc message = NbBundle.getBundle(PaletteItemHandler.class)
88                     .getString("MSG_UnknownEditorPaletteItemVersion"); // NOI18N
89
throw new SAXException JavaDoc(message);
90             } else if (!("1.0".equals(version) || "1.1".equals(version))) { // NOI18N
91
String JavaDoc message = NbBundle.getBundle(PaletteItemHandler.class)
92                     .getString("MSG_UnsupportedEditorPaletteItemVersion"); // NOI18N
93
throw new SAXException JavaDoc(message);
94             }
95         } else if (TAG_BODY.equals(qName)) {
96             bodyLines = new LinkedList JavaDoc<String JavaDoc>();
97             insideBody = true;
98         } else if (TAG_CLASS.equals(qName)) {
99             className = attributes.getValue(ATTR_CLASSNAME);
100         } else if (TAG_ICON16.equals(qName)) {
101             icon16URL = attributes.getValue(ATTR_URL);
102             // TODO support also class resource name for icons
103
} else if (TAG_ICON32.equals(qName)) {
104             icon32URL = attributes.getValue(ATTR_URL);
105             // TODO support also class resource name for icons
106
} else if (TAG_DESCRIPTION.equals(qName)) {
107             bundleName = attributes.getValue(ATTR_BUNDLE);
108             displayNameKey = attributes.getValue(ATTR_DISPLAY_NAME_KEY);
109             tooltipKey = attributes.getValue(ATTR_TOOLTIP_KEY);
110         } else if (TAG_INLINE_DESCRIPTION.equals(qName)) {
111             bundleName = null;
112             displayNameKey = null;
113             tooltipKey = null;
114         } else if (TAG_DISPLAY_NAME.equals(qName)) {
115             textBuffer = new StringBuffer JavaDoc();
116         } else if (TAG_TOOLTIP.equals(qName)) {
117             textBuffer = new StringBuffer JavaDoc();
118         }
119     }
120     
121     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
122
123         if (TAG_BODY.equals(qName)) {
124             insideBody = false;
125             body = trimSurroundingLines(bodyLines);
126         } else if( TAG_DISPLAY_NAME.equals(qName) ) {
127             displayName = textBuffer.toString();
128             textBuffer = null;
129         } else if( TAG_TOOLTIP.equals(qName) ) {
130             tooltip = textBuffer.toString();
131             textBuffer = null;
132         }
133     }
134     
135     public void characters(char buf[], int offset, int len)
136         throws SAXException JavaDoc
137     {
138         if (insideBody) {
139             String JavaDoc chars = new String JavaDoc(buf, offset, len).trim();
140             bodyLines.add(chars + "\n");
141         } else if( null != textBuffer ) {
142             textBuffer.append( buf, offset, len );
143         }
144     }
145
146     /**
147      * Trims empty lines from the beginning and the end of the line list
148      */

149     private String JavaDoc trimSurroundingLines(List JavaDoc<String JavaDoc> lines) {
150         
151         int nlines = lines.size();
152         
153         int firstNonEmpty = nlines;
154
155         //going from the beginning and skipping empty lines until the first nonempty line occurs
156
for (int i = 0; i < firstNonEmpty; i++) {
157             String JavaDoc line = (String JavaDoc)lines.get(i);
158             if (line.trim().length() != 0)
159                 firstNonEmpty = i;
160         }
161             
162         int lastNonEmpty = -1;
163         
164         //going from the end and skipping empty lines until the first nonempty line occurs
165
for (int i = nlines - 1; i > lastNonEmpty; i--) {
166             String JavaDoc line = lines.get(i);
167             if (line.trim().length() != 0)
168                 lastNonEmpty = i;
169         }
170
171         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
172         for (int i = firstNonEmpty; i <= lastNonEmpty; i++)
173             sb.append(lines.get(i));
174         
175         String JavaDoc body = sb.toString();
176         if (body.length() > 0 && body.charAt(body.length() - 1) == '\n') // cut trailing new-line
177
body = body.substring(0, body.length() - 1);
178         
179         return body;
180     }
181 }
182
183
Popular Tags