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 it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * 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 MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.ui.ide.wizards;
025:
026: import java.io.BufferedInputStream;
027: import java.io.DataInputStream;
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.io.FileWriter;
031: import java.io.StringWriter;
032:
033: import javax.xml.parsers.DocumentBuilder;
034: import javax.xml.parsers.DocumentBuilderFactory;
035: import javax.xml.transform.OutputKeys;
036: import javax.xml.transform.Transformer;
037: import javax.xml.transform.TransformerFactory;
038: import javax.xml.transform.dom.DOMSource;
039: import javax.xml.transform.stream.StreamResult;
040:
041: import org.eclipse.jface.viewers.IStructuredSelection;
042: import org.eclipse.jface.wizard.Wizard;
043: import org.eclipse.ui.INewWizard;
044: import org.eclipse.ui.IWorkbench;
045: import org.w3c.dom.Document;
046: import org.w3c.dom.Element;
047:
048: import com.bostechcorp.cbesb.common.i18n.I18N;
049: import com.bostechcorp.cbesb.common.i18n.Messages;
050: import com.bostechcorp.cbesb.ui.ide.Activator;
051: import com.bostechcorp.cbesb.ui.util.ProjectUtil;
052: import com.bostechcorp.cbesb.ui.util.variant.VariantCreation;
053:
054: /*
055: */
056: public class X12VariantCreationWizard extends Wizard implements
057: INewWizard {
058: private IStructuredSelection selection;
059:
060: private IWorkbench workbench;
061:
062: String xmlString = null;
063:
064: String x12VarFile = null;
065:
066: String header = null;
067:
068: String projectName = null;
069:
070: int index = 0;
071:
072: private X12VariantCreationWizardPage mainPage;
073:
074: /**
075: * (non-Javadoc) Method declared on Wizard.
076: */
077: public void addPages() {
078: mainPage = new X12VariantCreationWizardPage(
079: "NewX12VariantWizard", workbench, selection);
080: addPage(mainPage);
081: }
082:
083: /**
084: * (non-Javadoc) Method declared on IWorkbenchWizard
085: */
086: public void init(IWorkbench workbench,
087: IStructuredSelection selection) {
088: this .workbench = workbench;
089: this .selection = selection;
090: setWindowTitle(I18N.getString(Messages.IDE_X12_TITLE));// "New X12
091: // variant");
092: // //$NON-NLS-1$
093: setDefaultPageImageDescriptor(Activator
094: .imageDescriptorFromPlugin(Activator.PLUGIN_ID,
095: "icons/full/wizban/openX12VariantView.gif"));
096: }
097:
098: /**
099: * (non-Javadoc) Method declared on IWizard
100: */
101: public boolean performFinish() {
102:
103: projectName = mainPage.getProjectName();
104:
105: VariantCreation.version_name = X12VariantCreationWizardPage.versionChoice
106: .getText();
107: VariantCreation.var_name = X12VariantCreationWizardPage.newVarName;
108: // Create a folder for this x12 variant
109: String newVariantPath = X12VariantCreationWizardPage.pathOfFolder
110: + "/"
111: + X12VariantCreationWizardPage.versionChoice.getText();
112: boolean success = (new File(newVariantPath).mkdir());
113: String varName = newVariantPath + "/"
114: + X12VariantCreationWizardPage.newVarName;
115: success = (new File(varName).mkdir());
116: String mess = varName + "/"
117: + X12VariantCreationWizardPage.MESSAGE;
118: success = (new File(mess).mkdir());
119: String seg = varName + "/"
120: + X12VariantCreationWizardPage.SEGMENT;
121: success = (new File(seg).mkdir());
122:
123: ProjectUtil.refreshProject(projectName);
124: return mainPage.finish();
125: }
126:
127: public void dispose() {
128:
129: boolean exist = true;
130: x12VarFile = X12VariantCreationWizardPage.pathOfFolder + "/"
131: + X12VariantCreationWizardPage.newVarName + "."
132: + X12VariantCreationWizardPage.extension;
133:
134: try {
135: FileInputStream fis = new FileInputStream(x12VarFile);
136: BufferedInputStream bis = new BufferedInputStream(fis);
137: DataInputStream dis = new DataInputStream(bis);
138: } catch (Exception e) {
139: //TODO
140: exist = false;
141:
142: return;
143: }
144:
145: if (exist) {
146:
147: header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
148:
149: try {
150: // ///////////////////////////
151: // Creating an empty XML Document
152:
153: // We need a Document
154: DocumentBuilderFactory dbfac = DocumentBuilderFactory
155: .newInstance();
156: DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
157: Document doc = docBuilder.newDocument();
158:
159: // create the root element and add it to the document
160:
161: // Comment comment = doc.createComment(header);
162: // doc.appendChild(comment);
163:
164: Element variantcontrol = doc
165: .createElement("vcf:variantcontrol");
166: variantcontrol.setAttribute("xmlns:vcf",
167: "http://cbesb.bostechcorp.com/vcf/1.0");
168: variantcontrol.setAttribute("xmlns",
169: "http://www.bostechcorp.com/x12/demo");
170: variantcontrol.setAttribute("targetNamespace",
171: "http://cbesb.bostechcorp.com/vcf/1.0");
172:
173: Element variant = doc.createElement("vcf:variant");
174: variant.setAttribute("name",
175: X12VariantCreationWizardPage.newVarName);
176: variant.setAttribute("type", "X12");
177: variant.setAttribute("version",
178: X12VariantCreationWizardPage.versionChoice
179: .getText());
180: variantcontrol.appendChild(variant);
181:
182: doc.appendChild(variantcontrol);
183:
184: // ///////////////
185: // Output the XML
186:
187: // set up a transformer
188: TransformerFactory transfac = TransformerFactory
189: .newInstance();
190: Transformer trans = transfac.newTransformer();
191: trans.setOutputProperty(
192: OutputKeys.OMIT_XML_DECLARATION, "yes");
193: trans.setOutputProperty(OutputKeys.INDENT, "yes");
194:
195: // create string from xml tree
196: StringWriter sw = new StringWriter();
197:
198: StreamResult result = new StreamResult(sw);
199: DOMSource source = new DOMSource(doc);
200: trans.transform(source, result);
201: xmlString = sw.toString();
202:
203: } catch (Exception e) {
204: //TODO handle Exception
205: System.out.println(e);
206: }
207:
208: try {
209:
210: // x12VarFile =
211: // X12VariantCreationWizardPage.pathOfX12Folder+"/"+X12VariantCreationWizardPage.newX12Name+".txt";
212:
213: FileWriter out = new FileWriter(x12VarFile, true);
214:
215: out.write(header + "\n" + xmlString);
216: // Close the output stream
217: out.close();
218:
219: } catch (Exception e) {
220: //TODO handle Exception
221: System.out.println(e);
222: }
223:
224: }
225: //TODO remove sysout
226: System.out.println("projectName is " + projectName);
227: ProjectUtil.refreshProject(projectName);
228: }
229:
230: }
|