[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 597
  • Last Modified:

Sax Parser / Performance Question...

I've been working on a card game applet.  This kind of sent me off on a tangent; I was working on the GUI, and I had nice data/code separation.. So I thought, hey why not store the data in a file?  Long story short, I've come up with a neat little swing markup language.  I had originally implemented it with an org.w3c.dom.DocumentBuilder.  Of course, once you start reading the JAXP spec, you can't help but notice how SAX is ending world hunger..</sarcasm>  Anyhow, I've gone to a validating SAX parser, complete with DTD..

I have 25 different tags, 10 of which are container tags; and 15 different attribute types...

As you might imagine, cramming all these into 2 methods - startElement() and endElement(); it wasn't pretty...  I was doing all kinds of string compares - in a linear fashion.  I wanted to go to a binary search, only I had to do something different (but similar)for each of the 25 tags.  My programming teacher suggested I use a hashing algorithm to elimate the string compares altogether..

So much for long story short!!!

Basically, I've created 2 simple nested interfaces:
protected interface Builder { Object make( Attributes attr ); }
protected interface Packager { void make( Tag t, SimpleStack temp ); }

Tag is just an Object and an int hashcode; SimpleStack is just that (32 lines of code). The idea is that I have an array for each interface - 2 function tables.  This enables me to do a binary search of the hashcode of each tag.  The result of the binary search indexes the function table, and the appropriate Builder/Packager is called.

Only now, my class is broken up into 40 files!!! arrgghh.  It creates a .class file for each interface in the table - XMLWindowFactory$23.class and so forth..

Please tell me there's a way to trick the compiler into leaving my tables in one file.. Like putting the interfaces in a Vector, or using a nested class instead...  Something...

A mouselistener doesn't create an extra .class file does it??  I'm basically doing the same thing..  Dang.. guess it does...

Think the binary search is worth the extra files??
0
ShawnCurry
Asked:
ShawnCurry
  • 4
  • 4
  • 2
  • +1
1 Solution
 
TimYatesCommented:
You can package your classes up into a jar file...

Unfortunately, you can't stop java producing a class file for each inner/anonymous class :-(

But by making a jar out of them, you will only have to distribute one file instead of 40...
0
 
TimYatesCommented:
> Think the binary search is worth the extra files??

It does sound nice :-)
0
 
TimYatesCommented:
If you create a manifest file like this:

----------

Manifest-Version: 1.0
Main-Class: classname

----------

save it as "MyManifest.txt", then make the jar file by:

jar -cvfm Cards.jar MyManifest.txt classes

(assuming all your classes are in a "classes" folder, and you want to make a jar file called "Cards.jar")
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
TimYatesCommented:
you can then run the application by:

java -jar Cards.jar
0
 
CEHJCommented:
Have a look at the powerful commons Digester. This can take an xml file of Cards and return you a collection of Java objects

http://jakarta.apache.org/commons/digester/
0
 
objectsCommented:
one class file per class, dats the rules :)
0
 
ShawnCurryAuthor Commented:
I apologize for the long explanation; I think most of it was necessary to see where I'm coming from..

I guess my real question for you experts is whether or not you think the binary search is worth the extra files...

Tim Yates..  Thanks for the nice explanation!  But uhh..  I already know all about jar files!!

I think having all these anonymous classes is increasing the size of the overall class (in bytes) significantly.  Correct me if i'm wrong, but there must be some kind of header information stored with each class file, right??  It creates 35k of class files from 18k of java code.

For example:

new Builder() {//combo
      public Object make( Attributes attr ) {
            JComboBox out = new JComboBox();
            ctrl[2].add( out );
            return out;
      }
},
new Builder() {//glass                                <-- THIS ONE IS 412 BYTES!!
      public Object make( Attributes attr ) {
            return null;//no object is created for glass pane
      }
},
new Builder() {//label
      public Object make( Attributes attr ) {
            JLabel out = new JLabel();
            for( int i = 0; i < attr.getLength(); i++ )
            {
                  if( attr.getQName(i).hashCode() == aname[13] )//title
                        out.setText( attr.getValue(i) );
            }
            ctrl[3].add( out );
            return out;
      }
},

Now, given the fact that many of these classes have only 3-4 lines of code to run, and the fact that I'm searching an array of 25 hashcodes (this will definately expand); is it worth it??  Keep in mind - this is an APPLET...  I'm trying to optimize the download performance as well (*I* have a dialup modem)

Granted, this question is probably better suited for the "Philosophy and Religion" "technology channel", but a little expert opinion would be greatly appreciated..

PS:  If someone were to come up with a little o-notation breakdown, bonus points would definately be involved!! :)

Thanks,
Shawn
0
 
objectsCommented:
jarring the files will decrease the size.
0
 
objectsCommented:
If you are using an inner class with the same functionality in >1 spots then you could make it a class in it's own right to avoid multiple copies of the same class being created.
0
 
objectsCommented:
0
 
ShawnCurryAuthor Commented:
I think that's what it'll utimately boil down to.  CheckBoxes and RadioButtons are virtually identical(title,tooltip,value); so are Labels,Menus,Menuitems, and Buttons(title,tooltip)...

I've already combined a couple tags...

Hmm...  Oh well..  Guess I'll actually sit down and decide once and for all what my tag/attribute sets will look like before I rewrite this too many more times..

Just added the tooltips, and I thought I'd add Icons and Images.  I think I can sqeeze toolbars in there too...  I really have my work cut out for me!!

Thanks,

Shawn
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now