svn commit: r1818434 - in /ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile: DataFile.java DataFile2EntityXml.java ModelDataFile.java ModelDataFileReader.java ModelRecord.java Record.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1818434 - in /ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile: DataFile.java DataFile2EntityXml.java ModelDataFile.java ModelDataFileReader.java ModelRecord.java Record.java

mbrohl
Author: mbrohl
Date: Sat Dec 16 16:18:45 2017
New Revision: 1818434

URL: http://svn.apache.org/viewvc?rev=1818434&view=rev
Log:
Improved: General refactoring and code improvements, package
org.apache.ofbiz.datafile.
(OFBIZ-9963)

Thanks Dennis Balkir for reporting and providing the patches.

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/ModelDataFile.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/ModelRecord.java
    ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/Record.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=1818434&r1=1818433&r2=1818434&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 Dec 16 16:18:45 2017
@@ -43,7 +43,7 @@ public class DataFile {
     public static final String module = DataFile.class.getName();
 
     /** List of record in the file, contains Record objects */
-    protected List<Record> records = new ArrayList<Record>();
+    protected List<Record> records = new ArrayList<>();
 
     /** Contains the definition for the file */
     protected ModelDataFile modelDataFile;
@@ -132,8 +132,9 @@ public class DataFile {
      * @throws DataFileException Exception thown for various errors, generally has a nested exception
      */
     public void readDataFile(String content) throws DataFileException {
-        if (UtilValidate.isEmpty(content))
+        if (UtilValidate.isEmpty(content)) {
             throw new IllegalStateException("Content is empty, can't read file");
+        }
 
         ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes(UtilIO.getUtf8()));
 
@@ -170,7 +171,7 @@ public class DataFile {
 
     /**
      * Writes the records in this DataFile object to a text data file
-     *
+     *
      * @param filename
      *            The filename to put the data into
      * @throws DataFileException

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=1818434&r1=1818433&r2=1818434&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 Dec 16 16:18:45 2017
@@ -58,8 +58,9 @@ public class DataFile2EntityXml {
                 ModelRecord modelRecord = record.getModelRecord();
                 outFile.write("<" + modelRecord.name + " ");
                 for (ModelField modelField : modelRecord.fields) {
-                    if (modelField.ignored)
+                    if (modelField.ignored) {
                         continue;
+                    }
                     Object value = record.get(modelField.name);
                     if (value == null) {
                         value = modelField.defaultValue;

Modified: ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFile.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFile.java?rev=1818434&r1=1818433&r2=1818434&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFile.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelDataFile.java Sat Dec 16 16:18:45 2017
@@ -61,7 +61,7 @@ public class ModelDataFile {
     public String description = "";
 
     /** List of record definitions for the file */
-    public List<ModelRecord> records = new ArrayList<ModelRecord>();
+    public List<ModelRecord> records = new ArrayList<>();
 
     public ModelRecord getModelRecord(String recordName) {
         for (ModelRecord curRecord: records) {

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=1818434&r1=1818433&r2=1818434&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 Dec 16 16:18:45 2017
@@ -46,13 +46,15 @@ public final class ModelDataFileReader {
     public static ModelDataFileReader getModelDataFileReader(URL readerURL) throws DataFileException {
         ModelDataFileReader reader = readers.get(readerURL);
         if (reader == null) {
-            if (Debug.infoOn())
+            if (Debug.infoOn()) {
                 Debug.logInfo("[ModelDataFileReader.getModelDataFileReader] : creating reader for " + readerURL, module);
+            }
             reader = new ModelDataFileReader(readerURL);
             readers.putIfAbsent(readerURL, reader);
         }
-        if (Debug.infoOn())
+        if (Debug.infoOn()) {
             Debug.logInfo("[ModelDataFileReader.getModelDataFileReader] : returning reader for " + readerURL, module);
+        }
         return reader;
     }
 
@@ -137,7 +139,7 @@ public final class ModelDataFileReader {
             Debug.logWarning("No <data-file> elements found in " + this.readerURL, module);
             throw new DataFileException("No <data-file> elements found in " + this.readerURL);
         }
-        Map<String, ModelDataFile> result = new HashMap<String, ModelDataFile>();
+        Map<String, ModelDataFile> result = new HashMap<>();
         for (Element curDataFile : dataFileElements) {
             String dataFileName = UtilXml.checkEmpty(curDataFile.getAttribute("name"));
             if (result.containsKey(dataFileName)) {
@@ -200,11 +202,13 @@ public final class ModelDataFileReader {
         record.typeCode = UtilXml.checkEmpty(recordElement.getAttribute("type-code"));
 
         record.tcMin = UtilXml.checkEmpty(recordElement.getAttribute("tc-min"));
-        if (record.tcMin.length() > 0)
+        if (record.tcMin.length() > 0) {
             record.tcMinNum = Long.parseLong(record.tcMin);
+        }
         record.tcMax = UtilXml.checkEmpty(recordElement.getAttribute("tc-max"));
-        if (record.tcMax.length() > 0)
+        if (record.tcMax.length() > 0) {
             record.tcMaxNum = Long.parseLong(record.tcMax);
+        }
 
         tempStr = UtilXml.checkEmpty(recordElement.getAttribute("tc-isnum"));
         if (UtilValidate.isNotEmpty(tempStr)) {
@@ -245,7 +249,7 @@ public final class ModelDataFileReader {
     /**
      * Creates a Collection with the dataFileName of each DataFile defined in the
      * specified XML DataFile Descriptor file.
-     *
+     *
      * @return A Collection of dataFileName Strings
      */
     public Collection<String> getDataFileNames() {
@@ -255,7 +259,7 @@ public final class ModelDataFileReader {
     /**
      * Creates a Iterator with the dataFileName of each DataFile defined in the specified
      * XML DataFile Descriptor file.
-     *
+     *
      * @return A Iterator of dataFileName Strings
      */
     public Iterator<String> getDataFileNamesIterator() {
@@ -265,7 +269,7 @@ public final class ModelDataFileReader {
     /**
      * Gets an DataFile object based on a definition from the specified XML DataFile
      * descriptor file.
-     *
+     *
      * @param dataFileName
      *            The dataFileName of the DataFile definition to use.
      * @return An DataFile object describing the specified dataFile of the specified

Modified: ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelRecord.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelRecord.java?rev=1818434&r1=1818433&r2=1818434&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelRecord.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/datafile/src/main/java/org/apache/ofbiz/datafile/ModelRecord.java Sat Dec 16 16:18:45 2017
@@ -65,10 +65,10 @@ public class ModelRecord {
     public String limit = "";
 
     public ModelRecord parentRecord = null;
-    public List<ModelRecord> childRecords = new ArrayList<ModelRecord>();
+    public List<ModelRecord> childRecords = new ArrayList<>();
 
     /** List of the fields that compose this record */
-    public List<ModelField> fields = new ArrayList<ModelField>();
+    public List<ModelField> fields = new ArrayList<>();
 
     ModelField getModelField(String fieldName) {
         for (ModelField curField: fields) {

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=1818434&r1=1818433&r2=1818434&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 Dec 16 16:18:45 2017
@@ -51,24 +51,26 @@ public class Record implements Serializa
     protected transient ModelRecord modelRecord;
 
     protected Record parentRecord = null;
-    protected List<Record> childRecords = new ArrayList<Record>();
+    protected List<Record> childRecords = new ArrayList<>();
 
     /** Creates new Record */
     protected Record(ModelRecord modelRecord) {
-        if (modelRecord == null)
+        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>();
+        this.fields = new HashMap<>();
     }
 
     /** Creates new Record from existing Map */
     protected Record(ModelRecord modelRecord, Map<String, Object> fields) {
-        if (modelRecord == null)
+        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));
+        this.fields = (fields == null ? new HashMap<>() : new HashMap<>(fields));
     }
 
     public String getRecordName() {
@@ -92,23 +94,25 @@ public class Record implements Serializa
     public String getString(String name) {
         Object object = get(name);
 
-        if (object == null)
+        if (object == null) {
             return null;
-        if (object instanceof java.lang.String)
+        }
+        if (object instanceof java.lang.String) {
             return (String) object;
-        else
-            return object.toString();
+        }
+        return object.toString();
     }
 
     public String getStringAndEmpty(String name) {
         Object object = get(name);
 
-        if (object == null)
+        if (object == null) {
             return "";
-        if (object instanceof java.lang.String)
+        }
+        if (object instanceof java.lang.String) {
             return (String) object;
-        else
-            return object.toString();
+        }
+        return object.toString();
     }
 
     public java.sql.Timestamp getTimestamp(String name) {
@@ -194,12 +198,14 @@ 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(""))
+        if (name == null || value == null || value.equals("")) {
             return;
+        }
         ModelField field = getModelRecord().getModelField(name);
 
-        if (field == null)
+        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;
@@ -210,8 +216,9 @@ public class Record implements Serializa
                 break;
             }
         }
-        if (!nonSpace)
+        if (!nonSpace) {
             return;
+        }
 
         String fieldType = field.type;
 
@@ -251,48 +258,50 @@ public class Record implements Serializa
             number = number / divisor;
             set(name, Double.valueOf(number));
         } // standard types
-        else if (fieldType.equals("java.lang.String") || fieldType.equals("String"))
+        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")) {
+        } 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"))
+        } 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"))
+        } 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"))
+        } 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"))
+        } 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"))
+        } 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"))
+        } 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"))
+        } else if (fieldType.equals("java.lang.Double") || fieldType.equals("Double")) {
             set(name, Double.valueOf(value));
-        else if (fieldType.equals("LEShort"))
+        } else if (fieldType.equals("LEShort")) {
             set(name, Short.valueOf(readLEShort(value.getBytes(UtilIO.getUtf8()))));
-        else if (fieldType.equals("LEInteger"))
+        } else if (fieldType.equals("LEInteger")) {
             set(name, Integer.valueOf(readLEInt(value.getBytes(UtilIO.getUtf8()))));
-        else if (fieldType.equals("LELong"))
+        } else if (fieldType.equals("LELong")) {
             set(name, Long.valueOf(readLELong(value.getBytes(UtilIO.getUtf8()))));
-        else {
+        } else {
             throw new IllegalArgumentException("Field type " + fieldType + " not currently supported. Sorry.");
         }
     }
 
     public String getFixedString(String name) {
-        if (name == null)
+        if (name == null) {
             return null;
+        }
         ModelField field = getModelRecord().getModelField(name);
 
-        if (field == null)
+        if (field == null) {
             throw new IllegalArgumentException("Could not find model for field named \"" + name + "\"");
+        }
 
         Object value = get(name);
 
@@ -332,24 +341,23 @@ public class Record implements Serializa
 
             str = padFrontZeros(Long.toString(number), field.length);
         } // standard types
-        else if (fieldType.equals("java.lang.String") || fieldType.equals("String"))
+        else if (fieldType.equals("java.lang.String") || fieldType.equals("String")) {
             str = value.toString();
-        else if (fieldType.equals("java.sql.Timestamp") || fieldType.equals("Timestamp"))
+        } else if (fieldType.equals("java.sql.Timestamp") || fieldType.equals("Timestamp")) {
             str = value.toString();
-        else if (fieldType.equals("java.sql.Time") || fieldType.equals("Time"))
+        } else if (fieldType.equals("java.sql.Time") || fieldType.equals("Time")) {
             str = value.toString();
-        else if (fieldType.equals("java.sql.Date") || fieldType.equals("Date"))
+        } 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"))
+        } 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"))
+        } 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"))
+        } 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"))
+        } else if (fieldType.equals("java.lang.Double") || fieldType.equals("Double")) {
             str = padFrontZeros(value.toString(), field.length);
-        else {
+        } else {
             throw new IllegalArgumentException("Field type " + fieldType + " not currently supported. Sorry.");
         }
 
@@ -357,8 +365,9 @@ public class Record implements Serializa
             // pad the end with spaces
             StringBuilder strBuf = new StringBuilder(str);
 
-            while (strBuf.length() < field.length)
+            while (strBuf.length() < field.length) {
                 strBuf.append(' ');
+            }
             str = strBuf.toString();
         }
         return str;
@@ -385,19 +394,22 @@ public class Record implements Serializa
             if (data == null) {
                 StringBuilder sb = new StringBuilder("");
 
-                for (int i = 0; i < modelField.length; i++)
+                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())
+                while (modelField.position > lineBuf.length()) {
                     lineBuf.append(" ");
+                }
             }
-            if (modelField.length > 0 && data.length() != modelField.length)
+            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) {
@@ -413,17 +425,19 @@ public class Record implements Serializa
             lineBuf.setLength(lineBuf.length() - 1);
         }
 
-        if ((isFixedRecord || isFixedLength) && modelDataFile.recordLength > 0 && lineBuf.length() != modelDataFile.recordLength)
+        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);
         }
 
-        if (isFixedLength || isDelimited)
+        if (isFixedLength || isDelimited) {
             lineBuf.append('\n');
+        }
 
         return lineBuf.toString();
     }
@@ -434,12 +448,13 @@ public class Record implements Serializa
             StringBuilder zeros = new StringBuilder();
             int numZeros = totalLength - str.length();
 
-            for (int i = 0; i < numZeros; i++)
+            for (int i = 0; i < numZeros; i++) {
                 zeros.append('0');
+            }
             zeros.append(str);
             return zeros.toString();
-        } else
-            return str;
+        }
+        return str;
     }
 
     public Record getParentRecord() {