Class 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;//save dependency on parent node Nodes PARENTB
Ody = Parent.getbody ();
if (parentbody = = null) {parentbody = new Nodes ();
Parent.setbody (Parentbody); } parentbody.add (this); Save parent node dependencies on child nodes} @Override public String toString () {return "node [qname=" + QName + ", localname=" + LocalnamE + ", attrs=" + Attrs + "]";
/** * Node List * * * 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); }}/** * Accessor/public static class Visitor {public void Visit (CustomTag node) {} public void visit (I Ncludeaction 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); }/** * Root node * @author Administrator/public static class root extends Node {public root () {} @Over
Ride public void Accept (Visitor v) {v.visit (this); }/** * Node type 1 * @author Administrator * * */public static class CustomTag extends node {private final Stri
ng prefix; Public CustomTag (String qName, string prefix, String Localname,hashmap attrs, Node parent) {super (QName, localname,attr
S,parent);
This.prefix = prefix;
@Override public void Accept (Visitor v) {v.visit (this); @Override public String toString () {return "CustomTag [qname= + QName +", localname= "+ localname +", prefix
= "+ prefix +", attrs= "+ Attrs +"]; }/** * Node type 2 * @author Administrator * * */public static class Includeaction extends node {public incl
Udeaction (String qName, string Localname,hashmap attrs,node parent) {super (qName, localname,attrs,parent); } @Override
public void Accept (Visitor v) {v.visit (this); }/** * Node Type 3 * @author Administrator * * */public static class NodeType1 extends node {public NodeType
1 (String qName, String Localname,hashmap attrs, Node parent) {super (qName, localname,attrs,parent);
@Override public void Accept (Visitor v) {v.visit (this);
}
}
}
Class 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) { C6/>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 ());
}
Class 3: Main class 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);
}