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-2007 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: * CodeClipHandler.java
043: *
044: * Created on July 27, 2006, 10:16 AM
045: *
046: * Handler/SAX parser that grabs the data from the file
047: *
048: * @author Joelle Lam <joelle.lam@sun.com>
049: * @version %I%, %G%
050: * @see layer.xml
051: */
052:
053: package org.netbeans.modules.visualweb.palette.codeclips;
054:
055: import java.util.LinkedList;
056: import org.openide.util.NbBundle;
057: import org.xml.sax.Attributes;
058: import org.xml.sax.SAXException;
059: import org.xml.sax.helpers.DefaultHandler;
060:
061: public final class CodeClipHandler extends DefaultHandler {
062:
063: public static final String XML_ROOT = "codeclip_palette_item"; // NOI18N
064: public static final String ATTR_VERSION = "version"; // NOI18N
065: public static final String LATEST_VERSION = "1.0"; // NOI18N
066: public static final String TAG_BODY = "body"; // NOI18N
067: public static final String ATTR_CLASSNAME = "name"; // NOI18N
068: public static final String ATTR_CUSTNAME = "name"; // NOI18N
069: public static final String TAG_ICON16 = "icon16"; // NOI18N
070: public static final String ATTR_URL = "urlvalue"; // NOI18N
071: public static final String TAG_ICON32 = "icon32"; // NOI18N
072: public static final String TAG_DESCRIPTION = "description"; // NOI18N
073: public static final String ATTR_BUNDLE = "localizing-bundle"; // NOI18N
074: public static final String ATTR_DISPLAY_NAME_KEY = "display-name-key"; // NOI18N
075: public static final String ATTR_TOOLTIP_KEY = "tooltip-key"; // NOI18N
076:
077: private LinkedList<String> bodyLines;
078: private boolean insideBody = false;
079:
080: //raw data read from the file
081: // private HashMap attributeMap;
082: private String body;
083: private String icon16URL;
084: private String icon32URL;
085: private String bundleName;
086: private String displayNameKey;
087: private String tooltipKey;
088:
089: public String getBody() {
090: return body;
091: }
092:
093: public String getIcon16URL() {
094: return icon16URL;
095: }
096:
097: public String getIcon32URL() {
098: return icon32URL;
099: }
100:
101: public String getBundleName() {
102: return bundleName;
103: }
104:
105: public String getDisplayNameKey() {
106: return displayNameKey;
107: }
108:
109: public String getTooltipKey() {
110: return tooltipKey;
111: }
112:
113: @Override
114: public void startElement(String uri, String localName,
115: String qName, Attributes attributes) throws SAXException {
116: if (XML_ROOT.equals(qName)) {
117: String version = attributes.getValue(ATTR_VERSION);
118: if (version == null) {
119: String message = NbBundle.getBundle(
120: CodeClipHandler.class).getString(
121: "MSG_UnknownEditorPaletteItemVersion"); // NOI18N
122: throw new SAXException(message);
123: } else if (!version.equals(LATEST_VERSION)) { // NOI18N
124: String message = NbBundle.getBundle(
125: CodeClipHandler.class).getString(
126: "MSG_UnsupportedEditorPaletteItemVersion"); // NOI18N
127: throw new SAXException(message);
128: }
129: } else if (TAG_BODY.equals(qName)) {
130: bodyLines = new LinkedList<String>();
131: insideBody = true;
132: //We could ultimately parse the body here? Then we don't need to figure out the bundle.property file.
133: } else if (TAG_ICON16.equals(qName)) {
134: icon16URL = attributes.getValue(ATTR_URL);
135: // TODO support also class resource name for icons
136: } else if (TAG_ICON32.equals(qName)) {
137: icon32URL = attributes.getValue(ATTR_URL);
138: // TODO support also class resource name for icons
139: } else if (TAG_DESCRIPTION.equals(qName)) {
140: bundleName = attributes.getValue(ATTR_BUNDLE);
141: displayNameKey = attributes.getValue(ATTR_DISPLAY_NAME_KEY);
142: tooltipKey = attributes.getValue(ATTR_TOOLTIP_KEY);
143: }
144: }
145:
146: public void endElement(String uri, String localName, String qName)
147: throws SAXException {
148:
149: if (TAG_BODY.equals(qName)) {
150: insideBody = false;
151: body = trimSurroundingLines(bodyLines);
152: }
153: }
154:
155: public void characters(char buf[], int offset, int len)
156: throws SAXException {
157: if (insideBody) {
158: String chars = new String(buf, offset, len).trim();
159: bodyLines.add(chars + "\n");
160: }
161: }
162:
163: /**
164: * Trims empty lines from the beginning and the end of the line list
165: */
166: private String trimSurroundingLines(LinkedList/*<String>*/lines) {
167:
168: int nlines = lines.size();
169:
170: int firstNonEmpty = nlines;
171:
172: //going from the beginning and skipping empty lines until the first nonempty line occurs
173: for (int i = 0; i < firstNonEmpty; i++) {
174: String line = (String) lines.get(i);
175: if (line.trim().length() != 0)
176: firstNonEmpty = i;
177: }
178:
179: int lastNonEmpty = -1;
180:
181: //going from the end and skipping empty lines until the first nonempty line occurs
182: for (int i = nlines - 1; i > lastNonEmpty; i--) {
183: String line = (String) lines.get(i);
184: if (line.trim().length() != 0)
185: lastNonEmpty = i;
186: }
187:
188: StringBuffer sb = new StringBuffer();
189: for (int i = firstNonEmpty; i <= lastNonEmpty; i++)
190: sb.append((String) lines.get(i));
191:
192: String body = sb.toString();
193: if (body.length() > 0 && body.charAt(body.length() - 1) == '\n') // cut trailing new-line
194: body = body.substring(0, body.length() - 1);
195:
196: return body;
197: }
198: }
|