Author: rdonkin Date: Sun Dec 7 00:00:35 2008 New Revision: 724100 URL: http://svn.apache.org/viewvc?rev=724100&view=rev Log: Add API for generating a Sieve script from a parse tree (https://issues.apache.org/jira/browse/JSIEVE-43). AStart implementation by creating low level API. Starting to work in util for now, but this will need to be reviewed later JSIEVE-43
Added: james/jsieve/trunk/stage/jmock/ james/jsieve/trunk/stage/jmock/jars/ james/jsieve/trunk/stage/jmock/jars/jmock-1.1.0.LICENSE james/jsieve/trunk/stage/jmock/jars/jmock-1.1.0.jar (with props) james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/HaltTraversalException.java james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeHandler.java james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeTraverser.java james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/ james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/SimpleNodeTraverserTest.java Modified: james/jsieve/trunk/include.properties james/jsieve/trunk/util/build.xml Modified: james/jsieve/trunk/include.properties URL: http://svn.apache.org/viewvc/james/jsieve/trunk/include.properties?rev=724100&r1=724099&r2=724100&view=diff ============================================================================== --- james/jsieve/trunk/include.properties (original) +++ james/jsieve/trunk/include.properties Sun Dec 7 00:00:35 2008 @@ -68,3 +68,4 @@ mail.jar=${lib.dir}/javax.mail/jars/mail-1.4.1.jar activation.jar=${lib.dir}/javax.activation/jars/activation-1.1.1.jar +jmock.jar=${lib.dir}/jmock/jars/jmock-1.1.0.jar Added: james/jsieve/trunk/stage/jmock/jars/jmock-1.1.0.LICENSE URL: http://svn.apache.org/viewvc/james/jsieve/trunk/stage/jmock/jars/jmock-1.1.0.LICENSE?rev=724100&view=auto ============================================================================== --- james/jsieve/trunk/stage/jmock/jars/jmock-1.1.0.LICENSE (added) +++ james/jsieve/trunk/stage/jmock/jars/jmock-1.1.0.LICENSE Sun Dec 7 00:00:35 2008 @@ -0,0 +1,25 @@ +Copyright (c) 2000-2007, jMock.org +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +Redistributions of source code must retain the above copyright notice, this list of +conditions and the following disclaimer. Redistributions in binary form must reproduce +the above copyright notice, this list of conditions and the following disclaimer in +the documentation and/or other materials provided with the distribution. + +Neither the name of jMock nor the names of its contributors may be used to endorse +or promote products derived from this software without specific prior written +permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY +WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH +DAMAGE. Added: james/jsieve/trunk/stage/jmock/jars/jmock-1.1.0.jar URL: http://svn.apache.org/viewvc/james/jsieve/trunk/stage/jmock/jars/jmock-1.1.0.jar?rev=724100&view=auto ============================================================================== Binary file - no diff available. Propchange: james/jsieve/trunk/stage/jmock/jars/jmock-1.1.0.jar ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Modified: james/jsieve/trunk/util/build.xml URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/build.xml?rev=724100&r1=724099&r2=724100&view=diff ============================================================================== --- james/jsieve/trunk/util/build.xml (original) +++ james/jsieve/trunk/util/build.xml Sun Dec 7 00:00:35 2008 @@ -49,7 +49,6 @@ <path id="project.class.path"> <pathelement location="${commons-logging.jar}"/> <pathelement location="${log4j.jar}"/> - <pathelement location="${junit.jar}"/> <pathelement location='${lib.jsieve.dir}/${name}-${version}.jar'/> <pathelement location="${mailet.jar}"/> <path refid="mail.class.path"/> @@ -59,6 +58,8 @@ <path id="project.test.class.path"> <path refid="project.class.path"/> + <pathelement location="${junit.jar}"/> + <pathelement location="${jmock.jar}"/> </path> <!-- @@ -320,13 +321,8 @@ --> <classpath id="junit.class.path"> <pathelement location="${testconf.dir}"/> - <fileset dir="${dist.dir}/lib"> - <include name="**/*.jar"/> - </fileset> - <pathelement location="${dist.dir}/conf/"/> - <pathelement path="${java.class.path}"/> - <pathelement location="${junit.jar}"/> - <path refid='mail.class.path'/> + <pathelement location='${build.classes.test}'/> + <path refid='project.test.class.path'/> </classpath> <formatter type="plain"/> <batchtest fork="yes" todir="${dist.dir}/reports"> Added: james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/HaltTraversalException.java URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/HaltTraversalException.java?rev=724100&view=auto ============================================================================== --- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/HaltTraversalException.java (added) +++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/HaltTraversalException.java Sun Dec 7 00:00:35 2008 @@ -0,0 +1,44 @@ +/**************************************************************** + * 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.exception.SieveException; + +/** + * Indicators that the node traversal should be abandoned. + */ +public class HaltTraversalException extends SieveException { + + private static final long serialVersionUID = 0L; + + public HaltTraversalException() { + } + + public HaltTraversalException(String message) { + super(message); + } + + public HaltTraversalException(String message, Throwable cause) { + super(message, cause); + } + + public HaltTraversalException(Throwable cause) { + super(cause); + } +} Added: 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=724100&view=auto ============================================================================== --- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeHandler.java (added) +++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeHandler.java Sun Dec 7 00:00:35 2008 @@ -0,0 +1,205 @@ +/**************************************************************** + * 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.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; + +/** + * Presents a low level reporting view of a Sieve node tree. + * + * @see NodeTraverser + */ +public interface NodeHandler { + + /** + * Starts a tree traversal. + * @throws HaltTraversalException + */ + public void start() throws HaltTraversalException; + + /** + * Ends a tree traveral. + * @throws HaltTraversalException + */ + public void end() throws HaltTraversalException; + + /** + * Starts traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void start(SimpleNode node) throws HaltTraversalException; + + /** + * Ends traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void end(SimpleNode node) throws HaltTraversalException; + + /** + * Starts traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void start(ASTstart node) throws HaltTraversalException; + + /** + * Ends traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void end(ASTstart node) throws HaltTraversalException; + + /** + * Starts traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void start(ASTcommands node) throws HaltTraversalException; + + /** + * Ends traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void end(ASTcommands node) throws HaltTraversalException; + + /** + * Starts traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void start(ASTcommand node) throws HaltTraversalException; + + /** + * Ends traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void end(ASTcommand node) throws HaltTraversalException; + + /** + * Starts traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void start(ASTblock node) throws HaltTraversalException; + + /** + * Ends traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void end(ASTblock node) throws HaltTraversalException; + + /** + * Starts traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void start(ASTarguments node) throws HaltTraversalException; + + /** + * Ends traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void end(ASTarguments node) throws HaltTraversalException; + + /** + * Starts traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void start(ASTargument node) throws HaltTraversalException; + + /** + * Ends traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void end(ASTargument node) throws HaltTraversalException; + + /** + * Starts traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void start(ASTtest node) throws HaltTraversalException; + + /** + * Ends traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void end(ASTtest node) throws HaltTraversalException; + + /** + * Starts traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void start(ASTtest_list node) throws HaltTraversalException; + + /** + * Ends traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void end(ASTtest_list node) throws HaltTraversalException; + + /** + * Starts traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void start(ASTstring node) throws HaltTraversalException; + + /** + * Ends traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void end(ASTstring node) throws HaltTraversalException; + + /** + * Starts traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void start(ASTstring_list node) throws HaltTraversalException; + + /** + * Ends traversal of given node. + * @param node not null + * @throws HaltTraversalException + */ + public void end(ASTstring_list node) throws HaltTraversalException; +} Added: james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeTraverser.java URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeTraverser.java?rev=724100&view=auto ============================================================================== --- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeTraverser.java (added) +++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeTraverser.java Sun Dec 7 00:00:35 2008 @@ -0,0 +1,157 @@ +/**************************************************************** + * 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.exception.SieveException; +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.Node; +import org.apache.jsieve.parser.generated.SieveParserVisitor; +import org.apache.jsieve.parser.generated.SimpleNode; + +/** + * Traverses nodes. + * Once instance can be safely shared between threads. + */ +public class NodeTraverser { + + /** + * Traverses the tree structure rooted at the given node. + * The nodes contained are reported to the handler. + * @param root not null + * @param handler not null + * @throws SieveException when traversal fails + * @throws HaltTraversalException when traversal is halted by handler + */ + public void traverse(final NodeHandler handler, final Node root) throws SieveException { + final TraversalWorker worker = new TraversalWorker(handler); + handler.start(); + root.jjtAccept(worker, null); + handler.end(); + } + + + /** + * <p>Traverses a nodal tree structure. + * An inner worker: + * </p> + * <ul> + * <li>Allows a more fluent public API</li> + * <li>Isolated the monotheaded code</li> + * </ul> + */ + private static final class TraversalWorker implements SieveParserVisitor { + + private final NodeHandler handler; + + /** + * Constructs a traversal worker. + * @param handler not null + */ + public TraversalWorker(final NodeHandler handler) { + super(); + this.handler = handler; + } + + public Object visit(SimpleNode node, Object data) throws SieveException { + handler.start(node); + node.childrenAccept(this, null); + handler.end(node); + return null; + } + + public Object visit(ASTstart node, Object data) throws SieveException { + handler.start(node); + node.childrenAccept(this, null); + handler.end(node); + return null; + } + + public Object visit(ASTcommands node, Object data) throws SieveException { + handler.start(node); + node.childrenAccept(this, null); + handler.end(node); + return null; + } + + public Object visit(ASTcommand node, Object data) throws SieveException { + handler.start(node); + node.childrenAccept(this, null); + handler.end(node); + return null; + } + + public Object visit(ASTblock node, Object data) throws SieveException { + handler.start(node); + node.childrenAccept(this, null); + handler.end(node); + return null; + } + + public Object visit(ASTarguments node, Object data) throws SieveException { + handler.start(node); + node.childrenAccept(this, null); + handler.end(node); + return null; + } + + public Object visit(ASTargument node, Object data) throws SieveException { + handler.start(node); + node.childrenAccept(this, null); + handler.end(node); + return null; + } + + public Object visit(ASTtest node, Object data) throws SieveException { + handler.start(node); + node.childrenAccept(this, null); + handler.end(node); + return null; + } + + public Object visit(ASTtest_list node, Object data) throws SieveException { + handler.start(node); + node.childrenAccept(this, null); + handler.end(node); + return null; + } + + public Object visit(ASTstring node, Object data) throws SieveException { + handler.start(node); + node.childrenAccept(this, null); + handler.end(node); + return null; + } + + public Object visit(ASTstring_list node, Object data) throws SieveException { + handler.start(node); + node.childrenAccept(this, null); + handler.end(node); + return null; + } + } +} Added: james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/SimpleNodeTraverserTest.java URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/SimpleNodeTraverserTest.java?rev=724100&view=auto ============================================================================== --- james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/SimpleNodeTraverserTest.java (added) +++ james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/SimpleNodeTraverserTest.java Sun Dec 7 00:00:35 2008 @@ -0,0 +1,73 @@ +/**************************************************************** + * 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 java.io.ByteArrayInputStream; + +import org.apache.jsieve.ConfigurationManager; +import org.apache.jsieve.parser.generated.ASTarguments; +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.Node; +import org.jmock.Mock; +import org.jmock.MockObjectTestCase; + +public class SimpleNodeTraverserTest extends MockObjectTestCase { + + Mock mock; + NodeHandler handler; + + NodeTraverser subject; + + protected void setUp() throws Exception { + super.setUp(); + mock = mock(NodeHandler.class); + handler = (NodeHandler) mock.proxy(); + + subject = new NodeTraverser(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testTraverseSimpleScript() throws Exception { + mock.expects(once()).method("start").id("1"); + mock.expects(once()).method("start").with(isA(ASTstart.class)).after("1").id("2"); + mock.expects(once()).method("start").with(isA(ASTcommands.class)).after("2").id("3"); + mock.expects(once()).method("start").with(isA(ASTcommand.class)).after("3").id("4"); + mock.expects(once()).method("start").with(isA(ASTarguments.class)).after("4").id("5"); + mock.expects(once()).method("end").with(isA(ASTarguments.class)).after("5").id("6"); + mock.expects(once()).method("end").with(isA(ASTcommand.class)).after("6").id("7"); + mock.expects(once()).method("end").with(isA(ASTcommands.class)).after("7").id("8"); + mock.expects(once()).method("end").with(isA(ASTstart.class)).after("8").id("9"); + mock.expects(once()).method("end").after("9"); + traverse("Keep;"); + } + + private void traverse(String script) throws Exception { + subject.traverse(handler, parse(script)); + } + + private Node parse(String script) throws Exception { + return new ConfigurationManager().build().parse( + new ByteArrayInputStream(script.getBytes())); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]