~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to core/palette/src/org/netbeans/modules/palette/PaletteItemHandler.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
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;
 
49
 
 
50
 
 
51
/**
 
52
 *
 
53
 * @author Libor Kotouc
 
54
 */
 
55
public final class PaletteItemHandler extends DefaultHandler {
 
56
 
 
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
 
74
 
 
75
    private LinkedList<String> bodyLines;
 
76
    private boolean insideBody = false;
 
77
    
 
78
    //raw data read from the file
 
79
    private String body;
 
80
    private String className;
 
81
    
 
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;
 
89
    
 
90
    private StringBuffer textBuffer;
 
91
    
 
92
    public String getBody() { return body; }
 
93
    public String getClassName() { return className; }
 
94
    
 
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; }
 
102
    
 
103
    public void startElement(String uri, String localName, String qName, Attributes attributes) 
 
104
        throws SAXException 
 
105
    {
 
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);
 
116
            }
 
117
        } else if (TAG_BODY.equals(qName)) {
 
118
            bodyLines = new LinkedList<String>();
 
119
            insideBody = true;
 
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)) {
 
133
            bundleName = null;
 
134
            displayNameKey = null;
 
135
            tooltipKey = null;
 
136
        } else if (TAG_DISPLAY_NAME.equals(qName)) {
 
137
            textBuffer = new StringBuffer();
 
138
        } else if (TAG_TOOLTIP.equals(qName)) {
 
139
            textBuffer = new StringBuffer();
 
140
        }
 
141
    }
 
142
    
 
143
    public void endElement(String uri, String localName, String qName) throws SAXException {
 
144
 
 
145
        if (TAG_BODY.equals(qName)) {
 
146
            insideBody = false;
 
147
            body = trimSurroundingLines(bodyLines);
 
148
        } else if( TAG_DISPLAY_NAME.equals(qName) ) {
 
149
            displayName = textBuffer.toString();
 
150
            textBuffer = null;
 
151
        } else if( TAG_TOOLTIP.equals(qName) ) {
 
152
            tooltip = textBuffer.toString();
 
153
            textBuffer = null;
 
154
        }
 
155
    }
 
156
    
 
157
    public void characters(char buf[], int offset, int len)
 
158
        throws SAXException
 
159
    {
 
160
        if (insideBody) {
 
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 );
 
165
        }
 
166
    }
 
167
 
 
168
    /**
 
169
     * Trims empty lines from the beginning and the end of the line list
 
170
     */
 
171
    private String trimSurroundingLines(List<String> lines) {
 
172
        
 
173
        int nlines = lines.size();
 
174
        
 
175
        int firstNonEmpty = nlines;
 
176
 
 
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)
 
181
                firstNonEmpty = i;
 
182
        }
 
183
            
 
184
        int lastNonEmpty = -1;
 
185
        
 
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)
 
190
                lastNonEmpty = i;
 
191
        }
 
192
 
 
193
        StringBuffer sb = new StringBuffer();
 
194
        for (int i = firstNonEmpty; i <= lastNonEmpty; i++)
 
195
            sb.append(lines.get(i));
 
196
        
 
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); 
 
200
        
 
201
        return body;
 
202
    }
 
203
}
 
204