Solved

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

Posted on 2003-11-30
7
361 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
Comment Utility
you could use a different parser implementation.
0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
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
Comment Utility
This might be of use:
http://www.swixml.org/
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Author Comment

by:ShawnCurry
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
word0 challenge 4 52
java set up 1 45
Java Message handling in Service Layer 3 37
eclipse compiler vs Installed JREs option 3 36
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

15 Experts available now in Live!

Get 1:1 Help Now