KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > tools > TaskXMLFileMerger


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 24108
13  *******************************************************************************/

14
15 package org.eclipse.ant.internal.ui.editor.tools;
16
17 import java.io.IOException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.text.MessageFormat JavaDoc;
20 import java.util.Vector JavaDoc;
21
22 import javax.xml.parsers.DocumentBuilder JavaDoc;
23 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25
26 import org.eclipse.ant.internal.ui.AntUIPlugin;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.NamedNodeMap JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31 import org.xml.sax.InputSource JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33
34 /**
35  * This class can be used to merge the tasks.xml and XDOCtasks.xml.
36  * These files can be found in the Ant Editor Content Assist Dev folder.
37  * Move the files to the Ant Editor folder to use this class.
38  *
39  * The xml automatically generated from the proposed xdoclet in the Apache Ant's
40  * project currently has no information on required attributes. In the future the task writers
41  * hopefully will include this information in the source code comments. Our
42  * template currently inserts an attribute required="NOTDEFINED" and this class
43  * replaces that field if its defined in the xml file we have generated from the
44  * information based on the html files in the Apache Ant's manual directory.
45  */

46 public class TaskXMLFileMerger {
47
48     //Definitions for the HTML Generated XML File
49
public static String JavaDoc HTML_TASKS_DESCRIPTION_XML_FILE_NAME = "/tasks.xml"; //$NON-NLS-1$
50
public static String JavaDoc HTML_XML_TAG_TASKS = "TASKS"; //$NON-NLS-1$
51
public static String JavaDoc HTML_XML_TAG_TASK = "TASK"; //$NON-NLS-1$
52
public static String JavaDoc HTML_XML_TAG_ATTRIBUTE = "ATTRIBUTE"; //$NON-NLS-1$
53
//public static String HTML_XML_TAG_DESCRIPTION = "DESCRIPTION";
54
public static String JavaDoc HTML_XML_ATTRIBUTE_NAME = "NAME"; //$NON-NLS-1$
55
public static String JavaDoc HTML_XML_ATTRIBUTE_REQUIRED = "REQUIRED"; //$NON-NLS-1$
56

57     //Definitions for the XDoclet Genereated XML File
58
public static String JavaDoc XDOC_TASKS_DESCRIPTION_XML_FILE_NAME = "/XDOCtasks.xml"; //$NON-NLS-1$
59
public static String JavaDoc XDOC_XML_TAG_TASKS = "tasks"; //$NON-NLS-1$
60
public static String JavaDoc XDOC_XML_TAG_TASK = "task"; //$NON-NLS-1$
61
public static String JavaDoc XDOC_XML_TAG_NAME = "name"; //$NON-NLS-1$
62
public static String JavaDoc XDOC_XML_TAG_STRUCTURE = "structure"; //$NON-NLS-1$
63
public static String JavaDoc XDOC_XML_TAG_ATTRIBUTES = "attributes"; //$NON-NLS-1$
64
public static String JavaDoc XDOC_XML_TAG_ATTRIBUTE = "attribute"; //$NON-NLS-1$
65
public static String JavaDoc XDOC_XML_TAG_ELEMENTS = "elements"; //$NON-NLS-1$
66
public static String JavaDoc XDOC_XML_TAG_ELEMENT = "element"; //$NON-NLS-1$
67
public static String JavaDoc XDOC_XML_TAG_REQUIRED = "required"; //$NON-NLS-1$
68

69
70     protected NodeList JavaDoc taskNodes_HTML = null;
71     protected NodeList JavaDoc taskNodes_XDOC = null;
72     public Document JavaDoc xdocXMLDocument = null;
73
74     /**
75      * Creates an initialized instance.
76      */

77     public TaskXMLFileMerger() {
78         initialize();
79     }
80
81     
82     /**
83      * Parses the task description xml files and stores the information.
84      */

85     private void initialize() {
86         
87         Document JavaDoc tmpDocument = null;
88         
89         //Get All the Tasks in the HTML XML Generated file and store in the taskNodes_HTML
90
tmpDocument = parseFile(HTML_TASKS_DESCRIPTION_XML_FILE_NAME);
91         taskNodes_HTML = tmpDocument.getFirstChild().getChildNodes();
92         
93         //Do the same for the XDOC XML Generated file
94
tmpDocument = parseFile(XDOC_TASKS_DESCRIPTION_XML_FILE_NAME);
95         taskNodes_XDOC = tmpDocument.getFirstChild().getChildNodes();
96         xdocXMLDocument = tmpDocument;
97 /*
98         Document tempDocument = parseFile(aFileName);
99         Node tempRootNode = tempDocument.getDocumentElement();
100         NodeList tempChildNodes = tempRootNode.getChildNodes();
101         for(int i=0; i<tempChildNodes.getLength(); i++) {
102             Node tempNode = tempChildNodes.item(i);
103             if(tempNode.getNodeType() == Node.ELEMENT_NODE) {
104                 String tempTagName = tempNode.getNodeName();
105                 if(tempTagName.equals(anXML_TAG_TASK)) {
106                     NamedNodeMap tempAttributes = tempNode.getAttributes();
107                     Node tempAttributeNode = tempAttributes.getNamedItem(anXML_ATTRIBUTE_NAME);
108                     if(tempAttributeNode != null) {
109                         String tempTaskName = tempAttributeNode.getNodeValue();
110                         if(tempTaskName != null) {
111                            aHashMap.put(tempTaskName, tempNode);
112                         }
113                     }
114                 }
115             }
116         }
117 */

118     }
119     
120
121     /**
122      * This is the function that does all the work. Calling this
123      * will cause all Required fields in all Attributes in the
124      * XMLDoc file to be replaced with the value in the corresponding
125      * Attributes Required field in the HTML Xml file.
126      */

127     public void runReplaceAttributeRequiredProcess() {
128         
129         //Iterate over all the tasks. If task is found in sourceList,
130
//then iterate over all the attributes, try to find out if the
131
//the attribute is required.
132
for(int i = 0; i < taskNodes_XDOC.getLength(); ++i ) {
133             Node JavaDoc tmpTargetNode = taskNodes_XDOC.item(i);
134             
135             if(tmpTargetNode.getNodeType() == Node.ELEMENT_NODE ) {
136                 replaceAttributeRequiredInTaskNode(tmpTargetNode);
137             }
138         }
139     }
140     
141     private void replaceAttributeRequiredInTaskNode(Node JavaDoc aTargetTaskNode) {
142         
143         String JavaDoc tmpTaskName = aTargetTaskNode.getAttributes().getNamedItem(XDOC_XML_TAG_NAME).getNodeValue();
144         
145         if(tmpTaskName != null ) {
146             Node JavaDoc tmpSourceNode = getTaskInHTMLGeneratedTaskListNamed(tmpTaskName);
147             
148             if(tmpSourceNode != null) {
149                 replaceAttributeRequiredInXMLTaskNodeWithAttributeRequiredInHTMLNode(aTargetTaskNode,
150                                                                          tmpSourceNode);
151             }
152             else {
153                 System.out.println(MessageFormat.format("Did not find Task \"{0}\" in HTML XML file.", new String JavaDoc[]{tmpTaskName})); //$NON-NLS-1$
154
}
155         }
156         else {
157             System.out.println(MessageFormat.format("Did not find TaskName in TargetTaskNode: {0}", new String JavaDoc[]{aTargetTaskNode.toString()})); //$NON-NLS-1$
158
}
159     }
160     
161     private Node JavaDoc getTaskInHTMLGeneratedTaskListNamed(String JavaDoc aTaskName) {
162         
163         for(int i = 0; i<taskNodes_HTML.getLength(); ++i ) {
164             
165             Node JavaDoc tmpTaskNode = taskNodes_HTML.item(i);
166             if(tmpTaskNode.getNodeType() == Node.ELEMENT_NODE ) {
167                 String JavaDoc tmpTagName = tmpTaskNode.getNodeName();
168                 if(tmpTagName.equals(HTML_XML_TAG_TASK)) {
169                     NamedNodeMap JavaDoc tmpMap = tmpTaskNode.getAttributes();
170                     Node JavaDoc tmpNameNode = tmpMap.getNamedItem(HTML_XML_ATTRIBUTE_NAME);
171                     if( aTaskName.equals(tmpNameNode.getNodeValue()) ) {
172                         return tmpTaskNode;
173                     }
174                 }
175             }
176         }
177         //Not found
178
return null;
179     }
180     
181     private void replaceAttributeRequiredInXMLTaskNodeWithAttributeRequiredInHTMLNode(Node JavaDoc aTargetTaskNode,
182                                                                                           Node JavaDoc aSourceTaskNode) {
183         
184             Node JavaDoc tmpStructureNode = getChildNodeNamedWithTypeFromNode( XDOC_XML_TAG_STRUCTURE,
185                                                                         Node.ELEMENT_NODE,
186                                                                         aTargetTaskNode );
187                                                               
188             if(tmpStructureNode != null ) {
189                 Node JavaDoc tmpTargetAttributesNode = getChildNodeNamedWithTypeFromNode(XDOC_XML_TAG_ATTRIBUTES,
190                                                                                   Node.ELEMENT_NODE,
191                                                                                   tmpStructureNode);
192                 if(tmpTargetAttributesNode != null ) {
193                     Vector JavaDoc tmpTargetAttributesVector = getAttributeNodesFromXMLAttributesNode(tmpTargetAttributesNode);
194                     Vector JavaDoc tmpSourceAttributesVector = getAttributeNodesFromHTMLTaskNode(aSourceTaskNode);
195                     
196                     //Iterate over all the attributes in the targetTaskNode
197
for(int i=0; i < tmpTargetAttributesVector.size(); ++i) {
198                         Node JavaDoc tmpAttributeNode = (Node JavaDoc)tmpTargetAttributesVector.get(i);
199                         replaceAttributeRequiredInAttributeNodeWithValueFoundInNodeVector(tmpAttributeNode, tmpSourceAttributesVector);
200                     }
201                 }
202             }
203     }
204     
205     private void replaceAttributeRequiredInAttributeNodeWithValueFoundInNodeVector(Node JavaDoc aTargetAttributeNode, Vector JavaDoc aSourceAttributeVector) {
206         
207         NamedNodeMap JavaDoc tmpTargetNamedNodeMap = aTargetAttributeNode.getAttributes();
208         String JavaDoc tmpTargetAttributeName = tmpTargetNamedNodeMap.getNamedItem(XDOC_XML_TAG_NAME).getNodeValue();
209         
210         String JavaDoc tmpSourceAttributeName = null;
211         String JavaDoc tmpSourceRequiredValue = null;
212         
213         for(int i=0; i < aSourceAttributeVector.size(); ++i) {
214             Node JavaDoc tmpSourceAttributeNode = (Node JavaDoc)aSourceAttributeVector.get(i);
215             NamedNodeMap JavaDoc tmpSourceAttributeNamedNodeMap = tmpSourceAttributeNode.getAttributes();
216             tmpSourceAttributeName = tmpSourceAttributeNamedNodeMap.getNamedItem(HTML_XML_ATTRIBUTE_NAME).getNodeValue();
217             //If the Attribute Name is the same we replace the REQUIRED Value
218
if(tmpTargetAttributeName.equals(tmpSourceAttributeName) ){
219                 tmpSourceRequiredValue = tmpSourceAttributeNamedNodeMap.getNamedItem(HTML_XML_ATTRIBUTE_REQUIRED).getNodeValue();
220                 //Set the Vaule to the on we just got.
221
tmpTargetNamedNodeMap.getNamedItem(XDOC_XML_TAG_REQUIRED).setNodeValue(tmpSourceRequiredValue);
222             }
223         }
224     }
225                         
226     private Vector JavaDoc getAttributeNodesFromXMLAttributesNode(Node JavaDoc anXMLAttributesNode){
227         
228         Vector JavaDoc allAttributes = new Vector JavaDoc();
229         NodeList JavaDoc tmpList = anXMLAttributesNode.getChildNodes();
230         
231         for(int i = 0; i<tmpList.getLength(); ++i) {
232             Node JavaDoc tmpNode = tmpList.item(i);
233             if(tmpNode.getNodeType() == Node.ELEMENT_NODE
234                 && XDOC_XML_TAG_ATTRIBUTE.equals(tmpNode.getNodeName()) ) {
235                 allAttributes.add(tmpNode);
236             }
237         }
238         return allAttributes;
239     }
240     
241     private Vector JavaDoc getAttributeNodesFromHTMLTaskNode(Node JavaDoc anHTTP_XML_TaskNode) {
242         
243         Vector JavaDoc tmpVector = new Vector JavaDoc();
244         NodeList JavaDoc tmpList = anHTTP_XML_TaskNode.getChildNodes();
245         
246         for(int i = 0; i < tmpList.getLength(); ++i) {
247             Node JavaDoc tmpNode = tmpList.item(i);
248             if(tmpNode.getNodeType() == Node.ELEMENT_NODE
249                 && HTML_XML_TAG_ATTRIBUTE.equals(tmpNode.getNodeName()) ) {
250                     tmpVector.add(tmpNode);
251                 }
252         }
253         
254         return tmpVector;
255     }
256     
257     /**
258      * Returns the ChildNode of the node defined by the Arguments. The
259      * first child found matching the criterias is returned.
260      *
261      * @param aNodeName The Name of the Node to return.
262      * @param aType The Type of the node @see Node
263      * @param aParentNode The Node to get the child from
264      *
265      * @return The First Child Node found matching the criterias,
266      * or null if none is found.
267      */

268     private Node JavaDoc getChildNodeNamedWithTypeFromNode(String JavaDoc aName, short aNodeType, Node JavaDoc aNode ) {
269         
270         NodeList JavaDoc tmpNodeList = aNode.getChildNodes();
271         for(int i=0; i<tmpNodeList.getLength(); ++i ) {
272             Node JavaDoc tmpNode = tmpNodeList.item(i);
273             if( (tmpNode.getNodeType() == aNodeType) && aName.equals(tmpNode.getNodeName()) ) {
274                 return tmpNode;
275             }
276         }
277         //Not found
278
return null;
279     }
280             
281         
282     /**
283      * Returns the (DOM) document as a result of parsing the file with the
284      * specified file name.
285      * <P>
286      * The file will be loaded as resource, thus must begin with '/' and must
287      * be relative to the classpath.
288      */

289     private Document JavaDoc parseFile(String JavaDoc aFileName) {
290         Document JavaDoc tempDocument = null;
291
292         DocumentBuilderFactory JavaDoc tempFactory = DocumentBuilderFactory.newInstance();
293         tempFactory.setIgnoringComments(true);
294         tempFactory.setIgnoringElementContentWhitespace(true);
295         tempFactory.setCoalescing(true);
296
297         try {
298             DocumentBuilder JavaDoc tempDocBuilder = tempFactory.newDocumentBuilder();
299             URL JavaDoc tempURL = getClass().getResource(aFileName);
300             InputSource JavaDoc tempInputSource = new InputSource JavaDoc(tempURL.toExternalForm());
301             tempDocument = tempDocBuilder.parse(tempInputSource);
302         } catch (ParserConfigurationException JavaDoc e) {
303             AntUIPlugin.log(e);
304         }
305         catch (IOException JavaDoc ioException) {
306             AntUIPlugin.log(ioException);
307         }
308         catch (SAXException JavaDoc saxException) {
309             AntUIPlugin.log(saxException);
310         }
311
312         return tempDocument;
313     }
314     
315     /**
316      * This function writes the XMLDocument to the specified file.
317      * @param aFileName The filename to which the XMLDocument should be written.
318      */

319     public void writeXMLDocumentToFile(String JavaDoc aFileName) {
320         
321 // try {
322
// XmlDocument xmlDocument = (XmlDocument)xdocXMLDocument;
323
// xmlDocument.write(new FileWriter(aFileName), "UTF-8"); //$NON-NLS-1$
324
// }
325
// catch(IOException ioe) {
326
// System.out.println(MessageFormat.format(AntEditorToolsMessages.getString("TaskXMLFileMerger.Could_not_print"), new String[]{ioe.toString()})); //$NON-NLS-1$
327
// }
328
}
329     
330     public static void main(String JavaDoc[] args) {
331         
332         TaskXMLFileMerger tmpTaskXMLFileMerger = new TaskXMLFileMerger();
333         tmpTaskXMLFileMerger.runReplaceAttributeRequiredProcess();
334         tmpTaskXMLFileMerger.writeXMLDocumentToFile("src\\anttasks_1.5b.xml"); //$NON-NLS-1$
335
}
336 }
337
Popular Tags