You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rd...@apache.org on 2008/12/07 20:23:15 UTC

svn commit: r724179 - in /james/jsieve/trunk/util/src: main/java/org/apache/jsieve/util/ test/java/org/apache/jsieve/util/

Author: rdonkin
Date: Sun Dec  7 11:23:15 2008
New Revision: 724179

URL: http://svn.apache.org/viewvc?rev=724179&view=rev
Log:
High level API for JSIEVE-43 Add API for generating a Sieve script from a parse tree (https://issues.apache.org/jira/browse/JSIEVE-43)

Added:
    james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeToSieveAdapter.java
    james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/SieveHandler.java
    james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/NodeToSieveAdapterTest.java   (with props)
Modified:
    james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeHandler.java

Modified: james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeHandler.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeHandler.java?rev=724179&r1=724178&r2=724179&view=diff
==============================================================================
--- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeHandler.java (original)
+++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeHandler.java Sun Dec  7 11:23:15 2008
@@ -32,8 +32,12 @@
 
 /**
  * Presents a low level reporting view of a Sieve node tree.
+ * Familiarity with the 
+ * <a href='http://james.apache.org/jsieve/'>JSieve</a> implementation is assumed.
+ * Anyone requiring a high level view should see {@link SieveHandler}.
  * 
  * @see NodeTraverser
+ * @see SieveHandler
  */
 public interface NodeHandler {
     

Added: james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeToSieveAdapter.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeToSieveAdapter.java?rev=724179&view=auto
==============================================================================
--- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeToSieveAdapter.java (added)
+++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeToSieveAdapter.java Sun Dec  7 11:23:15 2008
@@ -0,0 +1,178 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.jsieve.util;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jsieve.NumberArgument;
+import org.apache.jsieve.TagArgument;
+import org.apache.jsieve.parser.generated.ASTargument;
+import org.apache.jsieve.parser.generated.ASTarguments;
+import org.apache.jsieve.parser.generated.ASTblock;
+import org.apache.jsieve.parser.generated.ASTcommand;
+import org.apache.jsieve.parser.generated.ASTcommands;
+import org.apache.jsieve.parser.generated.ASTstart;
+import org.apache.jsieve.parser.generated.ASTstring;
+import org.apache.jsieve.parser.generated.ASTstring_list;
+import org.apache.jsieve.parser.generated.ASTtest;
+import org.apache.jsieve.parser.generated.ASTtest_list;
+import org.apache.jsieve.parser.generated.SimpleNode;
+
+/**
+ * Adapters low level {@link NodeHandler} output into a
+ * high level {@link SieveHandler}.
+ */
+public class NodeToSieveAdapter implements NodeHandler {
+
+    private static final Log LOG = LogFactory.getLog(NodeToSieveAdapter.class);
+    
+    private final SieveHandler handler;
+    
+    /**
+     * Constructs an adapter to the given {@link SieveHandler}.
+     * @param handler not null
+     * @throws NullPointerException when handler is null
+     */
+    public NodeToSieveAdapter(final SieveHandler handler) {
+        super();
+        // Hard to debug a null pointer during parsing
+        if (handler == null) {
+            throw new NullPointerException("Handler must not be null");
+        }
+        this.handler = handler;
+    }
+
+
+    public void start() throws HaltTraversalException {
+//      Ignore
+    }
+    
+    public void end() throws HaltTraversalException {
+//      Ignore
+    }
+
+    public void end(SimpleNode node) throws HaltTraversalException {
+//      Ignore
+    }
+
+    public void end(ASTstart node) throws HaltTraversalException {
+        handler.endScript();
+    }
+
+    public void end(ASTcommands node) throws HaltTraversalException {
+        handler.endCommands();
+    }
+
+    public void end(ASTcommand node) throws HaltTraversalException {
+        handler.endCommand(node.getName());
+    }
+
+    public void end(ASTblock node) throws HaltTraversalException {
+        handler.endBlock();
+    }
+
+    public void end(ASTarguments node) throws HaltTraversalException {
+        handler.endArguments();
+    }
+
+    public void end(ASTargument node) throws HaltTraversalException {
+        // Processed in start
+    }
+
+    public void end(ASTtest node) throws HaltTraversalException {
+        final String name = node.getName();
+        handler.endTest(name);
+    }
+
+    public void end(ASTtest_list node) throws HaltTraversalException {
+        handler.endTestList();
+    }
+
+    public void end(ASTstring node) throws HaltTraversalException {
+        // Process ASTstring on start
+    }
+
+    public void end(ASTstring_list node) throws HaltTraversalException {
+        handler.endStringListArgument();
+    }
+
+
+    public void start(SimpleNode node) throws HaltTraversalException {
+        // Ignore
+    }
+
+    public void start(ASTstart node) throws HaltTraversalException {
+        handler.startScript();
+    }
+
+    public void start(ASTcommands node) throws HaltTraversalException {
+        handler.startCommands();
+    }
+
+    public void start(ASTcommand node) throws HaltTraversalException {
+        handler.startCommand(node.getName());
+    }
+
+    public void start(ASTblock node) throws HaltTraversalException {
+        handler.startBlock();
+    }
+
+    public void start(ASTarguments node) throws HaltTraversalException {
+        handler.startArguments();
+    }
+
+    public void start(ASTargument node) throws HaltTraversalException {
+        final Object value = node.getValue();
+        if (value == null) {
+            LOG.debug("Ignoring null argument");
+        } else if (value instanceof NumberArgument) {
+            final NumberArgument numberArgument = (NumberArgument) value;
+            Integer integer = numberArgument.getInteger();
+            if (integer == null) {
+                LOG.debug("Ignoring null numeric argument");
+            } else {
+                final int number = integer.intValue();
+                handler.argument(number);
+            }
+        } else if (value instanceof TagArgument) {
+            final TagArgument tagArgument = (TagArgument) value;
+            final String tag = tagArgument.getTag();
+            handler.argument(tag);
+        }
+    }
+
+    public void start(ASTtest node) throws HaltTraversalException {
+        final String name = node.getName();
+        handler.startTest(name);
+    }
+
+    public void start(ASTtest_list node) throws HaltTraversalException {
+        handler.startTestList();
+    }
+
+    public void start(ASTstring node) throws HaltTraversalException {
+        final String string =(String) node.getValue();
+        handler.listMember(string);
+    }
+
+    public void start(ASTstring_list node) throws HaltTraversalException {
+        handler.startStringListArgument();
+    }
+
+}

Added: james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/SieveHandler.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/SieveHandler.java?rev=724179&view=auto
==============================================================================
--- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/SieveHandler.java (added)
+++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/SieveHandler.java Sun Dec  7 11:23:15 2008
@@ -0,0 +1,176 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.jsieve.util;
+
+/**
+ * Presents a high level reporting view of a Sieve node tree.
+ * Familiarity with 
+ * <a href='http://www.ietf.org/rfc/rfc3028.txt'>Sieve</a> is assumed 
+ * (but not of the internals of the 
+ * <a href='http://james.apache.org/jsieve/'>JSieve</a> implementation).
+ * Anyone who requires a low level, <code>JSieve</code> specific view
+ * should see {@link NodeHandler}
+ * 
+ * @see NodeTraverser
+ * @see NodeHandler
+ */
+public interface SieveHandler {
+    
+    /**
+     * Handles the start of a Sieve script.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler startScript() throws HaltTraversalException;
+    
+    /**
+     * Handles the end of a Sieve script.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler endScript() throws HaltTraversalException;
+    
+    /**
+     * Handles the start of a block.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler startBlock() throws HaltTraversalException;
+    
+    /**
+     * Handles the end of a block.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler endBlock() throws HaltTraversalException;
+    
+    /**
+     * Handles the start of a block of commands.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler startCommands() throws HaltTraversalException;
+    
+    /**
+     * Handles the end of a block of commands.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler endCommands() throws HaltTraversalException;
+
+    /**
+     * Handles the start of a command.
+     * @param commandName name identifying the command
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler startCommand(String commandName) throws HaltTraversalException;
+    
+    /**
+     * Handles the end of a command.
+     * @param commandName name identifying the command
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler endCommand(String commandName) throws HaltTraversalException;
+    
+    
+    /**
+     * Handles the start of a block of arguments.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler startArguments() throws HaltTraversalException;
+    
+    /**
+     * Handles the end of a block of arguments.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler endArguments() throws HaltTraversalException;
+    
+    /**
+     * Handles a tag argument.
+     * @param tag not null
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler argument(String tag) throws HaltTraversalException;
+    
+    /**
+     * Handler a numeric argument.
+     * @param number not null
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler argument(int number) throws HaltTraversalException;
+    
+    /**
+     * Handles the start of an argument which is a list of strings.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler startStringListArgument() throws HaltTraversalException;
+    
+    /**
+     * Handles the end of an argument which is a list of strings.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler endStringListArgument() throws HaltTraversalException;
+    
+    /**
+     * One string from a list.
+     * @param string not null
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler listMember(String string) throws HaltTraversalException;
+    
+    /**
+     * Handles the start of a list of tests.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler startTestList() throws HaltTraversalException;
+    
+    /**
+     * Handles the end of a list of tests.
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler endTestList() throws HaltTraversalException;
+    
+    /**
+     * Handles the start of a test.
+     * @param testName name identifying the test
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler startTest(String testName) throws HaltTraversalException;
+    
+    /**
+     * Handles the end of a test.
+     * @param testName name identifying the test
+     * @throws HaltTraversalException
+     * @return this
+     */
+    public SieveHandler endTest(String testName) throws HaltTraversalException;
+    
+}

Added: james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/NodeToSieveAdapterTest.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/NodeToSieveAdapterTest.java?rev=724179&view=auto
==============================================================================
--- james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/NodeToSieveAdapterTest.java (added)
+++ james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/NodeToSieveAdapterTest.java Sun Dec  7 11:23:15 2008
@@ -0,0 +1,193 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+package org.apache.jsieve.util;
+
+import org.apache.jsieve.NumberArgument;
+import org.apache.jsieve.TagArgument;
+import org.apache.jsieve.parser.generated.ASTargument;
+import org.apache.jsieve.parser.generated.ASTarguments;
+import org.apache.jsieve.parser.generated.ASTblock;
+import org.apache.jsieve.parser.generated.ASTcommand;
+import org.apache.jsieve.parser.generated.ASTcommands;
+import org.apache.jsieve.parser.generated.ASTstart;
+import org.apache.jsieve.parser.generated.ASTstring;
+import org.apache.jsieve.parser.generated.ASTstring_list;
+import org.apache.jsieve.parser.generated.ASTtest;
+import org.apache.jsieve.parser.generated.ASTtest_list;
+import org.apache.jsieve.parser.generated.SieveParserTreeConstants;
+import org.apache.jsieve.parser.generated.SimpleNode;
+import org.apache.jsieve.parser.generated.Token;
+import org.jmock.Mock;
+import org.jmock.MockObjectTestCase;
+
+public class NodeToSieveAdapterTest extends MockObjectTestCase {
+
+    Mock mock;
+    NodeToSieveAdapter subject;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        mock = mock(SieveHandler.class);
+        subject = new NodeToSieveAdapter((SieveHandler)mock.proxy());
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+    
+    public void testStart() throws Exception {
+        // Not propergated
+        subject.start();
+    }
+    
+    public void testEnd() throws Exception {
+        // Not propergated
+        subject.end();
+    }
+
+    public void testEndSimpleNode() throws Exception {
+        // Not propergated
+        subject.end(new SimpleNode(SieveParserTreeConstants.JJTBLOCK));
+    }
+
+    public void testEndASTstart() throws Exception {
+        mock.expects(once()).method("endScript");
+        subject.end(new ASTstart(SieveParserTreeConstants.JJTSTART));
+    }
+
+    public void testEndASTcommands() throws Exception {
+        mock.expects(once()).method("endCommands");
+        subject.end(new ASTcommands(SieveParserTreeConstants.JJTCOMMANDS));
+    }
+
+    public void testEndASTcommand() throws Exception {
+        String name = "CommandName";
+        mock.expects(once()).method("endCommand").with(eq(name));
+        ASTcommand node = new ASTcommand(SieveParserTreeConstants.JJTCOMMAND);
+        node.setName(name);
+        subject.end(node);
+    }
+
+    public void testEndASTblock() throws Exception {
+        mock.expects(once()).method("endBlock");
+        subject.end(new ASTblock(SieveParserTreeConstants.JJTBLOCK)); 
+    }
+
+    public void testEndASTarguments() throws Exception {
+        mock.expects(once()).method("endArguments");
+        subject.end(new ASTarguments(SieveParserTreeConstants.JJTARGUMENTS)); 
+    }
+
+    public void testASTargumentTag() throws Exception {
+        String tag = "Hugo";
+        mock.expects(once()).method("argument").with(eq(tag));
+        ASTargument argument = new ASTargument(SieveParserTreeConstants.JJTARGUMENTS);
+        argument.setValue(new TagArgument(new Token(0, tag)));
+        subject.start(argument); 
+        subject.end(argument); 
+    }
+    
+    public void testASTargumentNumber() throws Exception {
+        int number = 17;
+        mock.expects(once()).method("argument").with(eq(number));
+        ASTargument argument = new ASTargument(SieveParserTreeConstants.JJTARGUMENTS);
+        argument.setValue(new NumberArgument(new Token(0, "17")));
+        subject.start(argument); 
+        subject.end(argument); 
+    }
+
+    public void testEndASTtest() throws Exception {
+        String name = "ATestName";
+        mock.expects(once()).method("endTest").with(eq(name));
+        ASTtest node = new ASTtest(SieveParserTreeConstants.JJTTEST);
+        node.setName(name);
+        subject.end(node);
+    }
+
+    public void testEndASTtest_list() throws Exception {
+        mock.expects(once()).method("endTestList");
+        subject.end(new ASTtest_list(SieveParserTreeConstants.JJTTEST_LIST)); 
+    }
+
+    public void testEndASTstring_list() throws Exception {
+        mock.expects(once()).method("endStringListArgument");
+        subject.end(new ASTstring_list(SieveParserTreeConstants.JJTSTRING_LIST)); 
+    }
+
+    public void testStartSimpleNode() throws Exception {
+        // Not propergated
+        subject.end(new SimpleNode(SieveParserTreeConstants.JJTBLOCK));
+    }
+
+    public void testStartASTstart() throws Exception {
+        mock.expects(once()).method("startScript");
+        subject.start(new ASTstart(SieveParserTreeConstants.JJTSTART));
+    }
+
+    public void testStartASTcommands() throws Exception {
+        mock.expects(once()).method("startCommands");
+        subject.start(new ASTcommands(SieveParserTreeConstants.JJTCOMMANDS));
+    }
+
+    public void testStartASTcommand() throws Exception {
+        String name = "CommandName";
+        mock.expects(once()).method("startCommand").with(eq(name));
+        ASTcommand node = new ASTcommand(SieveParserTreeConstants.JJTCOMMAND);
+        node.setName(name);
+        subject.start(node);
+    }
+
+    public void testStartASTblock() throws Exception {
+        mock.expects(once()).method("startBlock");
+        subject.start(new ASTblock(SieveParserTreeConstants.JJTBLOCK)); 
+    }
+
+    public void testStartASTarguments() throws Exception {
+        mock.expects(once()).method("startArguments");
+        subject.start(new ASTarguments(SieveParserTreeConstants.JJTARGUMENTS)); 
+    }
+
+    public void testStartASTtest() throws Exception {
+        String name = "ATestName";
+        mock.expects(once()).method("startTest").with(eq(name));
+        ASTtest node = new ASTtest(SieveParserTreeConstants.JJTTEST);
+        node.setName(name);
+        subject.start(node);
+    }
+
+    public void testStartASTtest_list() throws Exception {
+        mock.expects(once()).method("startTestList");
+        subject.start(new ASTtest_list(SieveParserTreeConstants.JJTTEST_LIST));
+    }
+
+    public void testASTstring() throws Exception {
+        String string = "A Value";
+        mock.expects(once()).method("listMember").with(eq(string));
+        ASTstring node = new ASTstring(SieveParserTreeConstants.JJTSTRING);
+        node.setValue(string);
+        subject.start(node); 
+        subject.end(node); 
+    }
+
+    public void testStartASTstring_list() throws Exception {
+        mock.expects(once()).method("startStringListArgument");
+        subject.start(new ASTstring_list(SieveParserTreeConstants.JJTSTRING_LIST)); 
+    }
+
+}

Propchange: james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/NodeToSieveAdapterTest.java
------------------------------------------------------------------------------
    svn:mergeinfo = 



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org