001: package com.bostechcorp.cbesb.ui.ide.wizards;
002:
003: import java.io.BufferedInputStream;
004: import java.io.DataInputStream;
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.FileWriter;
008: import java.io.StringWriter;
009:
010: import javax.xml.parsers.DocumentBuilder;
011: import javax.xml.parsers.DocumentBuilderFactory;
012: import javax.xml.transform.OutputKeys;
013: import javax.xml.transform.Transformer;
014: import javax.xml.transform.TransformerFactory;
015: import javax.xml.transform.dom.DOMSource;
016: import javax.xml.transform.stream.StreamResult;
017:
018: import org.eclipse.core.resources.IProject;
019: import org.eclipse.core.resources.ResourcesPlugin;
020: import org.eclipse.jface.viewers.IStructuredSelection;
021: import org.eclipse.ui.IWorkbench;
022: import org.w3c.dom.Document;
023: import org.w3c.dom.Element;
024:
025: import com.bostechcorp.cbesb.common.i18n.I18N;
026: import com.bostechcorp.cbesb.common.i18n.Messages;
027: import com.bostechcorp.cbesb.ui.ide.Activator;
028: import com.bostechcorp.cbesb.ui.util.ProjectUtil;
029: import com.bostechcorp.cbesb.ui.util.variant.VariantCreation;
030:
031: public class HL7VariantCreationWizard extends VariantCreationWizard {
032:
033: String xmlString = null;
034:
035: String HL7VarFile = null;
036:
037: String header = null;
038:
039: String projectName = null;
040:
041: int index = 1;
042:
043: private VariantCreationWizardPage mainPage;
044:
045: /**
046: * (non-Javadoc) Method declared on Wizard.
047: */
048: public void addPages() {
049: mainPage = new HL7VariantCreationWizardPage(
050: "NewHL7VariantWizard", workbench, selection);
051: addPage(mainPage);
052:
053: }
054:
055: /**
056: * (non-Javadoc) Method declared on IWorkbenchWizard
057: */
058: public void init(IWorkbench workbench,
059: IStructuredSelection selection) {
060:
061: super .init(workbench, selection);
062: setWindowTitle(I18N.getString(Messages.IDE_HL7_TITLE));// "New HL7
063: // variant");
064: // //$NON-NLS-1$
065: setDefaultPageImageDescriptor(Activator
066: .imageDescriptorFromPlugin(Activator.PLUGIN_ID,
067: "icons/full/wizban/openHL7VariantView.gif"));
068: }
069:
070: /**
071: * (non-Javadoc) Method declared on IWizard
072: */
073: public boolean performFinish() {
074:
075: projectName = mainPage.getProjectName();
076:
077: VariantCreation.version_name = HL7VariantCreationWizardPage.versionChoice
078: .getText();
079: VariantCreation.var_name = HL7VariantCreationWizardPage.newVarName;
080: // Create a folder for this HL7 variant
081: String newVariantPath = HL7VariantCreationWizardPage.pathOfFolder
082: + "/"
083: + HL7VariantCreationWizardPage.versionChoice.getText();
084: boolean success = (new File(newVariantPath).mkdir());
085: String varName = newVariantPath + "/"
086: + HL7VariantCreationWizardPage.newVarName;
087: success = (new File(varName).mkdir());
088: String mess = varName + "/"
089: + HL7VariantCreationWizardPage.MESSAGE;
090: success = (new File(mess).mkdir());
091: String seg = varName + "/"
092: + HL7VariantCreationWizardPage.SEGMENT;
093: success = (new File(seg).mkdir());
094:
095: IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
096: .getProjects();
097: ProjectUtil.refreshProject(projectName);
098:
099: return mainPage.finish();
100: }
101:
102: public void dispose() {
103:
104: boolean exist = true;
105: HL7VarFile = HL7VariantCreationWizardPage.pathOfFolder + "/"
106: + HL7VariantCreationWizardPage.newVarName + "."
107: + HL7VariantCreationWizardPage.extension;
108:
109: try {
110: FileInputStream fis = new FileInputStream(HL7VarFile);
111: BufferedInputStream bis = new BufferedInputStream(fis);
112: DataInputStream dis = new DataInputStream(bis);
113: } catch (Exception e) {
114: exist = false;
115:
116: return;
117: }
118:
119: if (exist) {
120:
121: header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
122:
123: try {
124: // ///////////////////////////
125: // Creating an empty XML Document
126:
127: // We need a Document
128: DocumentBuilderFactory dbfac = DocumentBuilderFactory
129: .newInstance();
130: DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
131: Document doc = docBuilder.newDocument();
132:
133: // create the root element and add it to the document
134:
135: // Comment comment = doc.createComment(header);
136: // doc.appendChild(comment);
137:
138: Element variantcontrol = doc
139: .createElement("vcf:variantcontrol");
140: variantcontrol.setAttribute("xmlns:vcf",
141: "http://cbesb.bostechcorp.com/vcf/1.0");
142: variantcontrol.setAttribute("xmlns",
143: "http://www.bostechcorp.com/HL7/demo");
144: variantcontrol.setAttribute("targetNamespace",
145: "http://cbesb.bostechcorp.com/vcf/1.0");
146:
147: Element variant = doc.createElement("vcf:variant");
148: variant.setAttribute("name",
149: HL7VariantCreationWizardPage.newVarName);
150: variant.setAttribute("type", "HL7");
151: variant.setAttribute("version",
152: HL7VariantCreationWizardPage.versionChoice
153: .getText());
154: variantcontrol.appendChild(variant);
155:
156: doc.appendChild(variantcontrol);
157:
158: // ///////////////
159: // Output the XML
160:
161: // set up a transformer
162: TransformerFactory transfac = TransformerFactory
163: .newInstance();
164: Transformer trans = transfac.newTransformer();
165: trans.setOutputProperty(
166: OutputKeys.OMIT_XML_DECLARATION, "yes");
167: trans.setOutputProperty(OutputKeys.INDENT, "yes");
168:
169: // create string from xml tree
170: StringWriter sw = new StringWriter();
171:
172: StreamResult result = new StreamResult(sw);
173: DOMSource source = new DOMSource(doc);
174: trans.transform(source, result);
175: xmlString = sw.toString();
176:
177: } catch (Exception e) {
178: //TODO handle Exception
179: System.out.println(e);
180: }
181:
182: try {
183:
184: // HL7VarFile =
185: // HL7VariantCreationWizardPage.pathOfHL7Folder+"/"+HL7VariantCreationWizardPage.newHL7Name+".txt";
186:
187: FileWriter out = new FileWriter(HL7VarFile, true);
188:
189: out.write(header + "\n" + xmlString);
190: // Close the output stream
191: out.close();
192:
193: } catch (Exception e) {
194: // TODO handle Exception
195: System.out.println(e);
196: }
197:
198: }
199: //TODO remove sysout
200: // System.out.println("projectName is " + projectName);
201: ProjectUtil.refreshProject(projectName);
202: }
203:
204: }
|