1 /** 2 The contents of this file are subject to the Mozilla Public License Version 1.1 3 (the "License"); you may not use this file except in compliance with the License. 4 You may obtain a copy of the License at http://www.mozilla.org/MPL/ 5 Software distributed under the License is distributed on an "AS IS" basis, 6 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 7 specific language governing rights and limitations under the License. 8 9 The Original Code is "FileGenerator.java". Description: 10 "This class writes a GeneratedClass object to a file" 11 12 The Initial Developer of the Original Code is University Health Network. Copyright (C) 13 2001. All Rights Reserved. 14 15 Contributor(s): James Agnew 16 Paul Brohman 17 Mitch Delachevrotiere 18 Shawn Dyck 19 Cory Metcalf 20 21 Alternatively, the contents of this file may be used under the terms of the 22 GNU General Public License (the ?GPL?), in which case the provisions of the GPL are 23 applicable instead of those above. If you wish to allow use of your version of this 24 file only under the terms of the GPL and not to allow others to use your version 25 of this file under the MPL, indicate your decision by deleting the provisions above 26 and replace them with the notice and other provisions required by the GPL License. 27 If you do not delete the provisions above, a recipient may use your version of 28 this file under either the MPL or the GPL. 29 30 */ 31 package ca.uhn.hl7v2.conf.classes.generator.genclasses; 32 33 import java.io.*; 34 35 /** This class writes a GeneratedClass object to a file 36 * @author <table><tr>James Agnew</tr> 37 * <tr>Paul Brohman</tr> 38 * <tr>Mitch Delachevrotiere</tr> 39 * <tr>Shawn Dyck</tr> 40 * <tr>Cory Metcalf</tr></table> 41 */ 42 public class FileGenerator { 43 private final String basePath; 44 45 /** Initialize member variable setting the base file path 46 * @param basePath representing base path location 47 */ 48 public FileGenerator(String basePath) { 49 // make sure there is a \ appended to ouput director path 50 System.out.println(basePath); 51 this.basePath = basePath.replace('\\', '/'); 52 if ((basePath.charAt(basePath.length() - 1)) != '/') { 53 } 54 55 } 56 57 /** This method returns the Base Path 58 * @return the Base Path 59 */ 60 public String getBasePath(){ 61 return basePath; 62 } 63 64 /** this method writes the GeneratedClass object to disk 65 * @param gc the object to be written to disk 66 * @param packageName representing the packageName 67 * @param fileName representing the file name 68 * @exception IOException if unable to create file 69 */ 70 public void storeFile(GeneratedClass gc, String packageName, String fileName) throws IOException { 71 72 //format package name 73 packageName = packageName.replace('.', '/'); 74 75 // set the file path 76 fileName = fileName.replaceAll(" ", "") + ".java"; 77 String filePath = basePath + "/" + packageName + "/" + fileName; 78 File f = new File(filePath); 79 StringBuilder dir = new StringBuilder(); 80 81 //check if file exist 82 // TODO: Reactivate this once everything works! 83 //if(f.exists()) 84 // throw new IOException("File already exists"); 85 86 //create subfolders 87 int i = 0; 88 while (i < filePath.length()) { 89 if (filePath.charAt(i) != '/') { 90 dir.append(filePath.charAt(i)); 91 } else { 92 dir.append(filePath.charAt(i)); 93 File d = new File(dir.toString()); 94 d.mkdir(); 95 } 96 ++i; 97 } 98 99 FileOutputStream fstream = new FileOutputStream(f); /* open file stream */ 100 DataOutputStream ostream = new DataOutputStream(fstream); /* open object stream */ 101 ostream.writeBytes(gc.toString()); 102 103 /* clean-up */ 104 ostream.flush(); 105 fstream.close(); 106 } 107 }