KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > xmleditor > XmlEditorXmlGenerator


1 package com.calipso.xmleditor;
2
3 import org.w3c.dom.Document JavaDoc;
4 import org.w3c.dom.Element JavaDoc;
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 JavaDoc;
10 import java.util.Vector JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.io.*;
13
14 /**
15  *
16  * User: soliveri
17  * Date: 06-oct-2003
18  * Time: 15:45:07
19  *
20  */

21
22 public class XmlEditorXmlGenerator {
23
24   private static String JavaDoc outputPath;
25
26   public XmlEditorXmlGenerator(DefaultTreeModel JavaDoc model, String JavaDoc fileName) throws XmlEditorException{
27     this.outputPath = fileName;
28     generateFrom(model, fileName);
29   }
30
31   /**
32    * Genera un XML en base al modelo en el archivo especificado
33    * @param model
34    * @param fileName
35    * @throws XmlEditorException
36    */

37   public static void generateFrom(DefaultTreeModel JavaDoc model, String JavaDoc fileName) throws XmlEditorException{
38     outputPath = fileName;
39     Document JavaDoc document = new DocumentImpl();
40     XmlEditorTreeModelNode modelNode = (XmlEditorTreeModelNode) model.getRoot();
41     Element JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc childrens, Element JavaDoc element, Document JavaDoc 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 JavaDoc newElement = document.createElement(modelNode.getUserObject().toString());
90             fillElementFrom(child.getAttributes(), child.getAttributeNames(), newElement);
91             element.appendChild(newElement);
92           } else if(!child.isLeaf()){
93             Element JavaDoc 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 JavaDoc attributes, Vector JavaDoc attrNames, Element JavaDoc 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