/******************************************************************************* * Copyright (c) 2002, 2005 GEBIT Gesellschaft fuer EDV-Beratung * und Informatik-Technologien mbH, * Berlin, Duesseldorf, Frankfurt (Germany) and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * GEBIT Gesellschaft fuer EDV-Beratung und Informatik-Technologien mbH - initial API and implementation * IBM Corporation - bug 24108 *******************************************************************************/ package org.eclipse.ant.internal.ui.editor.tools; import java.io.IOException; import java.net.URL; import com.ibm.icu.text.MessageFormat; import java.util.Vector; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.eclipse.ant.internal.ui.AntUIPlugin; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; /** * This class can be used to merge the tasks.xml and XDOCtasks.xml. * These files can be found in the Ant Editor Content Assist Dev folder. * Move the files to the Ant Editor folder to use this class. * * The xml automatically generated from the proposed xdoclet in the Apache Ant's * project currently has no information on required attributes. In the future the task writers * hopefully will include this information in the source code comments. Our * template currently inserts an attribute required="NOTDEFINED" and this class * replaces that field if its defined in the xml file we have generated from the * information based on the html files in the Apache Ant's manual directory. */ public class TaskXMLFileMerger { //Definitions for the HTML Generated XML File public static String HTML_TASKS_DESCRIPTION_XML_FILE_NAME = "/tasks.xml"; //$NON-NLS-1$ public static String HTML_XML_TAG_TASKS = "TASKS"; //$NON-NLS-1$ public static String HTML_XML_TAG_TASK = "TASK"; //$NON-NLS-1$ public static String HTML_XML_TAG_ATTRIBUTE = "ATTRIBUTE"; //$NON-NLS-1$ //public static String HTML_XML_TAG_DESCRIPTION = "DESCRIPTION"; public static String HTML_XML_ATTRIBUTE_NAME = "NAME"; //$NON-NLS-1$ public static String HTML_XML_ATTRIBUTE_REQUIRED = "REQUIRED"; //$NON-NLS-1$ //Definitions for the XDoclet Genereated XML File public static String XDOC_TASKS_DESCRIPTION_XML_FILE_NAME = "/XDOCtasks.xml"; //$NON-NLS-1$ public static String XDOC_XML_TAG_TASKS = "tasks"; //$NON-NLS-1$ public static String XDOC_XML_TAG_TASK = "task"; //$NON-NLS-1$ public static String XDOC_XML_TAG_NAME = "name"; //$NON-NLS-1$ public static String XDOC_XML_TAG_STRUCTURE = "structure"; //$NON-NLS-1$ public static String XDOC_XML_TAG_ATTRIBUTES = "attributes"; //$NON-NLS-1$ public static String XDOC_XML_TAG_ATTRIBUTE = "attribute"; //$NON-NLS-1$ public static String XDOC_XML_TAG_ELEMENTS = "elements"; //$NON-NLS-1$ public static String XDOC_XML_TAG_ELEMENT = "element"; //$NON-NLS-1$ public static String XDOC_XML_TAG_REQUIRED = "required"; //$NON-NLS-1$ protected NodeList taskNodes_HTML = null; protected NodeList taskNodes_XDOC = null; public Document xdocXMLDocument = null; /** * Creates an initialized instance. */ public TaskXMLFileMerger() { initialize(); } /** * Parses the task description xml files and stores the information. */ private void initialize() { Document tmpDocument = null; //Get All the Tasks in the HTML XML Generated file and store in the taskNodes_HTML tmpDocument = parseFile(HTML_TASKS_DESCRIPTION_XML_FILE_NAME); taskNodes_HTML = tmpDocument.getFirstChild().getChildNodes(); //Do the same for the XDOC XML Generated file tmpDocument = parseFile(XDOC_TASKS_DESCRIPTION_XML_FILE_NAME); taskNodes_XDOC = tmpDocument.getFirstChild().getChildNodes(); xdocXMLDocument = tmpDocument; /* Document tempDocument = parseFile(aFileName); Node tempRootNode = tempDocument.getDocumentElement(); NodeList tempChildNodes = tempRootNode.getChildNodes(); for(int i=0; i * The file will be loaded as resource, thus must begin with '/' and must * be relative to the classpath. */ private Document parseFile(String aFileName) { Document tempDocument = null; DocumentBuilderFactory tempFactory = DocumentBuilderFactory.newInstance(); tempFactory.setIgnoringComments(true); tempFactory.setIgnoringElementContentWhitespace(true); tempFactory.setCoalescing(true); try { DocumentBuilder tempDocBuilder = tempFactory.newDocumentBuilder(); URL tempURL = getClass().getResource(aFileName); InputSource tempInputSource = new InputSource(tempURL.toExternalForm()); tempDocument = tempDocBuilder.parse(tempInputSource); } catch (ParserConfigurationException e) { AntUIPlugin.log(e); } catch (IOException ioException) { AntUIPlugin.log(ioException); } catch (SAXException saxException) { AntUIPlugin.log(saxException); } return tempDocument; } /** * This function writes the XMLDocument to the specified file. * @param aFileName The filename to which the XMLDocument should be written. */ public void writeXMLDocumentToFile(String aFileName) { // try { // XmlDocument xmlDocument = (XmlDocument)xdocXMLDocument; // xmlDocument.write(new FileWriter(aFileName), "UTF-8"); //$NON-NLS-1$ // } // catch(IOException ioe) { // System.out.println(MessageFormat.format(AntEditorToolsMessages.getString("TaskXMLFileMerger.Could_not_print"), new String[]{ioe.toString()})); //$NON-NLS-1$ // } } public static void main(String[] args) { TaskXMLFileMerger tmpTaskXMLFileMerger = new TaskXMLFileMerger(); tmpTaskXMLFileMerger.runReplaceAttributeRequiredProcess(); tmpTaskXMLFileMerger.writeXMLDocumentToFile("src\\anttasks_1.5b.xml"); //$NON-NLS-1$ } }