KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > suggestions > SuggestionTypeProcessor


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.tasklist.suggestions;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import org.openide.loaders.XMLDataObject.Processor;
26 import org.xml.sax.AttributeList JavaDoc;
27 import org.xml.sax.HandlerBase JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29 import org.xml.sax.Parser JavaDoc;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.openide.util.actions.SystemAction;
32
33 import java.util.List JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.net.URL JavaDoc;
36 import javax.xml.parsers.SAXParserFactory JavaDoc;
37 import org.openide.ErrorManager;
38 import org.openide.cookies.InstanceCookie;
39 import org.openide.loaders.XMLDataObject;
40 import org.xml.sax.helpers.DefaultHandler JavaDoc;
41 import org.openide.xml.XMLUtil;
42 import org.xml.sax.Attributes JavaDoc;
43 import org.xml.sax.InputSource JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45 import org.xml.sax.XMLReader JavaDoc;
46 import org.xml.sax.helpers.DefaultHandler JavaDoc;
47 import org.openide.util.Lookup;
48
49
50 /** Processor of the Suggestion Type XML file.
51  * The parsing result is an instance of a SuggestionType object
52  * <p>
53  * Based on AnnotationTypeProcessor in the editor package.
54  * <p>
55  *
56  * @author Tor Norbye, David Konecny, Peter Nejedly
57  */

58 public final class SuggestionTypeProcessor implements InstanceCookie, Processor {
59     static final String JavaDoc DTD_PUBLIC_ID = "-//NetBeans//DTD suggestion type 1.0//EN"; // NOI18N
60
static final String JavaDoc DTD_SYSTEM_ID = "http://www.netbeans.org/dtds/suggestion-type-1_0.dtd"; // NOI18N
61

62     static final String JavaDoc TAG_TYPE = "type"; //NOI18N
63
static final String JavaDoc ATTR_TYPE_NAME = "name"; // NOI18N
64
static final String JavaDoc ATTR_TYPE_LOCALIZING_BUNDLE = "localizing_bundle"; // NOI18N
65
static final String JavaDoc ATTR_TYPE_DESCRIPTION_KEY = "description_key"; // NOI18N
66
static final String JavaDoc ATTR_TYPE_LONGDESCRIPTION_KEY = "long_description_key"; // NOI18N
67
static final String JavaDoc ATTR_TYPE_ICON = "icon"; // NOI18N
68
static final String JavaDoc TAG_TYPE_ACTIONS = "actions"; // NOI18N
69
static final String JavaDoc TAG_TYPE_ACTION = "action"; // NOI18N
70
static final String JavaDoc ATTR_ACTION_CLASS = "class"; // NOI18N
71

72     /** XML data object. */
73     private XMLDataObject xmlDataObject;
74     
75     /**
76      * Suggestion type created from XML file.
77      */

78     private SuggestionType suggestionType;
79
80     /** When the XMLDataObject creates new instance of the processor,
81      * it uses this method to attach the processor to the data object.
82      *
83      * @param xmlDO XMLDataObject
84      */

85     public void attachTo(XMLDataObject xmlDO) {
86         xmlDataObject = xmlDO;
87     }
88     
89     /** Create an instance.
90      * @return the instance of type {@link #instanceClass}
91      * @exception IOException if an I/O error occured
92      * @exception ClassNotFoundException if a class was not found
93      */

94     public Object JavaDoc instanceCreate() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
95         if (suggestionType != null)
96             return suggestionType;
97
98         parse();
99         return suggestionType;
100     }
101     
102     /** The representation type that may be created as instances.
103      * Can be used to test whether the instance is of an appropriate
104      * class without actually creating it.
105      *
106      * @return the representation class of the instance
107      * @exception IOException if an I/O error occurred
108      * @exception ClassNotFoundException if a class was not found
109      */

110     public Class JavaDoc instanceClass() {
111         return SuggestionType.class;
112     }
113     
114     /** The bean name for the instance.
115      * @return the name
116      */

117     public String JavaDoc instanceName() {
118         return instanceClass().getName();
119     }
120
121     ////////////////////////////////////////////////////////////////////////
122

123     private synchronized SuggestionType parse() {
124         if (suggestionType == null) {
125             try {
126                     XMLReader JavaDoc 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 JavaDoc(xmlDataObject.getPrimaryFile().getInputStream()));
133                     suggestionType = handler.getSuggestionType();
134             } catch (Exception JavaDoc e) {
135                 ErrorManager.getDefault().notify(e);
136             }
137
138         }
139         return suggestionType;
140     }
141     
142     private static class TypeHandler extends DefaultHandler JavaDoc {
143         private SuggestionType type = null;
144         
145         String JavaDoc id = null;
146         List JavaDoc actions = null;
147         String JavaDoc localizer = null;
148         String JavaDoc key = null;
149         String JavaDoc longkey = null;
150         URL JavaDoc icon = null;
151         
152         SuggestionType getSuggestionType() {
153             return type;
154         }
155
156         public void startElement(String JavaDoc uri, String JavaDoc localName,
157                                  String JavaDoc name, Attributes JavaDoc attrs)
158             throws SAXException JavaDoc {
159             if (TAG_TYPE.equals(name)) {
160             // basic properties
161
id = attrs.getValue(ATTR_TYPE_NAME);
162             actions = null;
163             
164             // localization stuff
165
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
170
icon = null;
171             String JavaDoc ur = attrs.getValue(ATTR_TYPE_ICON);
172             if (ur != null) {
173                 try {
174                     icon = new URL JavaDoc(ur);
175                 } catch (MalformedURLException JavaDoc ex) {
176                     SAXException JavaDoc saxe = new SAXException JavaDoc(ex);
177                     ErrorManager.getDefault().
178                         copyAnnotation(saxe, ex);
179                     throw saxe;
180                 }
181             }
182             } else if (TAG_TYPE_ACTIONS.equals(name)) {
183                 // Ignore
184
} else if (TAG_TYPE_ACTION.equals(name)) {
185                 String JavaDoc cln = attrs.getValue(ATTR_ACTION_CLASS);
186                 if (cln == null) {
187                     return;
188                 }
189                 try {
190                     ClassLoader JavaDoc l =
191                         (ClassLoader JavaDoc)Lookup.getDefault().lookup(
192                                                         ClassLoader JavaDoc.class);
193                     Class JavaDoc 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 JavaDoc();
200                             }
201                             actions.add(a);
202                         }
203                     }
204                 } catch (ClassNotFoundException JavaDoc e) {
205                     ErrorManager.getDefault().annotate(e, "TL: cannot load " + cln + " action, ignoring..."); // NOI18N
206
ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
207                 }
208             } else {
209                 throw new SAXException JavaDoc("malformed SuggestionType xml file"); // NOI18N
210
}
211         }
212
213         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc name) throws SAXException JavaDoc {
214             if (TAG_TYPE_ACTION.equals(name)) {
215                 // Ignore
216
} else if (TAG_TYPE_ACTIONS.equals(name)) {
217                 // Ignore
218
} 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 JavaDoc resolveEntity(java.lang.String JavaDoc pid,
229                                          java.lang.String JavaDoc sid) throws SAXException JavaDoc {
230             if (DTD_PUBLIC_ID.equals(pid)) {
231                 return new InputSource JavaDoc(new ByteArrayInputStream JavaDoc(new byte[0]));
232             }
233             return new InputSource JavaDoc (sid);
234         }
235     }
236     
237 }
238
Popular Tags