1 14 15 package org.eclipse.ant.internal.ui.editor; 16 17 import java.io.IOException ; 18 import java.lang.reflect.InvocationTargetException ; 19 import java.net.URL ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 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 ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.NamedNodeMap ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.NodeList ; 37 import org.w3c.dom.Text ; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.helpers.DefaultHandler ; 41 42 51 public class TaskDescriptionProvider { 52 53 56 public static final String TASKS_DESCRIPTION_XML_FILE_NAME = "/org/eclipse/ant/internal/ui/editor/anttasks_1.6.0.xml"; 58 public static final String XML_TAG_TASKS = "tasks"; public static final String XML_TAG_TASK = "task"; public static final String XML_TAG_ELEMENTS = "elements"; public static final String XML_TAG_ATTRIBUTE = "attribute"; public static final String XML_TAG_ATTRIBUTES = "attributes"; public static final String XML_TAG_ELEMENT = "element"; public static final String XML_TAG_STRUCTURE = "structure"; public static final String XML_TAG_DESCRIPTION = "description"; public static final String XML_ATTRIBUTE_NAME = "name"; public static final String XML_ATTRIBUTE_REQUIRED = "required"; 69 private static TaskDescriptionProvider fgDefault; 70 71 private Map taskNodes= null; 72 73 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 , InterruptedException { 85 fgDefault.initialize(); 86 } 87 }; 88 89 IProgressService service= PlatformUI.getWorkbench().getProgressService(); 90 try { 91 service.busyCursorWhile(runnable); 92 } catch (InvocationTargetException e) { 93 } catch (InterruptedException e) { 94 } 95 } 96 return fgDefault; 97 } 98 99 102 protected void initialize() { 103 taskNodes= new HashMap (); 104 Document tempDocument = parseFile(TASKS_DESCRIPTION_XML_FILE_NAME); 105 Node tempRootNode = tempDocument.getDocumentElement(); 106 NodeList tempChildNodes = tempRootNode.getChildNodes(); 107 for(int i=0; i<tempChildNodes.getLength(); i++) { 108 Node tempNode = tempChildNodes.item(i); 109 if(tempNode.getNodeType() == Node.ELEMENT_NODE) { 110 String tempTagName = tempNode.getNodeName(); 111 if(tempTagName.equals(XML_TAG_TASK)) { 112 NamedNodeMap tempAttributes = tempNode.getAttributes(); 113 Node tempAttributeNode = tempAttributes.getNamedItem(XML_ATTRIBUTE_NAME); 114 if(tempAttributeNode != null) { 115 String tempTaskName = tempAttributeNode.getNodeValue(); 116 if(tempTaskName != null) { 117 taskNodes.put(tempTaskName, tempNode); 118 } 119 } 120 } 121 } 122 } 123 } 124 125 126 133 private Document parseFile(String aFileName) { 134 Document tempDocument = null; 135 136 DocumentBuilderFactory tempFactory = DocumentBuilderFactory.newInstance(); 137 tempFactory.setIgnoringComments(true); 138 tempFactory.setIgnoringElementContentWhitespace(true); 139 tempFactory.setCoalescing(true); 140 141 try { 142 DocumentBuilder tempDocBuilder = tempFactory.newDocumentBuilder(); 143 tempDocBuilder.setErrorHandler(new DefaultHandler ()); 144 URL tempURL = getClass().getResource(aFileName); 145 InputSource tempInputSource = new InputSource (tempURL.toExternalForm()); 146 tempDocument = tempDocBuilder.parse(tempInputSource); 147 } catch (ParserConfigurationException e) { 148 AntUIPlugin.log(e); 149 } 150 catch (IOException ioException) { 151 AntUIPlugin.log(ioException); 152 } 153 catch (SAXException saxException) { 154 AntUIPlugin.log(saxException); 155 } 156 157 return tempDocument; 158 } 159 160 166 public String getDescriptionForTask(String aTaskName) { 167 Element taskElement = (Element )taskNodes.get(aTaskName); 168 if(taskElement != null) { 169 return getDescriptionOfNode(taskElement); 170 } 171 return null; 172 } 173 174 175 180 private String getDescriptionOfNode(Node aNode) { 181 NodeList tempChildNodes = aNode.getChildNodes(); 182 for (int i=0; i<tempChildNodes.getLength(); i++) { 183 Node tempNode = tempChildNodes.item(i); 184 if(tempNode instanceof Element && XML_TAG_DESCRIPTION.equals(tempNode.getNodeName())) { 185 Element tempDescriptionElement = (Element )tempNode; 186 Node tempChildNode = tempDescriptionElement.getFirstChild(); 187 if(tempChildNode instanceof Text ) { 188 return ((Text )tempChildNode).getData(); 189 } 190 break; 191 } 192 } 193 return null; 194 } 195 196 202 protected String getRequiredOfNode(Node aNode) { 203 204 String tmpNodeName = aNode.getNodeName(); 205 String 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")) { return ""; } 216 217 return tmpRequiredValue; 218 219 } 220 221 222 229 public String getDescriptionForTaskAttribute(String aTaskName, String anAttributeName) { 230 231 String tmpDescription = null; 232 233 Node tmpAttributesNode = getAttributesNode(aTaskName); 234 235 if(tmpAttributesNode != null) { 236 237 tmpDescription = getDescriptionForNodeNamedWithNameInNodeList( XML_TAG_ATTRIBUTE, anAttributeName, 238 tmpAttributesNode.getChildNodes()); 239 if( tmpDescription != null ) { 241 return tmpDescription; 242 } 243 } 244 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 263 public String getRequiredAttributeForTaskAttribute(String aTaskName, String anAttributeName) { 264 265 String tmpRequired = null; 266 267 Node tmpAttributesNode = getAttributesNode(aTaskName); 268 269 if(tmpAttributesNode != null) { 270 271 tmpRequired = getRequiredForNodeNamedWithNameInNodeList( XML_TAG_ATTRIBUTE, anAttributeName, 272 tmpAttributesNode.getChildNodes()); 273 274 if( tmpRequired != null ) { 276 return tmpRequired; 277 } 278 } 279 280 tmpAttributesNode = getElementsNode(aTaskName); 282 if(tmpAttributesNode != null) { 283 tmpRequired = getDescriptionForNodeNamedWithNameInNodeList( XML_TAG_ELEMENT, anAttributeName, 284 tmpAttributesNode.getChildNodes()); 285 return tmpRequired; 287 288 } 289 290 return null; 292 } 293 294 300 private Node getElementsNode(String aTaskName) { 301 302 Node 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 317 protected Node getAttributesNode(String aTaskName) { 318 319 Node 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 333 private Node getStructureNode(String aTaskName) { 334 Element taskElement = (Element )taskNodes.get(aTaskName); 335 if(taskElement != null) { 336 Node structureNode = getChildNodeNamedOfTypeFromNode(XML_TAG_STRUCTURE, Node.ELEMENT_NODE, 338 taskElement); 339 return structureNode; 340 } 341 return null; 342 } 343 344 353 private String getDescriptionForNodeNamedWithNameInNodeList( String aNodeName, String anAttributeName, 354 NodeList anAttributesNodeList) { 355 for (int i=0; i<anAttributesNodeList.getLength(); i++) { 356 Node 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 return null; 366 } 367 368 369 374 public String getTaskAttributeName(Node aTaskAttributeNode) { 375 NamedNodeMap tmpNamedNodeMap = aTaskAttributeNode.getAttributes(); 376 return tmpNamedNodeMap.getNamedItem(XML_ATTRIBUTE_NAME).getNodeValue(); 377 } 378 379 390 private Node getChildNodeNamedOfTypeFromNode(String aNodeName, short aNodeType, Node aParentNode) { 391 392 NodeList tmpNodeList = aParentNode.getChildNodes(); 393 for(int i=0; i<tmpNodeList.getLength(); ++i ) { 394 Node tmpNode = tmpNodeList.item(i); 395 if( (tmpNode.getNodeType() == aNodeType) && aNodeName.equals(tmpNode.getNodeName()) ) { 396 return tmpNode; 397 } 398 } 399 return null; 401 } 402 403 412 private String getRequiredForNodeNamedWithNameInNodeList( String aNodeName, String anAttributeName, 413 NodeList anAttributesNodeList) { 414 for (int i=0; i<anAttributesNodeList.getLength(); i++) { 415 Node 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 return null; 425 } 426 427 protected static void reset() { 428 fgDefault= null; 429 } 430 } | Popular Tags |