Author: mbrohl
Date: Sat Oct 28 14:45:17 2017
New Revision: 1813637
URL: http://svn.apache.org/viewvc?rev=1813637&view=rev
Log:
Improved: Fixing defects reported by FindBugs, package
org.apache.ofbiz.datafile.
(OFBIZ-9715)
I modified the patch slightly and did some additional code cleanup by
removing commented out code and some formatting.
Thanks Julian Leichert for reporting and providing the patch.
Modified:
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/DataFile.java
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/DataFile2EntityXml.java
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFileReader.java
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/Record.java
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java
Modified:
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/DataFile.java
URL:
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/DataFile.java?rev=1813637&r1=1813636&r2=1813637&view=diff
==============================================================================
---
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/DataFile.java
(original)
+++
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/DataFile.java
Sat Oct 28 14:45:17 2017
@@ -18,11 +18,9 @@
*******************************************************************************/
package org.apache.ofbiz.datafile;
-
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -32,9 +30,9 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.ofbiz.base.util.Debug;
+import org.apache.ofbiz.base.util.UtilIO;
import org.apache.ofbiz.base.util.UtilValidate;
-
/**
* DataFile main class
*
@@ -93,7 +91,8 @@ public class DataFile {
this.modelDataFile = modelDataFile;
}
- protected DataFile() {}
+ protected DataFile() {
+ }
public ModelDataFile getModelDataFile() {
return modelDataFile;
@@ -136,7 +135,7 @@ public class DataFile {
if (UtilValidate.isEmpty(content))
throw new IllegalStateException("Content is empty, can't read
file");
- ByteArrayInputStream bis = new
ByteArrayInputStream(content.getBytes());
+ ByteArrayInputStream bis = new
ByteArrayInputStream(content.getBytes(UtilIO.getUtf8()));
readDataFile(bis, null);
}
@@ -169,29 +168,23 @@ public class DataFile {
return new RecordIterator(dataFileStream, this.modelDataFile,
locationInfo);
}
- /** Writes the records in this DataFile object to a text data file
- * @param filename The filename to put the data into
- * @throws DataFileException Exception thown for various errors, generally
has a nested exception
+ /**
+ * Writes the records in this DataFile object to a text data file
+ *
+ * @param filename
+ * The filename to put the data into
+ * @throws DataFileException
+ * Exception thrown for various errors, generally has a nested
+ * exception
*/
public void writeDataFile(String filename) throws DataFileException {
File outFile = new File(filename);
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(outFile);
- } catch (FileNotFoundException e) {
- throw new DataFileException("Could not open file " + filename, e);
- }
-
- try {
+ try (FileOutputStream fos = new FileOutputStream(outFile);) {
writeDataFile(fos);
- } finally {
- try {
- if (fos != null)
- fos.close();
- } catch (IOException e) {
- throw new DataFileException("Could not close file " + filename
+ ", may not have written correctly;", e);
- }
+ }
+ catch (IOException e) {
+ throw new DataFileException("Error occured while writing data to
file" + filename, e);
}
}
@@ -200,15 +193,13 @@ public class DataFile {
* @return A String containing what would go into a data file as plain text
*/
public String writeDataFile() throws DataFileException {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
-
- writeDataFile(bos);
- String outString = bos.toString();
+ String outString = "";
+ try (ByteArrayOutputStream bos = new ByteArrayOutputStream();) {
+ writeDataFile(bos);
+ outString = bos.toString("UTF-8");
- try {
- if (bos != null)
- bos.close();
- } catch (IOException e) {
+ }
+ catch (IOException e) {
Debug.logWarning(e, module);
}
return outString;
@@ -223,12 +214,13 @@ public class DataFile {
}
protected void writeRecords(OutputStream outStream, List<Record> records)
throws DataFileException {
- for (Record record: records) {
+ for (Record record : records) {
String line = record.writeLineString(modelDataFile);
try {
- outStream.write(line.getBytes());
- } catch (IOException e) {
+ outStream.write(line.getBytes(UtilIO.getUtf8()));
+ }
+ catch (IOException e) {
throw new DataFileException("Could not write to stream;", e);
}
@@ -238,4 +230,3 @@ public class DataFile {
}
}
}
-
Modified:
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/DataFile2EntityXml.java
URL:
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/DataFile2EntityXml.java?rev=1813637&r1=1813636&r2=1813637&view=diff
==============================================================================
---
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/DataFile2EntityXml.java
(original)
+++
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/DataFile2EntityXml.java
Sat Oct 28 14:45:17 2017
@@ -22,17 +22,20 @@ package org.apache.ofbiz.datafile;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
-import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
+import org.apache.ofbiz.base.util.Debug;
import org.apache.ofbiz.base.util.UtilFormatOut;
+import org.apache.ofbiz.base.util.UtilIO;
import org.apache.ofbiz.base.util.UtilURL;
import org.apache.ofbiz.base.util.UtilValidate;
public class DataFile2EntityXml {
+ public static final String module = DataFile2EntityXml.class.getName();
+
/** Creates a new instance of DataFile2EntityXml */
public DataFile2EntityXml() {
}
@@ -44,39 +47,32 @@ public class DataFile2EntityXml {
*/
public static void writeToEntityXml(String fileName, DataFile dataFile)
throws DataFileException {
File file = new File(fileName);
- BufferedWriter outFile = null;
- try {
+ try (BufferedWriter outFile = new BufferedWriter(new
OutputStreamWriter(new FileOutputStream(file), UtilIO.getUtf8()));) {
- //outFile = new BufferedWriter(new FileWriter(file));
- outFile = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(file), "UTF-8"));
- } catch (Exception e) {
- throw new DataFileException("Could not open file " + fileName, e);
- }
- //----------------------------------------------------
- try {
outFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outFile.newLine();
outFile.write("<entity-engine-xml>");
outFile.newLine();
- for (Record record: dataFile.getRecords()) {
+ for (Record record : dataFile.getRecords()) {
ModelRecord modelRecord = record.getModelRecord();
outFile.write("<" + modelRecord.name + " ");
- for (ModelField modelField: modelRecord.fields) {
- if (modelField.ignored) continue;
+ for (ModelField modelField : modelRecord.fields) {
+ if (modelField.ignored)
+ continue;
Object value = record.get(modelField.name);
if (value == null) {
value = modelField.defaultValue;
}
if (value instanceof String) {
- value = ((String)value).trim();
- if (((String)value).length() == 0) {
+ value = ((String) value).trim();
+ if (((String) value).length() == 0) {
value = modelField.defaultValue;
}
}
if (value != null) {
if (value instanceof String) {
- outFile.write(modelField.name + "=\"" +
UtilFormatOut.encodeXmlValue((String)value) + "\" ");
+ outFile.write(modelField.name + "=\"" +
UtilFormatOut.encodeXmlValue((String) value) + "\" ");
} else {
outFile.write(modelField.name + "=\"" + value +
"\" ");
}
@@ -86,8 +82,8 @@ public class DataFile2EntityXml {
outFile.newLine();
}
outFile.write("</entity-engine-xml>");
- outFile.close();
- } catch (IOException e) {
+ }
+ catch (IOException e) {
throw new DataFileException("Error writing to file " + fileName,
e);
}
@@ -99,49 +95,41 @@ public class DataFile2EntityXml {
String definitionLoc = args[1];
String definitionName = args[2];
- BufferedWriter outFile = new BufferedWriter(new FileWriter(dataFileLoc
+ ".xml"));
-
- URL dataFileUrl = null;
- //try {
- dataFileUrl = UtilURL.fromFilename(dataFileLoc);
- //} catch (java.net.MalformedURLException e) {
- //messages.add(e.getMessage());
- //}
- URL definitionUrl = null;
- //try {
- definitionUrl = UtilURL.fromFilename(definitionLoc);
- //} catch (java.net.MalformedURLException e) {
- //messages.add(e.getMessage());
- //}
-
- DataFile dataFile = null;
- if (dataFileUrl != null && definitionUrl != null &&
UtilValidate.isNotEmpty(definitionName)) {
- try {
- dataFile = DataFile.readFile(dataFileUrl, definitionUrl,
definitionName);
- } catch (Exception e) {
- //messages.add(e.toString());
- //Debug.logInfo(e);
+ try (BufferedWriter outFile = new BufferedWriter(new
OutputStreamWriter(new FileOutputStream(dataFileLoc + ".xml"),
UtilIO.getUtf8()));) {
+ URL dataFileUrl = UtilURL.fromFilename(dataFileLoc);
+ URL definitionUrl = UtilURL.fromFilename(definitionLoc);
+
+ DataFile dataFile = null;
+ if (dataFileUrl != null && definitionUrl != null &&
UtilValidate.isNotEmpty(definitionName)) {
+ try {
+ dataFile = DataFile.readFile(dataFileUrl, definitionUrl,
definitionName);
+ }
+ catch (DataFileException e) {
+ Debug.logError("Error Occurred while reading Datafile,
Exception: " + e, module);
+ }
}
- }
- // -----------------------------------------
- for (Record record: dataFile.getRecords()) {
- ModelRecord modelRecord = record.getModelRecord();
- outFile.write("<" + modelRecord.name + " ");
- for (ModelField modelField: modelRecord.fields) {
- Object value = record.get(modelField.name);
- if (value instanceof String) {
- value = ((String)value).trim();
- outFile.write(modelField.name + "=\"" +
UtilFormatOut.encodeXmlValue((String)value) + "\" ");
- } else {
- outFile.write(modelField.name + "=\"" + value + "\" ");
+ if (dataFile != null) {
+ for (Record record : dataFile.getRecords()) {
+ ModelRecord modelRecord = record.getModelRecord();
+ outFile.write("<" + modelRecord.name + " ");
+ for (ModelField modelField : modelRecord.fields) {
+ Object value = record.get(modelField.name);
+ if (value instanceof String) {
+ value = ((String) value).trim();
+ outFile.write(modelField.name + "=\"" +
UtilFormatOut.encodeXmlValue((String) value) + "\" ");
+ } else {
+ outFile.write(modelField.name + "=\"" + value +
"\" ");
+ }
+ }
+ outFile.write("/>");
+ outFile.newLine();
}
}
- outFile.write("/>");
- outFile.newLine();
}
- outFile.close();
-
+ catch (IOException e) {
+ Debug.logError(e, module);
+ }
}
}
Modified:
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFileReader.java
URL:
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFileReader.java?rev=1813637&r1=1813636&r2=1813637&view=diff
==============================================================================
---
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFileReader.java
(original)
+++
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFileReader.java
Sat Oct 28 14:45:17 2017
@@ -95,12 +95,7 @@ public final class ModelDataFileReader {
for (int i = 0; i < rList.getLength(); i++) {
Element recordElement = (Element) rList.item(i);
ModelRecord modelRecord = createModelRecord(recordElement);
-
- if (modelRecord != null) {
- dataFile.records.add(modelRecord);
- } else {
- Debug.logWarning("[ModelDataFileReader.createModelDataFile]
Weird, modelRecord was null", module);
- }
+ dataFile.records.add(modelRecord);
}
for (ModelRecord modelRecord : dataFile.records) {
@@ -149,14 +144,9 @@ public final class ModelDataFileReader {
Debug.logWarning("DataFile " + dataFileName + " is defined
more than once, most recent will over-write previous definition(s)", module);
}
ModelDataFile dataFile = createModelDataFile(curDataFile);
- if (dataFile != null) {
- result.put(dataFileName, dataFile);
- if (Debug.verboseOn()) {
- Debug.logVerbose("Loaded dataFile: " + dataFileName,
module);
- }
- } else {
- Debug.logWarning("Could not create dataFile for dataFileName "
+ dataFileName, module);
- throw new DataFileException("Could not create dataFile for " +
dataFileName + " defined in " + this.readerURL);
+ result.put(dataFileName, dataFile);
+ if (Debug.verboseOn()) {
+ Debug.logVerbose("Loaded dataFile: " + dataFileName, module);
}
}
return result;
Modified:
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/Record.java
URL:
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/Record.java?rev=1813637&r1=1813636&r2=1813637&view=diff
==============================================================================
---
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/Record.java
(original)
+++
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/Record.java
Sat Oct 28 14:45:17 2017
@@ -31,6 +31,7 @@ import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import org.apache.ofbiz.base.crypto.HashCrypt;
+import org.apache.ofbiz.base.util.UtilIO;
import org.apache.ofbiz.base.util.UtilValidate;
import org.apache.ofbiz.common.login.LoginServices;
@@ -54,89 +55,88 @@ public class Record implements Serializa
/** Creates new Record */
protected Record(ModelRecord modelRecord) {
- if (modelRecord == null)
- throw new IllegalArgumentException("Cannont create a Record with a
null modelRecord parameter");
- this.recordName = modelRecord.name;
- this.modelRecord = modelRecord;
- this.fields = new HashMap<String, Object>();
+ if (modelRecord == null)
+ throw new IllegalArgumentException("Cannont create a Record with a
null modelRecord parameter");
+ this.recordName = modelRecord.name;
+ this.modelRecord = modelRecord;
+ this.fields = new HashMap<String, Object>();
}
/** Creates new Record from existing Map */
protected Record(ModelRecord modelRecord, Map<String, Object> fields) {
- if (modelRecord == null)
- throw new IllegalArgumentException("Cannont create a Record with a
null modelRecord parameter");
- this.recordName = modelRecord.name;
- this.modelRecord = modelRecord;
- this.fields = (fields == null ? new HashMap<String, Object>() : new
HashMap<String, Object>(fields));
+ if (modelRecord == null)
+ throw new IllegalArgumentException("Cannont create a Record with a
null modelRecord parameter");
+ this.recordName = modelRecord.name;
+ this.modelRecord = modelRecord;
+ this.fields = (fields == null ? new HashMap<String, Object>() : new
HashMap<String, Object>(fields));
}
public String getRecordName() {
- return recordName;
+ return recordName;
}
public ModelRecord getModelRecord() {
- if (modelRecord == null) {
- throw new IllegalStateException("[Record.getModelRecord] could not
find modelRecord for recordName " + recordName);
- }
- return modelRecord;
+ if (modelRecord == null) {
+ throw new IllegalStateException("[Record.getModelRecord] could not
find modelRecord for recordName " + recordName);
+ }
+ return modelRecord;
}
- public Object get(String name) {
- if (getModelRecord().getModelField(name) == null) {
- throw new IllegalArgumentException("[Record.get] \"" + name + "\" is
not a field of " + recordName);
- // Debug.logWarning("[GenericRecord.get] \"" + name + "\" is not a
field of " + recordName + ", but getting anyway...", module);
- }
- return fields.get(name);
+ public synchronized Object get(String name) {
+ if (getModelRecord().getModelField(name) == null) {
+ throw new IllegalArgumentException("[Record.get] \"" + name + "\"
is not a field of " + recordName);
+ }
+ return fields.get(name);
}
public String getString(String name) {
- Object object = get(name);
+ Object object = get(name);
- if (object == null)
- return null;
- if (object instanceof java.lang.String)
- return (String) object;
- else
- return object.toString();
+ if (object == null)
+ return null;
+ if (object instanceof java.lang.String)
+ return (String) object;
+ else
+ return object.toString();
}
public String getStringAndEmpty(String name) {
- Object object = get(name);
+ Object object = get(name);
- if (object == null)
- return "";
- if (object instanceof java.lang.String)
- return (String) object;
- else
- return object.toString();
+ if (object == null)
+ return "";
+ if (object instanceof java.lang.String)
+ return (String) object;
+ else
+ return object.toString();
}
public java.sql.Timestamp getTimestamp(String name) {
- return (java.sql.Timestamp) get(name);
+ return (java.sql.Timestamp) get(name);
}
public java.sql.Time getTime(String name) {
- return (java.sql.Time) get(name);
+ return (java.sql.Time) get(name);
}
public java.sql.Date getDate(String name) {
- return (java.sql.Date) get(name);
+ return (java.sql.Date) get(name);
}
public Integer getInteger(String name) {
- return (Integer) get(name);
+ return (Integer) get(name);
}
public Long getLong(String name) {
- return (Long) get(name);
+ return (Long) get(name);
}
public Float getFloat(String name) {
- return (Float) get(name);
+ return (Float) get(name);
}
public Double getDouble(String name) {
- return (Double) get(name);
+ return (Double) get(name);
}
/** Sets the named field to the passed value, even if the value is null
@@ -144,7 +144,7 @@ public class Record implements Serializa
* @param value The value to set
*/
public void set(String name, Object value) {
- set(name, value, true);
+ set(name, value, true);
}
/** Sets the named field to the passed value. If value is null, it is only
@@ -154,52 +154,39 @@ public class Record implements Serializa
* @param setIfNull Specifies whether or not to set the value if it is null
*/
public synchronized void set(String name, Object value, boolean setIfNull)
{
- if (getModelRecord().getModelField(name) == null) {
- throw new IllegalArgumentException("[Record.set] \"" + name + "\" is
not a field of " + recordName);
- // Debug.logWarning("[GenericRecord.set] \"" + name + "\" is not a
field of " + recordName + ", but setting anyway...", module);
- }
- if (value != null || setIfNull) {
- if (value instanceof Boolean) {
- value = ((Boolean) value).booleanValue() ? "Y" : "N";
+ if (getModelRecord().getModelField(name) == null) {
+ throw new IllegalArgumentException("[Record.set] \"" + name + "\"
is not a field of " + recordName);
+ }
+ if (value != null || setIfNull) {
+ if (value instanceof Boolean) {
+ value = ((Boolean) value).booleanValue() ? "Y" : "N";
+ }
+ fields.put(name, value);
}
- fields.put(name, value);
- }
}
/**
* little endian reader for 2 byte short.
*/
public final short readLEShort(byte[] byteArray) {
- return (short)(
- (byteArray[1]&0xff) << 8 |
- (byteArray[0]&0xff));
+ return (short) ((byteArray[1] & 0xff) << 8 | (byteArray[0] & 0xff));
}
/**
* little endian reader for 4 byte int.
*/
- public final int readLEInt(byte []byteArray) {
- return
- (byteArray[3]) << 24 |
- (byteArray[2]&0xff) << 16 |
- (byteArray[1]&0xff) << 8 |
- (byteArray[0]&0xff);
+ public final int readLEInt(byte[] byteArray) {
+ return (byteArray[3]) << 24 | (byteArray[2] & 0xff) << 16 |
(byteArray[1] & 0xff) << 8 | (byteArray[0] & 0xff);
}
/**
* little endian reader for 8 byte long.
*/
- public final long readLELong(byte []byteArray) {
- return
- (long)(byteArray[7]) << 56 | /* long cast needed or shift done
modulo 32 */
- (long)(byteArray[6]&0xff) << 48 |
- (long)(byteArray[5]&0xff) << 40 |
- (long)(byteArray[4]&0xff) << 32 |
- (long)(byteArray[3]&0xff) << 24 |
- (long)(byteArray[2]&0xff) << 16 |
- (long)(byteArray[1]&0xff) << 8 |
- (byteArray[0]&0xff);
+ public final long readLELong(byte[] byteArray) {
+ return (long) (byteArray[7]) << 56 | /* long cast needed or shift done
modulo 32 */
+ (long) (byteArray[6] & 0xff) << 48 | (long) (byteArray[5] &
0xff) << 40 | (long) (byteArray[4] & 0xff) << 32 | (long) (byteArray[3] & 0xff)
<< 24
+ | (long) (byteArray[2] & 0xff) << 16 | (long) (byteArray[1] &
0xff) << 8 | (byteArray[0] & 0xff);
}
/** Sets the named field to the passed value, converting the value from a
String to the current type using <code>Type.valueOf()</code>
@@ -207,272 +194,264 @@ public class Record implements Serializa
* @param value The String value to convert and set
*/
public void setString(String name, String value) throws ParseException {
- if (name == null || value == null || value.equals(""))
- return;
- ModelField field = getModelRecord().getModelField(name);
-
- if (field == null)
- set(name, value); // this will get an error in the set() method...
-
- // if the string is all spaces ignore
- boolean nonSpace = false;
-
- for (int i = 0; i < value.length(); i++) {
- if (value.charAt(i) != ' ') {
- nonSpace = true;
- break;
- }
- }
- if (!nonSpace)
- return;
-
- // if (Debug.verboseOn()) Debug.logVerbose("Value: " + value, module);
-
- String fieldType = field.type;
-
- // first the custom types that need to be parsed
- if (fieldType.equals("CustomTimestamp")) {
- // this custom type will take a string a parse according to date
formatting
- // string then put the result in a java.sql.Timestamp
- // a common timestamp format for flat files is with no separators:
yyyyMMddHHmmss
- SimpleDateFormat sdf = new SimpleDateFormat(field.format);
- java.util.Date tempDate = sdf.parse(value);
- java.sql.Timestamp timestamp = new
java.sql.Timestamp(tempDate.getTime());
-
- set(name, timestamp);
- } else if (fieldType.equals("CustomDate")) {
- // a common date only format for flat files is with no separators:
yyyyMMdd or MMddyyyy
- SimpleDateFormat sdf = new SimpleDateFormat(field.format);
- java.util.Date tempDate = sdf.parse(value);
- java.sql.Date date = new java.sql.Date(tempDate.getTime());
-
- set(name, date);
- } else if (fieldType.equals("CustomTime")) {
- // a common time only format for flat files is with no separators:
HHmmss
- SimpleDateFormat sdf = new SimpleDateFormat(field.format);
- java.util.Date tempDate = sdf.parse(value);
- java.sql.Time time = new java.sql.Time(tempDate.getTime());
-
- set(name, time);
- } else if (fieldType.equals("FixedPointDouble")) {
- // this custom type will parse a fixed point number according to the
number
- // of decimal places in the formatting string then place it in a Double
- NumberFormat nf = NumberFormat.getNumberInstance();
- Number tempNum = nf.parse(value);
- double number = tempNum.doubleValue();
- double decimalPlaces = Double.parseDouble(field.format);
- double divisor = Math.pow(10.0, decimalPlaces);
-
- number = number / divisor;
- set(name, Double.valueOf(number));
- } // standard types
- else if (fieldType.equals("java.lang.String") ||
fieldType.equals("String"))
- if (field.format.equals("EncryptedString")) {
- String hashType = LoginServices.getHashType();
- set(name, HashCrypt.digestHash(hashType, value.getBytes()));
- } else {
- set(name, value);
+ if (name == null || value == null || value.equals(""))
+ return;
+ ModelField field = getModelRecord().getModelField(name);
+
+ if (field == null)
+ set(name, value); // this will get an error in the set() method...
+
+ // if the string is all spaces ignore
+ boolean nonSpace = false;
+
+ for (int i = 0; i < value.length(); i++) {
+ if (value.charAt(i) != ' ') {
+ nonSpace = true;
+ break;
+ }
+ }
+ if (!nonSpace)
+ return;
+
+ String fieldType = field.type;
+
+ // first the custom types that need to be parsed
+ if (fieldType.equals("CustomTimestamp")) {
+ // this custom type will take a string a parse according to date
formatting
+ // string then put the result in a java.sql.Timestamp
+ // a common timestamp format for flat files is with no separators:
yyyyMMddHHmmss
+ SimpleDateFormat sdf = new SimpleDateFormat(field.format);
+ java.util.Date tempDate = sdf.parse(value);
+ java.sql.Timestamp timestamp = new
java.sql.Timestamp(tempDate.getTime());
+
+ set(name, timestamp);
+ } else if (fieldType.equals("CustomDate")) {
+ // a common date only format for flat files is with no separators:
yyyyMMdd or MMddyyyy
+ SimpleDateFormat sdf = new SimpleDateFormat(field.format);
+ java.util.Date tempDate = sdf.parse(value);
+ java.sql.Date date = new java.sql.Date(tempDate.getTime());
+
+ set(name, date);
+ } else if (fieldType.equals("CustomTime")) {
+ // a common time only format for flat files is with no separators:
HHmmss
+ SimpleDateFormat sdf = new SimpleDateFormat(field.format);
+ java.util.Date tempDate = sdf.parse(value);
+ java.sql.Time time = new java.sql.Time(tempDate.getTime());
+
+ set(name, time);
+ } else if (fieldType.equals("FixedPointDouble")) {
+ // this custom type will parse a fixed point number according to
the number
+ // of decimal places in the formatting string then place it in a
Double
+ NumberFormat nf = NumberFormat.getNumberInstance();
+ Number tempNum = nf.parse(value);
+ double number = tempNum.doubleValue();
+ double decimalPlaces = Double.parseDouble(field.format);
+ double divisor = Math.pow(10.0, decimalPlaces);
+
+ number = number / divisor;
+ set(name, Double.valueOf(number));
+ } // standard types
+ else if (fieldType.equals("java.lang.String") ||
fieldType.equals("String"))
+ if (field.format.equals("EncryptedString")) {
+ String hashType = LoginServices.getHashType();
+ set(name, HashCrypt.digestHash(hashType,
value.getBytes(UtilIO.getUtf8())));
+ } else {
+ set(name, value);
+ }
+ else if (fieldType.equals("NullTerminatedString")) {
+ int terminate = value.indexOf(0x0);
+ set(name, terminate > 0 ? value.substring(0, terminate) : value);
+ } else if (fieldType.equals("java.sql.Timestamp") ||
fieldType.equals("Timestamp"))
+ set(name, java.sql.Timestamp.valueOf(value));
+ else if (fieldType.equals("java.sql.Time") || fieldType.equals("Time"))
+ set(name, java.sql.Time.valueOf(value));
+ else if (fieldType.equals("java.sql.Date") || fieldType.equals("Date"))
+ set(name, java.sql.Date.valueOf(value));
+ else if (fieldType.equals("java.lang.Integer") ||
fieldType.equals("Integer"))
+ set(name, Integer.valueOf(value));
+ else if (fieldType.equals("java.lang.Long") ||
fieldType.equals("Long"))
+ set(name, Long.valueOf(value));
+ else if (fieldType.equals("java.lang.Float") ||
fieldType.equals("Float"))
+ set(name, Float.valueOf(value));
+ else if (fieldType.equals("java.lang.Double") ||
fieldType.equals("Double"))
+ set(name, Double.valueOf(value));
+ else if (fieldType.equals("LEShort"))
+ set(name,
Short.valueOf(readLEShort(value.getBytes(UtilIO.getUtf8()))));
+ else if (fieldType.equals("LEInteger"))
+ set(name,
Integer.valueOf(readLEInt(value.getBytes(UtilIO.getUtf8()))));
+ else if (fieldType.equals("LELong"))
+ set(name,
Long.valueOf(readLELong(value.getBytes(UtilIO.getUtf8()))));
+ else {
+ throw new IllegalArgumentException("Field type " + fieldType + "
not currently supported. Sorry.");
}
- else if (fieldType.equals("NullTerminatedString")) {
- int terminate = value.indexOf(0x0);
- set(name, terminate>0?value.substring(0,terminate):value);
- } else if (fieldType.equals("java.sql.Timestamp") ||
fieldType.equals("Timestamp"))
- set(name, java.sql.Timestamp.valueOf(value));
- else if (fieldType.equals("java.sql.Time") || fieldType.equals("Time"))
- set(name, java.sql.Time.valueOf(value));
- else if (fieldType.equals("java.sql.Date") || fieldType.equals("Date"))
- set(name, java.sql.Date.valueOf(value));
- else if (fieldType.equals("java.lang.Integer") ||
fieldType.equals("Integer"))
- set(name, Integer.valueOf(value));
- else if (fieldType.equals("java.lang.Long") || fieldType.equals("Long"))
- set(name, Long.valueOf(value));
- else if (fieldType.equals("java.lang.Float") || fieldType.equals("Float"))
- set(name, Float.valueOf(value));
- else if (fieldType.equals("java.lang.Double") ||
fieldType.equals("Double"))
- set(name, Double.valueOf(value));
- else if (fieldType.equals("LEShort"))
- set(name, Short.valueOf(readLEShort(value.getBytes())));
- else if (fieldType.equals("LEInteger"))
- set(name, Integer.valueOf(readLEInt(value.getBytes())));
- else if (fieldType.equals("LELong"))
- set(name, Long.valueOf(readLELong(value.getBytes())));
- else {
- throw new IllegalArgumentException("Field type " + fieldType + " not
currently supported. Sorry.");
- }
}
public String getFixedString(String name) {
- if (name == null)
- return null;
- if (getModelRecord() == null)
- throw new IllegalArgumentException("Could not find modelrecord for
field named \"" + name + "\"");
- ModelField field = getModelRecord().getModelField(name);
-
- if (field == null)
- throw new IllegalArgumentException("Could not find model for field
named \"" + name + "\"");
-
- Object value = get(name);
-
- if (value == null) {
- return null;
- }
-
- String fieldType = field.type;
- String str = null;
-
- // first the custom types that need to be parsed
- if (fieldType.equals("CustomTimestamp")) {
- // a common timestamp format for flat files is with no separators:
yyyyMMddHHmmss
- SimpleDateFormat sdf = new SimpleDateFormat(field.format);
- java.sql.Timestamp timestamp = (java.sql.Timestamp) value;
-
- str = sdf.format(new Date(timestamp.getTime()));
- } else if (fieldType.equals("CustomDate")) {
- // a common date only format for flat files is with no separators:
yyyyMMdd or MMddyyyy
- SimpleDateFormat sdf = new SimpleDateFormat(field.format);
- java.sql.Date date = (java.sql.Date) value;
-
- str = sdf.format(new Date(date.getTime()));
- } else if (fieldType.equals("CustomTime")) {
- // a common time only format for flat files is with no separators:
HHmmss
- SimpleDateFormat sdf = new SimpleDateFormat(field.format);
- java.sql.Time time = (java.sql.Time) value;
-
- str = sdf.format(new Date(time.getTime()));
- } else if (fieldType.equals("FixedPointDouble")) {
- // this custom type will parse a fixed point number according to the
number
- // of decimal places in the formatting string then place it in a Double
- double decimalPlaces = Double.parseDouble(field.format);
- double multiplier = Math.pow(10.0, decimalPlaces);
- double dnum = multiplier * ((Double) value).doubleValue();
- long number = Math.round(dnum);
-
- str = padFrontZeros(Long.toString(number), field.length);
- // if (Debug.infoOn()) Debug.logInfo("[Record.getFixedString]
FixedPointDouble: multiplier=" + multiplier + ", value=" + value + ", dnum=" +
dnum + ", number=" + number + ", str=" + str, module);
- } // standard types
- else if (fieldType.equals("java.lang.String") ||
fieldType.equals("String"))
- str = value.toString();
- else if (fieldType.equals("java.sql.Timestamp") ||
fieldType.equals("Timestamp"))
- str = value.toString();
- else if (fieldType.equals("java.sql.Time") || fieldType.equals("Time"))
- str = value.toString();
- else if (fieldType.equals("java.sql.Date") || fieldType.equals("Date"))
- str = value.toString();
- // for all numbers, pad front with zeros if field length is specified
- else if (fieldType.equals("java.lang.Integer") ||
fieldType.equals("Integer"))
- str = padFrontZeros(value.toString(), field.length);
- else if (fieldType.equals("java.lang.Long") || fieldType.equals("Long"))
- str = padFrontZeros(value.toString(), field.length);
- else if (fieldType.equals("java.lang.Float") || fieldType.equals("Float"))
- str = padFrontZeros(value.toString(), field.length);
- else if (fieldType.equals("java.lang.Double") ||
fieldType.equals("Double"))
- str = padFrontZeros(value.toString(), field.length);
- else {
- throw new IllegalArgumentException("Field type " + fieldType + " not
currently supported. Sorry.");
- }
-
- if (str != null && field.length > 0 && str.length() < field.length) {
- // pad the end with spaces
- StringBuilder strBuf = new StringBuilder(str);
-
- while (strBuf.length() < field.length)
- strBuf.append(' ');
- str = strBuf.toString();
- }
- return str;
+ if (name == null)
+ return null;
+ ModelField field = getModelRecord().getModelField(name);
+
+ if (field == null)
+ throw new IllegalArgumentException("Could not find model for field
named \"" + name + "\"");
+
+ Object value = get(name);
+
+ if (value == null) {
+ return null;
+ }
+
+ String fieldType = field.type;
+ String str = null;
+
+ // first the custom types that need to be parsed
+ if (fieldType.equals("CustomTimestamp")) {
+ // a common timestamp format for flat files is with no separators:
yyyyMMddHHmmss
+ SimpleDateFormat sdf = new SimpleDateFormat(field.format);
+ java.sql.Timestamp timestamp = (java.sql.Timestamp) value;
+
+ str = sdf.format(new Date(timestamp.getTime()));
+ } else if (fieldType.equals("CustomDate")) {
+ // a common date only format for flat files is with no separators:
yyyyMMdd or MMddyyyy
+ SimpleDateFormat sdf = new SimpleDateFormat(field.format);
+ java.sql.Date date = (java.sql.Date) value;
+
+ str = sdf.format(new Date(date.getTime()));
+ } else if (fieldType.equals("CustomTime")) {
+ // a common time only format for flat files is with no separators:
HHmmss
+ SimpleDateFormat sdf = new SimpleDateFormat(field.format);
+ java.sql.Time time = (java.sql.Time) value;
+
+ str = sdf.format(new Date(time.getTime()));
+ } else if (fieldType.equals("FixedPointDouble")) {
+ // this custom type will parse a fixed point number according to
the number
+ // of decimal places in the formatting string then place it in a
Double
+ double decimalPlaces = Double.parseDouble(field.format);
+ double multiplier = Math.pow(10.0, decimalPlaces);
+ double dnum = multiplier * ((Double) value).doubleValue();
+ long number = Math.round(dnum);
+
+ str = padFrontZeros(Long.toString(number), field.length);
+ } // standard types
+ else if (fieldType.equals("java.lang.String") ||
fieldType.equals("String"))
+ str = value.toString();
+ else if (fieldType.equals("java.sql.Timestamp") ||
fieldType.equals("Timestamp"))
+ str = value.toString();
+ else if (fieldType.equals("java.sql.Time") || fieldType.equals("Time"))
+ str = value.toString();
+ else if (fieldType.equals("java.sql.Date") || fieldType.equals("Date"))
+ str = value.toString();
+ // for all numbers, pad front with zeros if field length is specified
+ else if (fieldType.equals("java.lang.Integer") ||
fieldType.equals("Integer"))
+ str = padFrontZeros(value.toString(), field.length);
+ else if (fieldType.equals("java.lang.Long") ||
fieldType.equals("Long"))
+ str = padFrontZeros(value.toString(), field.length);
+ else if (fieldType.equals("java.lang.Float") ||
fieldType.equals("Float"))
+ str = padFrontZeros(value.toString(), field.length);
+ else if (fieldType.equals("java.lang.Double") ||
fieldType.equals("Double"))
+ str = padFrontZeros(value.toString(), field.length);
+ else {
+ throw new IllegalArgumentException("Field type " + fieldType + "
not currently supported. Sorry.");
+ }
+
+ if (str != null && field.length > 0 && str.length() < field.length) {
+ // pad the end with spaces
+ StringBuilder strBuf = new StringBuilder(str);
+
+ while (strBuf.length() < field.length)
+ strBuf.append(' ');
+ str = strBuf.toString();
+ }
+ return str;
}
public String writeLineString(ModelDataFile modelDataFile) throws
DataFileException {
- ModelRecord modelRecord = getModelRecord();
- boolean isFixedRecord =
ModelDataFile.SEP_FIXED_RECORD.equals(modelDataFile.separatorStyle);
- boolean isFixedLength =
ModelDataFile.SEP_FIXED_LENGTH.equals(modelDataFile.separatorStyle);
- boolean isDelimited =
ModelDataFile.SEP_DELIMITED.equals(modelDataFile.separatorStyle);
+ ModelRecord modelRecord = getModelRecord();
+ boolean isFixedRecord =
ModelDataFile.SEP_FIXED_RECORD.equals(modelDataFile.separatorStyle);
+ boolean isFixedLength =
ModelDataFile.SEP_FIXED_LENGTH.equals(modelDataFile.separatorStyle);
+ boolean isDelimited =
ModelDataFile.SEP_DELIMITED.equals(modelDataFile.separatorStyle);
- StringBuilder lineBuf = new StringBuilder();
+ StringBuilder lineBuf = new StringBuilder();
- for (ModelField modelField: modelRecord.fields) {
- String data = this.getFixedString(modelField.name);
+ for (ModelField modelField : modelRecord.fields) {
+ String data = this.getFixedString(modelField.name);
- if (isDelimited && null != modelDataFile.textDelimiter) {
- lineBuf.append(modelDataFile.textDelimiter);
- }
+ if (isDelimited && null != modelDataFile.textDelimiter) {
+ lineBuf.append(modelDataFile.textDelimiter);
+ }
- // if field is null (not set) then assume we want to pad the field
- char PAD_CHAR = ' ';
+ // if field is null (not set) then assume we want to pad the field
+ char PAD_CHAR = ' ';
- if (data == null) {
- StringBuilder sb = new StringBuilder("");
+ if (data == null) {
+ StringBuilder sb = new StringBuilder("");
- for (int i = 0; i < modelField.length; i++)
- sb.append(PAD_CHAR);
- data = sb.toString();
- }
+ for (int i = 0; i < modelField.length; i++)
+ sb.append(PAD_CHAR);
+ data = sb.toString();
+ }
- // Pad the record
- if (isFixedRecord) {
- while (modelField.position > lineBuf.length())
- lineBuf.append(" ");
+ // Pad the record
+ if (isFixedRecord) {
+ while (modelField.position > lineBuf.length())
+ lineBuf.append(" ");
+ }
+ if (modelField.length > 0 && data.length() != modelField.length)
+ throw new DataFileException("Got field length " +
data.length() + " but expected field length is " + modelField.length + " for
field \"" + modelField.name
+ + "\" of record \"" +
modelRecord.name + "\" data is: \"" + data + "\"");
+
+ lineBuf.append(data);
+ if (isDelimited) {
+ if (null != modelDataFile.textDelimiter) {
+ lineBuf.append(modelDataFile.textDelimiter);
+ }
+ lineBuf.append(modelDataFile.delimiter);
+ }
}
- // if (Debug.infoOn()) Debug.logInfo("Field: " + modelField.name + "
Position: " + modelField.position + " BufLen: " + lineBuf.length(), module);
-
- // if (Debug.infoOn()) Debug.logInfo("Got data \"" + data + "\" for
field " + modelField.name + " in record " + modelRecord.name, module);
- if (modelField.length > 0 && data.length() != modelField.length)
- throw new DataFileException("Got field length " + data.length() + "
but expected field length is " + modelField.length + " for field \"" +
- modelField.name + "\" of record \"" + modelRecord.name + "\" data
is: \"" + data + "\"");
- lineBuf.append(data);
if (isDelimited) {
- if (null != modelDataFile.textDelimiter) {
- lineBuf.append(modelDataFile.textDelimiter);
+ // just remove the last delimiter to finish clean, otherwise shows
as extra column
+ lineBuf.setLength(lineBuf.length() - 1);
}
- lineBuf.append(modelDataFile.delimiter);
- }
- }
- if (isDelimited) {
- // just remove the last delimiter to finish clean, otherwise shows as
extra column
- lineBuf.setLength(lineBuf.length() - 1);
- }
+ if ((isFixedRecord || isFixedLength) && modelDataFile.recordLength > 0
&& lineBuf.length() != modelDataFile.recordLength)
+ throw new DataFileException("Got record length " +
lineBuf.length() + " but expected record length is " +
modelDataFile.recordLength + " for record \""
+ + modelRecord.name + "\" data line is:
\"" + lineBuf + "\"");
- if ((isFixedRecord || isFixedLength) && modelDataFile.recordLength > 0 &&
lineBuf.length() != modelDataFile.recordLength)
- throw new DataFileException("Got record length " + lineBuf.length() +
" but expected record length is " + modelDataFile.recordLength +
- " for record \"" + modelRecord.name + "\" data line is: \"" +
lineBuf + "\"");
-
- // for convenience, insert the type-code in where it is looked for, if
exists
- if (modelRecord.tcPosition > 0 && modelRecord.typeCode.length() > 0) {
- lineBuf.replace(modelRecord.tcPosition, modelRecord.tcPosition +
modelRecord.tcLength, modelRecord.typeCode);
- }
+ // for convenience, insert the type-code in where it is looked for, if
exists
+ if (modelRecord.tcPosition > 0 && modelRecord.typeCode.length() > 0) {
+ lineBuf.replace(modelRecord.tcPosition, modelRecord.tcPosition +
modelRecord.tcLength, modelRecord.typeCode);
+ }
- if (isFixedLength || isDelimited)
- lineBuf.append('\n');
+ if (isFixedLength || isDelimited)
+ lineBuf.append('\n');
- return lineBuf.toString();
+ return lineBuf.toString();
}
String padFrontZeros(String str, int totalLength) {
- if (totalLength > 0 && str.length() < totalLength) {
- // pad the front with zeros
- StringBuilder zeros = new StringBuilder();
- int numZeros = totalLength - str.length();
-
- for (int i = 0; i < numZeros; i++)
- zeros.append('0');
- zeros.append(str);
- return zeros.toString();
- } else
- return str;
+ if (totalLength > 0 && str.length() < totalLength) {
+ // pad the front with zeros
+ StringBuilder zeros = new StringBuilder();
+ int numZeros = totalLength - str.length();
+
+ for (int i = 0; i < numZeros; i++)
+ zeros.append('0');
+ zeros.append(str);
+ return zeros.toString();
+ } else
+ return str;
}
public Record getParentRecord() {
- return parentRecord;
+ return parentRecord;
}
public List<Record> getChildRecords() {
- return childRecords;
+ return childRecords;
}
public void addChildRecord(Record record) {
- childRecords.add(record);
+ childRecords.add(record);
}
/** Creates new Record
@@ -481,9 +460,9 @@ public class Record implements Serializa
* @return return the Record Object created
*/
public static Record createRecord(ModelRecord modelRecord) throws
DataFileException {
- Record record = new Record(modelRecord);
+ Record record = new Record(modelRecord);
- return record;
+ return record;
}
/** Creates new Record from existing fields Map
@@ -493,9 +472,9 @@ public class Record implements Serializa
* @return return the Record Object created
*/
public static Record createRecord(ModelRecord modelRecord, Map<String,
Object> fields) throws DataFileException {
- Record record = new Record(modelRecord, fields);
+ Record record = new Record(modelRecord, fields);
- return record;
+ return record;
}
/**
@@ -506,30 +485,32 @@ public class Record implements Serializa
* @return return the Record Object created
*/
public static Record createRecord(String line, int lineNum, ModelRecord
modelRecord) throws DataFileException {
- Record record = new Record(modelRecord);
+ Record record = new Record(modelRecord);
- for (ModelField modelField: modelRecord.fields) {
- String strVal = null;
+ for (ModelField modelField : modelRecord.fields) {
+ String strVal = null;
-
- try {
- strVal = line.substring(modelField.position, modelField.position +
modelField.length);
- } catch (IndexOutOfBoundsException ioobe) {
- throw new DataFileException("Field " + modelField.name + " from " +
modelField.position +
- " for " + modelField.length + " chars could not be read from a
line (" + lineNum + ") with only " +
- line.length() + " chars.", ioobe);
- }
- try {
- record.setString(modelField.name, strVal);
- } catch (java.text.ParseException e) {
- throw new DataFileException("Could not parse field " + modelField.name
+ ", format string \"" + modelField.format + "\" with value " + strVal +
- " on line " + lineNum, e);
- } catch (java.lang.NumberFormatException e) {
- throw new DataFileException("Number not valid for field " +
modelField.name + ", format string \"" + modelField.format + "\" with value " +
- strVal + " on line " + lineNum, e);
+ try {
+ strVal = line.substring(modelField.position,
modelField.position + modelField.length);
+ }
+ catch (IndexOutOfBoundsException ioobe) {
+ throw new DataFileException("Field " + modelField.name + "
from " + modelField.position + " for " + modelField.length + " chars could not
be read from a line ("
+ + lineNum + ") with only " +
line.length() + " chars.",
+ ioobe);
+ }
+ try {
+ record.setString(modelField.name, strVal);
+ }
+ catch (java.text.ParseException e) {
+ throw new DataFileException(
+ "Could not parse field " + modelField.name + ", format
string \"" + modelField.format + "\" with value " + strVal + " on line " +
lineNum, e);
+ }
+ catch (java.lang.NumberFormatException e) {
+ throw new DataFileException(
+ "Number not valid for field " + modelField.name + ",
format string \"" + modelField.format + "\" with value " + strVal + " on line "
+ lineNum, e);
+ }
}
- }
- return record;
+ return record;
}
/**
@@ -541,66 +522,65 @@ public class Record implements Serializa
* @return return a Record Object
*/
public static Record createDelimitedRecord(String line, int lineNum,
ModelRecord modelRecord, char delimiter, String textDelimiter) throws
DataFileException {
- Record record = new Record(modelRecord);
+ Record record = new Record(modelRecord);
- StringTokenizer st = null;
- if (line.endsWith(String.valueOf(delimiter))) {
- st = new StringTokenizer(line + " ", "" + delimiter, true);
- }
- else {
- st = new StringTokenizer(line, "" + delimiter, true);
- }
- for (ModelField modelField: modelRecord.fields) {
- String strVal = null;
-
- if (modelField.expression) {
- if (UtilValidate.isNotEmpty(modelField.refField)) {
- strVal = record.getString(modelField.refField);
- }
- if (strVal == null) {
- strVal = (String)modelField.defaultValue;
- }
+ StringTokenizer st = null;
+ if (line.endsWith(String.valueOf(delimiter))) {
+ st = new StringTokenizer(line + " ", "" + delimiter, true);
} else {
- //some input lines may be less than the header model.
- if (st.hasMoreTokens()) {
+ st = new StringTokenizer(line, "" + delimiter, true);
+ }
+ for (ModelField modelField : modelRecord.fields) {
+ String strVal = null;
+
+ if (modelField.expression) {
+ if (UtilValidate.isNotEmpty(modelField.refField)) {
+ strVal = record.getString(modelField.refField);
+ }
+ if (strVal == null) {
+ strVal = (String) modelField.defaultValue;
+ }
+ } else {
+ //some input lines may be less than the header model.
+ if (st.hasMoreTokens()) {
+ try {
+ strVal = st.nextToken();
+ if (strVal.equals("" + delimiter)) {
+ strVal = null;
+ } else if (st.hasMoreTokens()) {
+ st.nextToken();
+ }
+ }
+ catch (NoSuchElementException nsee) {
+ throw new DataFileException("Field " + modelField.name
+ " could not be read from a line (" + lineNum + ") with only " + line.length()
+ " chars.", nsee);
+ }
+ } else { //if input line is less than the header model then
pad with null
+ strVal = null;
+ }
+ }
try {
- strVal = st.nextToken();
- if (strVal.equals("" + delimiter)) {
- strVal = null;
- } else if (st.hasMoreTokens()) {
- st.nextToken();
- }
- } catch (NoSuchElementException nsee) {
- throw new DataFileException("Field " + modelField.name + " could
not be read from a line (" + lineNum + ") with only " +
- line.length() + " chars.", nsee);
- }
- }
- else { //if input line is less than the header model then pad with null
- strVal = null;
- }
- }
- try {
- if (textDelimiter != null && strVal != null &&
(strVal.startsWith(textDelimiter) && (!strVal.endsWith(textDelimiter) ||
strVal.length()==1))) {
- strVal = strVal.concat(""+delimiter);
- while (!strVal.endsWith(textDelimiter)) {
- strVal = strVal.concat(st.nextToken());
- }
- st.nextToken();
- }
- if (textDelimiter != null && strVal != null &&
(strVal.startsWith(textDelimiter) && strVal.endsWith(textDelimiter))) {
- strVal = strVal.substring(textDelimiter.length(), strVal.length()
- textDelimiter.length());
- }
- record.setString(modelField.name, strVal);
- } catch (java.text.ParseException e) {
- throw new DataFileException("Could not parse field " + modelField.name
+ ", format string \"" + modelField.format + "\" with value " + strVal +
- " on line " + lineNum, e);
- } catch (java.lang.NumberFormatException e) {
- throw new DataFileException("Number not valid for field " +
modelField.name + ", format string \"" + modelField.format + "\" with value " +
- strVal + " on line " + lineNum, e);
+ if (textDelimiter != null && strVal != null &&
(strVal.startsWith(textDelimiter) && (!strVal.endsWith(textDelimiter) ||
strVal.length() == 1))) {
+ strVal = strVal.concat("" + delimiter);
+ while (!strVal.endsWith(textDelimiter)) {
+ strVal = strVal.concat(st.nextToken());
+ }
+ st.nextToken();
+ }
+ if (textDelimiter != null && strVal != null &&
(strVal.startsWith(textDelimiter) && strVal.endsWith(textDelimiter))) {
+ strVal = strVal.substring(textDelimiter.length(),
strVal.length() - textDelimiter.length());
+ }
+ record.setString(modelField.name, strVal);
+ }
+ catch (java.text.ParseException e) {
+ throw new DataFileException(
+ "Could not parse field " + modelField.name + ", format
string \"" + modelField.format + "\" with value " + strVal + " on line " +
lineNum, e);
+ }
+ catch (java.lang.NumberFormatException e) {
+ throw new DataFileException(
+ "Number not valid for field " + modelField.name + ",
format string \"" + modelField.format + "\" with value " + strVal + " on line "
+ lineNum, e);
+ }
}
- }
- return record;
+ return record;
}
}
-
Modified:
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java
URL:
http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java?rev=1813637&r1=1813636&r2=1813637&view=diff
==============================================================================
---
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java
(original)
+++
ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java
Sat Oct 28 14:45:17 2017
@@ -18,7 +18,6 @@
*******************************************************************************/
package org.apache.ofbiz.datafile;
-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@@ -31,7 +30,6 @@ import java.util.Stack;
* Note: this is a memory intensive and will not handle files that exceed
memory.
*
*/
-
public class RecordIterator {
public static final String module = RecordIterator.class.getName();
@@ -55,7 +53,8 @@ public class RecordIterator {
InputStream urlStream = null;
try {
urlStream = fileUrl.openStream();
- } catch (IOException e) {
+ }
+ catch (IOException e) {
throw new DataFileException("Error open URL: " +
fileUrl.toString(), e);
}
this.setupStream(urlStream, fileUrl.toString());
@@ -71,7 +70,8 @@ public class RecordIterator {
this.dataFileStream = dataFileStream;
try {
this.br = new BufferedReader(new InputStreamReader(dataFileStream,
"UTF-8"));
- } catch (Exception e) {
+ }
+ catch (Exception e) {
throw new DataFileException("UTF-8 is not supported");
}
// get the line seeded
@@ -84,7 +84,6 @@ public class RecordIterator {
boolean isFixedRecord =
ModelDataFile.SEP_FIXED_RECORD.equals(modelDataFile.separatorStyle);
boolean isDelimited =
ModelDataFile.SEP_DELIMITED.equals(modelDataFile.separatorStyle);
- // if (Debug.infoOn()) Debug.logInfo("[DataFile.readDataFile]
separatorStyle is " + modelDataFile.separatorStyle + ", isFixedRecord: " +
isFixedRecord, module);
if (isFixedRecord) {
if (modelDataFile.recordLength <= 0) {
@@ -93,29 +92,27 @@ public class RecordIterator {
try {
char[] charData = new char[modelDataFile.recordLength + 1];
- // if (Debug.infoOn()) Debug.logInfo("[DataFile.readDataFile]
reading line " + lineNum + " from position " +
(lineNum-1)*modelDataFile.recordLength + ", length is " +
modelDataFile.recordLength, module);
if (br.read(charData, 0, modelDataFile.recordLength) == -1) {
nextLine = null;
- // Debug.logInfo("[DataFile.readDataFile] found end of
file, got -1", module);
} else {
nextLine = new String(charData);
- // if (Debug.infoOn())
Debug.logInfo("[DataFile.readDataFile] read line " + lineNum + " line is: \"" +
line + "\"", module);
}
- } catch (IOException e) {
- throw new DataFileException("Error reading line #" +
nextLineNum + " (index " + (nextLineNum - 1) * modelDataFile.recordLength + "
length " +
- modelDataFile.recordLength + ") from location: " +
locationInfo, e);
+ }
+ catch (IOException e) {
+ throw new DataFileException("Error reading line #" +
nextLineNum + " (index " + (nextLineNum - 1) * modelDataFile.recordLength + "
length "
+ + modelDataFile.recordLength + ")
from location: " + locationInfo,
+ e);
}
} else {
try {
nextLine = br.readLine();
- //Debug.logInfo("br.readLine()=\"" + nextLine + "\"", module);
- } catch (IOException e) {
+ }
+ catch (IOException e) {
throw new DataFileException("Error reading line #" +
nextLineNum + " from location: " + locationInfo, e);
}
}
- //if (nextLine != null && !(eof.equals(nextLine.substring(0,1)) && 1
== nextLine.length())) {
- if (nextLine != null && !((nextLine.contains(eof) ) )) {
+ if (nextLine != null && !((nextLine.contains(eof)))) {
nextLineNum++;
ModelRecord modelRecord = findModelForLine(nextLine, nextLineNum,
modelDataFile);
if (isDelimited) {
@@ -135,8 +132,7 @@ public class RecordIterator {
}
public boolean hasNext() {
- //return nextLine != null && !(eof.equals(nextLine.substring(0,1)) &&
1 == nextLine.length());
- return nextLine != null && !((nextLine.contains(eof) ) );
+ return nextLine != null && !((nextLine.contains(eof)));
}
public Record next() throws DataFileException {
@@ -144,9 +140,9 @@ public class RecordIterator {
return null;
}
- if (ModelDataFile.SEP_DELIMITED.equals(modelDataFile.separatorStyle)
|| ModelDataFile.SEP_FIXED_RECORD.equals(modelDataFile.separatorStyle) ||
ModelDataFile.SEP_FIXED_LENGTH.equals(modelDataFile.separatorStyle)) {
+ if (ModelDataFile.SEP_DELIMITED.equals(modelDataFile.separatorStyle)
|| ModelDataFile.SEP_FIXED_RECORD.equals(modelDataFile.separatorStyle)
+ ||
ModelDataFile.SEP_FIXED_LENGTH.equals(modelDataFile.separatorStyle)) {
boolean isFixedRecord =
ModelDataFile.SEP_FIXED_RECORD.equals(modelDataFile.separatorStyle);
- // if (Debug.infoOn()) Debug.logInfo("[DataFile.readDataFile]
separatorStyle is " + modelDataFile.separatorStyle + ", isFixedRecord: " +
isFixedRecord, module);
// advance the line (we have already checked to make sure there is
a next line
this.curLine = this.nextLine;
this.curRecord = this.nextRecord;
@@ -156,7 +152,8 @@ public class RecordIterator {
// first check to see if the file type has a line size, and if so
if this line complies
if (!isFixedRecord && modelDataFile.recordLength > 0 &&
curLine.length() != modelDataFile.recordLength) {
- throw new DataFileException("Line number " +
this.getCurrentLineNumber() + " was not the expected length; expected: " +
modelDataFile.recordLength + ", got: " + curLine.length());
+ throw new DataFileException(
+ "Line number " + this.getCurrentLineNumber() + " was
not the expected length; expected: " + modelDataFile.recordLength + ", got: " +
curLine.length());
}
// if this record has children, put it on the parentStack and
get/check the children now
@@ -178,7 +175,8 @@ public class RecordIterator {
}
}
if (parentRecord == null) {
- throw new DataFileException("Expected Parent Record
not found for line " + this.getCurrentLineNumber() + "; record name of expected
parent is " + this.nextRecord.getModelRecord().parentName);
+ throw new DataFileException("Expected Parent Record
not found for line " + this.getCurrentLineNumber() + "; record name of expected
parent is "
+ +
this.nextRecord.getModelRecord().parentName);
}
parentRecord.addChildRecord(this.nextRecord);
@@ -203,12 +201,12 @@ public class RecordIterator {
try {
this.br.close(); // this should also close the stream
this.closed = true;
- } catch (IOException e) {
+ }
+ catch (IOException e) {
throw new DataFileException("Error closing data file input
stream", e);
}
}
-
/** Searches through the record models to find one with a matching
type-code, if no type-code exists that model will always be used if it gets to
it
* @param line
* @param lineNum
@@ -217,10 +215,9 @@ public class RecordIterator {
* @return return the ModelRecord Object found
*/
protected static ModelRecord findModelForLine(String line, int lineNum,
ModelDataFile modelDataFile) throws DataFileException {
- // if (Debug.infoOn()) Debug.logInfo("[DataFile.findModelForLine]
line: " + line, module);
ModelRecord modelRecord = null;
- for (ModelRecord curModelRecord: modelDataFile.records) {
+ for (ModelRecord curModelRecord : modelDataFile.records) {
if (curModelRecord.tcPosition < 0) {
modelRecord = curModelRecord;
break;
@@ -229,23 +226,19 @@ public class RecordIterator {
// try to match with a single typecode
if (curModelRecord.typeCode.length() > 0) {
- // if (Debug.infoOn())
Debug.logInfo("[DataFile.findModelForLine] Doing plain typecode match - code="
+ curModelRecord.typeCode + ", filelinecode=" + typeCode, module);
- if (typeCode != null &&
typeCode.equals(curModelRecord.typeCode)) {
+ if (!typeCode.isEmpty() &&
typeCode.equals(curModelRecord.typeCode)) {
modelRecord = curModelRecord;
break;
}
} // try to match a ranged typecode (tcMin <= typeCode <= tcMax)
else if (curModelRecord.tcMin.length() > 0 ||
curModelRecord.tcMax.length() > 0) {
if (curModelRecord.tcIsNum) {
- // if (Debug.infoOn())
Debug.logInfo("[DataFile.findModelForLine] Doing ranged number typecode match -
minNum=" + curModelRecord.tcMinNum + ", maxNum=" + curModelRecord.tcMaxNum + ",
filelinecode=" + typeCode, module);
long typeCodeNum = Long.parseLong(typeCode);
- if ((curModelRecord.tcMinNum < 0 || typeCodeNum >=
curModelRecord.tcMinNum) &&
- (curModelRecord.tcMaxNum < 0 || typeCodeNum <=
curModelRecord.tcMaxNum)) {
+ if ((curModelRecord.tcMinNum < 0 || typeCodeNum >=
curModelRecord.tcMinNum) && (curModelRecord.tcMaxNum < 0 || typeCodeNum <=
curModelRecord.tcMaxNum)) {
modelRecord = curModelRecord;
break;
}
} else {
- // if (Debug.infoOn())
Debug.logInfo("[DataFile.findModelForLine] Doing ranged String typecode match -
min=" + curModelRecord.tcMin + ", max=" + curModelRecord.tcMax + ",
filelinecode=" + typeCode, module);
if ((typeCode.compareTo(curModelRecord.tcMin) >= 0) &&
(typeCode.compareTo(curModelRecord.tcMax) <= 0)) {
modelRecord = curModelRecord;
break;
@@ -255,10 +248,8 @@ public class RecordIterator {
}
if (modelRecord == null) {
- throw new DataFileException("Could not find record definition for
line " + lineNum + "; first bytes: " +
- line.substring(0, (line.length() > 5) ? 5 :
line.length()));
+ throw new DataFileException("Could not find record definition for
line " + lineNum + "; first bytes: " + line.substring(0, (line.length() > 5) ?
5 : line.length()));
}
- // if (Debug.infoOn()) Debug.logInfo("[DataFile.findModelForLine] Got
record model named " + modelRecord.name, module);
return modelRecord;
}
}