1 package com.calipso.xmleditor; 2 3 import org.w3c.dom.Document ; 4 import org.w3c.dom.Element ; 5 import org.apache.xerces.dom.DocumentImpl; 6 import org.apache.xml.serialize.OutputFormat; 7 import org.apache.xml.serialize.XMLSerializer; 8 9 import javax.swing.tree.DefaultTreeModel ; 10 import java.util.Vector ; 11 import java.util.Enumeration ; 12 import java.io.*; 13 14 21 22 public class XmlEditorXmlGenerator { 23 24 private static String outputPath; 25 26 public XmlEditorXmlGenerator(DefaultTreeModel model, String fileName) throws XmlEditorException{ 27 this.outputPath = fileName; 28 generateFrom(model, fileName); 29 } 30 31 37 public static void generateFrom(DefaultTreeModel model, String fileName) throws XmlEditorException{ 38 outputPath = fileName; 39 Document document = new DocumentImpl(); 40 XmlEditorTreeModelNode modelNode = (XmlEditorTreeModelNode) model.getRoot(); 41 Element element = document.createElement(modelNode.getUserObject().toString()); 42 XmlEditorTreeModelNode rootSpecs = getRootSpecs(modelNode.children()); 43 document.appendChild(element); 44 fillElementFrom(rootSpecs.getAttributes(), rootSpecs.getAttributeNames(), element); 45 generateFrom(modelNode.children(), element, document, rootSpecs); 46 printOutputFrom(document, rootSpecs); 47 } 48 49 private static XmlEditorTreeModelNode getRootSpecs(Enumeration children) { 50 while(children.hasMoreElements()) { 51 XmlEditorTreeModelNode modelNode = (XmlEditorTreeModelNode) children.nextElement(); 52 if(modelNode.isLeaf()) { 53 return modelNode; 54 } 55 } 56 return null; 57 } 58 59 private static void printOutputFrom(Document document, XmlEditorTreeModelNode rootSpecs) throws XmlEditorException{ 60 OutputFormat format = new OutputFormat(document,"ISO-8859-1", true); 61 if(outputPath.equalsIgnoreCase("")) { 62 outputPath = System.getProperty("user.dir") + rootSpecs.getAttributes().elementAt(rootSpecs.getAttributeNames().indexOf("Id")).toString(); 63 } 64 try { 65 XMLSerializer serial = new XMLSerializer(new FileOutputStream(getFileFrom(outputPath)), format); 66 serial.serialize(document); 67 } catch (IOException e) { 68 throw new XmlEditorException(e); 69 } 70 } 71 72 private static File getFileFrom(String outputPath) { 73 File file; 74 if(outputPath.endsWith(".xml")) { 75 file = new File(outputPath); 76 } else { 77 file = new File(outputPath + ".xml"); 78 } 79 return file; 80 } 81 82 private static void generateFrom(Enumeration childrens, Element element, Document document, XmlEditorTreeModelNode rootSpec) { 83 while(childrens.hasMoreElements()) { 84 XmlEditorTreeModelNode modelNode = (XmlEditorTreeModelNode) childrens.nextElement(); 85 if(modelNode != rootSpec) { 86 for(int i = 0 ; i < modelNode.getChildCount() ; i++) { 87 XmlEditorTreeModelNode child = (XmlEditorTreeModelNode) modelNode.getChildAt(i); 88 if(child.getAttributes().size() > 0 && child.isLeaf()) { 89 Element newElement = document.createElement(modelNode.getUserObject().toString()); 90 fillElementFrom(child.getAttributes(), child.getAttributeNames(), newElement); 91 element.appendChild(newElement); 92 } else if(!child.isLeaf()){ 93 Element newElement = document.createElement(modelNode.getUserObject().toString()); 94 element.appendChild(newElement); 95 generateFrom(modelNode.children(), newElement, document, rootSpec); 96 } 97 } 98 } 99 } 100 } 101 102 private static void fillElementFrom(Vector attributes, Vector attrNames, Element element) { 103 for(int i = 0 ; i < attributes.size() ; i++) { 104 element.setAttribute(attrNames.elementAt(i).toString(), attributes.elementAt(i).toString()); 105 } 106 } 107 } 108 | Popular Tags |