4 Replies - 1147 Views - Last Post: 20 May 2014 - 02:30 AM

#1 peace_fixation  Icon User is offline

  • D.I.C Head

Reputation: 46
  • View blog
  • Posts: 198
  • Joined: 01-November 11

Representation of a structured file

Posted 18 May 2014 - 04:38 AM

Hi folks. I have a design related question. I have to create a program for work that processes a structured file and performs some business logic. The file contains a bunch of different types of records, kind of like this:

file header
-group header
--element header
---detail
---detail
--element footer
--element header
---detail
---detail
--element footer
-group footer
-group header
--element header
---detail
---detail
--element footer
-group footer
file footer

Now parsing the file is one thing (it's CSV-ish), but representing the file internally to my program is what I'm working on.

My first thought is to have a hierarchy of classes like:

class File {
  private FileHeader fileHeader;
  private List<Group> groups;
  private FileFooter fileFooter;
}

class Group {
  private GroupHeader groupHeader;
  private List<Element> element;
  private GroupFooter groupFooter;
}

class Element {
  private ElementHeader elementHeader;
  private List<Detail> details;
  private ElementFooter elementFooter;
}

class Detail {
  // fields
  private String field1;
  private String field2;
}

class ElementHeader {
  // fields
  private String field1;
  private String field2;
}

class ElementFooter {
  // fields
  private String field1;
  private String field2;
}

// etc



Does anyone have any comments about this kind of implementation for representing a file? I don't want to abstract stuff away just for the sake of it, but I'm struggling to think of a cleaner way to do it and I want to make very sure that I build a maintainable package. I have a lot of logic to build in with regards to how each type of row is formatted, and by using a class for each type of row, I can encapsulate that logic quite easily.

This post has been edited by peace_fixation: 18 May 2014 - 04:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Representation of a structured file

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: Representation of a structured file

Posted 18 May 2014 - 07:01 AM

The names "field1" and "field2" aren't real friendly, but other than that it looks fine. Should make it a snap to serialize so I don't think you have to go through all the pain of making it CSV-ish; unless you already have data coming *in* using that format from another source.

Of course if you are going to serialize you'll need to make public properties instead of private fields.

Separating data from GUI - PLUS - serializing the data to XML
Was This Post Helpful? 0
  • +
  • -

#3 peace_fixation  Icon User is offline

  • D.I.C Head

Reputation: 46
  • View blog
  • Posts: 198
  • Joined: 01-November 11

Re: Representation of a structured file

Posted 19 May 2014 - 03:02 AM

Thanks for the reply. My use of 'field1', 'field2' was just to show that the data is in those classes, the real names were hidden to protect the innocent. :P

I won't be serializing the classes, I'm reading an incoming file in this format and processing the data in the Detail rows, and also generating these files on my end to send back, so I was thinking to @Override toString() to generate my rows.

There's a lot of code at my place that does file parsing of one kind or another, and it's usually a big mess of arbitrary methods in an EJB, which I really want to avoid. I've had the displeasure of modifying that kind of code and it's awful to handle.
Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2045
  • View blog
  • Posts: 4,235
  • Joined: 11-December 07

Re: Representation of a structured file

Posted 19 May 2014 - 03:51 AM

That structure might be fine, but what are you going to do with the data? Depending on the business rules, a direct translation from the file format layout might not be a convenient object model. On the other hand, it might be perfect.

I don't recommend toString() for writing output. This version of the file format is a modified CSV. Another might be XML or HTML/CSS or Java serialisation. I would go with a separate class that formats the data for output. That's simple enough to be acceptable while staying true to the single responsibility principle. If you never need other formats, it's fine as is. If you do need others someday, these classes can be easily modified to something like the visitor pattern.
Was This Post Helpful? 1
  • +
  • -

#5 peace_fixation  Icon User is offline

  • D.I.C Head

Reputation: 46
  • View blog
  • Posts: 198
  • Joined: 01-November 11

Re: Representation of a structured file

Posted 20 May 2014 - 02:30 AM

The (very inner) detail rows are of interest, and I'll be digging out bank transactions and reconciling them.

The file headers and group headers are not particularly interesting, but they provide a few key pieces of data that I need, and they are encoded, so I do need to parse them to extract that data. I could just parse and store what I need in String primitives, but that strikes me as a cheap move that might bite me later if the situation changes.

I am wary of using too many classes though, I've gotten into trouble for it before. :S
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1