KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > datafile > DataFile


1 /*
2  * $Id: DataFile.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.datafile;
26
27
28 import java.io.ByteArrayInputStream JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.OutputStream JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.List JavaDoc;
39
40 import org.ofbiz.base.util.Debug;
41
42
43 /**
44  * DataFile main class
45  *
46  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
47  * @version $Rev: 5462 $
48  * @since 2.0
49  */

50
51 public class DataFile {
52     
53     public static final String JavaDoc module = DataFile.class.getName();
54
55     /** List of record in the file, contains Record objects */
56     protected List JavaDoc records = new ArrayList JavaDoc();
57
58     /** Contains the definition for the file */
59     protected ModelDataFile modelDataFile;
60
61     /** Creates a DataFile object which will contain the parsed objects for the specified datafile, using the specified definition.
62      * @param fileUrl The URL where the data file is located
63      * @param definitionUrl The location of the data file definition XML file
64      * @param dataFileName The data file model name, as specified in the definition XML file
65      * @throws DataFileException Exception thown for various errors, generally has a nested exception
66      * @return A new DataFile object with the specified file pre-loaded
67      */

68     public static DataFile readFile(URL JavaDoc fileUrl, URL JavaDoc definitionUrl, String JavaDoc dataFileName) throws DataFileException {
69         DataFile dataFile = makeDataFile(definitionUrl, dataFileName);
70
71         dataFile.readDataFile(fileUrl);
72         return dataFile;
73     }
74
75     /** Creates a DataFile object using the specified definition.
76      * @param definitionUrl The location of the data file definition XML file
77      * @param dataFileName The data file model name, as specified in the definition XML file
78      * @throws DataFileException Exception thown for various errors, generally has a nested exception
79      * @return A new DataFile object
80      */

81     public static DataFile makeDataFile(URL JavaDoc definitionUrl, String JavaDoc 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     /** Construct a DataFile object setting the model, does not load it
98      * @param modelDataFile The model of the DataFile to instantiate
99      */

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 JavaDoc getRecords() {
111         return records;
112     }
113
114     public void addRecord(Record record) {
115         records.add(record);
116     }
117
118     public Record makeRecord(String JavaDoc recordName) {
119         ModelRecord modelRecord = getModelDataFile().getModelRecord(recordName);
120         return new Record(modelRecord);
121     }
122
123     /** Loads (or reloads) the data file at the pre-specified location.
124      * @param fileUrl The URL that the file will be loaded from
125      * @throws DataFileException Exception thown for various errors, generally has a nested exception
126      */

127     public void readDataFile(URL JavaDoc fileUrl) throws DataFileException {
128         if (fileUrl == null) {
129             throw new IllegalStateException JavaDoc("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         // no need to manually close the stream since we are reading to the end of the file: recordIterator.close();
137
}
138
139     /** Populates (or reloads) the data file with the text of the given content
140      * @param content The text data to populate the DataFile with
141      * @throws DataFileException Exception thown for various errors, generally has a nested exception
142      */

143     public void readDataFile(String JavaDoc content) throws DataFileException {
144         if (content == null || content.length() <= 0)
145             throw new IllegalStateException JavaDoc("Content is empty, can't read file");
146
147         ByteArrayInputStream JavaDoc bis = new ByteArrayInputStream JavaDoc(content.getBytes());
148
149         readDataFile(bis, null);
150     }
151
152     /** Loads (or reloads) the data file from the given stream
153      * @param dataFileStream A stream containing the text data for the data file
154      * @param locationInfo Text information about where the data came from for exception messages
155      * @throws DataFileException Exception thown for various errors, generally has a nested exception
156      */

157     public void readDataFile(InputStream JavaDoc dataFileStream, String JavaDoc locationInfo) throws DataFileException {
158         if (modelDataFile == null) {
159             throw new IllegalStateException JavaDoc("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         // no need to manually close the stream since we are reading to the end of the file: recordIterator.close();
170
}
171     
172     public RecordIterator makeRecordIterator(URL JavaDoc fileUrl) throws DataFileException {
173         return new RecordIterator(fileUrl, this.modelDataFile);
174     }
175
176     public RecordIterator makeRecordIterator(InputStream JavaDoc dataFileStream, String JavaDoc locationInfo) throws DataFileException {
177         return new RecordIterator(dataFileStream, this.modelDataFile, locationInfo);
178     }
179
180     /** Writes the records in this DataFile object to a text data file
181      * @param filename The filename to put the data into
182      * @throws DataFileException Exception thown for various errors, generally has a nested exception
183      */

184     public void writeDataFile(String JavaDoc filename) throws DataFileException {
185         File JavaDoc outFile = new File JavaDoc(filename);
186         FileOutputStream JavaDoc fos = null;
187
188         try {
189             fos = new FileOutputStream JavaDoc(outFile);
190         } catch (FileNotFoundException JavaDoc 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 JavaDoc e) {
201                 throw new DataFileException("Could not close file " + filename + ", may not have written correctly;", e);
202             }
203         }
204     }
205
206     /** Returns the records in this DataFile object as a plain text data file content
207      * @throws DataFileException Exception thown for various errors, generally has a nested exception
208      * @return A String containing what would go into a data file as plain text
209      */

210     public String JavaDoc writeDataFile() throws DataFileException {
211         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
212
213         writeDataFile(bos);
214         String JavaDoc outString = bos.toString();
215
216         try {
217             if (bos != null)
218                 bos.close();
219         } catch (IOException JavaDoc e) {
220             Debug.logWarning(e, module);
221         }
222         return outString;
223     }
224
225     /** Writes the records in this DataFile object to the given OutputStream
226      * @param outStream The Stream to put the data into
227      * @throws DataFileException Exception thown for various errors, generally has a nested exception
228      */

229     public void writeDataFile(OutputStream JavaDoc outStream) throws DataFileException {
230         writeRecords(outStream, this.records);
231     }
232
233     protected void writeRecords(OutputStream JavaDoc outStream, List JavaDoc records) throws DataFileException {
234         for (int r = 0; r < records.size(); r++) {
235             Record record = (Record) records.get(r);
236             String JavaDoc line = record.writeLineString(modelDataFile);
237
238             try {
239                 outStream.write(line.getBytes());
240             } catch (IOException JavaDoc 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