001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: * $Id: VersionConversion.java 6998 2007-04-24 01:13:00Z jzhang $
023: *
024: */
025: package com.bostechcorp.cbesb.ui.util.version;
026:
027: import java.io.File;
028: import java.io.FileWriter;
029: import java.io.IOException;
030: import java.util.List;
031:
032: import org.jdom.Attribute;
033: import org.jdom.Document;
034: import org.jdom.Element;
035: import org.jdom.JDOMException;
036: import org.jdom.Namespace;
037: import org.jdom.input.SAXBuilder;
038: import org.jdom.output.Format;
039: import org.jdom.output.XMLOutputter;
040:
041: import com.bostechcorp.cbesb.common.util.FileUtil;
042:
043: /**
044: *
045: */
046: public class VersionConversion {
047:
048: public static final String CFE = "cfe";
049: public static final String XSI = "xsi";
050: public static final String XMI = "xmi";
051:
052: public static final String VERSION_1_0 = "1.0";
053: public static final String VERSION_1_1 = "1.1";
054: private String currentVersion;
055:
056: public VersionConversion() {
057: }
058:
059: public String getCurrentVersion() {
060: return currentVersion;
061: }
062:
063: public void setCurrentVersion(String currentVersion) {
064: this .currentVersion = currentVersion;
065: }
066:
067: /**
068: * @return the version
069: */
070: public static String getVersion(String filename) {
071: String componentflow = searchComponentFlow(filename);
072: String version = "";
073: SAXBuilder sb = new SAXBuilder();
074: try {
075: Document doc = sb.build(componentflow);
076: Element root = doc.getRootElement();
077: Namespace cfeNS = root.getNamespace(CFE);
078: String cfeURI = cfeNS.getURI();
079: if (cfeURI.endsWith(VERSION_1_1)) {
080: version = VERSION_1_1;
081: } else {
082: version = VERSION_1_0;
083: }
084: } catch (JDOMException e) {
085: e.printStackTrace();
086: } catch (IOException e) {
087: e.printStackTrace();
088: }
089:
090: return version;
091: }
092:
093: public static void convert(String filename) {
094: String version = VersionConversion.getVersion(filename);
095: if (VERSION_1_0.equals(version)) {
096: convert10to11(filename);
097: }
098: }
099:
100: private static void convert10to11(String filename) {
101: String componentflow = searchComponentFlow(filename);
102: FileUtil.copyFile(componentflow, componentflow + ".bak");
103: SAXBuilder sb = new SAXBuilder();
104: try {
105: Document doc = sb.build(componentflow);
106: Element root = doc.getRootElement();
107:
108: Namespace newCfeNS = Namespace.getNamespace(CFE, root
109: .getNamespace().getURI()
110: + "/" + VERSION_1_1);
111:
112: Document newDoc = new Document();
113: Element newRoot = new Element(root.getName(), newCfeNS);
114: newRoot.addNamespaceDeclaration(root.getNamespace(XSI));
115: newRoot.addNamespaceDeclaration(root.getNamespace(XMI));
116: newRoot.setAttribute("version", "2.0", root
117: .getNamespace(XMI));
118: newDoc.addContent(newRoot);
119:
120: List componentFlowEditorElements = root.getChildren();
121: for (int i = 0; i < componentFlowEditorElements.size(); i++) {
122: Element element = (Element) componentFlowEditorElements
123: .get(i);
124: Element newElement = new Element(element.getName(),
125: element.getNamespace());
126: ConversionUtil.convertAttribute(element, newElement);
127: if ("componentFlowEditorElements".equals(element
128: .getName())) {
129: Attribute type = element.getAttribute("type", root
130: .getNamespace(XSI));
131: String s = type.getValue();
132: if (s.endsWith("FTP")) {
133: FTPConversion.convert(element, newElement);
134: } else if (s.endsWith("File")) {
135: FileConversion.convert(element, newElement);
136: } else if (s.endsWith("HTTP")) {
137: HTTPConversion.convert(element, newElement);
138: } else if (s.endsWith("JMS")) {
139: JMSConversion.convert(element, newElement);
140: } else if (s.endsWith("Transformer")) {
141: TransformerConversion.convert(element,
142: newElement);
143: } else if (s.endsWith("Parser")) {
144: ParserConversion.convert(element, newElement);
145: } else if (s.endsWith("XSLT")) {
146: XSLTConversion.convert(element, newElement);
147: } else if (s.endsWith("Sequencer")) {
148: SequencerConversion
149: .convert(element, newElement);
150: } else if (s.endsWith("CBR")) {
151: CBRConversion.convert(element, newElement);
152: } else if (s.endsWith("JDBC")) {
153: JDBCConversion.convert(element, newElement);
154: } else if (s.endsWith("Script")) {
155: ScriptConversion.convert(element, newElement);
156: } else if (s.endsWith("TCPIP")) {
157: TCPIPConversion.convert(element, newElement);
158: }
159: }
160: newRoot.addContent(newElement);
161: }
162: XMLOutputter xmlOut = new XMLOutputter(Format
163: .getPrettyFormat().setEncoding("ASCII"));
164: File file = new File(componentflow);
165: FileWriter writer = new FileWriter(file);
166: xmlOut.output(newDoc, writer);
167: writer.close();
168: } catch (JDOMException e) {
169: e.printStackTrace();
170: } catch (IOException e) {
171: e.printStackTrace();
172: }
173: }
174:
175: private static String searchComponentFlow(String filename) {
176: int index = filename.lastIndexOf("_");
177: String componentFlow = filename.substring(0, index);
178: return componentFlow;
179: }
180: }
|