tagstack.java

来自「简单的html解释器」· Java 代码 · 共 49 行

JAVA
49
字号
package html_parser;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.ListIterator;

class NLineELink {
	boolean isNewLine = false;
	boolean isEndLink = false;
}

public class TagStack extends LinkedList<Tag> {
	final static long serialVersionUID = 8273;
	FileOutputStream dstFile;
	public TagStack(FileOutputStream dstFile) {
		this.dstFile = dstFile;  
	}
	NLineELink pop(Tag dst) throws IOException {
		boolean isPopable = isPopable(dst);
		NLineELink ret = new NLineELink();
		if (isPopable) {
			while ( this.size() != 0 ) {
				Tag oldTag = this.pop();
				
				if (oldTag.isNewLine()) {
					dstFile.write('\n');
					ret.isNewLine = true;
				}
				if (oldTag.match("a")) {
					ret.isEndLink = true;
				}
				if (oldTag.match(dst)) break;
			}
		}
		return ret;
	}
	private boolean isPopable(Tag dst) {
		ListIterator<Tag> iter = listIterator();
		boolean isPopable = false;
		while (iter.hasNext()) {
			Tag oldTag = iter.next();		
			if (oldTag.match(dst)) {
				isPopable = true;
				break;
			}
		}
		return isPopable;
	}
}

⌨️ 快捷键说明

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