[Htmlparser-cvs] htmlparser/src/org/htmlparser/tags Tag.java,1.37,1.38
Brought to you by:
derrickoswald
From: <der...@us...> - 2003-08-02 16:23:00
|
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags In directory sc8-pr-cvs1:/tmp/cvs-serv9188/org/htmlparser/tags Modified Files: Tag.java Log Message: Fixed bug #757337 Standalone attributes should remain standalone Changing to a HashMap was not adopted for two reasons. The first is that it would break Java 1.1.x compatibility (which I believe we still maintain, although I haven't checked lately), and using a HashMap would return the attributes in a different order, leading to a *lot* of broken tests that rely on toHTML() returning a specific string with attributes in a certain order (if I'm going to fix a bunch of tests I would like the attributes to come out in the order they were originally, so this will need an AttributeStorage class when the attribute parser is rewritten). The solution adopted is to subclass HashTable and store two special values, which are new constants in the Tag class. For naive programs, and for backward compatibility, this SpecialHashtable class translates these constants into null and "" respectively, so the old behavior is the same. However, savvy programs, and the toHTML() method in the Tag class can call getRaw() on this hashtable to get at these special constants and behave appropriately. For this specific bug, toHTML returns the stand-alone attribute as is, but the missing value case is also handled. Index: Tag.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/tags/Tag.java,v retrieving revision 1.37 retrieving revision 1.38 diff -C2 -d -r1.37 -r1.38 *** Tag.java 27 Jul 2003 19:19:19 -0000 1.37 --- Tag.java 2 Aug 2003 16:22:57 -0000 1.38 *************** *** 37,40 **** --- 37,41 ---- import org.htmlparser.NodeReader; import org.htmlparser.parserHelper.AttributeParser; + import org.htmlparser.parserHelper.SpecialHashtable; import org.htmlparser.parserHelper.TagParser; import org.htmlparser.scanners.TagScanner; *************** *** 59,62 **** --- 60,65 ---- public final static String TAGNAME = "$<TAGNAME>$"; public final static String EMPTYTAG = "$<EMPTYTAG>$"; + public final static String NULLVALUE = "$<NULL>$"; + public final static String NOTHING = "$<NOTHING>$"; private final static String EMPTY_STRING=""; *************** *** 413,446 **** * @see org.htmlparser.Node#toHTML() */ ! public String toHtml() { ! StringBuffer sb = new StringBuffer(); ! sb.append("<"); ! sb.append(getTagName()); ! if (containsMoreThanOneKey()) sb.append(" "); ! String key,value; ! String empty=null; ! int i=0; ! for (Enumeration e = attributes.keys();e.hasMoreElements();) { ! key = (String)e.nextElement(); ! i++; ! if (!key.equals(TAGNAME)) { ! if (key.equals(EMPTYTAG)){ ! empty="/"; ! } else { ! value = getAttribute(key); ! sb.append(key+"=\""+value+"\""); ! if (i<attributes.size()) sb.append(" "); ! } } } ! if (empty != null) sb.append(empty); ! if (isEmptyXmlTag()) sb.append("/"); ! sb.append(">"); ! return sb.toString(); ! } ! private boolean containsMoreThanOneKey() { ! return attributes.keySet().size()>1; ! } /** --- 416,465 ---- * @see org.htmlparser.Node#toHTML() */ ! public String toHtml() ! { ! StringBuffer ret; ! String key; ! String value; ! String empty; ! ! ret = new StringBuffer (); ! ret.append ("<"); ! ret.append (getTagName ()); ! empty = null; ! for (Enumeration e = attributes.keys(); e.hasMoreElements(); ) ! { ! key = (String)e.nextElement (); ! if (!key.equals (TAGNAME)) ! { ! if (key.equals (EMPTYTAG)) ! empty="/"; ! else ! { ! ret.append (" "); ! ret.append (key); ! value = (String)(((SpecialHashtable)getAttributes()).getRaw (key.toUpperCase ())); ! if (Tag.NULLVALUE != value) ! { ! ret.append ("="); ! if (!(Tag.NOTHING == value)) ! { ! ret.append ("\""); ! ret.append (value); ! ret.append ("\""); ! } ! else ! ret.append (""); ! } ! } } } ! if (null != empty) ! ret.append (empty); ! if (isEmptyXmlTag ()) ! ret.append ("/"); ! ret.append (">"); ! return (ret.toString ()); ! } /** |