Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

Q: Pattern for this?

Thread view: 
Casper B - 03 May 2005 15:07 GMT
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
Raymond DeCampo - 03 May 2005 21:37 GMT
> I have a parser that validates and constructs 2-dimentional array from
> data within a flat file:
[quoted text clipped - 15 lines]
> 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

Signature

XML is the programmer's duct tape.

Casper B - 06 May 2005 15:08 GMT
> 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
Raymond DeCampo - 06 May 2005 18:00 GMT
>> Assuming you pass the parser some information about the names of the
>> columns, this looks like a job for JavaBeans and reflection.  With
[quoted text clipped - 4 lines]
> 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

Signature

XML is the programmer's duct tape.

Casper B - 06 May 2005 23:33 GMT
> You are right, reflection does not allow one to create fields, etc.
> dynamically.  But I do not understand why you would want to.  The
[quoted text clipped - 3 lines]
>
> 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
Raymond DeCampo - 07 May 2005 02:19 GMT
>> You are right, reflection does not allow one to create fields, etc.
>> dynamically.  But I do not understand why you would want to.  The
[quoted text clipped - 46 lines]
> 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
[quoted text clipped - 3 lines]
>
> /Casper

HTH,
Ray

Signature

XML is the programmer's duct tape.

Casper B - 07 May 2005 21:47 GMT
Thanks, the newInstance() seems to be what I need!

/Casper


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.