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/08 23:23:38 UTC

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

Author: rdonkin
Date: Wed Apr  8 21:23:36 2009
New Revision: 763401

URL: http://svn.apache.org/viewvc?rev=763401&view=rev
Log:
Added integration test. JSIEVE-43 https://issues.apache.org/jira/browse/JSIEVE-43

Added:
    james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/OutputUtils.java   (with props)
    james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/XmlGenerationTest.java   (with props)
Modified:
    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/main/java/org/apache/jsieve/util/SieveToXml.java
    james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/XmlOut.java

Modified: 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=763401&r1=763400&r2=763401&view=diff
==============================================================================
--- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeToSieveAdapter.java (original)
+++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/NodeToSieveAdapter.java Wed Apr  8 21:23:36 2009
@@ -153,7 +153,15 @@
         } else if (value instanceof TagArgument) {
             final TagArgument tagArgument = (TagArgument) value;
             final String tag = tagArgument.getTag();
-            handler.argument(tag);
+            // tag = ":" identifier
+            // handlers are only interesting in the identifier for the tag
+            final String identifier;
+            if (tag.charAt(0) == ':') {
+                identifier = tag.substring(1);
+            } else {
+                identifier = tag;
+            }
+            handler.argument(identifier);
         }
     }
 

Added: 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=763401&view=auto
==============================================================================
--- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/OutputUtils.java (added)
+++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/OutputUtils.java Wed Apr  8 21:23:36 2009
@@ -0,0 +1,67 @@
+/****************************************************************
+ * 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;
+
+import org.apache.jsieve.exception.SieveException;
+import org.apache.jsieve.parser.generated.Node;
+
+/**
+ * Output utilities.
+ * These are mostly convenience methods.
+ * More power and flexibility is available when using the objects directly.
+ */
+public class OutputUtils {
+
+    /**
+     * Writes the given node as xml.
+     * This convenience method first writes a prolog before calling {@link #toXml(Node, Writer)}.
+     * @param node not null
+     * @param writer not null
+     * @throws IOException when prolog cannot be written
+     * @throws SieveException when script cannot be converted to xml
+     * @see #toXml(Node, Writer)
+     */
+    public static void toXmlDocument(final Node node, final Writer writer) throws IOException, SieveException {
+        writer.append("<?xml version='1.0'?>");
+        toXml(node, writer);
+    }
+    
+    /**
+     * Writes the given node as xml.
+     * Note that the xml will be written as a fragment.
+     * An appropriate prolog must be added to convert this fragment 
+     * to a document.
+     * @param node not null
+     * @param writer not null
+     * @throws SieveException when script cannot be converted to xml
+     * @see XmlOut
+     * @see SieveToXml
+     * @see SieveHandler
+     */
+    public static void toXml(final Node node, final Writer writer) throws SieveException {
+        final XmlOut out = new XmlOut(writer);
+        final SieveToXml sieveToXml = new SieveToXml();
+        final SieveHandler handler = sieveToXml.build(out);
+        final NodeTraverser traverser = new NodeTraverser();
+        traverser.traverse(handler, node);
+    }
+}

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

Modified: 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=763401&r1=763400&r2=763401&view=diff
==============================================================================
--- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/SieveHandler.java (original)
+++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/SieveHandler.java Wed Apr  8 21:23:36 2009
@@ -107,11 +107,13 @@
     
     /**
      * Handles a tag argument.
-     * @param tag not null
+     * Note that this supplies the identifier for the tag
+     * (after the leading ':' has been stripped).
+     * @param identifier not null
      * @throws HaltTraversalException
      * @return this
      */
-    public SieveHandler argument(String tag) throws HaltTraversalException;
+    public SieveHandler argument(String identifier) throws HaltTraversalException;
     
     /**
      * Handler a numeric argument.

Modified: james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/SieveToXml.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/SieveToXml.java?rev=763401&r1=763400&r2=763401&view=diff
==============================================================================
--- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/SieveToXml.java (original)
+++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/SieveToXml.java Wed Apr  8 21:23:36 2009
@@ -49,7 +49,7 @@
 
     public static final String DEFAULT_PREFIX = "sieve";
     
-    public static final String DEFAULT_NAMESPACE = "http://james.apache.org/sieve";
+    public static final String DEFAULT_NAMESPACE = "urn:ietf:params:xml:ns:sieve";
     
     /** Control commands (as listed in RFC 3028) */
     public static final String[] CONTROL_COMMANDS = {"If", "Require", "Stop"};

Modified: james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/XmlOut.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/XmlOut.java?rev=763401&r1=763400&r2=763401&view=diff
==============================================================================
--- james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/XmlOut.java (original)
+++ james/jsieve/trunk/util/src/main/java/org/apache/jsieve/util/XmlOut.java Wed Apr  8 21:23:36 2009
@@ -20,8 +20,10 @@
 
 import java.io.IOException;
 import java.io.Writer;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 import java.util.Stack;
 
@@ -395,6 +397,7 @@
 
     }
     
+    private final List prefixesDefined;
     private final Writer writer;
     private final Stack elementNames;
     private final Set currentAttributes = new HashSet();
@@ -406,6 +409,7 @@
     public XmlOut(final Writer writer) {
         this.writer = writer;
         this.elementNames = new Stack();
+        prefixesDefined = new ArrayList();
     }
     
     /**
@@ -664,5 +668,9 @@
     public void openElement(CharSequence localName, CharSequence uri, CharSequence prefix) throws IOException {
         final CharSequence name = toName(localName, uri, prefix);
         openElement(name);
+        if (!prefixesDefined.contains(prefix)) {
+            prefixesDefined.add(prefix);
+            attribute("xmlns:" + prefix, uri);
+        }
     }
 }

Added: james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/XmlGenerationTest.java
URL: http://svn.apache.org/viewvc/james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/XmlGenerationTest.java?rev=763401&view=auto
==============================================================================
--- james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/XmlGenerationTest.java (added)
+++ james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/XmlGenerationTest.java Wed Apr  8 21:23:36 2009
@@ -0,0 +1,61 @@
+/****************************************************************
+ * 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 XmlGenerationTest extends TestCase {
+
+    //@Override
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    //@Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    public void testShouldGenerateXmlFromSimpleScript() 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.toXml(node, monitor);
+        
+        // Verify
+        assertEquals("<sieve:control xmlns:sieve='urn:ietf:params:xml:ns:sieve' sieve:name='if'>" +
+                "<sieve:test sieve:name='address'>" +
+                "<sieve:tag>all</sieve:tag>" +
+                "<sieve:tag>is</sieve:tag>" +
+                "<sieve:str>from</sieve:str>" +
+                "<sieve:str>user@domain</sieve:str>" +
+                "</sieve:test>" +
+                "<sieve:control sieve:name='stop'/>" +
+                "</sieve:control>", monitor.toString());
+    }
+}

Propchange: james/jsieve/trunk/util/src/test/java/org/apache/jsieve/util/XmlGenerationTest.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