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 2009/04/09 13:23:27 UTC

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

Author: rdonkin
Date: Thu Apr  9 11:23:26 2009
New Revision: 763606

URL: http://svn.apache.org/viewvc?rev=763606&view=rev
Log:
Basic support for output to Sieve script JSIEVE-43 https://issues.apache.org/jira/browse/JSIEVE-43

Added:
    james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/ToSieveHandlerFactory.java   (with props)
    james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/SieveGenerationTest.java   (with props)
    james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/ToSieveHandlerFactoryTest.java   (with props)
Modified:
    james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/OutputUtils.java

Modified: james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/OutputUtils.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/OutputUtils.java?rev=763606&r1=763605&r2=763606&view=diff
==============================================================================
--- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/OutputUtils.java (original)
+++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/OutputUtils.java Thu Apr  9 11:23:26 2009
@@ -74,4 +74,17 @@
         final NodeTraverser traverser = new NodeTraverser();
         traverser.traverse(handler, node);
     }
+    
+    /**
+     * <p>Writes the tree rooted at the given node to a Sieve script.
+     * @param node not null
+     * @param writer not null
+     * @throws SieveException whenever the serialization fails
+     */
+    public static void toSieve(final Node node, final Writer writer) throws SieveException {
+        final ToSieveHandlerFactory factory = new ToSieveHandlerFactory();
+        final SieveHandler handler = factory.build(writer);
+        final NodeTraverser traverser = new NodeTraverser();
+        traverser.traverse(handler, node);
+    }
 }

Added: james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/ToSieveHandlerFactory.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/ToSieveHandlerFactory.java?rev=763606&view=auto
==============================================================================
--- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/ToSieveHandlerFactory.java (added)
+++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/ToSieveHandlerFactory.java Thu Apr  9 11:23:26 2009
@@ -0,0 +1,207 @@
+/****************************************************************
+ * 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.IOException;
+import java.io.Writer;
+
+/**
+ * <p>Builds sieve handlers that convert nodes to a Sieve script.</p>
+ * <h4>Usage</h4>
+ * <ul>
+ * <li>Create an instance</li>
+ * <li>Configure using setters</li>
+ * <li>{@link #build(Writer)} configured {@link SieveHandler}</li>
+ * <li>Supply handler to {@link NodeTraverser}</li>
+ * </ul>
+ * <p>
+ * Handler instances are fully configured when built. 
+ * Changes to the factory configuration will not effect 
+ * them.
+ * </p>
+ */
+public class ToSieveHandlerFactory {
+
+    /**
+     * Builds a configured handler.
+     * @return not null
+     * @see NodeToSieveAdapter
+     * @see NodeTraverser
+     */
+    public SieveHandler build(final Writer writer) {
+        return new ToSieveHandler(writer);
+    }
+    
+    /**
+     * Thread safe worker.
+     */
+    private static final class ToSieveHandler extends SieveHandler.Base {
+        private final Writer writer;
+        
+        private boolean commaRequiredBeforeNextTest;
+        private boolean firstTestInList;
+        private boolean commandUsedBlock;
+     
+        public ToSieveHandler(final Writer writer) {
+            this.writer = writer;
+            commaRequiredBeforeNextTest = false;
+            firstTestInList = false;
+            commandUsedBlock = false;
+        }
+        
+        /** @see SieveHandler#endBlock() */
+        //@Override
+        public SieveHandler endBlock() throws HaltTraversalException {
+            commandUsedBlock = true;
+            return append('}');
+        }
+
+        /** @see SieveHandler#startBlock() */
+        //@Override
+        public SieveHandler startBlock() throws HaltTraversalException {
+            space();
+            return append('{');
+        }
+        
+        /** @see SieveHandler#startCommand() */
+        //@Override
+        public SieveHandler startCommand(String commandName) throws HaltTraversalException {
+            return append(commandName);
+        }
+        
+        /** @see SieveHandler#endCommand() */
+        //@Override
+        public SieveHandler endCommand(String commandName) throws HaltTraversalException {
+            if (!commandUsedBlock) {
+                append(';');
+            }
+            commandUsedBlock = false;
+            return this;
+        }
+        
+        /** @see SieveHandler#argument(String) */
+        //@Override
+        public SieveHandler argument(String tag) throws HaltTraversalException {
+            space();
+            append(':');
+            return append(tag);
+        }
+
+        /**
+         * Appends white space.
+         * @throws HaltTraversalException
+         */
+        private void space() throws HaltTraversalException {
+            append(' ');
+        }
+
+        /** @see SieveHandler#argument(int) */
+        //@Override
+        public SieveHandler argument(int number) throws HaltTraversalException {
+            space();
+            return append(Integer.toString(number));
+        }
+        
+        /** @see SieveHandler#startStringListArgument() */
+        //@Override
+        public SieveHandler startStringListArgument() throws HaltTraversalException {
+            space();
+            return append('[');
+        }
+        
+        /** @see SieveHandler#endStringListArgument() */
+        //@Override
+        public SieveHandler endStringListArgument() throws HaltTraversalException {
+            return append(']');
+        }
+        
+        /** @see SieveHandler#listMember(String) */
+        //@Override
+        public SieveHandler listMember(String string) throws HaltTraversalException {
+            append('"');
+            for (int i=0;i<string.length();i++) {
+                char next = string.charAt(i);
+                if (next == '"' || next == '\\' || next =='\r' || next =='\f') {
+                    append('\\');
+                } 
+                append(next);
+            }
+            return append('"');
+        }
+        
+        
+        /** @see SieveHandler#startTestList() */
+        //@Override
+        public SieveHandler startTestList() throws HaltTraversalException {
+            firstTestInList = true;
+            commaRequiredBeforeNextTest = false;
+            return append('(');
+        }
+        
+        /** @see SieveHandler#endTestList() */
+        //@Override
+        public SieveHandler endTestList() throws HaltTraversalException {
+            return append(')');
+        }
+
+        /** @see SieveHandler#startTest(String) */
+        //@Override
+        public SieveHandler startTest(String testName) throws HaltTraversalException {
+            if (commaRequiredBeforeNextTest) {
+                append(",");
+            }
+            if (!firstTestInList) {
+                space();
+            }
+            commaRequiredBeforeNextTest = true;
+            firstTestInList = false;
+            return append(testName);
+        }
+
+        /**
+         * Appends the given sequence.
+         * @param character to be appended
+         * @return this
+         * @throws HaltTraversalException when character cannot be written
+         */
+        private SieveHandler append(CharSequence characterSequence) throws HaltTraversalException {
+            try {
+                writer.append(characterSequence);
+                return this;
+            } catch (IOException e) {
+                throw new HaltTraversalException(e);
+            }
+        }
+        
+        /**
+         * Appends the given character.
+         * @param character to be appended
+         * @return this
+         * @throws HaltTraversalException when character cannot be written
+         */
+        private SieveHandler append(char character) throws HaltTraversalException {
+            try {
+                writer.append(character);
+                return this;
+            } catch (IOException e) {
+                throw new HaltTraversalException(e);
+            }
+        }
+    }
+}

Propchange: james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/ToSieveHandlerFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/SieveGenerationTest.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/SieveGenerationTest.java?rev=763606&view=auto
==============================================================================
--- james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/SieveGenerationTest.java (added)
+++ james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/SieveGenerationTest.java Thu Apr  9 11:23:26 2009
@@ -0,0 +1,51 @@
+/****************************************************************
+ * 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 java.io.StringWriter;
+
+import org.apache.jsieve.ConfigurationManager;
+import org.apache.jsieve.parser.generated.Node;
+
+import junit.framework.TestCase;
+
+public class SieveGenerationTest extends TestCase {
+
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testShouldGenerateSimpleScriptFromNode() throws Exception {
+        // Set up
+        final String script = "if address :all :is [\"from\"] [\"user@domain\"] {stop;}";
+        final Node node = new ConfigurationManager().build().parse(new ByteArrayInputStream(script.getBytes()));
+        final StringWriter monitor = new StringWriter();
+        
+        // Exercise
+        OutputUtils.toSieve(node, monitor);
+        
+        // Verify
+        assertEquals(script, monitor.toString());
+    }
+}

Propchange: james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/SieveGenerationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/ToSieveHandlerFactoryTest.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/ToSieveHandlerFactoryTest.java?rev=763606&view=auto
==============================================================================
--- james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/ToSieveHandlerFactoryTest.java (added)
+++ james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/ToSieveHandlerFactoryTest.java Thu Apr  9 11:23:26 2009
@@ -0,0 +1,356 @@
+/****************************************************************
+ * 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.StringWriter;
+
+import junit.framework.TestCase;
+
+public class ToSieveHandlerFactoryTest extends TestCase {
+
+    ToSieveHandlerFactory factory;
+    StringWriter monitor;
+    
+    protected void setUp() throws Exception {
+        super.setUp();
+        factory = new ToSieveHandlerFactory();
+        monitor = new StringWriter();
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testDefaultConfigurationShouldBuildNotNullHandler() throws Exception {
+        assertNotNull(factory.build(monitor));
+    }
+    
+    public void testStartScriptShouldBeIgnored() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.startScript();
+        
+        // Verify
+        assertEquals("", monitor.toString());
+    }
+    
+    public void testEndScriptShouldBeIgnored() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.endScript();
+        
+        // Verify
+        assertEquals("", monitor.toString());
+    }
+    
+    public void testStartBlockShouldOpenBracket() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.startBlock();
+        
+        // Verify
+        assertEquals(" {", monitor.toString());
+    }
+    
+    public void testEndBlockShouldCloseBracket() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.endBlock();
+        
+        // Verify
+        assertEquals("}", monitor.toString());
+    }
+    
+    public void testStartCommandsShouldBeIgnored() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.startCommands();
+        
+        // Verify
+        assertEquals("", monitor.toString());
+    }
+    
+    public void testEndCommandsShouldBeIgnored() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.endCommands();
+        
+        // Verify
+        assertEquals("", monitor.toString());
+    }
+    
+    public void testStartCommandShouldPrintIdentifier() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String commandName = "SomeCommand";
+        
+        // Exercise
+        handler.startCommand(commandName);
+        
+        // Verify
+        assertEquals("SomeCommand", monitor.toString());
+    }
+    
+    public void testEndCommandShouldPrintColon() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String commandName = "SomeCommand";
+        
+        // Exercise
+        handler.endCommand(commandName);
+        
+        // Verify
+        assertEquals(";", monitor.toString());
+    }
+    
+    public void testStartArgumentsShouldBeIgnored() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.startArguments();
+        
+        // Verify
+        assertEquals("", monitor.toString());
+    }
+    
+    public void testEndArgumentsShouldBeIgnored() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.endArguments();
+        
+        // Verify
+        assertEquals("", monitor.toString());
+    }
+    
+    public void testArgumentShouldPrintTag() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String identifier = "AnIdentifier";
+        
+        // Exercise
+        handler.argument(identifier);
+        
+        // Verify
+        assertEquals(" :" + identifier, monitor.toString());
+    }
+    
+    public void testArgumentShouldPrintNumber() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        int number = 99;
+        
+        // Exercise
+        handler.argument(number);
+        
+        // Verify
+        assertEquals(" " + Integer.toString(number), monitor.toString());
+    }
+    
+    public void testStartStringListShouldOpenBracket() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.startStringListArgument();
+        
+        // Verify
+        assertEquals(" [", monitor.toString());
+    }
+    
+    public void testEndStringListShouldCloseBracket() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.endStringListArgument();
+        
+        // Verify
+        assertEquals("]", monitor.toString());
+    }
+    
+    public void testListMemberShouldQuoteString() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String member = "A List Member";
+        
+        // Exercise
+        handler.listMember(member);
+        
+        // Verify
+        assertEquals('"' + member + '"', monitor.toString());
+    }
+    
+    public void testListMemberShouldEscapeDoubleQuote() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String prefix = "A Prefix";
+        String suffix = "A Suffix";
+        
+        // Exercise
+        handler.listMember(prefix + '"' + suffix);
+        
+        // Verify
+        assertEquals('"' + prefix + "\\\"" + suffix + '"', monitor.toString());
+    }
+    
+    public void testListMemberShouldEscapeBackSlash() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String prefix = "A Prefix";
+        String suffix = "A Suffix";
+        
+        // Exercise
+        handler.listMember(prefix + '\\' + suffix);
+        
+        // Verify
+        assertEquals('"' + prefix + "\\\\" + suffix + '"', monitor.toString());
+    }
+    
+    public void testListMemberShouldEscapeCR() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String prefix = "A Prefix";
+        String suffix = "A Suffix";
+        
+        // Exercise
+        handler.listMember(prefix + '\r' + suffix);
+        
+        // Verify
+        assertEquals('"' + prefix + "\\\r" + suffix + '"', monitor.toString());
+    }
+    
+    public void testListMemberShouldEscapeLF() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String prefix = "A Prefix";
+        String suffix = "A Suffix";
+        
+        // Exercise
+        handler.listMember(prefix + '\f' + suffix);
+        
+        // Verify
+        assertEquals('"' + prefix + "\\\f" + suffix + '"', monitor.toString());
+    }
+    
+    public void testStartTestListShouldOpenBracket() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.startTestList();
+        
+        // Verify
+        assertEquals("(", monitor.toString());
+    }
+    
+    public void testEndTestListShouldCloseBracket() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        
+        // Exercise
+        handler.endTestList();
+        
+        // Verify
+        assertEquals(")", monitor.toString());
+    }
+    
+    public void testStartTestShouldPrintIdentifier() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String identifier = "AnIdentifier";
+        
+        // Exercise
+        handler.startTest(identifier);
+        
+        // Verify
+        assertEquals(" " + identifier, monitor.toString());
+    }
+    
+    public void testStartSecondTestShouldPrefixComma() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String firstIdentifier = "FirstIdentifier";
+        String secondIdentifier = "SecondIdentifier";
+        
+        // Exercise
+        handler.startTest(firstIdentifier);
+        handler.endTest(firstIdentifier);
+        handler.startTest(secondIdentifier);
+        
+        // Verify
+        assertEquals(" " +firstIdentifier + ", " + secondIdentifier, monitor.toString());
+    }
+    
+    public void testAfterEndTestListShouldNotNextPrefixTestWithComma() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String firstIdentifier = "FirstIdentifier";
+        String secondIdentifier = "SecondIdentifier";
+        
+        // Exercise
+        handler.startTest(firstIdentifier);
+        handler.endTest(firstIdentifier);
+        handler.endTestList();
+        handler.startTestList();
+        handler.startTest(secondIdentifier);
+        
+        // Verify
+        assertEquals(" " + firstIdentifier + ")(" + secondIdentifier, monitor.toString());
+    }
+    
+    public void testEndTestShouldBeIgnored() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String identifier = "AnIdentifier";
+        
+        // Exercise
+        handler.endTest(identifier);
+        
+        // Verify
+        assertEquals("", monitor.toString());
+    }
+    
+    public void testEndCommandShouldNotPrintSemiColonAfterBlock() throws Exception {
+        // Setup
+        SieveHandler handler = factory.build(monitor);
+        String commandName = "SomeCommand";
+        
+        // Exercise
+        handler.endBlock();
+        handler.endCommand(commandName);
+        
+        // Verify
+        assertEquals("}", monitor.toString());
+    }
+}

Propchange: james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/ToSieveHandlerFactoryTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



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