Author: rdonkin
Date: Sun Jan 18 13:49:46 2009
New Revision: 735551
URL: http://svn.apache.org/viewvc?rev=735551&view=rev
Log:
JSIEVE-47 Added access for comments from scripts.
Added:
james/jsieve/trunk/main/src/test/java/org/apache/jsieve/parser/SieveNodeCommentTest.java
Modified:
james/jsieve/trunk/main/src/main/java/org/apache/jsieve/parser/SieveNode.java
Modified:
james/jsieve/trunk/main/src/main/java/org/apache/jsieve/parser/SieveNode.java
URL:
http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/main/java/org/apache/jsieve/parser/SieveNode.java?rev=735551&r1=735550&r2=735551&view=diff
==============================================================================
---
james/jsieve/trunk/main/src/main/java/org/apache/jsieve/parser/SieveNode.java
(original)
+++
james/jsieve/trunk/main/src/main/java/org/apache/jsieve/parser/SieveNode.java
Sun Jan 18 13:49:46 2009
@@ -19,6 +19,9 @@
package org.apache.jsieve.parser;
+import java.util.LinkedList;
+import java.util.List;
+
import org.apache.jsieve.ScriptCoordinate;
import org.apache.jsieve.parser.generated.Token;
@@ -141,4 +144,62 @@
firstLine, firstColumn, lastList, lastColumn);
return scriptCoordinate;
}
+
+ /**
+ * Get any comments between this node and the previous one.
+ * Each comment is returned without whitespace trimming.
+ * Comments are returned in the order of occurance in the script.
+ * @return collection of strings, not null
+ */
+ public List getPrecedingComments() {
+ final LinkedList results = new LinkedList();
+ if (firstToken != null) {
+ Token special = firstToken.specialToken;
+ while (special != null) {
+ final String comment = parseComment(special);
+ results.addFirst(comment);
+ special = special.specialToken;
+ }
+ }
+ return results;
+ }
+
+ private String parseComment(Token special) {
+ final String image = special.image;
+ final String comment;
+ if ('#' == image.charAt(0)) {
+ final int leftHandCharactersToIgnore;
+ if ('\r' == image.charAt(image.length()-2)) {
+ leftHandCharactersToIgnore = 2;
+ } else {
+ leftHandCharactersToIgnore = 1;
+ }
+ comment = image.substring(1,
image.length()-leftHandCharactersToIgnore);
+ } else {
+ comment = image.substring(2, image.length()-2);
+ }
+ return comment;
+ }
+
+ /**
+ * Get the last comment before this node and after the last node.
+ * Each comment is returned without whitespace trimming.
+ * Comments are returned in the order of occurance in the script.
+ * @return the comment without whitespace trimming,
+ * or null if there is no comment between this and the last node
+ */
+ public String getLastComment() {
+ final String result;
+ if (firstToken == null) {
+ result = null;
+ } else {
+ Token special = firstToken.specialToken;
+ if (special == null) {
+ result = null;
+ } else {
+ result = parseComment(special);
+ }
+ }
+ return result;
+ }
}
Added:
james/jsieve/trunk/main/src/test/java/org/apache/jsieve/parser/SieveNodeCommentTest.java
URL:
http://svn.apache.org/viewvc/james/jsieve/trunk/main/src/test/java/org/apache/jsieve/parser/SieveNodeCommentTest.java?rev=735551&view=auto
==============================================================================
---
james/jsieve/trunk/main/src/test/java/org/apache/jsieve/parser/SieveNodeCommentTest.java
(added)
+++
james/jsieve/trunk/main/src/test/java/org/apache/jsieve/parser/SieveNodeCommentTest.java
Sun Jan 18 13:49:46 2009
@@ -0,0 +1,91 @@
+/****************************************************************
+ * 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.parser;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.jsieve.utils.JUnitUtils;
+
+public class SieveNodeCommentTest extends TestCase {
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testGetNoCommentsBefore() throws Exception {
+ SieveNode node = (SieveNode) JUnitUtils.parse("if address :contains
[\"To\", \"From\"] \"Fish!\"{ }");
+ List comments = node.getPrecedingComments();
+ assertNotNull(comments);
+ assertEquals(0, comments.size());
+ }
+
+ public void testGetBracketCommentsBefore() throws Exception {
+ SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment *//*
Another comment */if address :contains [\"To\", \"From\"] \"Fish!\"{ }");
+ List comments = node.getPrecedingComments();
+ assertNotNull(comments);
+ assertEquals(2, comments.size());
+ assertEquals(" A Comment ", comments.get(0));
+ assertEquals(" Another comment ", comments.get(1));
+ }
+
+ public void testGetHashCommentsBefore() throws Exception {
+ SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment */#A Line
Comment\nif address :contains [\"To\", \"From\"] \"Fish!\"{ }");
+ List comments = node.getPrecedingComments();
+ assertNotNull(comments);
+ assertEquals(2, comments.size());
+ assertEquals(" A Comment ", comments.get(0));
+ assertEquals("A Line Comment", comments.get(1));
+ }
+
+ public void testGetHashCommentsBeforeCRLF() throws Exception {
+ SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment */#A Line
Comment\r\nif address :contains [\"To\", \"From\"] \"Fish!\"{ }");
+ List comments = node.getPrecedingComments();
+ assertNotNull(comments);
+ assertEquals(2, comments.size());
+ assertEquals(" A Comment ", comments.get(0));
+ assertEquals("A Line Comment", comments.get(1));
+ }
+
+
+ public void testGetLastCommentNoneBefore() throws Exception {
+ SieveNode node = (SieveNode) JUnitUtils.parse("if address :contains
[\"To\", \"From\"] \"Fish!\"{ }");
+ assertNull(node.getLastComment());
+ }
+
+ public void testGetBracketLastCommentBefore() throws Exception {
+ SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment *//*
Another comment */if address :contains [\"To\", \"From\"] \"Fish!\"{ }");
+ assertEquals(" Another comment ", node.getLastComment());
+ }
+
+ public void testGetHashLastCommentBefore() throws Exception {
+ SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment */#A Line
Comment\nif address :contains [\"To\", \"From\"] \"Fish!\"{ }");
+ assertEquals("A Line Comment", node.getLastComment());
+ }
+
+ public void testGetHashLastCommentBeforeCRLF() throws Exception {
+ SieveNode node = (SieveNode) JUnitUtils.parse("/* A Comment */#A Line
Comment\r\nif address :contains [\"To\", \"From\"] \"Fish!\"{ }");;
+ assertEquals("A Line Comment", node.getLastComment());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]