KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > options > AnnotationTypeProcessor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.options;
21
22 import java.awt.Color JavaDoc;
23 import java.io.ByteArrayInputStream JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc;
36 import org.xml.sax.HandlerBase JavaDoc;
37 import org.xml.sax.InputSource JavaDoc;
38 import org.xml.sax.Parser JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 /** Processor of the XML file. The result of parsing is instance of AnnotationType
42  * class.
43  *
44  * @author David Konecny, Petr Nejedly
45  * @since 07/2001
46 */

47 public class AnnotationTypeProcessor implements XMLDataObject.Processor, InstanceCookie {
48     static final String JavaDoc DTD_PUBLIC_ID = "-//NetBeans//DTD annotation type 1.0//EN"; // NOI18N
49
static final String JavaDoc DTD_SYSTEM_ID = "http://www.netbeans.org/dtds/annotation-type-1_0.dtd"; // NOI18N
50
static final String JavaDoc DTD_PUBLIC_ID11 = "-//NetBeans//DTD annotation type 1.1//EN"; // NOI18N
51
static final String JavaDoc DTD_SYSTEM_ID11 = "http://www.netbeans.org/dtds/annotation-type-1_1.dtd"; // NOI18N
52

53     static final String JavaDoc TAG_TYPE = "type"; //NOI18N
54
static final String JavaDoc ATTR_TYPE_NAME = "name"; // NOI18N
55
static final String JavaDoc ATTR_TYPE_LOCALIZING_BUNDLE = "localizing_bundle"; // NOI18N
56
static final String JavaDoc ATTR_TYPE_DESCRIPTION_KEY = "description_key"; // NOI18N
57
static final String JavaDoc ATTR_TYPE_VISIBLE = "visible"; // NOI18N
58
static final String JavaDoc ATTR_TYPE_GLYPH = "glyph"; // NOI18N
59
static final String JavaDoc ATTR_TYPE_HIGHLIGHT = "highlight"; // NOI18N
60
static final String JavaDoc ATTR_TYPE_FOREGROUND = "foreground"; // NOI18N
61
static final String JavaDoc ATTR_TYPE_WAVEUNDERLINE = "waveunderline"; // NOI18N
62
static final String JavaDoc ATTR_TYPE_TYPE = "type"; // NOI18N
63
static final String JavaDoc ATTR_TYPE_CONTENTTYPE = "contenttype"; // NOI18N
64
static final String JavaDoc ATTR_TYPE_ACTIONS = "actions"; // NOI18N
65
static final String JavaDoc ATTR_ACTION_NAME = "name"; // NOI18N
66
static final String JavaDoc TAG_COMBINATION = "combination"; // NOI18N
67
static final String JavaDoc ATTR_COMBINATION_TIPTEXT_KEY = "tiptext_key"; // NOI18N
68
static final String JavaDoc ATTR_COMBINATION_ORDER = "order"; // NOI18N
69
static final String JavaDoc ATTR_COMBINATION_MIN_OPTIONALS = "min_optionals"; // NOI18N
70
static final String JavaDoc TAG_COMBINE = "combine"; // NOI18N
71
static final String JavaDoc ATTR_COMBINE_ANNOTATIONTYPE = "annotationtype"; // NOI18N
72
static final String JavaDoc ATTR_COMBINE_ABSORBALL = "absorb_all"; // NOI18N
73
static final String JavaDoc ATTR_COMBINE_OPTIONAL = "optional"; // NOI18N
74
static final String JavaDoc ATTR_COMBINE_MIN = "min"; // NOI18N
75
static final String JavaDoc ATTR_USE_HIHGLIGHT_COLOR = "use_highlight_color"; //NOI18N
76
static final String JavaDoc ATTR_USE_WAVE_UNDERLINE_COLOR = "use_wave_underline_color"; //NOI18N
77
static final String JavaDoc ATTR_INHERIT_FOREGROUND_COLOR = "inherit_foreground_color"; //NOI18N
78
static final String JavaDoc ATTR_USE_CUSTOM_SIDEBAR_COLOR = "use_custom_sidebar_color"; //NOI18N
79
static final String JavaDoc ATTR_CUSTOM_SIDEBAR_COLOR = "custom_sidebar_color"; //NOI18N
80
static final String JavaDoc ATTR_SEVERITY = "severity"; //NOI18N
81
static final String JavaDoc ATTR_BROWSEABLE = "browseable"; //NOI18N
82
static final String JavaDoc ATTR_PRIORITY = "priority"; //NOI18N
83

84     /** XML data object. */
85     private FileObject xmlDataObject;
86     
87     /**
88      * Annotation type created from XML file.
89      */

90     private AnnotationType annotationType;
91
92     /** When the XMLDataObject creates new instance of the processor,
93      * it uses this method to attach the processor to the data object.
94      *
95      * @param xmlDO XMLDataObject
96      */

97     public void attachTo(XMLDataObject xmlDO) {
98         xmlDataObject = xmlDO.getPrimaryFile();
99     }
100
101     //testeability:
102
public void attachTo(FileObject xmlDO) {
103         xmlDataObject = xmlDO;
104     }
105
106     /** Create an instance.
107      * @return the instance of type {@link #instanceClass}
108      * @exception IOException if an I/O error occured
109      * @exception ClassNotFoundException if a class was not found
110      */

111     public Object JavaDoc instanceCreate() throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
112         annotationType = null;
113         parse();
114         return annotationType;
115     }
116     
117     /** The representation type that may be created as instances.
118      * Can be used to test whether the instance is of an appropriate
119      * class without actually creating it.
120      *
121      * @return the representation class of the instance
122      * @exception IOException if an I/O error occurred
123      * @exception ClassNotFoundException if a class was not found
124      */

125     public Class JavaDoc instanceClass() {
126         return AnnotationType.class;
127     }
128     
129     /** The bean name for the instance.
130      * @return the name
131      */

132     public String JavaDoc instanceName() {
133         return instanceClass().getName();
134     }
135
136     ////////////////////////////////////////////////////////////////////////
137

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 JavaDoc xp;
145                 SAXParserFactory JavaDoc 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 JavaDoc(xmlDataObject.getInputStream()));
153                 at.putProp(AnnotationType.PROP_FILE, xmlDataObject);
154                 annotationType = at;
155             } catch (Exception JavaDoc e) {
156                 ErrorManager.getDefault().notify(e);
157             }
158
159         }
160         return annotationType;
161     }
162     
163     private static class Handler extends HandlerBase JavaDoc {
164         private AnnotationType at;
165         private int depth = 0;
166         private List JavaDoc combinations;
167         
168         Handler(AnnotationType at) {
169             this.at=at;
170         }
171
172     private void rethrow(Exception JavaDoc e) throws SAXException JavaDoc {
173             throw new SAXException JavaDoc(e);
174     }
175
176         public void startElement(String JavaDoc name, AttributeList JavaDoc amap) throws SAXException JavaDoc {
177             switch (depth++) {
178                 case 0:
179                     if (! TAG_TYPE.equals(name)) {
180                         throw new SAXException JavaDoc("malformed AnnotationType xml file"); // NOI18N
181
}
182                     // basic properties
183
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))); // NOI18N
188

189                     // localization stuff
190
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 JavaDoc localizer = amap.getValue(ATTR_TYPE_LOCALIZING_BUNDLE);
196                         String JavaDoc 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                     // boolean checkboxes
202
String JavaDoc useHighlightString = amap.getValue(ATTR_USE_HIHGLIGHT_COLOR);
203                     String JavaDoc useWaveUnderlineString = amap.getValue(ATTR_USE_WAVE_UNDERLINE_COLOR);
204                     String JavaDoc inheritForeString = amap.getValue(ATTR_INHERIT_FOREGROUND_COLOR);
205                     String JavaDoc 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                     // colors
212
try {
213                         String JavaDoc 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 JavaDoc ex) {
244                         rethrow(ex);
245                     }
246
247                     // glyph
248
try {
249                         String JavaDoc uri = amap.getValue(ATTR_TYPE_GLYPH);
250                         if (uri != null) {
251                             at.setGlyph(new URL JavaDoc(uri));
252                         }
253                     } catch (MalformedURLException JavaDoc ex) {
254                         rethrow(ex);
255                     }
256
257                     // actions
258
String JavaDoc 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                     //extended properties:
265
at.setSeverity(AnnotationType.Severity.valueOf(amap.getValue(ATTR_SEVERITY)));
266                     at.setBrowseable(Boolean.valueOf(amap.getValue(ATTR_BROWSEABLE)).booleanValue());
267                     
268                     String JavaDoc priorityString = amap.getValue(ATTR_PRIORITY);
269                     int priority = 0;
270                     
271                     if (priorityString != null) {
272                         try {
273                             priority = Integer.parseInt(priorityString);
274                         } catch (NumberFormatException JavaDoc e) {
275                             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
276                         }
277                     }
278                     
279                     at.setPriority(priority);
280                     
281                     break;
282                     
283                 case 1: // <combination ...
284
if (! TAG_COMBINATION.equals(name)) {
285                         throw new SAXException JavaDoc("malformed AnnotationType xml file"); // NOI18N
286
}
287                    
288                    combinations = new ArrayList JavaDoc();
289                    
290                    String JavaDoc 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 JavaDoc order = amap.getValue(ATTR_COMBINATION_ORDER);
296                    if (order != null)
297                        at.setCombinationOrder(order);
298
299                    String JavaDoc min = amap.getValue(ATTR_COMBINATION_MIN_OPTIONALS);
300                    if (min != null)
301                        at.setMinimumOptionals(min);
302
303                    break;
304                    
305                  case 2: // <combine ...
306
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, // NOI18N
309
amap.getValue(ATTR_COMBINE_OPTIONAL) == null ? false : "true".equals(amap.getValue(ATTR_COMBINE_OPTIONAL)) ? true : false, // NOI18N
310
amap.getValue(ATTR_COMBINE_MIN)
311                      ));
312
313                      break;
314                 default:
315                     throw new SAXException JavaDoc("malformed AnnotationType xml file"); // NOI18N
316
}
317         }
318         
319         public void endElement(String JavaDoc name) throws SAXException JavaDoc {
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 JavaDoc resolveEntity(java.lang.String JavaDoc pid,java.lang.String JavaDoc sid) throws SAXException JavaDoc {
328             if (DTD_PUBLIC_ID.equals(pid)) {
329                 return new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(new byte[0]));
330             }
331             if (DTD_PUBLIC_ID11.equals(pid)) {
332                 return new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(new byte[0]));
333             }
334             return new InputSource JavaDoc (sid);
335         }
336
337     }
338     
339 }
340
Popular Tags