001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.palette;
043:
044: import java.util.LinkedList;
045: import java.util.List;
046: import org.openide.util.NbBundle;
047: import org.xml.sax.Attributes;
048: import org.xml.sax.SAXException;
049: import org.xml.sax.helpers.DefaultHandler;
050:
051: /**
052: *
053: * @author Libor Kotouc
054: */
055: public final class PaletteItemHandler extends DefaultHandler {
056:
057: private static final String XML_ROOT = "editor_palette_item"; // NOI18N
058: private static final String ATTR_VERSION = "version"; // NOI18N
059: private static final String TAG_BODY = "body"; // NOI18N
060: private static final String TAG_CLASS = "class"; // NOI18N
061: private static final String ATTR_CLASSNAME = "name"; // NOI18N
062: private static final String TAG_CUSTOMIZER = "customizer"; // NOI18N
063: private static final String ATTR_CUSTNAME = "name"; // NOI18N
064: private static final String TAG_ICON16 = "icon16"; // NOI18N
065: private static final String ATTR_URL = "urlvalue"; // NOI18N
066: private static final String TAG_ICON32 = "icon32"; // NOI18N
067: private static final String TAG_DESCRIPTION = "description"; // NOI18N
068: private static final String TAG_INLINE_DESCRIPTION = "inline-description"; // NOI18N
069: private static final String TAG_DISPLAY_NAME = "display-name"; // NOI18N
070: private static final String TAG_TOOLTIP = "tooltip"; // NOI18N
071: private static final String ATTR_BUNDLE = "localizing-bundle"; // NOI18N
072: private static final String ATTR_DISPLAY_NAME_KEY = "display-name-key"; // NOI18N
073: private static final String ATTR_TOOLTIP_KEY = "tooltip-key"; // NOI18N
074:
075: private LinkedList<String> bodyLines;
076: private boolean insideBody = false;
077:
078: //raw data read from the file
079: private String body;
080: private String className;
081:
082: private String icon16URL;
083: private String icon32URL;
084: private String bundleName;
085: private String displayNameKey;
086: private String tooltipKey;
087: private String displayName;
088: private String tooltip;
089:
090: private StringBuffer textBuffer;
091:
092: public String getBody() {
093: return body;
094: }
095:
096: public String getClassName() {
097: return className;
098: }
099:
100: public String getIcon16URL() {
101: return icon16URL;
102: }
103:
104: public String getIcon32URL() {
105: return icon32URL;
106: }
107:
108: public String getBundleName() {
109: return bundleName;
110: }
111:
112: public String getDisplayNameKey() {
113: return displayNameKey;
114: }
115:
116: public String getTooltipKey() {
117: return tooltipKey;
118: }
119:
120: public String getDisplayName() {
121: return displayName;
122: }
123:
124: public String getTooltip() {
125: return tooltip;
126: }
127:
128: public void startElement(String uri, String localName,
129: String qName, Attributes attributes) throws SAXException {
130: if (XML_ROOT.equals(qName)) {
131: String version = attributes.getValue(ATTR_VERSION);
132: if (version == null) {
133: String message = NbBundle.getBundle(
134: PaletteItemHandler.class).getString(
135: "MSG_UnknownEditorPaletteItemVersion"); // NOI18N
136: throw new SAXException(message);
137: } else if (!("1.0".equals(version) || "1.1".equals(version))) { // NOI18N
138: String message = NbBundle.getBundle(
139: PaletteItemHandler.class).getString(
140: "MSG_UnsupportedEditorPaletteItemVersion"); // NOI18N
141: throw new SAXException(message);
142: }
143: } else if (TAG_BODY.equals(qName)) {
144: bodyLines = new LinkedList<String>();
145: insideBody = true;
146: } else if (TAG_CLASS.equals(qName)) {
147: className = attributes.getValue(ATTR_CLASSNAME);
148: } else if (TAG_ICON16.equals(qName)) {
149: icon16URL = attributes.getValue(ATTR_URL);
150: // TODO support also class resource name for icons
151: } else if (TAG_ICON32.equals(qName)) {
152: icon32URL = attributes.getValue(ATTR_URL);
153: // TODO support also class resource name for icons
154: } else if (TAG_DESCRIPTION.equals(qName)) {
155: bundleName = attributes.getValue(ATTR_BUNDLE);
156: displayNameKey = attributes.getValue(ATTR_DISPLAY_NAME_KEY);
157: tooltipKey = attributes.getValue(ATTR_TOOLTIP_KEY);
158: } else if (TAG_INLINE_DESCRIPTION.equals(qName)) {
159: bundleName = null;
160: displayNameKey = null;
161: tooltipKey = null;
162: } else if (TAG_DISPLAY_NAME.equals(qName)) {
163: textBuffer = new StringBuffer();
164: } else if (TAG_TOOLTIP.equals(qName)) {
165: textBuffer = new StringBuffer();
166: }
167: }
168:
169: public void endElement(String uri, String localName, String qName)
170: throws SAXException {
171:
172: if (TAG_BODY.equals(qName)) {
173: insideBody = false;
174: body = trimSurroundingLines(bodyLines);
175: } else if (TAG_DISPLAY_NAME.equals(qName)) {
176: displayName = textBuffer.toString();
177: textBuffer = null;
178: } else if (TAG_TOOLTIP.equals(qName)) {
179: tooltip = textBuffer.toString();
180: textBuffer = null;
181: }
182: }
183:
184: public void characters(char buf[], int offset, int len)
185: throws SAXException {
186: if (insideBody) {
187: String chars = new String(buf, offset, len).trim();
188: bodyLines.add(chars + "\n");
189: } else if (null != textBuffer) {
190: textBuffer.append(buf, offset, len);
191: }
192: }
193:
194: /**
195: * Trims empty lines from the beginning and the end of the line list
196: */
197: private String trimSurroundingLines(List<String> lines) {
198:
199: int nlines = lines.size();
200:
201: int firstNonEmpty = nlines;
202:
203: //going from the beginning and skipping empty lines until the first nonempty line occurs
204: for (int i = 0; i < firstNonEmpty; i++) {
205: String line = (String) lines.get(i);
206: if (line.trim().length() != 0)
207: firstNonEmpty = i;
208: }
209:
210: int lastNonEmpty = -1;
211:
212: //going from the end and skipping empty lines until the first nonempty line occurs
213: for (int i = nlines - 1; i > lastNonEmpty; i--) {
214: String line = lines.get(i);
215: if (line.trim().length() != 0)
216: lastNonEmpty = i;
217: }
218:
219: StringBuffer sb = new StringBuffer();
220: for (int i = firstNonEmpty; i <= lastNonEmpty; i++)
221: sb.append(lines.get(i));
222:
223: String body = sb.toString();
224: if (body.length() > 0 && body.charAt(body.length() - 1) == '\n') // cut trailing new-line
225: body = body.substring(0, body.length() - 1);
226:
227: return body;
228: }
229: }
|