View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.config.generator.model;
18  
19  import java.util.List;
20  
21  /***
22   * 
23   * Implementation of {@link NodeElementVisitor} that does a depth-first traversal of the element. Depth first means the visit starts at the
24   * element and goes deeper and deeper in each child element until there are no children and then backtracks, doing the same thing for each
25   * children in each child element.
26   * This class is an abstract class and provides empty methods that sub-classes can override as needed. The visit methods in this class are
27   * called in this order for each element visited by this visitor:
28   * <ul>
29   * <li>{@link #startElement(NodeElement)}</li>
30   * <li>{@link #startAttributes(NodeElement)}</li>
31   * <li>{@link #visitAttributes(NodeElement, List)}</li>
32   * <li>{@link #endAttributes(NodeElement)}</li>
33   * <li>{@link #visitElement(NodeElement)}</li>
34   * <li>{@link #startChildren(NodeElement)}</li>
35   * <li>same sequence for each child element</li>
36   * <li>{@link #endChildren(NodeElement)}</li>
37   * <li>{@link #endElement(NodeElement)}</li>
38   * </ul>
39   * 
40   * @author Abhishek Sanoujam
41   * 
42   */
43  public abstract class AbstractDepthFirstVisitor implements NodeElementVisitor {
44  
45      /***
46       * {@inheritDoc}
47       */
48      public void visit(NodeElement element) {
49          if (element == null) {
50              throw new NullPointerException("element cannot be null");
51          }
52          doDfs(element);
53      }
54  
55      private void doDfs(NodeElement element) {
56          startElement(element);
57          startAttributes(element);
58          visitAttributes(element, element.getAttributes());
59          endAttributes(element);
60          visitElement(element);
61          startChildren(element);
62          for (NodeElement child : element.getChildElements()) {
63              doDfs(child);
64          }
65          endChildren(element);
66          endElement(element);
67      }
68  
69      /***
70       * Starts visiting an element. Override as needed
71       * 
72       * @param element
73       *            the element
74       */
75      protected void startElement(NodeElement element) {
76          // override if needed
77      }
78  
79      /***
80       * Starts visiting the attributes of the element. Override as needed
81       * 
82       * @param element
83       *            the element
84       */
85      protected void startAttributes(NodeElement element) {
86          // override if needed
87      }
88  
89      /***
90       * Visits the attributes of the element. Override as needed
91       * 
92       * @param element
93       *            the element
94       * @param attributes
95       *            the attributes
96       */
97      protected void visitAttributes(NodeElement element, List<NodeAttribute> attributes) {
98          // override if needed
99      }
100 
101     /***
102      * Finish visiting attributes of the element. Override as needed
103      * 
104      * @param element
105      *            the element
106      */
107     protected void endAttributes(NodeElement element) {
108         // override if needed
109     }
110 
111     /***
112      * Visits the element. Override as needed
113      * 
114      * @param element
115      *            the element
116      */
117     protected void visitElement(NodeElement element) {
118         // override if needed
119     }
120 
121     /***
122      * Starts visiting children of the element. Override as needed
123      * 
124      * @param element
125      *            the element
126      */
127     protected void startChildren(NodeElement element) {
128         // override if needed
129     }
130 
131     /***
132      * Finish visiting children of the element. Override as needed
133      * 
134      * @param element
135      *            the element
136      */
137     protected void endChildren(NodeElement element) {
138         // override if needed
139     }
140 
141     /***
142      * Finish visiting the element. Override as needed
143      * 
144      * @param element
145      *            the element
146      */
147     protected void endElement(NodeElement element) {
148         // override if needed
149     }
150 
151 }