⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attributelist.java

📁 利用广度优先遍历搜索一定范围内的所有网页,可用于建立搜索引擎和查找网络错误.
💻 JAVA
字号:
/**
 * Represents attributes and their values in an HTML/XML start-tag.
 *
 * @author  SeungJin Lim
 * @version 1.0, 2001/02/17
 * @since   JDK1.0
 */
package html;

import java.util.*;
public class AttributeList {
	public AttributeList( String tag ) {
	_generateAttributeList(tag);
	}

    public Enumeration getAttributes() {        return (this.attrkeys.elements());    }
    public Enumeration getAttributeValues() {        return (this.attrvalues.elements());    }

    public String getAttributeValueOf( String key ) {
		if( null==key || key.length()==0 ) return null;
		int loc = attrkeys.indexOf(key.toUpperCase());
		if( loc>=0 ) return (String.valueOf(this.attrvalues.elementAt(loc)));
		else return null;    }
    public void putAttributeValueOf( String key, String val ) {
		if( null==key || key.length()==0 ) return;
		int loc = attrkeys.indexOf(key.toUpperCase());
		if( loc>=0 ) this.attrvalues.setElementAt(val, loc);
    }
    public Hashtable toHashtable() {		Hashtable<String, String> ht = new Hashtable<String, String>(1);
		for( int i=0; i<attrkeys.size(); i++ ) {
			String key = String.valueOf(attrkeys.elementAt(i));
			String value = String.valueOf(attrvalues.elementAt(i));
		    ht.put(key, value);
		} // for i
		return(ht);
    }

    /**     * Returns a <code>String</code> representation of the attribute list
     *     * @param  none none     * @return     String representation of attribute list     * @exception       *             If the named encoding is not supported     * @since      1.2     */    public String toString() {		StringBuffer sb = new StringBuffer();
		for( int i=0; i<attrkeys.size(); i++ ) {
			String key = String.valueOf(attrkeys.elementAt(i));
			String value = String.valueOf(attrvalues.elementAt(i));
			sb.append(key+"=\""+value+"\" ");
		} // for i
		if( sb.length()>1 ) sb.setLength(sb.length()-1); 
		return(new String(sb));
    }

	public static void main( String[] args ) {		String[] src = { 				"<A href =  \'http://www.ucjournal.com/StoryLink.php3?id=1342877&amp;c=1130314&amp;s=8\' src = fig1.jpg>",				"<img src = fig1.jpg  alt= \"Figure 1\" width=40  height=30>"		};				AttributeList[] attrlist = new AttributeList[src.length];		for( int i=0; i<attrlist.length; i++ ) {		attrlist[i] = new AttributeList( src[i] );		System.out.println("Input: " + src[i]);		//System.out.print("- Keys: ");		//for (Enumeration e = attrlist[i].getKeys() ; e.hasMoreElements() ;) {		//	String key = String.valueOf(e.nextElement());		//	System.out.print("\n\t"+key+": "+attrlist[i].getValue(key));		//}		//System.out.println();		System.out.println("\t- Normal form: " + attrlist[i]);		} // for i		//System.out.println("- Value(src): " + attrlist.getValue("src"));		//System.out.println("- Value(ALT): " + attrlist.getValue("ALT"));	}	// =========================================================================
	//	Private methods
	// =========================================================================

    //---------------------------------------------------------------------------
	//
    //	Attribute names will be uppercased.
	//
	//	node will look like
	//	<A href="http://www.ucjournal.com/StoryLink.php3?id=1342877&amp;c=1130314&amp;s=8">
    //---------------------------------------------------------------------------
    private void _generateAttributeList(String node) {
		// There must exist at least one '=' if there exist
		// attributes in this node -------------------------------------
	    if(node==null || node.equals("") || node.indexOf("=")<0) return;

		if(_DEBUG) System.out.println("AttributeList._generateAttributeList: node=" + node);

		parsePosition = -1;
		String tmp;

	    try{
            // if there exists an attribute, there must be a space
            // between the tag name and the first attribute --------------------
			parsePosition = node.indexOf(" ");
			if( parsePosition<0 ) return;

			// Exclude the space
			parsePosition++;
			// tmp is the rest of the string following the space
			tmp = node.substring(parsePosition);
			// Move parsePosition to the beginning of tmp
			parsePosition = 0;

			if( _DEBUG ) System.out.println("AttributeList._generateAttributeList: tmp="+tmp+", parsePosition="+parsePosition);

			// Find the first '='
            parsePosition = tmp.indexOf("=", parsePosition);
			String attrName = null;
			String attrValue = null;

			// there exists a valid attribute.
			while( parsePosition>0 ) {
				// parsePosition points the first '=' in tmp.
				// ASSERT: _getLeftFirstStr() does not alter tmp nor parsePosition.
			    attrName = _getLeftFirstStr(tmp, parsePosition);

				// Exclude the '=' sign prior to passing it to	_getRightFirstStr()
				tmp = tmp.substring(parsePosition+1);
				if(_DEBUG) System.out.println("AttributeList._generateAttributeList: 1. attrName="+attrName+", tmp="+tmp+", parsePosition="+parsePosition);

				// ASSERT: _getRightFirstStr() will alter parsePosition to the very next char
				// after this attribute value
			    attrValue = _getRightFirstStr(tmp);

				// tmp is the rest of the string following attrValue
				tmp = tmp.substring(parsePosition);
				// Move parsePosition to the beginning of tmp
				parsePosition = 0;

				if(_DEBUG) System.out.println("AttributeList._generateAttributeList: 2. attrValue="+attrValue+", tmp="+tmp+", parsePosition="+parsePosition);

			    attrkeys.addElement(attrName.toUpperCase());
			    attrvalues.addElement(attrValue);

                parsePosition = tmp.indexOf("=", parsePosition);
			} // while
		}
		catch (Exception ex) {
		}
	} // _getAttributeListOf(String node)

    //---------------------------------------------------------------------------
    //
    // Returns the space-trimmed string on the left of the index-th character
    //	in s.
	//
	//	s will look like
	//	href="http://www.ucjournal.com/StoryLink.php3?id=1342877&amp;c=1130314&amp;s=8">
	//	index points the first '=' in s.
	//
    //---------------------------------------------------------------------------
    private String _getLeftFirstStr(String s, int index) {
	    StringBuffer word = new StringBuffer();

		// Move pointer to the left of '=' in s.
		index--;

        // remove spaces between the target word and the char at index.
        while(s.charAt(index)==' ')  index--;

		// Collect all non-space characters in a backward order
        while(index>-1 && s.charAt(index)!=' ') {
		    word.append(s.charAt(index));
			index--;
		} // while index
		return(new String(word.reverse()));
	} // getLeftFirstStr(String s, int index)


    //---------------------------------------------------------------------------
    //
    // Returns the first double-quoted, single-quoted or space-delimited string 
    //   in s.
    //
	//	s will look like
	//	"http://www.ucjournal.com/StoryLink.php3?id=1342877&amp;c=1130314&amp;s=8">
	//	without leading '='
	//
    //---------------------------------------------------------------------------
    private String _getRightFirstStr(String s) {
		if( null==s || 0==s.length() ) return "";

		if(_DEBUG) System.out.println("AttributeList._getRightFirstStr: s="+s);

		int lower = 0;
		String delim = "";

        // remove all preceding spaces
        while( s.charAt(lower)==' ' ) lower++;

		// This value is not quoted but Space-delimited.
		if( s.charAt(lower)!='\"' && s.charAt(lower)!='\'' ) {  
		    StringBuffer word = new StringBuffer("");
            while( lower<s.length() && s.charAt(lower)!=' ' && s.charAt(lower)!='>' ) {
		        word.append(s.charAt(lower++));
		    } // while
			parsePosition = lower;
		    return(new String(word));
		} // if

		// This value is double-quoted.
        if(s.charAt(lower)=='\"') delim = "\"";
		// This value is single-quoted. 
        else if(s.charAt(lower)=='\'') delim = "\'";
        
		int endQuote = s.indexOf(delim, lower+1);
		if( endQuote>-1 ) {
			// Move parsePosition right after the ending double quote
			parsePosition = endQuote+1;
			if(_DEBUG) System.out.println("AttributeList._getRightFirstStr: parsePosition="+parsePosition+", endQuote="+endQuote);
		    return(s.substring(lower+1, endQuote));
		} // if endQuote
		else return "";

	} // getRightFirstStr(String s, int index)




	// =========================================================================
	//	Globals
	// =========================================================================

	private boolean _DEBUG = false;

	//-------------------------------------------------------------
	// main storage: attrList is a hashtable of key/value pairs
	//-------------------------------------------------------------
	private Vector<String> attrkeys = new Vector<String>(0);
	private Vector<String> attrvalues = new Vector<String>(0);

	//-------------------------------------------------------------
    // 
	//-------------------------------------------------------------
    int parsePosition;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -