類1:cn.java.visitor.Node
package cn.java.visitor;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Vector;abstract class Node {protected String qName;protected String localName;protected HashMap attrs;protected Node parent;protected Nodes body;public abstract void accept(Visitor v);public Node(String qName, String localName,HashMap attrs,Node parent){this.qName = qName;this.localName = localName;this.attrs = attrs;addToParent(parent);}public Node(){}public Nodes getBody() {return body;}public void setBody(Nodes body) {this.body = body;}private void addToParent(Node parent) {if (parent != null) {this.parent = parent; // 儲存對父節點的依賴Nodes parentBody = parent.getBody();if (parentBody == null) {parentBody = new Nodes();parent.setBody(parentBody);}parentBody.add(this); // 儲存父節點對子節點的依賴}}@Overridepublic String toString() {return "Node [qName=" + qName + ", localName=" + localName + ", attrs=" + attrs + "]";}/** * 節點列表 */public static class Nodes {private final List<Node> list;private Node.Root root;public Nodes() {list = new Vector<>();}public Nodes(Node.Root root) {this.root = root;list = new Vector<>();list.add(root);}public void add(Node n) {list.add(n);root = null;}public void remove(Node n) {list.remove(n);}public int size() {return list.size();}public void visit(Visitor v) {Iterator<Node> iter = list.iterator();while (iter.hasNext()) {Node n = iter.next();n.accept(v);}}}/** * 訪問器 */public static class Visitor {public void visit(CustomTag node){}public void visit(IncludeAction node){}public void visit(NodeType1 node){}protected void doVisit(Node n) {}protected void visitBody(Node n){if (n.getBody() != null) {n.getBody().visit(this);}}public void visit(Root n) {doVisit(n);visitBody(n);}}/** * 根節點 * @author Administrator */public static class Root extends Node {public Root() {}@Overridepublic void accept(Visitor v){v.visit(this);}}/** * 節點類型1 * @author Administrator * */public static class CustomTag extends Node {private final String prefix;public CustomTag(String qName, String prefix, String localName,HashMap attrs ,Node parent){super(qName, localName,attrs,parent);this.prefix = prefix;}@Overridepublic void accept(Visitor v) {v.visit(this);}@Overridepublic String toString() {return "CustomTag [qName=" + qName + ", localName=" + localName + ", prefix=" + prefix + ", attrs=" + attrs + "]";}}/** * 節點類型2 * @author Administrator * */public static class IncludeAction extends Node {public IncludeAction(String qName, String localName,HashMap attrs,Node parent) {super(qName, localName,attrs,parent);}@Overridepublic void accept(Visitor v) {v.visit(this);}}/** * 節點類型3 * @author Administrator * */public static class NodeType1 extends Node {public NodeType1(String qName, String localName,HashMap attrs ,Node parent) {super(qName, localName,attrs,parent);}@Overridepublic void accept(Visitor v) {v.visit(this);}}}
類2:cn.java.visitor.Generator
package cn.java.visitor;import cn.java.visitor.Node.CustomTag;import cn.java.visitor.Node.IncludeAction;import cn.java.visitor.Node.NodeType1;public class Generator {private class GenerateVisitor extends Node.Visitor {public void visit(CustomTag node){System.out.println(node);}public void visit(IncludeAction node){System.out.println(node);}public void visit(NodeType1 node){System.out.println(node);}}public static void generate(Node.Nodes page){Generator gen = new Generator();page.visit(gen.new GenerateVisitor());}}
類3:主類 cn.java.visitor.Test
package cn.java.visitor;import java.util.HashMap;import cn.java.visitor.Node.CustomTag;import cn.java.visitor.Node.IncludeAction;public class Test {public static void main(String[] args) {Node.Root root = new Node.Root();new CustomTag("myprefix:tag1","myprefix","tag1" ,new HashMap(){{put("attr1", "attr1_value");put("attr2", "attr2_value");}},root);new CustomTag("myprefix:tag2","myprefix","tag2",null,root);new CustomTag("myprefix:tag3","myprefix","tag3",new HashMap(){{put("attr1", "attr1_value");put("attr2", "attr2_value");}},root);new IncludeAction("jsp:include","include",new HashMap(){{put("attr1", "attr1_value");put("attr2", "attr2_value");}},root);new IncludeAction("jsp:include","include",null,root);Node.Nodes page = new Node.Nodes(root);Generator.generate(page);}}