📄 pageattribute.java
字号:
// HTMLParser Library $Name: v1_6 $ - A java-based parser for HTML// http://sourceforge.org/projects/htmlparser// Copyright (C) 2004 Derrick Oswald//// Revision Control Information//// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/lexer/PageAttribute.java,v $// $Author: derrickoswald $// $Date: 2005/05/15 11:49:04 $// $Revision: 1.4 $//// 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//package org.htmlparser.lexer;import org.htmlparser.Attribute;/** * An attribute within a tag on a page. * This attribute is similar to Attribute but 'lazy loaded' from the * <code>Page</code> by providing the page and cursor offsets * into the page for the name and value. This is done for speed, since * if the name and value are not needed we can avoid the cost and memory * overhead of creating the strings. * <p> * Thus the property getters, defer to the base class unless the property * is null, in which case an attempt is made to read it from the underlying * page. Optimizations in the predicates and length calculation defer the * actual instantiation of strings until absolutely needed. */public class PageAttribute extends Attribute{ /** * The page this attribute is extracted from. */ protected Page mPage; /** * The starting offset of the name within the page. * If negative, the name is considered <code>null</code>. */ protected int mNameStart; /** * The ending offset of the name within the page. */ protected int mNameEnd; /** * The starting offset of the value within the page. * If negative, the value is considered <code>null</code>. */ protected int mValueStart; /** * The ending offset of the name within the page. */ protected int mValueEnd; /** * Create an attribute. * @param page The page containing the attribute. * @param name_start The starting offset of the name within the page. * If this is negative, the name is considered null. * @param name_end The ending offset of the name within the page. * @param value_start he starting offset of the value within the page. * If this is negative, the value is considered null. * @param value_end The ending offset of the value within the page. * @param quote The quote, if any, surrounding the value of the attribute, * (i.e. ' or "), or zero if none. */ public PageAttribute (Page page, int name_start, int name_end, int value_start, int value_end, char quote) { mPage = page; mNameStart = name_start; mNameEnd = name_end; mValueStart = value_start; mValueEnd = value_end; setName (null); setAssignment (null); setValue (null); setQuote (quote); } // // provide same constructors as super class // private void init () { mPage = null; mNameStart = -1; mNameEnd = -1; mValueStart = -1; mValueEnd = -1; } /** * Create an attribute with the name, assignment string, value and quote given. * If the quote value is zero, assigns the value using {@link #setRawValue} * which sets the quote character to a proper value if necessary. * @param name The name of this attribute. * @param assignment The assignment string of this attribute. * @param value The value of this attribute. * @param quote The quote around the value of this attribute. */ public PageAttribute (String name, String assignment, String value, char quote) { super (name, assignment, value, quote); init (); } /** * Create an attribute with the name, value and quote given. * Uses an equals sign as the assignment string if the value is not * <code>null</code>, and calls {@link #setRawValue} to get the * correct quoting if <code>quote</code> is zero. * @param name The name of this attribute. * @param value The value of this attribute. * @param quote The quote around the value of this attribute. */ public PageAttribute (String name, String value, char quote) { super (name, value, quote); init (); } /** * Create a whitespace attribute with the value given. * @param value The value of this attribute. * @exception IllegalArgumentException if the value contains other than * whitespace. To set a real value use {@link #PageAttribute(String,String)}. */ public PageAttribute (String value) throws IllegalArgumentException { super (value); init (); } /** * Create an attribute with the name and value given. * Uses an equals sign as the assignment string if the value is not * <code>null</code>, and calls {@link #setRawValue} to get the * correct quoting. * @param name The name of this attribute. * @param value The value of this attribute. */ public PageAttribute (String name, String value) { super (name, value); init (); } /** * Create an attribute with the name, assignment string and value given. * Calls {@link #setRawValue} to get the correct quoting. * @param name The name of this attribute. * @param assignment The assignment string of this attribute. * @param value The value of this attribute. */ public PageAttribute (String name, String assignment, String value) { super (name, assignment, value); init (); } /** * Create an empty attribute. * This will provide "" from the {@link #toString} and * {@link #toString(StringBuffer)} methods. */ public PageAttribute () { super (); init (); } /** * Get the name of this attribute. * The part before the equals sign, or the contents of the * stand-alone attribute. * @return The name, or <code>null</code> if it's just a whitepace * 'attribute'. */ public String getName () { String ret; ret = super.getName (); if (null == ret) { if ((null != mPage) && (0 <= mNameStart)) { ret = mPage.getText (mNameStart, mNameEnd); setName (ret); // cache the value } } return (ret); } /** * Get the name of this attribute. * @param buffer The buffer to place the name in. * @see #getName() */ public void getName (StringBuffer buffer) { String name; name = super.getName (); if (null == name) { if ((null != mPage) && (0 <= mNameStart)) mPage.getText (buffer, mNameStart, mNameEnd); } else buffer.append (name); } /** * Get the assignment string of this attribute. * This is usually just an equals sign, but in poorly formed attributes it * can include whitespace on either or both sides of an equals sign. * @return The assignment string. */ public String getAssignment () { String ret; ret = super.getAssignment (); if (null == ret) { if ((null != mPage) && (0 <= mNameEnd) && (0 <= mValueStart)) { ret = mPage.getText (mNameEnd, mValueStart); // remove a possible quote included in the assignment // since mValueStart points at the real start of the value if (ret.endsWith ("\"") || ret.endsWith ("'")) ret = ret.substring (0, ret.length () - 1); setAssignment (ret); // cache the value } } return (ret); } /** * Get the assignment string of this attribute. * @param buffer The buffer to place the assignment string in. * @see #getAssignment() */ public void getAssignment (StringBuffer buffer) { int length; char ch; String assignment; assignment = super.getAssignment (); if (null == assignment) { if ((null != mPage) && (0 <= mNameEnd) && (0 <= mValueStart)) { mPage.getText (buffer, mNameEnd, mValueStart); // remove a possible quote included in the assignment // since mValueStart points at the real start of the value length = buffer.length () - 1; ch = buffer.charAt (length); if (('\'' == ch) || ('"' == ch)) buffer.setLength (length); } } else buffer.append (assignment); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -