package org.dom4j;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
public interface Node extends Cloneable {
.
.
.
/**
* <p>
* <code>accept</code> is the method used in the Visitor Pattern.
* </p>
*
* @param visitor
* is the visitor in the Visitor Pattern
*/
void accept(Visitor visitor);
.
.
.
}
<!------------------------------------------------------------------------------------->
/*
* Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
*
* This software is open source.
* See the bottom of this file for the licence.
*
* $Id: Visitor.java,v 1.6 2004/10/16 19:08:08 maartenc Exp $
*/
package org.dom4j;
/** <p><code>Visitor</code> is used to implement the <code>Visitor</code>
* pattern in DOM4J.
* An object of this interface can be passed to a <code>Node</code> which will
* then call its typesafe methods.
* Please refer to the <i>Gang of Four</i> book of Design Patterns
* for more details on the <code>Visitor</code> pattern.</p>
*
* <p> This
* <a href="http://www.patterndepot.com/put/8/JavaPatterns.htm">site</a>
* has further discussion on design patterns and links to the GOF book.
* This <a href="http://www.patterndepot.com/put/8/visitor.pdf">link</a>
* describes the Visitor pattern in detail.
* </p>
*
* @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
* @version $Revision: 1.6 $
*/
public interface Visitor {
/** <p>Visits the given <code>Document</code></p>
*
* @param document is the <code>Document</code> node to visit.
*/
public void visit(Document document);
/** <p>Visits the given <code>DocumentType</code></p>
*
* @param documentType is the <code>DocumentType</code> node to visit.
*/
public void visit(DocumentType documentType);
/** <p>Visits the given <code>Element</code></p>
*
* @param node is the <code>Element</code> node to visit.
*/
public void visit(Element node);
/** <p>Visits the given <code>Attribute</code></p>
*
* @param node is the <code>Attribute</code> node to visit.
*/
public void visit(Attribute node);
/** <p>Visits the given <code>CDATA</code></p>
*
* @param node is the <code>CDATA</code> node to visit.
*/
public void visit(CDATA node);
/** <p>Visits the given <code>Comment</code></p>
*
* @param node is the <code>Comment</code> node to visit.
*/
public void visit(Comment node);
/** <p>Visits the given <code>Entity</code></p>
*
* @param node is the <code>Entity</code> node to visit.
*/
public void visit(Entity node);
/** <p>Visits the given <code>Namespace</code></p>
*
* @param namespace is the <code>Namespace</code> node to visit.
*/
public void visit(Namespace namespace);
/** <p>Visits the given <code>ProcessingInstruction</code>
* </p>
*
* @param node is the <code>ProcessingInstruction</code> node to visit.
*/
public void visit(ProcessingInstruction node);
/** <p>Visits the given <code>Text</code>
* </p>
*
* @param node is the <code>Text</code> node to visit.
*/
public void visit(Text node);
}
<!------------------------------------------------------------------------------------>
/*
* Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
*
* This software is open source.
* See the bottom of this file for the licence.
*/
package org.dom4j.tree;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.IllegalAddException;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.QName;
import org.dom4j.Text;
import org.dom4j.Visitor;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
/**
* <p>
* <code>AbstractDocument</code> is an abstract base class for tree
* implementors to use for implementation inheritence.
* </p>
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
* @version $Revision: 1.33 $
*/
public abstract class AbstractDocument extends AbstractBranch implements
Document {
/** The encoding of this document as stated in the XML declaration */
protected String encoding;
public AbstractDocument() {
}
public short getNodeType() {
return DOCUMENT_NODE;
}
public String getPath(Element context) {
return "/";
}
public String getUniquePath(Element context) {
return "/";
}
public Document getDocument() {
return this;
}
public String getXMLEncoding() {
return null;
}
public String getStringValue() {
Element root = getRootElement();
return (root != null) ? root.getStringValue() : "";
}
public String asXML() {
OutputFormat format = new OutputFormat();
format.setEncoding(encoding);
try {
StringWriter out = new StringWriter();
XMLWriter writer = new XMLWriter(out, format);
writer.write(this);
writer.flush();
return out.toString();
} catch (IOException e) {
throw new RuntimeException("IOException while generating textual "
+ "representation: " + e.getMessage());
}
}
public void write(Writer out) throws IOException {
OutputFormat format = new OutputFormat();
format.setEncoding(encoding);
XMLWriter writer = new XMLWriter(out, format);
writer.write(this);
}
/**
* <p>
* <code>accept</code> method is the <code>Visitor Pattern</code>
* method.
* </p>
*
* @param visitor
* <code>Visitor</code> is the visitor.
*/
public void accept(Visitor visitor) {
visitor.visit(this);
DocumentType docType = getDocType();
if (docType != null) {
visitor.visit(docType);
}
// visit content
List content = content();
if (content != null) {
for (Iterator iter = content.iterator(); iter.hasNext();) {
Object object = iter.next();
if (object instanceof String) {
Text text = getDocumentFactory()
.createText((String) object);
visitor.visit(text);
} else {
Node node = (Node) object;
node.accept(visitor);
}
}
}
}
public String toString() {
return super.toString() + " [Document: name " + getName() + "]";
}
public void normalize() {
Element element = getRootElement();
if (element != null) {
element.normalize();
}
}
public Document addComment(String comment) {
Comment node = getDocumentFactory().createComment(comment);
add(node);
return this;
}
public Document addProcessingInstruction(String target, String data) {
ProcessingInstruction node = getDocumentFactory()
.createProcessingInstruction(target, data);
add(node);
return this;
}
public Document addProcessingInstruction(String target, Map data) {
ProcessingInstruction node = getDocumentFactory()
.createProcessingInstruction(target, data);
add(node);
return this;
}
public Element addElement(String name) {
Element element = getDocumentFactory().createElement(name);
add(element);
return element;
}
public Element addElement(String qualifiedName, String namespaceURI) {
Element element = getDocumentFactory().createElement(qualifiedName,
namespaceURI);
add(element);
return element;
}
public Element addElement(QName qName) {
Element element = getDocumentFactory().createElement(qName);
add(element);
return element;
}
public void setRootElement(Element rootElement) {
clearContent();
if (rootElement != null) {
super.add(rootElement);
rootElementAdded(rootElement);
}
}
public void add(Element element) {
checkAddElementAllowed(element);
super.add(element);
rootElementAdded(element);
}
public boolean remove(Element element) {
boolean answer = super.remove(element);
Element root = getRootElement();
if ((root != null) && answer) {
setRootElement(null);
}
element.setDocument(null);
return answer;
}
public Node asXPathResult(Element parent) {
return this;
}
protected void childAdded(Node node) {
if (node != null) {
node.setDocument(this);
}
}
protected void childRemoved(Node node) {
if (node != null) {
node.setDocument(null);
}
}
protected void checkAddElementAllowed(Element element) {
Element root = getRootElement();
if (root != null) {
throw new IllegalAddException(this, element,
"Cannot add another element to this "
+ "Document as it already has a root "
+ "element of: " + root.getQualifiedName());
}
}
/**
* Called to set the root element variable
*
* @param rootElement
* DOCUMENT ME!
*/
protected abstract void rootElementAdded(Element rootElement);
public void setXMLEncoding(String enc) {
this.encoding = enc;
}
}
分享到:
相关推荐
三、使用dom4j解析xml文件 5 1. 构建dom4j树 5 2. 获取节点 5 3. 获取属性 6 4. 使用XPath获取节点和属性 6 四、使用dom4j修改xml文件 7 五、常用方法 8 1.Element元素API 8 2. Attribute属性API 8 2. 字符串转化 8...
设计模式C++学习之访问者模式(Visitor)
dom4j的例子 xpp3 dom4j说明 visitor解析
访问者模式(Visitor) 用意:适用于数据结构相对未定的系统,把数据结构和作用于结构上的操作间的耦合解开。
C#面向对象设计模式纵横谈(24):(行为型模式) Visitor 访问者模式
C#面向对象设计模式 (行为型模式) Visitor 访问者模式 视频讲座下载
访问者模式(Visitor Pattern)是GoF提出的23种设计模式中的一种,属于行为模式。它表示一个作用于某对象结构中的各元素的操作,它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作。从定义可以看出,...
访问者模式(Java代码实现),博文附件
Visitor模式.docVisitor模式.doc
面向对象 北工大 课设 使用多种设计模式,实现的求值引擎,包括表达式树的构建
设计模式 -访问者(Visitor)模式详解和应用.pdf
Visitor(访问者模式)属于行为型模式。意图:表示一个作用于某对象结构中的各元素的操作。
表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
23种设计模式之二十(行为模式)Visitor模式
也许最开始出现这种模式,是因为另外的原因: 我有一堆数据放在一个库里头,不想让其它人拿着, 如果你要用数据干活,那你就把函数指针给我,我来替你使用这个数据。...然后人们就说,这是visitor模式。
该PPT包含了Visitor里面所有的知识点和扩展,对于想要学习该模式的人来说是不二的选择,并且配上了源代,未经允许不可传至其他网站。
访问者(Visitor)模式的定义:将作用于某种数据结构中的各元素的操作分离出来封装成独立的类,使其在不改变数据结构的前提下可以添加作用于这些元素的新的操作,为数据结构中的每个元素提供多种访问方式。...
主要介绍了iOS应用的设计模式开发中的Visitor访问者模式的实例,示例代码为传统的Objective-C,需要的朋友可以参考下
C#面向对象设计模式纵横谈(24):(行为型模式) Visitor 访问者模式.rar 李建忠主讲 上海祝成信息科技有限公司担任软件架构师,并讲授C#/.NET软件开发课程