1 19 20 package org.netbeans.modules.palette; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 import org.openide.util.NbBundle; 24 import org.xml.sax.Attributes ; 25 import org.xml.sax.SAXException ; 26 import org.xml.sax.helpers.DefaultHandler ; 27 28 29 33 public final class PaletteItemHandler extends DefaultHandler { 34 35 private static final String XML_ROOT = "editor_palette_item"; private static final String ATTR_VERSION = "version"; private static final String TAG_BODY = "body"; private static final String TAG_CLASS = "class"; private static final String ATTR_CLASSNAME = "name"; private static final String TAG_CUSTOMIZER = "customizer"; private static final String ATTR_CUSTNAME = "name"; private static final String TAG_ICON16 = "icon16"; private static final String ATTR_URL = "urlvalue"; private static final String TAG_ICON32 = "icon32"; private static final String TAG_DESCRIPTION = "description"; private static final String TAG_INLINE_DESCRIPTION = "inline-description"; private static final String TAG_DISPLAY_NAME = "display-name"; private static final String TAG_TOOLTIP = "tooltip"; private static final String ATTR_BUNDLE = "localizing-bundle"; private static final String ATTR_DISPLAY_NAME_KEY = "display-name-key"; private static final String ATTR_TOOLTIP_KEY = "tooltip-key"; 53 private LinkedList <String > bodyLines; 54 private boolean insideBody = false; 55 56 private String body; 58 private String className; 59 60 private String icon16URL; 61 private String icon32URL; 62 private String bundleName; 63 private String displayNameKey; 64 private String tooltipKey; 65 private String displayName; 66 private String tooltip; 67 68 private StringBuffer textBuffer; 69 70 public String getBody() { return body; } 71 public String getClassName() { return className; } 72 73 public String getIcon16URL() { return icon16URL; } 74 public String getIcon32URL() { return icon32URL; } 75 public String getBundleName() { return bundleName; } 76 public String getDisplayNameKey() { return displayNameKey; } 77 public String getTooltipKey() { return tooltipKey; } 78 public String getDisplayName() { return displayName; } 79 public String getTooltip() { return tooltip; } 80 81 public void startElement(String uri, String localName, String qName, Attributes attributes) 82 throws SAXException 83 { 84 if (XML_ROOT.equals(qName)) { 85 String version = attributes.getValue(ATTR_VERSION); 86 if (version == null) { 87 String message = NbBundle.getBundle(PaletteItemHandler.class) 88 .getString("MSG_UnknownEditorPaletteItemVersion"); throw new SAXException (message); 90 } else if (!("1.0".equals(version) || "1.1".equals(version))) { String message = NbBundle.getBundle(PaletteItemHandler.class) 92 .getString("MSG_UnsupportedEditorPaletteItemVersion"); throw new SAXException (message); 94 } 95 } else if (TAG_BODY.equals(qName)) { 96 bodyLines = new LinkedList <String >(); 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 } else if (TAG_ICON32.equals(qName)) { 104 icon32URL = attributes.getValue(ATTR_URL); 105 } 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 (); 116 } else if (TAG_TOOLTIP.equals(qName)) { 117 textBuffer = new StringBuffer (); 118 } 119 } 120 121 public void endElement(String uri, String localName, String qName) throws SAXException { 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 137 { 138 if (insideBody) { 139 String chars = new String (buf, offset, len).trim(); 140 bodyLines.add(chars + "\n"); 141 } else if( null != textBuffer ) { 142 textBuffer.append( buf, offset, len ); 143 } 144 } 145 146 149 private String trimSurroundingLines(List <String > lines) { 150 151 int nlines = lines.size(); 152 153 int firstNonEmpty = nlines; 154 155 for (int i = 0; i < firstNonEmpty; i++) { 157 String line = (String )lines.get(i); 158 if (line.trim().length() != 0) 159 firstNonEmpty = i; 160 } 161 162 int lastNonEmpty = -1; 163 164 for (int i = nlines - 1; i > lastNonEmpty; i--) { 166 String line = lines.get(i); 167 if (line.trim().length() != 0) 168 lastNonEmpty = i; 169 } 170 171 StringBuffer sb = new StringBuffer (); 172 for (int i = firstNonEmpty; i <= lastNonEmpty; i++) 173 sb.append(lines.get(i)); 174 175 String body = sb.toString(); 176 if (body.length() > 0 && body.charAt(body.length() - 1) == '\n') body = body.substring(0, body.length() - 1); 178 179 return body; 180 } 181 } 182 183 | Popular Tags |