2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.palette;
43
import java.util.LinkedList;
44
import java.util.List;
45
import org.openide.util.NbBundle;
46
import org.xml.sax.Attributes;
47
import org.xml.sax.SAXException;
48
import org.xml.sax.helpers.DefaultHandler;
53
* @author Libor Kotouc
55
public final class PaletteItemHandler extends DefaultHandler {
57
private static final String XML_ROOT = "editor_palette_item"; // NOI18N
58
private static final String ATTR_VERSION = "version"; // NOI18N
59
private static final String TAG_BODY = "body"; // NOI18N
60
private static final String TAG_CLASS = "class"; // NOI18N
61
private static final String ATTR_CLASSNAME = "name"; // NOI18N
62
private static final String TAG_CUSTOMIZER = "customizer"; // NOI18N
63
private static final String ATTR_CUSTNAME = "name"; // NOI18N
64
private static final String TAG_ICON16 = "icon16"; // NOI18N
65
private static final String ATTR_URL = "urlvalue"; // NOI18N
66
private static final String TAG_ICON32 = "icon32"; // NOI18N
67
private static final String TAG_DESCRIPTION = "description"; // NOI18N
68
private static final String TAG_INLINE_DESCRIPTION = "inline-description"; // NOI18N
69
private static final String TAG_DISPLAY_NAME = "display-name"; // NOI18N
70
private static final String TAG_TOOLTIP = "tooltip"; // NOI18N
71
private static final String ATTR_BUNDLE = "localizing-bundle"; // NOI18N
72
private static final String ATTR_DISPLAY_NAME_KEY = "display-name-key"; // NOI18N
73
private static final String ATTR_TOOLTIP_KEY = "tooltip-key"; // NOI18N
75
private LinkedList<String> bodyLines;
76
private boolean insideBody = false;
78
//raw data read from the file
80
private String className;
82
private String icon16URL;
83
private String icon32URL;
84
private String bundleName;
85
private String displayNameKey;
86
private String tooltipKey;
87
private String displayName;
88
private String tooltip;
90
private StringBuffer textBuffer;
92
public String getBody() { return body; }
93
public String getClassName() { return className; }
95
public String getIcon16URL() { return icon16URL; }
96
public String getIcon32URL() { return icon32URL; }
97
public String getBundleName() { return bundleName; }
98
public String getDisplayNameKey() { return displayNameKey; }
99
public String getTooltipKey() { return tooltipKey; }
100
public String getDisplayName() { return displayName; }
101
public String getTooltip() { return tooltip; }
103
public void startElement(String uri, String localName, String qName, Attributes attributes)
106
if (XML_ROOT.equals(qName)) {
107
String version = attributes.getValue(ATTR_VERSION);
108
if (version == null) {
109
String message = NbBundle.getBundle(PaletteItemHandler.class)
110
.getString("MSG_UnknownEditorPaletteItemVersion"); // NOI18N
111
throw new SAXException(message);
112
} else if (!("1.0".equals(version) || "1.1".equals(version))) { // NOI18N
113
String message = NbBundle.getBundle(PaletteItemHandler.class)
114
.getString("MSG_UnsupportedEditorPaletteItemVersion"); // NOI18N
115
throw new SAXException(message);
117
} else if (TAG_BODY.equals(qName)) {
118
bodyLines = new LinkedList<String>();
120
} else if (TAG_CLASS.equals(qName)) {
121
className = attributes.getValue(ATTR_CLASSNAME);
122
} else if (TAG_ICON16.equals(qName)) {
123
icon16URL = attributes.getValue(ATTR_URL);
124
// TODO support also class resource name for icons
125
} else if (TAG_ICON32.equals(qName)) {
126
icon32URL = attributes.getValue(ATTR_URL);
127
// TODO support also class resource name for icons
128
} else if (TAG_DESCRIPTION.equals(qName)) {
129
bundleName = attributes.getValue(ATTR_BUNDLE);
130
displayNameKey = attributes.getValue(ATTR_DISPLAY_NAME_KEY);
131
tooltipKey = attributes.getValue(ATTR_TOOLTIP_KEY);
132
} else if (TAG_INLINE_DESCRIPTION.equals(qName)) {
134
displayNameKey = null;
136
} else if (TAG_DISPLAY_NAME.equals(qName)) {
137
textBuffer = new StringBuffer();
138
} else if (TAG_TOOLTIP.equals(qName)) {
139
textBuffer = new StringBuffer();
143
public void endElement(String uri, String localName, String qName) throws SAXException {
145
if (TAG_BODY.equals(qName)) {
147
body = trimSurroundingLines(bodyLines);
148
} else if( TAG_DISPLAY_NAME.equals(qName) ) {
149
displayName = textBuffer.toString();
151
} else if( TAG_TOOLTIP.equals(qName) ) {
152
tooltip = textBuffer.toString();
157
public void characters(char buf[], int offset, int len)
161
String chars = new String(buf, offset, len).trim();
162
bodyLines.add(chars + "\n");
163
} else if( null != textBuffer ) {
164
textBuffer.append( buf, offset, len );
169
* Trims empty lines from the beginning and the end of the line list
171
private String trimSurroundingLines(List<String> lines) {
173
int nlines = lines.size();
175
int firstNonEmpty = nlines;
177
//going from the beginning and skipping empty lines until the first nonempty line occurs
178
for (int i = 0; i < firstNonEmpty; i++) {
179
String line = (String)lines.get(i);
180
if (line.trim().length() != 0)
184
int lastNonEmpty = -1;
186
//going from the end and skipping empty lines until the first nonempty line occurs
187
for (int i = nlines - 1; i > lastNonEmpty; i--) {
188
String line = lines.get(i);
189
if (line.trim().length() != 0)
193
StringBuffer sb = new StringBuffer();
194
for (int i = firstNonEmpty; i <= lastNonEmpty; i++)
195
sb.append(lines.get(i));
197
String body = sb.toString();
198
if (body.length() > 0 && body.charAt(body.length() - 1) == '\n') // cut trailing new-line
199
body = body.substring(0, body.length() - 1);