1 25 package org.ofbiz.datafile; 26 27 28 import java.io.IOException ; 29 import java.net.URL ; 30 import java.util.Collection ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 import javax.xml.parsers.ParserConfigurationException ; 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 ; 42 import org.w3c.dom.Element ; 43 import org.w3c.dom.Node ; 44 import org.w3c.dom.NodeList ; 45 import org.xml.sax.SAXException ; 46 47 48 55 56 public class ModelDataFileReader { 57 58 public static final String module = ModelDataFileReader.class.getName(); 59 60 public static UtilCache readers = new UtilCache("ModelDataFile", 0, 0); 61 62 public URL readerURL = null; 63 public Map modelDataFiles = null; 64 65 public static ModelDataFileReader getModelDataFileReader(URL readerURL) { 66 ModelDataFileReader reader = null; 67 68 reader = (ModelDataFileReader) readers.get(readerURL); 69 if (reader == null) { synchronized (ModelDataFileReader.class) { 71 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 readerURL) { 89 this.readerURL = readerURL; 90 91 getModelDataFiles(); 93 } 94 95 public Map getModelDataFiles() { 96 if (modelDataFiles == null) { synchronized (ModelDataFileReader.class) { 98 if (modelDataFiles == null) { modelDataFiles = new HashMap (); 101 102 UtilTimer utilTimer = new UtilTimer(); 103 104 utilTimer.timerString("Before getDocument in file " + readerURL); 105 Document 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 docElement = document.getDocumentElement(); 114 115 if (docElement == null) { 116 modelDataFiles = null; 117 return null; 118 } 119 docElement.normalize(); 120 Node 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 curDataFile = (Element ) curChild; 130 String dataFileName = UtilXml.checkEmpty(curDataFile.getAttribute("name")); 131 132 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 ModelDataFile dataFile = createModelDataFile(curDataFile); 140 141 if (dataFile != null) { 143 modelDataFiles.put(dataFileName, dataFile); 144 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 165 public ModelDataFile getModelDataFile(String dataFileName) { 166 Map ec = getModelDataFiles(); 167 168 if (ec != null) { 169 return (ModelDataFile) ec.get(dataFileName); 170 } else { 171 return null; 172 } 173 } 174 175 178 public Iterator getDataFileNamesIterator() { 179 Collection collection = getDataFileNames(); 180 181 if (collection != null) { 182 return collection.iterator(); 183 } else { 184 return null; 185 } 186 } 187 188 191 public Collection getDataFileNames() { 192 Map ec = getModelDataFiles(); 193 194 return ec.keySet(); 195 } 196 197 protected ModelDataFile createModelDataFile(Element dataFileElement) { 198 ModelDataFile dataFile = new ModelDataFile(); 199 String 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 rList = dataFileElement.getElementsByTagName("record"); 219 220 for (int i = 0; i < rList.getLength(); i++) { 221 Element recordElement = (Element ) 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 recordElement) { 250 ModelRecord record = new ModelRecord(); 251 String 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 fList = recordElement.getElementsByTagName("field"); 280 int priorEnd = -1; 281 282 for (int i = 0; i < fList.getLength(); i++) { 283 Element fieldElement = (Element ) fList.item(i); 284 ModelField modelField = createModelField(fieldElement); 285 286 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 fieldElement) { 303 ModelField field = new ModelField(); 304 String 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 getDocument(URL url) { 343 if (url == null) 344 return null; 345 Document document = null; 346 347 try { 348 document = UtilXml.readXmlDocument(url); 349 } catch (SAXException sxe) { 350 Exception x = sxe; 352 353 if (sxe.getException() != null) { 354 x = sxe.getException(); 355 } 356 x.printStackTrace(); 357 } catch (ParserConfigurationException pce) { 358 pce.printStackTrace(); 360 } catch (IOException ioe) { 361 ioe.printStackTrace(); 362 } 363 364 return document; 365 } 366 } 367 368 | Popular Tags |