001: package com.calipso.xmleditor;
002:
003: import org.w3c.dom.Document;
004: import org.w3c.dom.Element;
005: import org.apache.xerces.dom.DocumentImpl;
006: import org.apache.xml.serialize.OutputFormat;
007: import org.apache.xml.serialize.XMLSerializer;
008:
009: import javax.swing.tree.DefaultTreeModel;
010: import java.util.Vector;
011: import java.util.Enumeration;
012: import java.io.*;
013:
014: /**
015: *
016: * User: soliveri
017: * Date: 06-oct-2003
018: * Time: 15:45:07
019: *
020: */
021:
022: public class XmlEditorXmlGenerator {
023:
024: private static String outputPath;
025:
026: public XmlEditorXmlGenerator(DefaultTreeModel model, String fileName)
027: throws XmlEditorException {
028: this .outputPath = fileName;
029: generateFrom(model, fileName);
030: }
031:
032: /**
033: * Genera un XML en base al modelo en el archivo especificado
034: * @param model
035: * @param fileName
036: * @throws XmlEditorException
037: */
038: public static void generateFrom(DefaultTreeModel model,
039: String fileName) throws XmlEditorException {
040: outputPath = fileName;
041: Document document = new DocumentImpl();
042: XmlEditorTreeModelNode modelNode = (XmlEditorTreeModelNode) model
043: .getRoot();
044: Element element = document.createElement(modelNode
045: .getUserObject().toString());
046: XmlEditorTreeModelNode rootSpecs = getRootSpecs(modelNode
047: .children());
048: document.appendChild(element);
049: fillElementFrom(rootSpecs.getAttributes(), rootSpecs
050: .getAttributeNames(), element);
051: generateFrom(modelNode.children(), element, document, rootSpecs);
052: printOutputFrom(document, rootSpecs);
053: }
054:
055: private static XmlEditorTreeModelNode getRootSpecs(
056: Enumeration children) {
057: while (children.hasMoreElements()) {
058: XmlEditorTreeModelNode modelNode = (XmlEditorTreeModelNode) children
059: .nextElement();
060: if (modelNode.isLeaf()) {
061: return modelNode;
062: }
063: }
064: return null;
065: }
066:
067: private static void printOutputFrom(Document document,
068: XmlEditorTreeModelNode rootSpecs) throws XmlEditorException {
069: OutputFormat format = new OutputFormat(document, "ISO-8859-1",
070: true);
071: if (outputPath.equalsIgnoreCase("")) {
072: outputPath = System.getProperty("user.dir")
073: + rootSpecs.getAttributes()
074: .elementAt(
075: rootSpecs.getAttributeNames()
076: .indexOf("Id")).toString();
077: }
078: try {
079: XMLSerializer serial = new XMLSerializer(
080: new FileOutputStream(getFileFrom(outputPath)),
081: format);
082: serial.serialize(document);
083: } catch (IOException e) {
084: throw new XmlEditorException(e);
085: }
086: }
087:
088: private static File getFileFrom(String outputPath) {
089: File file;
090: if (outputPath.endsWith(".xml")) {
091: file = new File(outputPath);
092: } else {
093: file = new File(outputPath + ".xml");
094: }
095: return file;
096: }
097:
098: private static void generateFrom(Enumeration childrens,
099: Element element, Document document,
100: XmlEditorTreeModelNode rootSpec) {
101: while (childrens.hasMoreElements()) {
102: XmlEditorTreeModelNode modelNode = (XmlEditorTreeModelNode) childrens
103: .nextElement();
104: if (modelNode != rootSpec) {
105: for (int i = 0; i < modelNode.getChildCount(); i++) {
106: XmlEditorTreeModelNode child = (XmlEditorTreeModelNode) modelNode
107: .getChildAt(i);
108: if (child.getAttributes().size() > 0
109: && child.isLeaf()) {
110: Element newElement = document
111: .createElement(modelNode
112: .getUserObject().toString());
113: fillElementFrom(child.getAttributes(), child
114: .getAttributeNames(), newElement);
115: element.appendChild(newElement);
116: } else if (!child.isLeaf()) {
117: Element newElement = document
118: .createElement(modelNode
119: .getUserObject().toString());
120: element.appendChild(newElement);
121: generateFrom(modelNode.children(), newElement,
122: document, rootSpec);
123: }
124: }
125: }
126: }
127: }
128:
129: private static void fillElementFrom(Vector attributes,
130: Vector attrNames, Element element) {
131: for (int i = 0; i < attributes.size(); i++) {
132: element.setAttribute(attrNames.elementAt(i).toString(),
133: attributes.elementAt(i).toString());
134: }
135: }
136: }
|