Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

fun with getAttribute

 
Mark Ayad
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Im having problems with this snippet of code. I'm trying
to get all the key value pairs for a html tag element using
HTMLEditorKit.ParserCallback.
public void showAttributes(MutableAttributeSet attrSet)
{
Enumeration attrEnum = attrSet.getAttributeNames();

while(attrEnum.hasMoreElements())
{
String s = (attrEnum.nextElement().toString());

if(s != null)
{
attrSet.getAttribute(s); !!always returns null!!
}
)
}
}
I can Enumeration the AttributeSet which brings back all
the attribute names (s) contained in the tag. However
attrSet.getAttribute(s); !!always returns null!!.
Looking a bit deeper say the tag is < a href="index.htm" target="blank">
then the attribute names is:
href
target
now you would thought that attrSet.getAttribute("href") would bring
back index.htm, but no it returns null.
So I tried attrSet.getAttribute(HTML.Attribute.HREF) which worked.
....And so it seems that if the attribute name is defined in Class HTML.Attribute
then you can't obtain the attribute value using attrSet.getAttribute(s).
However if the attribute name is not present in Class HTML.Attribute then
attrSet.getAttribute(s) works fine.
One very bad solution is to use a lookup
if(name.equals("action"))
{
return HTML.Attribute.ACTION;
}
else if(name.equals("align"))
{
return HTML.Attribute.ALIGN;
}
else if(name.equals("alink"))
{
return HTML.Attribute.ALINK;
}

.... but this is a log if - else ladder

Any ideas??

Thanks


[This message has been edited by Jim Yingst (edited October 11, 2001).]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method you want is in the class javax.swing.text.html.HTML:
<pre>
HTML.Attribute attr = HTML.getAttributeKey(name);
return attr;
</pre>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic