1 19 20 package org.netbeans.modules.tasklist.suggestions; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.IOException ; 24 import java.net.MalformedURLException ; 25 import org.openide.loaders.XMLDataObject.Processor; 26 import org.xml.sax.AttributeList ; 27 import org.xml.sax.HandlerBase ; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.Parser ; 30 import org.xml.sax.SAXException ; 31 import org.openide.util.actions.SystemAction; 32 33 import java.util.List ; 34 import java.util.ArrayList ; 35 import java.net.URL ; 36 import javax.xml.parsers.SAXParserFactory ; 37 import org.openide.ErrorManager; 38 import org.openide.cookies.InstanceCookie; 39 import org.openide.loaders.XMLDataObject; 40 import org.xml.sax.helpers.DefaultHandler ; 41 import org.openide.xml.XMLUtil; 42 import org.xml.sax.Attributes ; 43 import org.xml.sax.InputSource ; 44 import org.xml.sax.SAXException ; 45 import org.xml.sax.XMLReader ; 46 import org.xml.sax.helpers.DefaultHandler ; 47 import org.openide.util.Lookup; 48 49 50 58 public final class SuggestionTypeProcessor implements InstanceCookie, Processor { 59 static final String DTD_PUBLIC_ID = "-//NetBeans//DTD suggestion type 1.0//EN"; static final String DTD_SYSTEM_ID = "http://www.netbeans.org/dtds/suggestion-type-1_0.dtd"; 62 static final String TAG_TYPE = "type"; static final String ATTR_TYPE_NAME = "name"; static final String ATTR_TYPE_LOCALIZING_BUNDLE = "localizing_bundle"; static final String ATTR_TYPE_DESCRIPTION_KEY = "description_key"; static final String ATTR_TYPE_LONGDESCRIPTION_KEY = "long_description_key"; static final String ATTR_TYPE_ICON = "icon"; static final String TAG_TYPE_ACTIONS = "actions"; static final String TAG_TYPE_ACTION = "action"; static final String ATTR_ACTION_CLASS = "class"; 72 73 private XMLDataObject xmlDataObject; 74 75 78 private SuggestionType suggestionType; 79 80 85 public void attachTo(XMLDataObject xmlDO) { 86 xmlDataObject = xmlDO; 87 } 88 89 94 public Object instanceCreate() throws IOException , ClassNotFoundException { 95 if (suggestionType != null) 96 return suggestionType; 97 98 parse(); 99 return suggestionType; 100 } 101 102 110 public Class instanceClass() { 111 return SuggestionType.class; 112 } 113 114 117 public String instanceName() { 118 return instanceClass().getName(); 119 } 120 121 123 private synchronized SuggestionType parse() { 124 if (suggestionType == null) { 125 try { 126 XMLReader reader = XMLUtil.createXMLReader(false); 127 128 TypeHandler handler = new TypeHandler(); 129 reader.setContentHandler(handler); 130 reader.setErrorHandler(handler); 131 reader.setEntityResolver(handler); 132 reader.parse(new InputSource (xmlDataObject.getPrimaryFile().getInputStream())); 133 suggestionType = handler.getSuggestionType(); 134 } catch (Exception e) { 135 ErrorManager.getDefault().notify(e); 136 } 137 138 } 139 return suggestionType; 140 } 141 142 private static class TypeHandler extends DefaultHandler { 143 private SuggestionType type = null; 144 145 String id = null; 146 List actions = null; 147 String localizer = null; 148 String key = null; 149 String longkey = null; 150 URL icon = null; 151 152 SuggestionType getSuggestionType() { 153 return type; 154 } 155 156 public void startElement(String uri, String localName, 157 String name, Attributes attrs) 158 throws SAXException { 159 if (TAG_TYPE.equals(name)) { 160 id = attrs.getValue(ATTR_TYPE_NAME); 162 actions = null; 163 164 localizer = attrs.getValue(ATTR_TYPE_LOCALIZING_BUNDLE); 166 key = attrs.getValue(ATTR_TYPE_DESCRIPTION_KEY); 167 longkey = attrs.getValue(ATTR_TYPE_LONGDESCRIPTION_KEY); 168 169 icon = null; 171 String ur = attrs.getValue(ATTR_TYPE_ICON); 172 if (ur != null) { 173 try { 174 icon = new URL (ur); 175 } catch (MalformedURLException ex) { 176 SAXException saxe = new SAXException (ex); 177 ErrorManager.getDefault(). 178 copyAnnotation(saxe, ex); 179 throw saxe; 180 } 181 } 182 } else if (TAG_TYPE_ACTIONS.equals(name)) { 183 } else if (TAG_TYPE_ACTION.equals(name)) { 185 String cln = attrs.getValue(ATTR_ACTION_CLASS); 186 if (cln == null) { 187 return; 188 } 189 try { 190 ClassLoader l = 191 (ClassLoader )Lookup.getDefault().lookup( 192 ClassLoader .class); 193 Class c = Class.forName(cln, true, l); 194 195 if (c != null) { 196 SystemAction a = SystemAction.get(c); 197 if (a != null) { 198 if (actions == null) { 199 actions = new ArrayList (); 200 } 201 actions.add(a); 202 } 203 } 204 } catch (ClassNotFoundException e) { 205 ErrorManager.getDefault().annotate(e, "TL: cannot load " + cln + " action, ignoring..."); ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 207 } 208 } else { 209 throw new SAXException ("malformed SuggestionType xml file"); } 211 } 212 213 public void endElement(String uri, String localName, String name) throws SAXException { 214 if (TAG_TYPE_ACTION.equals(name)) { 215 } else if (TAG_TYPE_ACTIONS.equals(name)) { 217 } else if (TAG_TYPE.equals(name)) { 219 if (id != null) { 220 type = new SuggestionType(id, localizer, 221 key, longkey, icon, 222 actions); 223 } 224 } 225 } 226 227 228 public InputSource resolveEntity(java.lang.String pid, 229 java.lang.String sid) throws SAXException { 230 if (DTD_PUBLIC_ID.equals(pid)) { 231 return new InputSource (new ByteArrayInputStream (new byte[0])); 232 } 233 return new InputSource (sid); 234 } 235 } 236 237 } 238 | Popular Tags |