Solved

Sax Parsers - Attributes object lifetime!!!!! >:(

Posted on 2003-11-30
7
362 Views
Last Modified: 2013-11-23
This is related to the project I've been working on here:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20809982.html#9840257
Guess this is will be more of a rant than anything else...  I came up with a great idea for my swing markup language - I previously had 2 arrays of interfaces; the first was called from the startElement(), and it created each object according to the attributes specified.  The second was used in endElement();  this one only dealt with filling container objects such as combos or panels -  by the time the end element tag of the container was reached, all the objects that belonged to the container were already constructed.

I thought that if I could just tag the Attributes object and place it onto my stack(instead of the complete object), I could put off constructing the object until the end tag was reached - thereby reducing my number of anonymous interface classes by the number of container tags (11)...  I would be able to combine the two function tables.

UNFORTUNATELY, the lifetime of the Attributes object seems to end when the startElement() function returns...  When I pop it from the stack in endElement(), it's empty....

_?_ tag      title           hashcode       attr.getLength()
___________________________________
begin: XMLWINDOW    -2093412697     0
begin: menubar  950367828       0
begin: menu     3347807            1
begin: menuitem -603141902      1
end: menuitem   -603141902      0
begin: menuitem -603141902      1
end: menuitem   -603141902      0
end: menu       3347807             0
begin: menu     3347807            1
end: menu       3347807            0
end: menubar    950367828     0
begin: content  951530617       0
begin: desktop  1557106716      0
begin: internal 570410685       5
begin: panel    106433028       2
begin: label    102727412       1
end: label      102727412        0
begin: check    94627080        2
end: check      94627080         0
begin: radio    108270587       2
end: radio      108270587       0
end: panel      106433028      0
end: internal   570410685      0
end: desktop    1557106716   0
end: content    951530617     0
end: XMLWINDOW  -2093412697  0

Now I'm going to have to copy it or something...  This really sux!!!!!!!!!
0
Comment
Question by:ShawnCurry
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9844799
you could use a different parser implementation.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9844919
If you want to read in the entire XML file before starting to create your objects, you could use DOM instead of SAX.  If you're going to do that, I would recommend using JDOM (http://www.jdom.org).
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9845060
This might be of use:
http://www.swixml.org/
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:ShawnCurry
ID: 9845340
Huh..   That swixml is really close to what i have...  Mine's structured a little differently..

I originally implemented this with a DOM parser...  I moved to a SAX implementation because it was supposed to be faster; and it IS noticably faster.

Using another parser implementation had occurred to me..  Any good tutorials out there??

Thanks

Shawn
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9845354
In your previous question, I mentioned the commons Digester. That also uses SAX. There's all the source and good docs for it. It works by using matching rules based on XPath in order to decide which objects are created as children of what. Very flexible. Although not primarily designed for GUIs, probably could be quite easily used for them.
0
 
LVL 92

Expert Comment

by:objects
ID: 9846417
http://www.javaworld.com/javaworld/javatips/jw-javatip128.html
Nice and light and you can tweak it to your exact requirements.
0
 
LVL 3

Author Comment

by:ShawnCurry
ID: 9918524
I did find the solution.. just had to read the source code.  I checked to see if it was a known bug, and they said it wasn't a bug..  They do provide a default implemention of the Attributes interface - AttributesImpl - with a copy constructor, in case you need to save the Attributes...

It's moving along nice..  I mapped to almost 60 of the KeyEvent.VK_CONSTANTS to implement accelerator keys for my menus.  I'm also parsing the title for a mnemonic key..  I had to use @ though - xml already uses the ampersand..  32 tags and 25 attributes..

Almost through my 4th iteration.. back up to 2 function tables - tags and attributes.. BUT - I'm down to 6 if statements ;).. That's no joke, I just counted em..   Heh figure that one out.

Thanks for the link..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse formatting 6 73
mockito example issue 8 64
Java - Why doesn't this JFrame work 3 44
MySQL  on Tomcat 8 30
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now