1 25 package org.ofbiz.datafile; 26 27 28 import java.io.ByteArrayInputStream ; 29 import java.io.ByteArrayOutputStream ; 30 import java.io.File ; 31 import java.io.FileNotFoundException ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.io.InputStream ; 35 import java.io.OutputStream ; 36 import java.net.URL ; 37 import java.util.ArrayList ; 38 import java.util.List ; 39 40 import org.ofbiz.base.util.Debug; 41 42 43 50 51 public class DataFile { 52 53 public static final String module = DataFile.class.getName(); 54 55 56 protected List records = new ArrayList (); 57 58 59 protected ModelDataFile modelDataFile; 60 61 68 public static DataFile readFile(URL fileUrl, URL definitionUrl, String dataFileName) throws DataFileException { 69 DataFile dataFile = makeDataFile(definitionUrl, dataFileName); 70 71 dataFile.readDataFile(fileUrl); 72 return dataFile; 73 } 74 75 81 public static DataFile makeDataFile(URL definitionUrl, String dataFileName) throws DataFileException { 82 ModelDataFileReader reader = ModelDataFileReader.getModelDataFileReader(definitionUrl); 83 84 if (reader == null) { 85 throw new DataFileException("Could not load definition file located at \"" + definitionUrl + "\""); 86 } 87 ModelDataFile modelDataFile = reader.getModelDataFile(dataFileName); 88 89 if (modelDataFile == null) { 90 throw new DataFileException("Could not find file definition for data file named \"" + dataFileName + "\""); 91 } 92 DataFile dataFile = new DataFile(modelDataFile); 93 94 return dataFile; 95 } 96 97 100 public DataFile(ModelDataFile modelDataFile) { 101 this.modelDataFile = modelDataFile; 102 } 103 104 protected DataFile() {} 105 106 public ModelDataFile getModelDataFile() { 107 return modelDataFile; 108 } 109 110 public List getRecords() { 111 return records; 112 } 113 114 public void addRecord(Record record) { 115 records.add(record); 116 } 117 118 public Record makeRecord(String recordName) { 119 ModelRecord modelRecord = getModelDataFile().getModelRecord(recordName); 120 return new Record(modelRecord); 121 } 122 123 127 public void readDataFile(URL fileUrl) throws DataFileException { 128 if (fileUrl == null) { 129 throw new IllegalStateException ("File URL is null, cannot load file"); 130 } 131 132 RecordIterator recordIterator = this.makeRecordIterator(fileUrl); 133 while (recordIterator.hasNext()) { 134 this.records.add(recordIterator.next()); 135 } 136 } 138 139 143 public void readDataFile(String content) throws DataFileException { 144 if (content == null || content.length() <= 0) 145 throw new IllegalStateException ("Content is empty, can't read file"); 146 147 ByteArrayInputStream bis = new ByteArrayInputStream (content.getBytes()); 148 149 readDataFile(bis, null); 150 } 151 152 157 public void readDataFile(InputStream dataFileStream, String locationInfo) throws DataFileException { 158 if (modelDataFile == null) { 159 throw new IllegalStateException ("DataFile model is null, cannot load file"); 160 } 161 if (locationInfo == null) { 162 locationInfo = "unknown"; 163 } 164 165 RecordIterator recordIterator = this.makeRecordIterator(dataFileStream, locationInfo); 166 while (recordIterator.hasNext()) { 167 this.records.add(recordIterator.next()); 168 } 169 } 171 172 public RecordIterator makeRecordIterator(URL fileUrl) throws DataFileException { 173 return new RecordIterator(fileUrl, this.modelDataFile); 174 } 175 176 public RecordIterator makeRecordIterator(InputStream dataFileStream, String locationInfo) throws DataFileException { 177 return new RecordIterator(dataFileStream, this.modelDataFile, locationInfo); 178 } 179 180 184 public void writeDataFile(String filename) throws DataFileException { 185 File outFile = new File (filename); 186 FileOutputStream fos = null; 187 188 try { 189 fos = new FileOutputStream (outFile); 190 } catch (FileNotFoundException e) { 191 throw new DataFileException("Could not open file " + filename, e); 192 } 193 194 try { 195 writeDataFile(fos); 196 } finally { 197 try { 198 if (fos != null) 199 fos.close(); 200 } catch (IOException e) { 201 throw new DataFileException("Could not close file " + filename + ", may not have written correctly;", e); 202 } 203 } 204 } 205 206 210 public String writeDataFile() throws DataFileException { 211 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 212 213 writeDataFile(bos); 214 String outString = bos.toString(); 215 216 try { 217 if (bos != null) 218 bos.close(); 219 } catch (IOException e) { 220 Debug.logWarning(e, module); 221 } 222 return outString; 223 } 224 225 229 public void writeDataFile(OutputStream outStream) throws DataFileException { 230 writeRecords(outStream, this.records); 231 } 232 233 protected void writeRecords(OutputStream outStream, List records) throws DataFileException { 234 for (int r = 0; r < records.size(); r++) { 235 Record record = (Record) records.get(r); 236 String line = record.writeLineString(modelDataFile); 237 238 try { 239 outStream.write(line.getBytes()); 240 } catch (IOException e) { 241 throw new DataFileException("Could not write to stream;", e); 242 } 243 244 if (record.getChildRecords() != null && record.getChildRecords().size() > 0) { 245 writeRecords(outStream, record.getChildRecords()); 246 } 247 } 248 } 249 } 250 251 | Popular Tags |