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

📄 htmlnode.java

📁 java html 解析小程序,文件包很小
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	/**	 * Adds an object to this node's content before	 * the specified child node.	 * @param child the object to be added.	 * @param before the node before which the child will be placed.	 */	public void addChildBefore (Object child, HTMLNode before) {		int total; // Total number of child nodes.		int idx;   // Index of the 'before' node.		// Return if the child is invalid.		if (child == null) return;		// Return if this node has no children.		if (children == null) return;		// Add the child at the beginning if the before node is		// invalid.		if (before == null) {			addChild(child);			return;		}		total = children.size();		idx = children.indexOf(before);			// Add the child to the beginning if the 'before' node		// was not found.		if (idx < 0) idx = 0;		// Return if the child is not of the right type.		if (! ((child instanceof String) ||		  (child instanceof HTMLNode))) return;		// Check if the 'before' node is the last node.		if (idx == total - 1) {			// Add the child to the end of the list.			children.addElement(child);		} else {			// Add the child before the 'before' node.			children.insertElementAt(child, idx);		}		// If the child is an HTMLNode, set its parent.		if (child instanceof HTMLNode)			((HTMLNode) child).setParent(this);	}	/**	 * Removes an attribute with the specified name from the	 * attribute list.	 * @param name the name of the attribute to remove.	 */	public void removeAttribute (String name) {		// Return if the attribute list is not there.		if (attr == null) return;		// Otherwise, remove the attribute from the list.		attr.unset(name);	}	/**	 * Returns the node after this one in the parent's	 * list of children.	 */	public HTMLNode nextSibling () {		// Return nothing if the node has no parent.		if (parent == null) return null;		// Ask the parent to return the node after this one.		return parent.nextChild(this);	}	/**	 * Returns the node before this one in the parent's	 * list of children.	 */	public HTMLNode previousSibling () {		// Return nothing if the node has no parent.		if (parent == null) return null;		// Ask the parent to return the node before this one.		return parent.previousChild(this);	}	/**	 * Returns the first child of this node.	 */	public HTMLNode firstChild () {		Enumeration list; // Enumeration of this node's children.		Object curr;	  // Current node from the list.		// Return nothing if this node has no children.		if (children == null) return null;		// Return the first child node.		list = children.elements();		while (list.hasMoreElements()) {			curr = list.nextElement();			// Return the first HTMLNode in the list.			if (curr instanceof HTMLNode)				return (HTMLNode) curr;		}		// Return nothing if there were no HTMLNodes in the list.		return null;	}	/**	 * Returns the HTMLNode after the specified one in this	 * nodes content.	 * @param child the HTMLNode before the one we want.	 */	public HTMLNode nextChild (HTMLNode child) {				Enumeration list;	 // List of this node's children.		Object curr;		 // Current object from the list.		boolean getNext = false; // True when child has been found.		// Return nothing if this node has no children.		if (children == null) return null;		// Get a list of this node's children		list = children.elements();		while (list.hasMoreElements()) {			curr = list.nextElement();			// Check if we have found the specified child.			if (getNext) {				// Return the next HTMLNode we encounter.				if (curr instanceof HTMLNode)					return (HTMLNode) curr;			} else {				// Check if we have found the specified child.				if (curr == child) getNext = true;			}		}		return null;	}	/**	 * Returns the HTMLNode before the specified one in this	 * nodes content.	 * @param child the HTMLNode after the one we want.	 */	public HTMLNode previousChild (HTMLNode child) {				Enumeration list;	   // List of this node's children.		Object curr;		   // Current object from the list.		HTMLNode prev = null;      // Stores last found HTMLNode.		boolean returnPrev = true; // True when child has been found.		// Return nothing if this node has no children.		if (children == null) return null;		// Get a list of this node's children		list = children.elements();		while (list.hasMoreElements()) {			curr = list.nextElement();			// Check if we have found the specified child.			if (curr == child) return prev;			// Check if curr is an HTMLNode.			if (curr instanceof HTMLNode) {				// Make curr the previously found HTMLNode.				prev = (HTMLNode) curr;			}		}		return null;	}	/**	 * Parses the contents of this HTML node from the enumeration	 * of tokens provided.	 * @param src an enumeration of tokens.	 */	private Vector parseChildren (Enumeration src) {		// Create a new Vector to store the contents.		Vector store = new Vector();		// Loop round the enumeration of tokens.		while (src.hasMoreElements()) {			// Get the next token from the enumeration.			Object token = src.nextElement();			// Check if the token is simple text.			if (token instanceof TextToken) {				// Cast the token into type TextToken.				TextToken text = (TextToken) token;				// Add the text string to the vector.				store.addElement(text.getText());				continue;			} 			// Check if the token is a tag.			if (token instanceof TagToken) {				// Cast the token into type TagToken.				TagToken tag = (TagToken) token;				// Check if the token is an end tag.				if (tag.isEndTag()) {				  // Break if the end tags name matches.				  if (name != null &&				    name.equals(tag.getName())) break;				  // Otherwise ignore the end tag.				  continue;				}				// Otherwise make it into an HTMLNode.				HTMLNode he =				  new HTMLNode(tag, this, src);				// Add the node to the vector.				store.addElement(he);			}		}		if (store.size() > 0)			return store;		else			return null;	}	 	/**	 * String of default node names which are standalone.	 */	private static String[] defaultStandaloneList = {		"area", "base", "basefont", "bgsound", "br",		"col", "dd", "dl", "dt", "font", "frame",		"hr", "img", "input", "isindex", "li",		"link", "meta", "nextid", "option", "overlay", "p",		"param", "tab", "wbr", "!", "!--"	};	// Full list of standalone names.	private static Vector standaloneList = null;	// Load the default standalones into the list after class resolution.	static {		setupStandaloneList();	}	/**	 * Utility method which people can use to find out exactly	 * which nodes are in the default standalone list. The default 	 * list is printed to the standard output.	 */	public static void printDefaultStandaloneList () {		System.out.println(defaultStandaloneList);	}	/**	 * Adds the specified string to the standalone list.	 * @param name the new standalone name.	 */	public static void addStandalone (String name) {		// Check if the list has been initialized first.		if (standaloneList == null) return;		// Convert the String to lower case.		String lc = name.toLowerCase();		// Check that the list does not have the String already.		if (standaloneList.contains(lc)) return;		// Otherwise add the lowercase string to the list.		standaloneList.addElement(lc);	}	/**	 * Removes the specified string from the standalone list.	 * @param name the standalone name to remove.	 */	public static void removeStandalone (String name) {		// Check if the standaloneList has been initialized first.		if (standaloneList == null) return;		// Convert the String to lower case.		String lc = name.toLowerCase();		// Remove the lowercase string from the list.		standaloneList.removeElement(lc);	}	/**	 * Checks the standalone list to see if it mentions the specified	 * tag name and returns true if so.	 * @param name the tag name to check against the list.	 */	public static boolean isStandalone (String name) {		// Check if the standaloneList has been initialized first.		if (standaloneList == null) return true;		// Otherwise check the list to see if it contains the tag name.		return standaloneList.contains(name);	}	/**	 * Sets up the standalone vector at runtime using the list of	 * default standalone tags. New standalone tags can then be added	 * to the vector. <p>	 * This method will only be executed once, since it is guarded	 * by a private boolean variable.	 */	private static void setupStandaloneList () {		// Create a new vector to store the defaults.		standaloneList = new Vector(defaultStandaloneList.length);		// Add all of the strings in the default list.		for (int i = 0; i < defaultStandaloneList.length; i++)			standaloneList.addElement(defaultStandaloneList[i]);	}}

⌨️ 快捷键说明

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