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.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.jsieve.NumberArgument;
24  import org.apache.jsieve.TagArgument;
25  import org.apache.jsieve.parser.generated.ASTargument;
26  import org.apache.jsieve.parser.generated.ASTarguments;
27  import org.apache.jsieve.parser.generated.ASTblock;
28  import org.apache.jsieve.parser.generated.ASTcommand;
29  import org.apache.jsieve.parser.generated.ASTcommands;
30  import org.apache.jsieve.parser.generated.ASTstart;
31  import org.apache.jsieve.parser.generated.ASTstring;
32  import org.apache.jsieve.parser.generated.ASTstring_list;
33  import org.apache.jsieve.parser.generated.ASTtest;
34  import org.apache.jsieve.parser.generated.ASTtest_list;
35  import org.apache.jsieve.parser.generated.SimpleNode;
36  
37  /**
38   * Adapters low level {@link NodeHandler} output into a
39   * high level {@link SieveHandler}.
40   */
41  public class NodeToSieveAdapter implements NodeHandler {
42  
43      private static final Log LOG = LogFactory.getLog(NodeToSieveAdapter.class);
44      
45      private final SieveHandler handler;
46      
47      /**
48       * Constructs an adapter to the given {@link SieveHandler}.
49       * @param handler not null
50       * @throws NullPointerException when handler is null
51       */
52      public NodeToSieveAdapter(final SieveHandler handler) {
53          super();
54          // Hard to debug a null pointer during parsing
55          if (handler == null) {
56              throw new NullPointerException("Handler must not be null");
57          }
58          this.handler = handler;
59      }
60  
61  
62      public void start() throws HaltTraversalException {
63  //      Ignore
64      }
65      
66      public void end() throws HaltTraversalException {
67  //      Ignore
68      }
69  
70      public void end(SimpleNode node) throws HaltTraversalException {
71  //      Ignore
72      }
73  
74      public void end(ASTstart node) throws HaltTraversalException {
75          handler.endScript();
76      }
77  
78      public void end(ASTcommands node) throws HaltTraversalException {
79          handler.endCommands();
80      }
81  
82      public void end(ASTcommand node) throws HaltTraversalException {
83          handler.endCommand(node.getName());
84      }
85  
86      public void end(ASTblock node) throws HaltTraversalException {
87          handler.endBlock();
88      }
89  
90      public void end(ASTarguments node) throws HaltTraversalException {
91          handler.endArguments();
92      }
93  
94      public void end(ASTargument node) throws HaltTraversalException {
95          // Processed in start
96      }
97  
98      public void end(ASTtest node) throws HaltTraversalException {
99          final String name = node.getName();
100         handler.endTest(name);
101     }
102 
103     public void end(ASTtest_list node) throws HaltTraversalException {
104         handler.endTestList();
105     }
106 
107     public void end(ASTstring node) throws HaltTraversalException {
108         // Process ASTstring on start
109     }
110 
111     public void end(ASTstring_list node) throws HaltTraversalException {
112         handler.endStringListArgument();
113     }
114 
115 
116     public void start(SimpleNode node) throws HaltTraversalException {
117         // Ignore
118     }
119 
120     public void start(ASTstart node) throws HaltTraversalException {
121         handler.startScript();
122     }
123 
124     public void start(ASTcommands node) throws HaltTraversalException {
125         handler.startCommands();
126     }
127 
128     public void start(ASTcommand node) throws HaltTraversalException {
129         handler.startCommand(node.getName());
130     }
131 
132     public void start(ASTblock node) throws HaltTraversalException {
133         handler.startBlock();
134     }
135 
136     public void start(ASTarguments node) throws HaltTraversalException {
137         handler.startArguments();
138     }
139 
140     public void start(ASTargument node) throws HaltTraversalException {
141         final Object value = node.getValue();
142         if (value == null) {
143             LOG.debug("Ignoring null argument");
144         } else if (value instanceof NumberArgument) {
145             final NumberArgument numberArgument = (NumberArgument) value;
146             Integer integer = numberArgument.getInteger();
147             if (integer == null) {
148                 LOG.debug("Ignoring null numeric argument");
149             } else {
150                 final int number = integer.intValue();
151                 handler.argument(number);
152             }
153         } else if (value instanceof TagArgument) {
154             final TagArgument tagArgument = (TagArgument) value;
155             final String tag = tagArgument.getTag();
156             // tag = ":" identifier
157             // handlers are only interesting in the identifier for the tag
158             final String identifier;
159             if (tag.charAt(0) == ':') {
160                 identifier = tag.substring(1);
161             } else {
162                 identifier = tag;
163             }
164             handler.argument(identifier);
165         }
166     }
167 
168     public void start(ASTtest node) throws HaltTraversalException {
169         final String name = node.getName();
170         handler.startTest(name);
171     }
172 
173     public void start(ASTtest_list node) throws HaltTraversalException {
174         handler.startTestList();
175     }
176 
177     public void start(ASTstring node) throws HaltTraversalException {
178         final String string =(String) node.getValue();
179         handler.listMember(string);
180     }
181 
182     public void start(ASTstring_list node) throws HaltTraversalException {
183         handler.startStringListArgument();
184     }
185 
186 }