[Htmlparser-cvs] htmlparser/src/org/htmlparser/parserHelper SpecialHashtable.java,NONE,1.1 Attribute
Brought to you by:
derrickoswald
From: <der...@us...> - 2003-08-02 16:23:00
|
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser/parserHelper In directory sc8-pr-cvs1:/tmp/cvs-serv9188/org/htmlparser/parserHelper Modified Files: AttributeParser.java Added Files: SpecialHashtable.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. --- NEW FILE: SpecialHashtable.java --- // HTMLParser Library v1_4_20030727 - A java-based parser for HTML // Copyright (C) Dec 31, 2000 Somik Raha // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // For any questions or suggestions, you can write to me at : // Email :so...@in... // // Postal Address : // Somik Raha // Extreme Programmer & Coach // Industrial Logic Corporation // 2583 Cedar Street, Berkeley, // CA 94708, USA // Website : http://www.industriallogic.com package org.htmlparser.parserHelper; import java.util.Hashtable; import org.htmlparser.tags.Tag; /** * Acts like a regular HashTable, except some values are translated in get(String). * Specifically, <code>Tag.NULLVALUE</code> is translated to <code>null</code> and * <code>Tag.NOTHING</code> is translated to <code>""</code>. * This is done for backwards compatibility, users are expecting a HashTable, * but Tag.toHTML needs to know when there is no attribute value (<<TAG ATTRIBUTE>) * and when the value was not present (<<TAG ATTRIBUTE=>). */ public class SpecialHashtable extends Hashtable { /** * Constructs a new, empty hashtable with a default initial capacity (11) and load factor, which is 0.75. */ public SpecialHashtable () { super (); } /** * Constructs a new, empty hashtable with the specified initial capacity and default load factor, which is 0.75. */ public SpecialHashtable (int initialCapacity) { super (initialCapacity); } /** * Constructs a new, empty hashtable with the specified initial capacity and the specified load factor. */ public SpecialHashtable (int initialCapacity, float loadFactor) { super (initialCapacity, loadFactor); } /** * Returns the value to which the specified key is mapped in this hashtable. * This is translated to provide backwards compatibility. */ public Object get (Object key) { Object ret; ret = getRaw (key); if (Tag.NULLVALUE == ret) ret = null; else if (Tag.NOTHING == ret) ret = ""; return (ret); } /** * Returns the raw value to which the specified key is mapped in this hashtable. */ public Object getRaw (Object key) { return (super.get (key)); } } Index: AttributeParser.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/parserHelper/AttributeParser.java,v retrieving revision 1.31 retrieving revision 1.32 diff -C2 -d -r1.31 -r1.32 *** AttributeParser.java 27 Jul 2003 19:19:17 -0000 1.31 --- AttributeParser.java 2 Aug 2003 16:22:57 -0000 1.32 *************** *** 105,109 **** */ public Hashtable parseAttributes(Tag tag) { ! attributeTable = new Hashtable(); part = null; empty = null; --- 105,109 ---- */ public Hashtable parseAttributes(Tag tag) { ! attributeTable = new SpecialHashtable(); part = null; empty = null; *************** *** 137,151 **** } ! private void processInvalidPart() { ! if (name != null) { ! if (name.equals("/")) { ! putDataIntoTable(attributeTable,Tag.EMPTYTAG,"",false); ! } else { ! putDataIntoTable(attributeTable,name,"",false); ! } ! name=null; ! value=null; ! } ! } private boolean isValid(String part) { --- 137,161 ---- } ! private void processInvalidPart () ! { ! if (name != null) ! { ! if (name.equals ("/")) ! putDataIntoTable (attributeTable,Tag.EMPTYTAG,"",false); ! else ! { ! if (null == part) ! if ((null != value) && value == Tag.NOTHING) ! putDataIntoTable (attributeTable,name,Tag.NOTHING,false); ! else ! putDataIntoTable (attributeTable,name,Tag.NULLVALUE,false); ! else ! putDataIntoTable (attributeTable,name,"",false); ! } ! name=null; ! value=null; ! equal=false; ! } ! } private boolean isValid(String part) { *************** *** 165,171 **** equal=false; delim=DELIMETERS_WITHOUT_EQUALS; } else { ! putDataIntoTable(attributeTable,name,"",false); name=part; value=null; --- 175,182 ---- equal=false; delim=DELIMETERS_WITHOUT_EQUALS; + value=Tag.NOTHING; } else { ! putDataIntoTable(attributeTable,name,Tag.NULLVALUE,false); name=part; value=null; |