`

dom4j解析xml的访问者visitor模式

阅读更多
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;
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics