Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Flat File Parser (jffp) LineFormatTest

Tags:

java

I want to work with Java Flat File Parser (jffp) and am trying to run the TestCase (JUnit) "LineFormatTest" which is inside the src-file (and there after some clicking inside the file "test"). What I get is this error:

org.sadun.text.ffp.FieldDefinitionException: Programming error: the fields field_1_1 (from position 0 to position5, length 5, type numeric) and field_1_1 (from position 0 to position5, length 5, type numeric) intersect

Has anyone tried to run this TestCase and stumbled across this error as well?

like image 246
Lexsan Avatar asked Jul 15 '26 01:07

Lexsan


2 Answers

I have such trouble with jffp and JDK1.7. I investigated sources and found out that oracle correted a bit implementation of TreeMap which is used in TreeSet in JDK1.7, so jffp adds field definitions into the set but new implementation of map calls compare method for the first element with itself and comparator implemented in the jffp contains checking of positions of field element and if position the same it throws "programming exception". And if you take a look to exception description you will see that it compares the same field.

So, if you use jdk1.7 also then I don't see any light way solutions as rid of jffp or jdk1.7(back to 1.6).

like image 88
salvezza Avatar answered Jul 16 '26 16:07

salvezza


I just encountered the bug when using jdk1.7.0_55 and jffp. In Java 7 the comparator is invoked as soon as the first element is added to the TreeSet. So the first FieldInfo instance (o1) is compared against itself (o2); o1 and o2 obviously intersect.

Here is the fast and dirty solution I have tested successfully:

  1. Download jffp source code at sourceforge: http://sourceforge.net/projects/jffp/

  2. Take the LineFormat.java file and copy it in the org.sadun.text.ffp package in your project.

  3. Now modify the addFieldInfo private method and add a (f1 != f2) condition to the f1.intersects(f2) test to ensure a field is not tested against itself for intersection:

     private void addFieldInfo(int physicalLine, FieldInfo info) {
           if (physicalLine > currentPhysicalLine)
              currentPhysicalLine = physicalLine;
           final Integer pl = new Integer(physicalLine);
           SortedSet l = (SortedSet) fieldsByLine.get(pl);
           if (l == null) {
              l = new TreeSet(new Comparator() {
                 public int compare(Object o1, Object o2) {
                    FieldInfo f1 = (FieldInfo) o1;
                    FieldInfo f2 = (FieldInfo) o2;
                    // fields must not intersect
                    if ((f1 != f2)
                          && f1.intersects(f2))
                       throw new FieldDefinitionException(
                             "Programming error: the fields "
                                   + f1
                                   + " and "
                                   + f2
                                   + " intersect");
                    return f1.start - f2.start;
    
                 }
              });
              fieldsByLine.put(pl, l);
           }
           l.add(info);
     }
    
  4. Compile and test.

I am going to reach the developer. Maybe he could build an official version to fix the bug.

like image 43
Nicolas Avatar answered Jul 16 '26 16:07

Nicolas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!