View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  package org.apache.jsieve.util;
20  
21  import org.apache.jsieve.exception.SieveException;
22  import org.apache.jsieve.parser.generated.ASTargument;
23  import org.apache.jsieve.parser.generated.ASTarguments;
24  import org.apache.jsieve.parser.generated.ASTblock;
25  import org.apache.jsieve.parser.generated.ASTcommand;
26  import org.apache.jsieve.parser.generated.ASTcommands;
27  import org.apache.jsieve.parser.generated.ASTstart;
28  import org.apache.jsieve.parser.generated.ASTstring;
29  import org.apache.jsieve.parser.generated.ASTstring_list;
30  import org.apache.jsieve.parser.generated.ASTtest;
31  import org.apache.jsieve.parser.generated.ASTtest_list;
32  import org.apache.jsieve.parser.generated.Node;
33  import org.apache.jsieve.parser.generated.SieveParserVisitor;
34  import org.apache.jsieve.parser.generated.SimpleNode;
35  
36  /**
37   * Traverses nodes.
38   * Once instance can be safely shared between threads.
39   */
40  public class NodeTraverser {
41      
42      /**
43       * Traverses the tree structure rooted at the given node.
44       * The nodes contained are reported to the handler.
45       * @param root not null
46       * @param handler not null
47       * @throws SieveException when traversal fails
48       * @throws HaltTraversalException when traversal is halted by handler
49       */
50      public void traverse(final NodeHandler handler, final Node root) throws SieveException {
51          final TraversalWorker worker = new TraversalWorker(handler);
52          handler.start();
53          root.jjtAccept(worker, null);
54          handler.end();
55      }
56      
57      /**
58       * Traverses the tree structure rooted at the given node.
59       * The nodes contained are reported to the handler.
60       * @param root not null
61       * @param handler not null
62       * @throws SieveException when traversal fails
63       * @throws HaltTraversalException when traversal is halted by handler
64       */
65      public void traverse(final SieveHandler handler, final Node root) throws SieveException {
66          traverse(new NodeToSieveAdapter(handler), root);
67      }
68    
69  
70      /**
71       * <p>Traverses a nodal tree structure.
72       * An inner worker:
73       * </p>
74       * <ul>
75       * <li>Allows a more fluent public API</li>
76       * <li>Isolated the monotheaded code</li>
77       * </ul>
78       */
79      private static final class TraversalWorker implements SieveParserVisitor {
80          
81          private final NodeHandler handler;
82          
83          /**
84           * Constructs a traversal worker.
85           * @param handler not null
86           */
87          public TraversalWorker(final NodeHandler handler) {
88              super();
89              this.handler = handler;
90          }
91          
92          public Object visit(SimpleNode node, Object data) throws SieveException {
93              handler.start(node);
94              node.childrenAccept(this, null);
95              handler.end(node);
96              return null;
97          }
98      
99          public Object visit(ASTstart node, Object data) throws SieveException {
100             handler.start(node);
101             node.childrenAccept(this, null);
102             handler.end(node);
103             return null;
104         }
105     
106         public Object visit(ASTcommands node, Object data) throws SieveException {
107             handler.start(node);
108             node.childrenAccept(this, null);
109             handler.end(node);
110             return null;
111         }
112     
113         public Object visit(ASTcommand node, Object data) throws SieveException {
114             handler.start(node);
115             node.childrenAccept(this, null);
116             handler.end(node);
117             return null;
118         }
119     
120         public Object visit(ASTblock node, Object data) throws SieveException {
121             handler.start(node);
122             node.childrenAccept(this, null);
123             handler.end(node);
124             return null;
125         }
126     
127         public Object visit(ASTarguments node, Object data) throws SieveException {
128             handler.start(node);
129             node.childrenAccept(this, null);
130             handler.end(node);
131             return null;
132         }
133     
134         public Object visit(ASTargument node, Object data) throws SieveException {
135             handler.start(node);
136             node.childrenAccept(this, null);
137             handler.end(node);
138             return null;
139         }
140     
141         public Object visit(ASTtest node, Object data) throws SieveException {
142             handler.start(node);
143             node.childrenAccept(this, null);
144             handler.end(node);
145             return null;
146         }
147     
148         public Object visit(ASTtest_list node, Object data) throws SieveException {
149             handler.start(node);
150             node.childrenAccept(this, null);
151             handler.end(node);
152             return null;
153         }
154     
155         public Object visit(ASTstring node, Object data) throws SieveException {
156             handler.start(node);
157             node.childrenAccept(this, null);
158             handler.end(node);
159             return null;
160         }
161     
162         public Object visit(ASTstring_list node, Object data) throws SieveException {
163             handler.start(node);
164             node.childrenAccept(this, null);
165             handler.end(node);
166             return null;
167         }
168     }
169 }