KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ModelDataFileReader.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.IOException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.xml.parsers.ParserConfigurationException JavaDoc;
36
37 import org.ofbiz.base.util.Debug;
38 import org.ofbiz.base.util.UtilTimer;
39 import org.ofbiz.base.util.UtilXml;
40 import org.ofbiz.base.util.cache.UtilCache;
41 import org.w3c.dom.Document JavaDoc;
42 import org.w3c.dom.Element JavaDoc;
43 import org.w3c.dom.Node JavaDoc;
44 import org.w3c.dom.NodeList JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46
47
48 /**
49  * Flat File definition reader
50  *
51  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
52  * @version $Rev: 5462 $
53  * @since 2.0
54  */

55
56 public class ModelDataFileReader {
57     
58     public static final String JavaDoc module = ModelDataFileReader.class.getName();
59
60     public static UtilCache readers = new UtilCache("ModelDataFile", 0, 0);
61
62     public URL JavaDoc readerURL = null;
63     public Map JavaDoc modelDataFiles = null;
64
65     public static ModelDataFileReader getModelDataFileReader(URL JavaDoc readerURL) {
66         ModelDataFileReader reader = null;
67
68         reader = (ModelDataFileReader) readers.get(readerURL);
69         if (reader == null) { // don't want to block here
70
synchronized (ModelDataFileReader.class) {
71                 // must check if null again as one of the blocked threads can still enter
72
reader = (ModelDataFileReader) readers.get(readerURL);
73                 if (reader == null) {
74                     if (Debug.infoOn()) Debug.logInfo("[ModelDataFileReader.getModelDataFileReader] : creating reader.", module);
75                     reader = new ModelDataFileReader(readerURL);
76                     readers.put(readerURL, reader);
77                 }
78             }
79         }
80         if (reader != null && (reader.modelDataFiles == null || reader.modelDataFiles.size() == 0)) {
81             readers.remove(readerURL);
82             return null;
83         }
84         if (Debug.infoOn()) Debug.logInfo("[ModelDataFileReader.getModelDataFileReader] : returning reader.", module);
85         return reader;
86     }
87
88     public ModelDataFileReader(URL JavaDoc readerURL) {
89         this.readerURL = readerURL;
90
91         // preload models...
92
getModelDataFiles();
93     }
94
95     public Map JavaDoc getModelDataFiles() {
96         if (modelDataFiles == null) { // don't want to block here
97
synchronized (ModelDataFileReader.class) {
98                 // must check if null again as one of the blocked threads can still enter
99
if (modelDataFiles == null) { // now it's safe
100
modelDataFiles = new HashMap JavaDoc();
101
102                     UtilTimer utilTimer = new UtilTimer();
103
104                     utilTimer.timerString("Before getDocument in file " + readerURL);
105                     Document JavaDoc document = getDocument(readerURL);
106
107                     if (document == null) {
108                         modelDataFiles = null;
109                         return null;
110                     }
111
112                     utilTimer.timerString("Before getDocumentElement in file " + readerURL);
113                     Element JavaDoc docElement = document.getDocumentElement();
114
115                     if (docElement == null) {
116                         modelDataFiles = null;
117                         return null;
118                     }
119                     docElement.normalize();
120                     Node JavaDoc curChild = docElement.getFirstChild();
121
122                     int i = 0;
123
124                     if (curChild != null) {
125                         utilTimer.timerString("Before start of dataFile loop in file " + readerURL);
126                         do {
127                             if (curChild.getNodeType() == Node.ELEMENT_NODE && "data-file".equals(curChild.getNodeName())) {
128                                 i++;
129                                 Element JavaDoc curDataFile = (Element JavaDoc) curChild;
130                                 String JavaDoc dataFileName = UtilXml.checkEmpty(curDataFile.getAttribute("name"));
131
132                                 // check to see if dataFile with same name has already been read
133
if (modelDataFiles.containsKey(dataFileName)) {
134                                     Debug.logWarning("WARNING: DataFile " + dataFileName +
135                                         " is defined more than once, most recent will over-write previous definition(s)", module);
136                                 }
137
138                                 // utilTimer.timerString(" After dataFileName -- " + i + " --");
139
ModelDataFile dataFile = createModelDataFile(curDataFile);
140
141                                 // utilTimer.timerString(" After createModelDataFile -- " + i + " --");
142
if (dataFile != null) {
143                                     modelDataFiles.put(dataFileName, dataFile);
144                                     // utilTimer.timerString(" After modelDataFiles.put -- " + i + " --");
145
if (Debug.infoOn()) Debug.logInfo("-- getModelDataFile: #" + i + " Loaded dataFile: " + dataFileName, module);
146                                 } else
147                                     Debug.logWarning("-- -- SERVICE ERROR:getModelDataFile: Could not create dataFile for dataFileName: " + dataFileName, module);
148
149                             }
150                         } while ((curChild = curChild.getNextSibling()) != null);
151                     } else {
152                         Debug.logWarning("No child nodes found.", module);
153                     }
154                     utilTimer.timerString("Finished file " + readerURL + " - Total Flat File Defs: " + i + " FINISHED");
155                 }
156             }
157         }
158         return modelDataFiles;
159     }
160
161     /** Gets an DataFile object based on a definition from the specified XML DataFile descriptor file.
162      * @param dataFileName The dataFileName of the DataFile definition to use.
163      * @return An DataFile object describing the specified dataFile of the specified descriptor file.
164      */

165     public ModelDataFile getModelDataFile(String JavaDoc dataFileName) {
166         Map JavaDoc ec = getModelDataFiles();
167
168         if (ec != null) {
169             return (ModelDataFile) ec.get(dataFileName);
170         } else {
171             return null;
172         }
173     }
174
175     /** Creates a Iterator with the dataFileName of each DataFile defined in the specified XML DataFile Descriptor file.
176      * @return A Iterator of dataFileName Strings
177      */

178     public Iterator JavaDoc getDataFileNamesIterator() {
179         Collection JavaDoc collection = getDataFileNames();
180
181         if (collection != null) {
182             return collection.iterator();
183         } else {
184             return null;
185         }
186     }
187
188     /** Creates a Collection with the dataFileName of each DataFile defined in the specified XML DataFile Descriptor file.
189      * @return A Collection of dataFileName Strings
190      */

191     public Collection JavaDoc getDataFileNames() {
192         Map JavaDoc ec = getModelDataFiles();
193
194         return ec.keySet();
195     }
196
197     protected ModelDataFile createModelDataFile(Element JavaDoc dataFileElement) {
198         ModelDataFile dataFile = new ModelDataFile();
199         String JavaDoc tempStr;
200
201         dataFile.name = UtilXml.checkEmpty(dataFileElement.getAttribute("name"));
202         dataFile.typeCode = UtilXml.checkEmpty(dataFileElement.getAttribute("type-code"));
203         dataFile.sender = UtilXml.checkEmpty(dataFileElement.getAttribute("sender"));
204         dataFile.receiver = UtilXml.checkEmpty(dataFileElement.getAttribute("receiver"));
205
206         tempStr = UtilXml.checkEmpty(dataFileElement.getAttribute("record-length"));
207         if (tempStr != null && tempStr.length() > 0) {
208             dataFile.recordLength = Integer.parseInt(tempStr);
209         }
210         tempStr = UtilXml.checkEmpty(dataFileElement.getAttribute("delimiter"));
211         if (tempStr != null && tempStr.length() == 1) {
212             dataFile.delimiter = tempStr.charAt(0);
213         }
214
215         dataFile.separatorStyle = UtilXml.checkEmpty(dataFileElement.getAttribute("separator-style"));
216         dataFile.description = UtilXml.checkEmpty(dataFileElement.getAttribute("description"));
217
218         NodeList JavaDoc rList = dataFileElement.getElementsByTagName("record");
219
220         for (int i = 0; i < rList.getLength(); i++) {
221             Element JavaDoc recordElement = (Element JavaDoc) rList.item(i);
222             ModelRecord modelRecord = createModelRecord(recordElement);
223
224             if (modelRecord != null) {
225                 dataFile.records.add(modelRecord);
226             } else {
227                 Debug.logWarning("[ModelDataFileReader.createModelDataFile] Weird, modelRecord was null", module);
228             }
229         }
230
231         for (int i = 0; i < dataFile.records.size(); i++) {
232             ModelRecord modelRecord = (ModelRecord) dataFile.records.get(i);
233
234             if (modelRecord.parentName.length() > 0) {
235                 ModelRecord parentRecord = dataFile.getModelRecord(modelRecord.parentName);
236
237                 if (parentRecord != null) {
238                     parentRecord.childRecords.add(modelRecord);
239                     modelRecord.parentRecord = parentRecord;
240                 } else {
241                     Debug.logError("[ModelDataFileReader.createModelDataFile] ERROR: Could not find parentRecord with name " + modelRecord.parentName, module);
242                 }
243             }
244         }
245
246         return dataFile;
247     }
248
249     protected ModelRecord createModelRecord(Element JavaDoc recordElement) {
250         ModelRecord record = new ModelRecord();
251         String JavaDoc tempStr;
252
253         record.name = UtilXml.checkEmpty(recordElement.getAttribute("name"));
254         record.typeCode = UtilXml.checkEmpty(recordElement.getAttribute("type-code"));
255
256         record.tcMin = UtilXml.checkEmpty(recordElement.getAttribute("tc-min"));
257         if (record.tcMin.length() > 0) record.tcMinNum = Long.parseLong(record.tcMin);
258         record.tcMax = UtilXml.checkEmpty(recordElement.getAttribute("tc-max"));
259         if (record.tcMax.length() > 0) record.tcMaxNum = Long.parseLong(record.tcMax);
260
261         tempStr = UtilXml.checkEmpty(recordElement.getAttribute("tc-isnum"));
262         if (tempStr != null && tempStr.length() > 0) {
263             record.tcIsNum = Boolean.valueOf(tempStr).booleanValue();
264         }
265
266         tempStr = UtilXml.checkEmpty(recordElement.getAttribute("tc-position"));
267         if (tempStr != null && tempStr.length() > 0) {
268             record.tcPosition = Integer.parseInt(tempStr);
269         }
270         tempStr = UtilXml.checkEmpty(recordElement.getAttribute("tc-length"));
271         if (tempStr != null && tempStr.length() > 0) {
272             record.tcLength = Integer.parseInt(tempStr);
273         }
274
275         record.description = UtilXml.checkEmpty(recordElement.getAttribute("description"));
276         record.parentName = UtilXml.checkEmpty(recordElement.getAttribute("parent-name"));
277         record.limit = UtilXml.checkEmpty(recordElement.getAttribute("limit"));
278
279         NodeList JavaDoc fList = recordElement.getElementsByTagName("field");
280         int priorEnd = -1;
281
282         for (int i = 0; i < fList.getLength(); i++) {
283             Element JavaDoc fieldElement = (Element JavaDoc) fList.item(i);
284             ModelField modelField = createModelField(fieldElement);
285
286             // if the position is not specified, assume the start position based on last entry
287
if ((i > 0) && (modelField.position == -1)) {
288                 modelField.position = priorEnd;
289             }
290             priorEnd = modelField.position + modelField.length;
291
292             if (modelField != null) {
293                 record.fields.add(modelField);
294             } else {
295                 Debug.logWarning("[ModelDataFileReader.createModelRecord] Weird, modelField was null", module);
296             }
297         }
298
299         return record;
300     }
301
302     protected ModelField createModelField(Element JavaDoc fieldElement) {
303         ModelField field = new ModelField();
304         String JavaDoc tempStr;
305
306         field.name = UtilXml.checkEmpty(fieldElement.getAttribute("name"));
307
308         tempStr = UtilXml.checkEmpty(fieldElement.getAttribute("position"));
309         if (tempStr != null && tempStr.length() > 0) {
310             field.position = Integer.parseInt(tempStr);
311         }
312         tempStr = UtilXml.checkEmpty(fieldElement.getAttribute("length"));
313         if (tempStr != null && tempStr.length() > 0) {
314             field.length = Integer.parseInt(tempStr);
315         }
316
317         field.type = UtilXml.checkEmpty(fieldElement.getAttribute("type"));
318         field.format = UtilXml.checkEmpty(fieldElement.getAttribute("format"));
319         field.validExp = UtilXml.checkEmpty(fieldElement.getAttribute("valid-exp"));
320         field.description = UtilXml.checkEmpty(fieldElement.getAttribute("description"));
321         field.defaultValue = UtilXml.checkEmpty(fieldElement.getAttribute("default-value"));
322         field.refField = UtilXml.checkEmpty(fieldElement.getAttribute("ref-field"));
323         
324         tempStr = UtilXml.checkEmpty(fieldElement.getAttribute("prim-key"));
325         if (tempStr != null && tempStr.length() > 0) {
326             field.isPk = Boolean.valueOf(tempStr).booleanValue();
327         }
328
329         tempStr = UtilXml.checkEmpty(fieldElement.getAttribute("ignored"));
330         if (tempStr != null && tempStr.length() > 0) {
331             field.ignored = Boolean.valueOf(tempStr).booleanValue();
332         }
333
334         tempStr = UtilXml.checkEmpty(fieldElement.getAttribute("expression"));
335         if (tempStr != null && tempStr.length() > 0) {
336             field.expression = Boolean.valueOf(tempStr).booleanValue();
337         }
338
339         return field;
340     }
341
342     protected Document JavaDoc getDocument(URL JavaDoc url) {
343         if (url == null)
344             return null;
345         Document JavaDoc document = null;
346
347         try {
348             document = UtilXml.readXmlDocument(url);
349         } catch (SAXException JavaDoc sxe) {
350             // Error generated during parsing)
351
Exception JavaDoc x = sxe;
352
353             if (sxe.getException() != null) {
354                 x = sxe.getException();
355             }
356             x.printStackTrace();
357         } catch (ParserConfigurationException JavaDoc pce) {
358             // Parser with specified options can't be built
359
pce.printStackTrace();
360         } catch (IOException JavaDoc ioe) {
361             ioe.printStackTrace();
362         }
363
364         return document;
365     }
366 }
367
368
Popular Tags