万能的树(HtmlTree)
看到置顶的两个贴子竟然是目录树,感慨良多,java这么强大的功能,单单只是为了目录树是不是有点太可惜了,我写了一段代码,可以在树上加入超连接、checkBox、select、甚至table,所有implements 了如下Component interface 的组件都可以。一下是代码,欢迎各位高手指教:
以下一个接口两个抽象类是组件包的基础,可以方便的以他们为基础编写自己的组件
//********************************
package htmlComponent;
/**
* @author brian.yang
*组件包的唯一接口
*/
public interface Component {
Object getKey();//用来定位组件的标识
String getHtmlSource();
}
//***********************************
package htmlComponent;
/**
* @author brian.yang
*为了方便编写组件而写的抽象类
*/
public abstract class AbstractComponent implements Component,Comparable {
protected Object key;
protected Attributes attrs;
public AbstractComponent(){
this(null,null);
}
public AbstractComponent(Object key){
this(key,null);
}
public AbstractComponent(Object key,Attributes attributes){
this.key=key;
this.attrs=new Attributes();
if (attributes!=null)
this.attrs.putAll(attributes);
}
public String getName() {
return key.toString();
}
public java.util.HashMap getAttributes() {
return attrs;
}
public String getTag() {
return "";
}
public String getContent(){
return "";
}
public String getEndTag() {
return "";
}
public Object getKey(){
return key;
}
public int compareTo(Object o){
if (o instanceof Component)
return ((Comparable)getKey()).compareTo(
((AbstractComponent)o).getKey() );
return ((Comparable)getKey()).compareTo(o);
}
public boolean equals(Object o){
if (o instanceof Component)
return getKey().equals(((Component)o).getKey());
return getKey().equals(o);
}
public String getHtmlSource(){
//根据html标签的特点进一步将getHtmlSource()分成了三个方法
return getTag()+getContent()+getEndTag();
}
public String toString(){
return getHtmlSource();
}
}
//*************************
package htmlComponent;
/**
* @author brian.yang
*含有多个元素的组件的基类,如:<div>块,<select>,<table>,<tr>等
*/
public abstract class MultiComponent extends AbstractComponent {
protected java.util.ArrayList elements=new java.util.ArrayList();
protected String seperator="";
public MultiComponent() {
super();
}
public MultiComponent(Object key) {
super(key);
}
public MultiComponent(Object key, Attributes attributes) {
super(key, attributes);
}
public java.util.List getElements(){
return elements;
}
/* *
*将包含的多个elements输出的代码,包含对实现了Component
*接口的对象的特殊处理
*/
public String getContent(){
String strContent="";
if (elements.size()>0)
if(elements.get(0) instanceof Component){
Component o=(Component)elements.get(0);
strContent+=o.getHtmlSource();
}else strContent=elements.get(0).toString();
for(int i=1 ;i<elements.size();i++)
if(elements.get(i) instanceof Component){
Component o=(Component)elements.get(i);
strContent+=seperator+o.getHtmlSource();
}else
strContent+=seperator+elements.get(i);
return strContent;
}
}
//******************************
package htmlComponent;
/**
* @author brian.yang
*标签的属性类,无关紧要,可以略过
*
*/
public class Attributes extends java.util.HashMap {
public static final String NAME="name";
public static final String VALUE="value";
public static final String SRC="src";
public static final String LINK="href";
public static final String BORDER="border";
public static final String HEIGHT="height";
public static final String ONCLICK="onclick";
public class Input{
public static final String TYPE="type";
}
public class Style{
public static final String STYLEID="Id";
public static final String STYLECLASS="Class";
public static final String STYLE="style";
}
public Attributes(){
super();
}
/*未完成
public Attributes(String strAttrs){
super();
}*/
public String toString(){
String strAttr="";
java.util.Iterator it=this.entrySet().iterator();
while(it.hasNext()){
java.util.Map.Entry entry=(java.util.Map.Entry)it.next();
if (entry.getValue()==null)
strAttr+= " "+entry.getKey();
else
strAttr+=" "+entry.getKey()+"=\""+entry.getValue()+"\" ";
}
return strAttr;
}
}
以下一个接口两个抽象类是组件包的基础,可以方便的以他们为基础编写自己的组件
//********************************
package htmlComponent;
/**
* @author brian.yang
*组件包的唯一接口
*/
public interface Component {
Object getKey();//用来定位组件的标识
String getHtmlSource();
}
//***********************************
package htmlComponent;
/**
* @author brian.yang
*为了方便编写组件而写的抽象类
*/
public abstract class AbstractComponent implements Component,Comparable {
protected Object key;
protected Attributes attrs;
public AbstractComponent(){
this(null,null);
}
public AbstractComponent(Object key){
this(key,null);
}
public AbstractComponent(Object key,Attributes attributes){
this.key=key;
this.attrs=new Attributes();
if (attributes!=null)
this.attrs.putAll(attributes);
}
public String getName() {
return key.toString();
}
public java.util.HashMap getAttributes() {
return attrs;
}
public String getTag() {
return "";
}
public String getContent(){
return "";
}
public String getEndTag() {
return "";
}
public Object getKey(){
return key;
}
public int compareTo(Object o){
if (o instanceof Component)
return ((Comparable)getKey()).compareTo(
((AbstractComponent)o).getKey() );
return ((Comparable)getKey()).compareTo(o);
}
public boolean equals(Object o){
if (o instanceof Component)
return getKey().equals(((Component)o).getKey());
return getKey().equals(o);
}
public String getHtmlSource(){
//根据html标签的特点进一步将getHtmlSource()分成了三个方法
return getTag()+getContent()+getEndTag();
}
public String toString(){
return getHtmlSource();
}
}
//*************************
package htmlComponent;
/**
* @author brian.yang
*含有多个元素的组件的基类,如:<div>块,<select>,<table>,<tr>等
*/
public abstract class MultiComponent extends AbstractComponent {
protected java.util.ArrayList elements=new java.util.ArrayList();
protected String seperator="";
public MultiComponent() {
super();
}
public MultiComponent(Object key) {
super(key);
}
public MultiComponent(Object key, Attributes attributes) {
super(key, attributes);
}
public java.util.List getElements(){
return elements;
}
/* *
*将包含的多个elements输出的代码,包含对实现了Component
*接口的对象的特殊处理
*/
public String getContent(){
String strContent="";
if (elements.size()>0)
if(elements.get(0) instanceof Component){
Component o=(Component)elements.get(0);
strContent+=o.getHtmlSource();
}else strContent=elements.get(0).toString();
for(int i=1 ;i<elements.size();i++)
if(elements.get(i) instanceof Component){
Component o=(Component)elements.get(i);
strContent+=seperator+o.getHtmlSource();
}else
strContent+=seperator+elements.get(i);
return strContent;
}
}
//******************************
package htmlComponent;
/**
* @author brian.yang
*标签的属性类,无关紧要,可以略过
*
*/
public class Attributes extends java.util.HashMap {
public static final String NAME="name";
public static final String VALUE="value";
public static final String SRC="src";
public static final String LINK="href";
public static final String BORDER="border";
public static final String HEIGHT="height";
public static final String ONCLICK="onclick";
public class Input{
public static final String TYPE="type";
}
public class Style{
public static final String STYLEID="Id";
public static final String STYLECLASS="Class";
public static final String STYLE="style";
}
public Attributes(){
super();
}
/*未完成
public Attributes(String strAttrs){
super();
}*/
public String toString(){
String strAttr="";
java.util.Iterator it=this.entrySet().iterator();
while(it.hasNext()){
java.util.Map.Entry entry=(java.util.Map.Entry)it.next();
if (entry.getValue()==null)
strAttr+= " "+entry.getKey();
else
strAttr+=" "+entry.getKey()+"=\""+entry.getValue()+"\" ";
}
return strAttr;
}
}
...全文
相关推荐
前端开源库-htmltree
htmltree"是一个专为前端设计的开源库,它致力于将HTML源代码转化为简单的树数据结构,方便进行DOM操作、解析和分析。在现代前端开发中,HTML作为页面的基础结构,它的解析和处理至关重要。
HTMLTree:HTML节点树工具箱
HTML树HTML节点树工具箱。 一个用于创建HTML节点树对象的Python 2和3库。 尽管在某些方面类似于ElementTree,但它是专为HTML重新设计的,并不遵循ElementTree的许
【QGIS跨平台编译】之【libXML2跨平台编译】:Windows环境下编译成果(支撑QGIS跨平台编译,以及二次研发)
本文档展示了libXML2在Windows环境下的跨平台编译成果,包含HTML解析、SAX接口、XML树操作及规范化功能,头文件涵盖HTMLparser、HTMLtree、SAX1/SAX2和c14n
XML解析器LIBXML2多个平台下的库和头文件
包含SAX1/SAX2接口、HTML非验证解析器及树形结构处理API,主要用于构建DOM树。部分模块已废弃,建议使
Matlab爬虫程序,matlab爬虫代码,matlab
**解析HTML**:使用htmlTree或其他解析工具解析网页内容,找到数据所在的位置。4. **提取数据**:使用正则表达式或XPath选择器提取目标数据,如股票价格、日期等。5.


Web 开发
