Link to home
Start Free TrialLog in
Avatar of ShawnCurry
ShawnCurry

asked on

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

This is related to the project I've been working on here:
https://www.experts-exchange.com/questions/20809982/Sax-Parser-Performance-Question.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!!!!!!!!!
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you could use a different parser implementation.
Avatar of jimmack
jimmack

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).
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ShawnCurry

ASKER

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
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.
http://www.javaworld.com/javaworld/javatips/jw-javatip128.html
Nice and light and you can tweak it to your exact requirements.
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..