1 14 15 package org.eclipse.ant.internal.ui.editor.tools; 16 17 import java.io.IOException ; 18 import java.net.URL ; 19 import java.text.MessageFormat ; 20 import java.util.Vector ; 21 22 import javax.xml.parsers.DocumentBuilder ; 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import javax.xml.parsers.ParserConfigurationException ; 25 26 import org.eclipse.ant.internal.ui.AntUIPlugin; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.NamedNodeMap ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.SAXException ; 33 34 46 public class TaskXMLFileMerger { 47 48 public static String HTML_TASKS_DESCRIPTION_XML_FILE_NAME = "/tasks.xml"; public static String HTML_XML_TAG_TASKS = "TASKS"; public static String HTML_XML_TAG_TASK = "TASK"; public static String HTML_XML_TAG_ATTRIBUTE = "ATTRIBUTE"; public static String HTML_XML_ATTRIBUTE_NAME = "NAME"; public static String HTML_XML_ATTRIBUTE_REQUIRED = "REQUIRED"; 57 public static String XDOC_TASKS_DESCRIPTION_XML_FILE_NAME = "/XDOCtasks.xml"; public static String XDOC_XML_TAG_TASKS = "tasks"; public static String XDOC_XML_TAG_TASK = "task"; public static String XDOC_XML_TAG_NAME = "name"; public static String XDOC_XML_TAG_STRUCTURE = "structure"; public static String XDOC_XML_TAG_ATTRIBUTES = "attributes"; public static String XDOC_XML_TAG_ATTRIBUTE = "attribute"; public static String XDOC_XML_TAG_ELEMENTS = "elements"; public static String XDOC_XML_TAG_ELEMENT = "element"; public static String XDOC_XML_TAG_REQUIRED = "required"; 69 70 protected NodeList taskNodes_HTML = null; 71 protected NodeList taskNodes_XDOC = null; 72 public Document xdocXMLDocument = null; 73 74 77 public TaskXMLFileMerger() { 78 initialize(); 79 } 80 81 82 85 private void initialize() { 86 87 Document tmpDocument = null; 88 89 tmpDocument = parseFile(HTML_TASKS_DESCRIPTION_XML_FILE_NAME); 91 taskNodes_HTML = tmpDocument.getFirstChild().getChildNodes(); 92 93 tmpDocument = parseFile(XDOC_TASKS_DESCRIPTION_XML_FILE_NAME); 95 taskNodes_XDOC = tmpDocument.getFirstChild().getChildNodes(); 96 xdocXMLDocument = tmpDocument; 97 118 } 119 120 121 127 public void runReplaceAttributeRequiredProcess() { 128 129 for(int i = 0; i < taskNodes_XDOC.getLength(); ++i ) { 133 Node 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 aTargetTaskNode) { 142 143 String tmpTaskName = aTargetTaskNode.getAttributes().getNamedItem(XDOC_XML_TAG_NAME).getNodeValue(); 144 145 if(tmpTaskName != null ) { 146 Node 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 []{tmpTaskName})); } 155 } 156 else { 157 System.out.println(MessageFormat.format("Did not find TaskName in TargetTaskNode: {0}", new String []{aTargetTaskNode.toString()})); } 159 } 160 161 private Node getTaskInHTMLGeneratedTaskListNamed(String aTaskName) { 162 163 for(int i = 0; i<taskNodes_HTML.getLength(); ++i ) { 164 165 Node tmpTaskNode = taskNodes_HTML.item(i); 166 if(tmpTaskNode.getNodeType() == Node.ELEMENT_NODE ) { 167 String tmpTagName = tmpTaskNode.getNodeName(); 168 if(tmpTagName.equals(HTML_XML_TAG_TASK)) { 169 NamedNodeMap tmpMap = tmpTaskNode.getAttributes(); 170 Node tmpNameNode = tmpMap.getNamedItem(HTML_XML_ATTRIBUTE_NAME); 171 if( aTaskName.equals(tmpNameNode.getNodeValue()) ) { 172 return tmpTaskNode; 173 } 174 } 175 } 176 } 177 return null; 179 } 180 181 private void replaceAttributeRequiredInXMLTaskNodeWithAttributeRequiredInHTMLNode(Node aTargetTaskNode, 182 Node aSourceTaskNode) { 183 184 Node tmpStructureNode = getChildNodeNamedWithTypeFromNode( XDOC_XML_TAG_STRUCTURE, 185 Node.ELEMENT_NODE, 186 aTargetTaskNode ); 187 188 if(tmpStructureNode != null ) { 189 Node tmpTargetAttributesNode = getChildNodeNamedWithTypeFromNode(XDOC_XML_TAG_ATTRIBUTES, 190 Node.ELEMENT_NODE, 191 tmpStructureNode); 192 if(tmpTargetAttributesNode != null ) { 193 Vector tmpTargetAttributesVector = getAttributeNodesFromXMLAttributesNode(tmpTargetAttributesNode); 194 Vector tmpSourceAttributesVector = getAttributeNodesFromHTMLTaskNode(aSourceTaskNode); 195 196 for(int i=0; i < tmpTargetAttributesVector.size(); ++i) { 198 Node tmpAttributeNode = (Node )tmpTargetAttributesVector.get(i); 199 replaceAttributeRequiredInAttributeNodeWithValueFoundInNodeVector(tmpAttributeNode, tmpSourceAttributesVector); 200 } 201 } 202 } 203 } 204 205 private void replaceAttributeRequiredInAttributeNodeWithValueFoundInNodeVector(Node aTargetAttributeNode, Vector aSourceAttributeVector) { 206 207 NamedNodeMap tmpTargetNamedNodeMap = aTargetAttributeNode.getAttributes(); 208 String tmpTargetAttributeName = tmpTargetNamedNodeMap.getNamedItem(XDOC_XML_TAG_NAME).getNodeValue(); 209 210 String tmpSourceAttributeName = null; 211 String tmpSourceRequiredValue = null; 212 213 for(int i=0; i < aSourceAttributeVector.size(); ++i) { 214 Node tmpSourceAttributeNode = (Node )aSourceAttributeVector.get(i); 215 NamedNodeMap tmpSourceAttributeNamedNodeMap = tmpSourceAttributeNode.getAttributes(); 216 tmpSourceAttributeName = tmpSourceAttributeNamedNodeMap.getNamedItem(HTML_XML_ATTRIBUTE_NAME).getNodeValue(); 217 if(tmpTargetAttributeName.equals(tmpSourceAttributeName) ){ 219 tmpSourceRequiredValue = tmpSourceAttributeNamedNodeMap.getNamedItem(HTML_XML_ATTRIBUTE_REQUIRED).getNodeValue(); 220 tmpTargetNamedNodeMap.getNamedItem(XDOC_XML_TAG_REQUIRED).setNodeValue(tmpSourceRequiredValue); 222 } 223 } 224 } 225 226 private Vector getAttributeNodesFromXMLAttributesNode(Node anXMLAttributesNode){ 227 228 Vector allAttributes = new Vector (); 229 NodeList tmpList = anXMLAttributesNode.getChildNodes(); 230 231 for(int i = 0; i<tmpList.getLength(); ++i) { 232 Node 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 getAttributeNodesFromHTMLTaskNode(Node anHTTP_XML_TaskNode) { 242 243 Vector tmpVector = new Vector (); 244 NodeList tmpList = anHTTP_XML_TaskNode.getChildNodes(); 245 246 for(int i = 0; i < tmpList.getLength(); ++i) { 247 Node 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 268 private Node getChildNodeNamedWithTypeFromNode(String aName, short aNodeType, Node aNode ) { 269 270 NodeList tmpNodeList = aNode.getChildNodes(); 271 for(int i=0; i<tmpNodeList.getLength(); ++i ) { 272 Node tmpNode = tmpNodeList.item(i); 273 if( (tmpNode.getNodeType() == aNodeType) && aName.equals(tmpNode.getNodeName()) ) { 274 return tmpNode; 275 } 276 } 277 return null; 279 } 280 281 282 289 private Document parseFile(String aFileName) { 290 Document tempDocument = null; 291 292 DocumentBuilderFactory tempFactory = DocumentBuilderFactory.newInstance(); 293 tempFactory.setIgnoringComments(true); 294 tempFactory.setIgnoringElementContentWhitespace(true); 295 tempFactory.setCoalescing(true); 296 297 try { 298 DocumentBuilder tempDocBuilder = tempFactory.newDocumentBuilder(); 299 URL tempURL = getClass().getResource(aFileName); 300 InputSource tempInputSource = new InputSource (tempURL.toExternalForm()); 301 tempDocument = tempDocBuilder.parse(tempInputSource); 302 } catch (ParserConfigurationException e) { 303 AntUIPlugin.log(e); 304 } 305 catch (IOException ioException) { 306 AntUIPlugin.log(ioException); 307 } 308 catch (SAXException saxException) { 309 AntUIPlugin.log(saxException); 310 } 311 312 return tempDocument; 313 } 314 315 319 public void writeXMLDocumentToFile(String aFileName) { 320 321 } 329 330 public static void main(String [] args) { 331 332 TaskXMLFileMerger tmpTaskXMLFileMerger = new TaskXMLFileMerger(); 333 tmpTaskXMLFileMerger.runReplaceAttributeRequiredProcess(); 334 tmpTaskXMLFileMerger.writeXMLDocumentToFile("src\\anttasks_1.5b.xml"); } 336 } 337 | Popular Tags |