KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > TaskDescriptionProvider


1 /*******************************************************************************
2  * Copyright (c) 2002, 2005 GEBIT Gesellschaft fuer EDV-Beratung
3  * und Informatik-Technologien mbH,
4  * Berlin, Duesseldorf, Frankfurt (Germany) and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * GEBIT Gesellschaft fuer EDV-Beratung und Informatik-Technologien mbH - initial API and implementation
12  * IBM Corporation - bug fixes
13  *******************************************************************************/

14
15 package org.eclipse.ant.internal.ui.editor;
16
17 import java.io.IOException JavaDoc;
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26
27 import org.eclipse.ant.internal.ui.AntUIPlugin;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.jface.operation.IRunnableWithProgress;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.progress.IProgressService;
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.NamedNodeMap JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37 import org.w3c.dom.Text JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40 import org.xml.sax.helpers.DefaultHandler JavaDoc;
41
42 /**
43  * The <code>TaskDescriptionProvider</code> provides the additional descriptions
44  * for tasks and attributes for the code assist.
45  * <P>
46  * Descriptions for task are originally provided with the XML file
47  * <code>TASKS_DESCRIPTION_XML_FILE_NAME</code>. This file is parsed by the
48  * provider and requested descriptions are returned.
49  * <P>
50  */

51 public class TaskDescriptionProvider {
52
53     /**
54      * The file that contains all task descriptions.
55      */

56     public static final String JavaDoc TASKS_DESCRIPTION_XML_FILE_NAME = "/org/eclipse/ant/internal/ui/editor/anttasks_1.6.0.xml"; //$NON-NLS-1$
57

58     public static final String JavaDoc XML_TAG_TASKS = "tasks"; //$NON-NLS-1$
59
public static final String JavaDoc XML_TAG_TASK = "task"; //$NON-NLS-1$
60
public static final String JavaDoc XML_TAG_ELEMENTS = "elements"; //$NON-NLS-1$
61
public static final String JavaDoc XML_TAG_ATTRIBUTE = "attribute"; //$NON-NLS-1$
62
public static final String JavaDoc XML_TAG_ATTRIBUTES = "attributes"; //$NON-NLS-1$
63
public static final String JavaDoc XML_TAG_ELEMENT = "element"; //$NON-NLS-1$
64
public static final String JavaDoc XML_TAG_STRUCTURE = "structure"; //$NON-NLS-1$
65
public static final String JavaDoc XML_TAG_DESCRIPTION = "description"; //$NON-NLS-1$
66
public static final String JavaDoc XML_ATTRIBUTE_NAME = "name"; //$NON-NLS-1$
67
public static final String JavaDoc XML_ATTRIBUTE_REQUIRED = "required"; //$NON-NLS-1$
68

69     private static TaskDescriptionProvider fgDefault;
70
71     private Map JavaDoc taskNodes= null;
72     
73     /**
74      * Meant to be a singleton
75      */

76     private TaskDescriptionProvider() {
77     }
78     
79     public static TaskDescriptionProvider getDefault() {
80         if (fgDefault == null) {
81             fgDefault= new TaskDescriptionProvider();
82             IRunnableWithProgress runnable= new IRunnableWithProgress() {
83                 public void run(IProgressMonitor monitor)
84                                     throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
85                         fgDefault.initialize();
86                 }
87             };
88             
89             IProgressService service= PlatformUI.getWorkbench().getProgressService();
90             try {
91                 service.busyCursorWhile(runnable);
92             } catch (InvocationTargetException JavaDoc e) {
93             } catch (InterruptedException JavaDoc e) {
94             }
95         }
96         return fgDefault;
97     }
98
99     /**
100      * Parses the task description xml file and stores the information.
101      */

102     protected void initialize() {
103         taskNodes= new HashMap JavaDoc();
104         Document JavaDoc tempDocument = parseFile(TASKS_DESCRIPTION_XML_FILE_NAME);
105         Node JavaDoc tempRootNode = tempDocument.getDocumentElement();
106         NodeList JavaDoc tempChildNodes = tempRootNode.getChildNodes();
107         for(int i=0; i<tempChildNodes.getLength(); i++) {
108             Node JavaDoc tempNode = tempChildNodes.item(i);
109             if(tempNode.getNodeType() == Node.ELEMENT_NODE) {
110                 String JavaDoc tempTagName = tempNode.getNodeName();
111                 if(tempTagName.equals(XML_TAG_TASK)) {
112                     NamedNodeMap JavaDoc tempAttributes = tempNode.getAttributes();
113                     Node JavaDoc tempAttributeNode = tempAttributes.getNamedItem(XML_ATTRIBUTE_NAME);
114                     if(tempAttributeNode != null) {
115                         String JavaDoc tempTaskName = tempAttributeNode.getNodeValue();
116                         if(tempTaskName != null) {
117                             taskNodes.put(tempTaskName, tempNode);
118                         }
119                     }
120                 }
121             }
122         }
123     }
124     
125
126     /**
127      * Returns the (DOM) document as a result of parsing the file with the
128      * specified file name.
129      * <P>
130      * The file will be loaded as resource, thus must begin with '/' and must
131      * be relative to the classpath.
132      */

133     private Document JavaDoc parseFile(String JavaDoc aFileName) {
134         Document JavaDoc tempDocument = null;
135
136         DocumentBuilderFactory JavaDoc tempFactory = DocumentBuilderFactory.newInstance();
137         tempFactory.setIgnoringComments(true);
138         tempFactory.setIgnoringElementContentWhitespace(true);
139         tempFactory.setCoalescing(true);
140
141         try {
142             DocumentBuilder JavaDoc tempDocBuilder = tempFactory.newDocumentBuilder();
143             tempDocBuilder.setErrorHandler(new DefaultHandler JavaDoc());
144             URL JavaDoc tempURL = getClass().getResource(aFileName);
145             InputSource JavaDoc tempInputSource = new InputSource JavaDoc(tempURL.toExternalForm());
146             tempDocument = tempDocBuilder.parse(tempInputSource);
147         } catch (ParserConfigurationException JavaDoc e) {
148             AntUIPlugin.log(e);
149         }
150         catch (IOException JavaDoc ioException) {
151             AntUIPlugin.log(ioException);
152         }
153         catch (SAXException JavaDoc saxException) {
154             AntUIPlugin.log(saxException);
155         }
156
157         return tempDocument;
158     }
159
160     /**
161      * Returns the description string for the specified task.
162      *
163      * @return description string or <code>null</code> if task not known or
164      * no description available.
165      */

166     public String JavaDoc getDescriptionForTask(String JavaDoc aTaskName) {
167         Element JavaDoc taskElement = (Element JavaDoc)taskNodes.get(aTaskName);
168         if(taskElement != null) {
169             return getDescriptionOfNode(taskElement);
170         }
171         return null;
172     }
173
174
175     /**
176      * Returns the description of the specified node.
177      * <P>
178      * The node must be either one of task node or attribute node.
179      */

180     private String JavaDoc getDescriptionOfNode(Node JavaDoc aNode) {
181         NodeList JavaDoc tempChildNodes = aNode.getChildNodes();
182         for (int i=0; i<tempChildNodes.getLength(); i++) {
183             Node JavaDoc tempNode = tempChildNodes.item(i);
184             if(tempNode instanceof Element JavaDoc && XML_TAG_DESCRIPTION.equals(tempNode.getNodeName())) {
185                 Element JavaDoc tempDescriptionElement = (Element JavaDoc)tempNode;
186                 Node JavaDoc tempChildNode = tempDescriptionElement.getFirstChild();
187                 if(tempChildNode instanceof Text JavaDoc) {
188                     return ((Text JavaDoc)tempChildNode).getData();
189                 }
190                 break;
191             }
192         }
193         return null;
194     }
195     
196     /**
197      * Returns the Required value of the specified node.
198      * <P>
199      * Currently the XML file has Required defined as NOTDEFINED in
200      * some cases. If so the value returned is an empty string
201      */

202     protected String JavaDoc getRequiredOfNode(Node JavaDoc aNode) {
203         
204         String JavaDoc tmpNodeName = aNode.getNodeName();
205         String JavaDoc tmpRequiredValue = null;
206         
207         if(aNode.getNodeType() == Node.ELEMENT_NODE &&
208             (XML_TAG_ATTRIBUTE.equals(tmpNodeName) || XML_TAG_ELEMENT.equals(tmpNodeName)) ) {
209               
210               tmpRequiredValue = aNode.getAttributes().getNamedItem(XML_ATTRIBUTE_REQUIRED).getNodeValue();
211         }
212         
213         if(tmpRequiredValue == null || tmpRequiredValue.equals("NOTDEFINED")) { //$NON-NLS-1$
214
return ""; //$NON-NLS-1$
215
}
216         
217         return tmpRequiredValue;
218                    
219     }
220
221     
222     /**
223      * Returns the description string for the specified attribute of the
224      * specified task.
225      *
226      * @return description string or <code>null</code> if task or attribute
227      * not known or no description available.
228      */

229     public String JavaDoc getDescriptionForTaskAttribute(String JavaDoc aTaskName, String JavaDoc anAttributeName) {
230         
231         String JavaDoc tmpDescription = null;
232             
233         Node JavaDoc tmpAttributesNode = getAttributesNode(aTaskName);
234             
235         if(tmpAttributesNode != null) {
236             
237             tmpDescription = getDescriptionForNodeNamedWithNameInNodeList( XML_TAG_ATTRIBUTE, anAttributeName,
238                                                                         tmpAttributesNode.getChildNodes());
239             //If Description is null we try the elements section else we're satisfied.
240
if( tmpDescription != null ) {
241                 return tmpDescription;
242             }
243         }
244         //Not yet found. Try the elements Node
245
tmpAttributesNode = getElementsNode(aTaskName);
246         if(tmpAttributesNode != null) {
247             tmpDescription = getDescriptionForNodeNamedWithNameInNodeList( XML_TAG_ELEMENT, anAttributeName,
248                                                                        tmpAttributesNode.getChildNodes());
249             
250             return tmpDescription;
251             
252         }
253         return null;
254     }
255     
256     /**
257      * Returns the required string value for the specified attribute of the
258      * specified task.
259      *
260      * @return required string or <code>null</code> if task or attribute not
261      * known or no description available.
262      */

263     public String JavaDoc getRequiredAttributeForTaskAttribute(String JavaDoc aTaskName, String JavaDoc anAttributeName) {
264  
265         String JavaDoc tmpRequired = null;
266             
267         Node JavaDoc tmpAttributesNode = getAttributesNode(aTaskName);
268             
269         if(tmpAttributesNode != null) {
270             
271             tmpRequired = getRequiredForNodeNamedWithNameInNodeList( XML_TAG_ATTRIBUTE, anAttributeName,
272                                                                         tmpAttributesNode.getChildNodes());
273             
274             //If Required is null we try the elements section else we're satisfied.
275
if( tmpRequired != null ) {
276                 return tmpRequired;
277             }
278         }
279         
280         //Not yet found. Try the elements Node
281
tmpAttributesNode = getElementsNode(aTaskName);
282         if(tmpAttributesNode != null) {
283             tmpRequired = getDescriptionForNodeNamedWithNameInNodeList( XML_TAG_ELEMENT, anAttributeName,
284                                                                        tmpAttributesNode.getChildNodes());
285             //Return it even if its null
286
return tmpRequired;
287             
288         }
289         
290         //Not found return null
291
return null;
292     }
293     
294     /**
295      * Returns the Elements Node of the specified TaskName
296      *
297      * @param aTaskName The name of the task
298      * @return The Elements Node of the Task.
299      */

300     private Node JavaDoc getElementsNode(String JavaDoc aTaskName) {
301         
302         Node JavaDoc tmpStructureNode = getStructureNode(aTaskName);
303         if(tmpStructureNode != null) {
304             return getChildNodeNamedOfTypeFromNode(XML_TAG_ELEMENTS, Node.ELEMENT_NODE,
305                                                     tmpStructureNode);
306         }
307         return null;
308     }
309     
310     /**
311      * Returns the Attributes Node of the specified TaskName
312      *
313      * @param aTaskName The name of the task
314      * @return The Attributes Node of the Task or <code>null</code> if one
315      * does not exist.
316      */

317     protected Node JavaDoc getAttributesNode(String JavaDoc aTaskName) {
318         
319         Node JavaDoc tmpStructureNode = getStructureNode(aTaskName);
320         if(tmpStructureNode != null){
321             return getChildNodeNamedOfTypeFromNode(XML_TAG_ATTRIBUTES, Node.ELEMENT_NODE,
322                                                              tmpStructureNode);
323         }
324         return null;
325     }
326
327     /**
328      * Returns the Structure Node of the specified TaskName
329      *
330      * @param aTaskName The name of the task
331      * @return The Structure Node of the Task.
332      */

333     private Node JavaDoc getStructureNode(String JavaDoc aTaskName) {
334         Element JavaDoc taskElement = (Element JavaDoc)taskNodes.get(aTaskName);
335         if(taskElement != null) {
336             //Dig us down to the Structure node
337
Node JavaDoc structureNode = getChildNodeNamedOfTypeFromNode(XML_TAG_STRUCTURE, Node.ELEMENT_NODE,
338                                                                  taskElement);
339             return structureNode;
340         }
341         return null;
342     }
343     
344     /**
345      * Returns the Description for a Node satisfying the criterias in the
346      * NodeList given as Argument.
347      *
348      * @param aNodeName The Name of the Node
349      * @param anAttributeName The string of the Name value
350      * @param anAttributesNodeList The NodeList to search in.
351      * @return The Description found or null if none is found
352      */

353     private String JavaDoc getDescriptionForNodeNamedWithNameInNodeList( String JavaDoc aNodeName, String JavaDoc anAttributeName,
354                                                                      NodeList JavaDoc anAttributesNodeList) {
355         for (int i=0; i<anAttributesNodeList.getLength(); i++) {
356                 Node JavaDoc tempNode = anAttributesNodeList.item(i);
357                 if(tempNode.getNodeType() == Node.ELEMENT_NODE && aNodeName.equals(tempNode.getNodeName())) {
358                     if( anAttributeName.equals(getTaskAttributeName(tempNode)) ) {
359                         return getDescriptionOfNode(tempNode);
360                     }
361                 }
362         }
363         
364         //Not found
365
return null;
366     }
367     
368     
369     /**
370      * Returns the Name of Task Attribute.
371      *
372      * @return The Name of the Attribute.
373      */

374     public String JavaDoc getTaskAttributeName(Node JavaDoc aTaskAttributeNode) {
375         NamedNodeMap JavaDoc tmpNamedNodeMap = aTaskAttributeNode.getAttributes();
376         return tmpNamedNodeMap.getNamedItem(XML_ATTRIBUTE_NAME).getNodeValue();
377     }
378     
379     /**
380      * Returns the ChildNode of the node defined by the Arguments. The
381      * first child found matching the criterias is returned.
382      *
383      * @param aNodeName The Name of the Node to return.
384      * @param aType The Type of the node @see Node
385      * @param aParentNode The Node to get the child from
386      *
387      * @return The First Child Node found matching the criterias,
388      * or null if none is found.
389      */

390     private Node JavaDoc getChildNodeNamedOfTypeFromNode(String JavaDoc aNodeName, short aNodeType, Node JavaDoc aParentNode) {
391     
392         NodeList JavaDoc tmpNodeList = aParentNode.getChildNodes();
393         for(int i=0; i<tmpNodeList.getLength(); ++i ) {
394             Node JavaDoc tmpNode = tmpNodeList.item(i);
395             if( (tmpNode.getNodeType() == aNodeType) && aNodeName.equals(tmpNode.getNodeName()) ) {
396                 return tmpNode;
397             }
398         }
399         //Not found
400
return null;
401     }
402     
403      /**
404      * Returns the Required Field for a Node satisfying the criterias in the
405      * NodeList given as Argument.
406      *
407      * @param aNodeName The Name of the Node
408      * @param anAttributeName The string of the Name value
409      * @param anAttributesNodeList The NodeList to search in.
410      * @return The Description found or null if none is found
411      */

412     private String JavaDoc getRequiredForNodeNamedWithNameInNodeList( String JavaDoc aNodeName, String JavaDoc anAttributeName,
413                                                                      NodeList JavaDoc anAttributesNodeList) {
414         for (int i=0; i<anAttributesNodeList.getLength(); i++) {
415             Node JavaDoc tempNode = anAttributesNodeList.item(i);
416             if(tempNode.getNodeType() == Node.ELEMENT_NODE && aNodeName.equals(tempNode.getNodeName())) {
417                 if( anAttributeName.equals(getTaskAttributeName(tempNode)) ) {
418                     return getRequiredOfNode(tempNode);
419                 }
420             }
421         }
422         
423         //Not found
424
return null;
425     }
426     
427     protected static void reset() {
428         fgDefault= null;
429     }
430 }
Popular Tags