473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Q: Pattern for this?

I have a parser that validates and constructs 2-dimentional array from
data within a flat file:

Record[] = parse("grammar.xml", "flat.txt");

//Record = {"Michael", "Moore", "48" };

The array carries no semantic information (at least, not that Java can
use) so how would I go about adding an OO accessor layer:

getFirstName(), getLastName(), getAge();

....while remaining flexibile, such that I can also parse other files
which records do not conform to the accessors above. Appropiate wrapper
object which get passed a record instance seems a bit crude.

Is there a pattern or smart OO-technique I can make use of? I
particulary wonder about letting each Record implement different
accessor interfaces, but this gives me nothing but ClassCastExceptions.
Thanks in advance,
Casper Bang
Jul 18 '05 #1
6 2858
Casper B wrote:
I have a parser that validates and constructs 2-dimentional array from
data within a flat file:

Record[] = parse("grammar.xml", "flat.txt");

//Record = {"Michael", "Moore", "48" };

The array carries no semantic information (at least, not that Java can
use) so how would I go about adding an OO accessor layer:

getFirstName(), getLastName(), getAge();

...while remaining flexibile, such that I can also parse other files
which records do not conform to the accessors above. Appropiate wrapper
object which get passed a record instance seems a bit crude.

Is there a pattern or smart OO-technique I can make use of? I
particulary wonder about letting each Record implement different
accessor interfaces, but this gives me nothing but ClassCastExceptions.


Assuming you pass the parser some information about the names of the
columns, this looks like a job for JavaBeans and reflection. With
JavaBeans you use conventions to indicate properties of your classes and
use reflection to dynamically access the properties at runtime.

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 18 '05 #2
> Assuming you pass the parser some information about the names of the
columns, this looks like a job for JavaBeans and reflection. With
JavaBeans you use conventions to indicate properties of your classes and
use reflection to dynamically access the properties at runtime.


Is that to say, I can create fields and accessors dynamically? I only
thought reflection is about "looking in at yourself" and only be able to
get a discription and access existing object, not create these.

/Casper
Jul 18 '05 #3
Casper B wrote:
Assuming you pass the parser some information about the names of the
columns, this looks like a job for JavaBeans and reflection. With
JavaBeans you use conventions to indicate properties of your classes
and use reflection to dynamically access the properties at runtime.

Is that to say, I can create fields and accessors dynamically? I only
thought reflection is about "looking in at yourself" and only be able to
get a discription and access existing object, not create these.


You are right, reflection does not allow one to create fields, etc.
dynamically. But I do not understand why you would want to. The
advantage of creating fields, methods, etc. is to invoke them by name,
in code. If you are doing that, then you already know what the names
are and you do not need to make them dynamically.

Perhaps you are looking for a simple java.util.Map implementation?

HTH,
Ray

--
XML is the programmer's duct tape.
Jul 18 '05 #4
> You are right, reflection does not allow one to create fields, etc.
dynamically. But I do not understand why you would want to. The
advantage of creating fields, methods, etc. is to invoke them by name,
in code. If you are doing that, then you already know what the names
are and you do not need to make them dynamically.

Perhaps you are looking for a simple java.util.Map implementation?


I apologise if my explanation of the problem was inadequate, I'll try to
be a little more verbose.

My flatfile parser accepts a simple grammar (XML file) and an flat file
(inputstream) to parse. The result of this is a 2-dimensional array of
records (1'st dimension) with fields (2'nd dimension).

However, when time comes to actually use these records in other parts of
my applications, the fields within the records have to be accessed
through indices.

Records[record][field];

This is hard to build on and error-prone, especially as I am aiming for
a generic loosely-coupled design to parse any kind of flat file.

What I would love is to be able to use methods of the record to access
specific fields. This means I would somehow need to encapsulate a simple
record array, which currently looks like this:

Record{"Martin", "Short", "37"}

....and wrap, such that I can now access the records by appropiate calls:

getFirstname() // Returns Record[0] ("Martin")

Rather than letting fields be placed into a record array, I would be
happy with a solution where the desired "record object" is passed to the
parser, such that it can use this very object (which has the desired
accessors) to represent a record in the array.

parse(InputStream grammar,InputStream flat, RecordType NameAgeRecord );

This allows other parts of program to use the accessors like this:

( (NameAgeRecord) Record[31]).getFirstName();

I think this is the best solution for mapping a flat file, using a
descriptive grammar, to a Java runtime object. The problem I have with
this solution is that while I can pass an interface, class or object,
neither allows me to use "new" when creating the records and placing it
into the array. My current idea is that perhaps a factory can help here.

I hope this explains a little better what I am trying to do, and how I
am desperately searching for some way to customize accessors rather than
indexing [row][field] as I am currently doing.

Thank you so much for reading!

/Casper
Jul 18 '05 #5
Casper B wrote:
You are right, reflection does not allow one to create fields, etc.
dynamically. But I do not understand why you would want to. The
advantage of creating fields, methods, etc. is to invoke them by name,
in code. If you are doing that, then you already know what the names
are and you do not need to make them dynamically.

Perhaps you are looking for a simple java.util.Map implementation?

I apologise if my explanation of the problem was inadequate, I'll try to
be a little more verbose.

My flatfile parser accepts a simple grammar (XML file) and an flat file
(inputstream) to parse. The result of this is a 2-dimensional array of
records (1'st dimension) with fields (2'nd dimension).

However, when time comes to actually use these records in other parts of
my applications, the fields within the records have to be accessed
through indices.

Records[record][field];

This is hard to build on and error-prone, especially as I am aiming for
a generic loosely-coupled design to parse any kind of flat file.

What I would love is to be able to use methods of the record to access
specific fields. This means I would somehow need to encapsulate a simple
record array, which currently looks like this:

Record{"Martin", "Short", "37"}

...and wrap, such that I can now access the records by appropiate calls:

getFirstname() // Returns Record[0] ("Martin")

Rather than letting fields be placed into a record array, I would be
happy with a solution where the desired "record object" is passed to the
parser, such that it can use this very object (which has the desired
accessors) to represent a record in the array.

parse(InputStream grammar,InputStream flat, RecordType NameAgeRecord );

This allows other parts of program to use the accessors like this:

( (NameAgeRecord) Record[31]).getFirstName();

I think this is the best solution for mapping a flat file, using a
descriptive grammar, to a Java runtime object. The problem I have with
this solution is that while I can pass an interface, class or object,
neither allows me to use "new" when creating the records and placing it
into the array. My current idea is that perhaps a factory can help here.


OK, it sounds as if this is the only piece of the puzzle you are
missing. What you want is part of reflection, in particular the
java.lang.Class.newInstance() method. Look over the javadocs for this
method, it is not complex.

You may also be interested in the BeanInfo class.
I hope this explains a little better what I am trying to do, and how I
am desperately searching for some way to customize accessors rather than
indexing [row][field] as I am currently doing.

Thank you so much for reading!

/Casper


HTH,
Ray

--
XML is the programmer's duct tape.
Jul 18 '05 #6
Thanks, the newInstance() seems to be what I need!

/Casper
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
2
by: CV | last post by:
How can I match 'n' number of neighbouring words of a pattern using regular expressions? For example, suppose I am looking for the pattern "length xyz cm" in some text. where xyz is a number -...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
1
by: ltruett | last post by:
Last week I continued my series of design patterns examples using PHP 5 with the Bridge Pattern, Flyweight Pattern, and Proxy Pattern. Here now is my 20th PHP 5 design pattern example, the...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
11
by: td0g03 | last post by:
Hello, I just have a few questions. The first one be how would you print a pattern. I could use the if else, but I remember my teacher talking about something like for(i=1;i<=size;i) ...
1
by: halekio | last post by:
Hi all, Please bear with me as I've only started programming in C# 2 weeks ago and this is my first contact with OOP. I ran into a situation where I needed to catch an event in an object that...
1
by: DAXU | last post by:
Hi, I am bit confused about the differences between plugin pattern and factory pattern. Can someone explain the differences? Many Thanks Jerry
2
by: =?Utf-8?B?QWFyb24=?= | last post by:
Hi, I'm having a tricky problem where I want to accept a regular expression pattern from user input but can't get teh escape characters to be prcoessed correctly. If I take the same pattern and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.