1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.awt.Color ; 23 import java.io.ByteArrayInputStream ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 import java.util.ResourceBundle ; 29 import javax.xml.parsers.SAXParserFactory ; 30 import org.netbeans.editor.AnnotationType; 31 import org.openide.ErrorManager; 32 import org.openide.cookies.InstanceCookie; 33 import org.openide.filesystems.FileObject; 34 import org.openide.loaders.XMLDataObject; 35 import org.xml.sax.AttributeList ; 36 import org.xml.sax.HandlerBase ; 37 import org.xml.sax.InputSource ; 38 import org.xml.sax.Parser ; 39 import org.xml.sax.SAXException ; 40 41 47 public class AnnotationTypeProcessor implements XMLDataObject.Processor, InstanceCookie { 48 static final String DTD_PUBLIC_ID = "-//NetBeans//DTD annotation type 1.0//EN"; static final String DTD_SYSTEM_ID = "http://www.netbeans.org/dtds/annotation-type-1_0.dtd"; static final String DTD_PUBLIC_ID11 = "-//NetBeans//DTD annotation type 1.1//EN"; static final String DTD_SYSTEM_ID11 = "http://www.netbeans.org/dtds/annotation-type-1_1.dtd"; 53 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_VISIBLE = "visible"; static final String ATTR_TYPE_GLYPH = "glyph"; static final String ATTR_TYPE_HIGHLIGHT = "highlight"; static final String ATTR_TYPE_FOREGROUND = "foreground"; static final String ATTR_TYPE_WAVEUNDERLINE = "waveunderline"; static final String ATTR_TYPE_TYPE = "type"; static final String ATTR_TYPE_CONTENTTYPE = "contenttype"; static final String ATTR_TYPE_ACTIONS = "actions"; static final String ATTR_ACTION_NAME = "name"; static final String TAG_COMBINATION = "combination"; static final String ATTR_COMBINATION_TIPTEXT_KEY = "tiptext_key"; static final String ATTR_COMBINATION_ORDER = "order"; static final String ATTR_COMBINATION_MIN_OPTIONALS = "min_optionals"; static final String TAG_COMBINE = "combine"; static final String ATTR_COMBINE_ANNOTATIONTYPE = "annotationtype"; static final String ATTR_COMBINE_ABSORBALL = "absorb_all"; static final String ATTR_COMBINE_OPTIONAL = "optional"; static final String ATTR_COMBINE_MIN = "min"; static final String ATTR_USE_HIHGLIGHT_COLOR = "use_highlight_color"; static final String ATTR_USE_WAVE_UNDERLINE_COLOR = "use_wave_underline_color"; static final String ATTR_INHERIT_FOREGROUND_COLOR = "inherit_foreground_color"; static final String ATTR_USE_CUSTOM_SIDEBAR_COLOR = "use_custom_sidebar_color"; static final String ATTR_CUSTOM_SIDEBAR_COLOR = "custom_sidebar_color"; static final String ATTR_SEVERITY = "severity"; static final String ATTR_BROWSEABLE = "browseable"; static final String ATTR_PRIORITY = "priority"; 84 85 private FileObject xmlDataObject; 86 87 90 private AnnotationType annotationType; 91 92 97 public void attachTo(XMLDataObject xmlDO) { 98 xmlDataObject = xmlDO.getPrimaryFile(); 99 } 100 101 public void attachTo(FileObject xmlDO) { 103 xmlDataObject = xmlDO; 104 } 105 106 111 public Object instanceCreate() throws java.io.IOException , ClassNotFoundException { 112 annotationType = null; 113 parse(); 114 return annotationType; 115 } 116 117 125 public Class instanceClass() { 126 return AnnotationType.class; 127 } 128 129 132 public String instanceName() { 133 return instanceClass().getName(); 134 } 135 136 138 private synchronized AnnotationType parse() { 139 if (annotationType == null) { 140 AnnotationType at = new AnnotationType(); 141 Handler h = new Handler(at); 142 143 try { 144 Parser xp; 145 SAXParserFactory factory = SAXParserFactory.newInstance (); 146 factory.setValidating (false); 147 factory.setNamespaceAware(false); 148 xp = factory.newSAXParser ().getParser (); 149 xp.setEntityResolver(h); 150 xp.setDocumentHandler(h); 151 xp.setErrorHandler(h); 152 xp.parse(new InputSource (xmlDataObject.getInputStream())); 153 at.putProp(AnnotationType.PROP_FILE, xmlDataObject); 154 annotationType = at; 155 } catch (Exception e) { 156 ErrorManager.getDefault().notify(e); 157 } 158 159 } 160 return annotationType; 161 } 162 163 private static class Handler extends HandlerBase { 164 private AnnotationType at; 165 private int depth = 0; 166 private List combinations; 167 168 Handler(AnnotationType at) { 169 this.at=at; 170 } 171 172 private void rethrow(Exception e) throws SAXException { 173 throw new SAXException (e); 174 } 175 176 public void startElement(String name, AttributeList amap) throws SAXException { 177 switch (depth++) { 178 case 0: 179 if (! TAG_TYPE.equals(name)) { 180 throw new SAXException ("malformed AnnotationType xml file"); } 182 at.setName(amap.getValue(ATTR_TYPE_NAME)); 184 if (amap.getValue(ATTR_TYPE_TYPE) == null) 185 at.setWholeLine(true); 186 else 187 at.setWholeLine("line".equals(amap.getValue(ATTR_TYPE_TYPE))); 189 if (amap.getValue(ATTR_TYPE_VISIBLE) == null) 191 at.setVisible(true); 192 else 193 at.setVisible(amap.getValue(ATTR_TYPE_VISIBLE)); 194 if (at.isVisible()) { 195 String localizer = amap.getValue(ATTR_TYPE_LOCALIZING_BUNDLE); 196 String key = amap.getValue(ATTR_TYPE_DESCRIPTION_KEY); 197 at.putProp(AnnotationType.PROP_LOCALIZING_BUNDLE, localizer); 198 at.putProp(AnnotationType.PROP_DESCRIPTION_KEY, key); 199 } 200 201 String useHighlightString = amap.getValue(ATTR_USE_HIHGLIGHT_COLOR); 203 String useWaveUnderlineString = amap.getValue(ATTR_USE_WAVE_UNDERLINE_COLOR); 204 String inheritForeString = amap.getValue(ATTR_INHERIT_FOREGROUND_COLOR); 205 String useCustomSidebarColor = amap.getValue(ATTR_USE_CUSTOM_SIDEBAR_COLOR); 206 at.setUseHighlightColor(Boolean.valueOf(useHighlightString).booleanValue()); 207 at.setUseWaveUnderlineColor(Boolean.valueOf(useWaveUnderlineString).booleanValue()); 208 at.setInheritForegroundColor(Boolean.valueOf(inheritForeString).booleanValue()); 209 at.setUseCustomSidebarColor(Boolean.valueOf(useCustomSidebarColor).booleanValue()); 210 211 try { 213 String color = amap.getValue(ATTR_TYPE_HIGHLIGHT); 214 if (color != null) { 215 at.setHighlight(Color.decode(color)); 216 if (useHighlightString == null) at.setUseHighlightColor(true); 217 } else { 218 if (useHighlightString == null) at.setUseHighlightColor(false); 219 } 220 221 color = amap.getValue(ATTR_TYPE_FOREGROUND); 222 if (color != null) { 223 at.setForegroundColor(Color.decode(color)); 224 if (inheritForeString == null) at.setInheritForegroundColor(false); 225 } else { 226 if (inheritForeString == null) at.setInheritForegroundColor(true); 227 } 228 229 color = amap.getValue(ATTR_TYPE_WAVEUNDERLINE); 230 if (color != null) { 231 at.setWaveUnderlineColor(Color.decode(color)); 232 if (useWaveUnderlineString == null) at.setUseWaveUnderlineColor(true); 233 } else { 234 if (useWaveUnderlineString == null) at.setUseWaveUnderlineColor(false); 235 } 236 color = amap.getValue(ATTR_CUSTOM_SIDEBAR_COLOR); 237 if (color != null) { 238 at.setCustomSidebarColor(Color.decode(color)); 239 if (useCustomSidebarColor == null) at.setUseCustomSidebarColor(true); 240 } else { 241 if (useCustomSidebarColor == null) at.setUseCustomSidebarColor(false); 242 } 243 } catch (NumberFormatException ex) { 244 rethrow(ex); 245 } 246 247 try { 249 String uri = amap.getValue(ATTR_TYPE_GLYPH); 250 if (uri != null) { 251 at.setGlyph(new URL (uri)); 252 } 253 } catch (MalformedURLException ex) { 254 rethrow(ex); 255 } 256 257 String actions = amap.getValue(ATTR_TYPE_ACTIONS); 259 if (actions != null) { 260 AnnotationTypeActionsFolder.readActions(at, actions); 261 at.putProp(AnnotationType.PROP_ACTIONS_FOLDER, actions); 262 } 263 264 at.setSeverity(AnnotationType.Severity.valueOf(amap.getValue(ATTR_SEVERITY))); 266 at.setBrowseable(Boolean.valueOf(amap.getValue(ATTR_BROWSEABLE)).booleanValue()); 267 268 String priorityString = amap.getValue(ATTR_PRIORITY); 269 int priority = 0; 270 271 if (priorityString != null) { 272 try { 273 priority = Integer.parseInt(priorityString); 274 } catch (NumberFormatException e) { 275 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 276 } 277 } 278 279 at.setPriority(priority); 280 281 break; 282 283 case 1: if (! TAG_COMBINATION.equals(name)) { 285 throw new SAXException ("malformed AnnotationType xml file"); } 287 288 combinations = new ArrayList (); 289 290 String key = amap.getValue(ATTR_COMBINATION_TIPTEXT_KEY); 291 if (key != null) { 292 at.putProp(AnnotationType.PROP_COMBINATION_TOOLTIP_TEXT_KEY, key); 293 } 294 295 String order = amap.getValue(ATTR_COMBINATION_ORDER); 296 if (order != null) 297 at.setCombinationOrder(order); 298 299 String min = amap.getValue(ATTR_COMBINATION_MIN_OPTIONALS); 300 if (min != null) 301 at.setMinimumOptionals(min); 302 303 break; 304 305 case 2: combinations.add(new AnnotationType.CombinationMember( 307 amap.getValue(ATTR_COMBINE_ANNOTATIONTYPE), 308 amap.getValue(ATTR_COMBINE_ABSORBALL) == null ? false : "true".equals(amap.getValue(ATTR_COMBINE_ABSORBALL)) ? true : false, amap.getValue(ATTR_COMBINE_OPTIONAL) == null ? false : "true".equals(amap.getValue(ATTR_COMBINE_OPTIONAL)) ? true : false, amap.getValue(ATTR_COMBINE_MIN) 311 )); 312 313 break; 314 default: 315 throw new SAXException ("malformed AnnotationType xml file"); } 317 } 318 319 public void endElement(String name) throws SAXException { 320 if (--depth == 1) { 321 AnnotationType.CombinationMember[] combs = new AnnotationType.CombinationMember[combinations.size()]; 322 combinations.toArray(combs); 323 at.setCombinations(combs); 324 }; 325 } 326 327 public InputSource resolveEntity(java.lang.String pid,java.lang.String sid) throws SAXException { 328 if (DTD_PUBLIC_ID.equals(pid)) { 329 return new InputSource (new ByteArrayInputStream (new byte[0])); 330 } 331 if (DTD_PUBLIC_ID11.equals(pid)) { 332 return new InputSource (new ByteArrayInputStream (new byte[0])); 333 } 334 return new InputSource (sid); 335 } 336 337 } 338 339 } 340 | Popular Tags |