📄 stanza.java
字号:
package com.hiany.xml;
import java.util.HashMap;
import java.util.Set;
import org.apache.log4j.Logger;
/**
* XML节点对象,代表XML的一个节点 如“<message from="1" to="2">hello,world!</message>”
*
*/
public class Stanza {
/**
* 部分
*/
public class Part {
/**
* 前
*/
public static final int FRONT = 1;
/**
* 中
*/
public static final int BODY = 2;
/**
* 后
*/
public static final int BACK = 3;
}
static Logger logger = Logger.getLogger(Stanza.class);
/**
* 节点名
*/
private String name;
/**
* 属性
*/
private HashMap<String, String> attributes = new HashMap<String, String>();
/**
* 文本
*/
private String text = "";
/**
* XML节点是否已经闭合 注:若未闭合,则text值为NULL。
*/
private boolean closed;
/**
* @param attribute
* The attribute to get.
*/
public String getAttribute(String attr) {
return this.attributes.get(attr);
}
/**
* @param attribute
* The attribute to set.
*/
public void setAttribute(String attr, String value) {
this.attributes.put(attr, value);
}
/**
* @return Returns the text.
*/
public String getText() {
return text;
}
/**
* @param text
* The text to set.
*/
public void setText(String content) {
this.text = content;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name
* The name to set.
*/
public void setName(String name) {
if (name != null) {
this.name = name.toLowerCase();
}
}
/**
* @return Returns the closed.
*/
public boolean isClosed() {
return closed;
}
/**
* @param closed
* The closed to set.
*/
public void setClosed(boolean completed) {
this.closed = completed;
}
/**
* 合并两个Stanza <br />
* 合并前提:两个Stanza的name必须相同,this必须是未闭合的,secondhalf必须是闭合的。<br />
* 合并规则:属性取this的,text取secondhalf的,设置返回的Stanza(也是this)为闭合。
*
* @param secondhalf
* @return this
*/
public Stanza merge(Stanza secondhalf) {
if (this.getName().equals(secondhalf.getName()) && !this.isClosed()
&& secondhalf.isClosed()) {
this.setText(secondhalf.getText());
this.setClosed(true);
}
return this;
}
/**
* @return the attributes
*/
public Set getAttributeNames() {
return attributes.keySet();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -