Author: tfischer
Date: Fri Feb 28 22:02:39 2014
New Revision: 1573094

URL: http://svn.apache.org/r1573094
Log:
TORQUE-310 add processors to organize imports and to convert windows linefeeds 
to unix linefeeds.

Added:
    
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/processor/string/OrganizeImportsProcessor.java
    
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/processor/string/UnixLinefeedProcessor.java
    
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/processor/string/OrganizeImportsProcessorTest.java
    
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/processor/string/UnixLinefeedProcessorTest.java

Added: 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/processor/string/OrganizeImportsProcessor.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/processor/string/OrganizeImportsProcessor.java?rev=1573094&view=auto
==============================================================================
--- 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/processor/string/OrganizeImportsProcessor.java
 (added)
+++ 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/processor/string/OrganizeImportsProcessor.java
 Fri Feb 28 22:02:39 2014
@@ -0,0 +1,84 @@
+package org.apache.torque.generator.processor.string;
+
+/*
+ * 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.
+ */
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Tries to remove unused imports
+ */
+public class OrganizeImportsProcessor implements StringProcessor
+{
+    /** Regex for import lines. */
+    private static final Pattern IMPORT_PATTERN
+            = Pattern.compile("^import .*\\.[^\r\n\\.]*$", Pattern.MULTILINE);
+
+    /**
+     * Converts Windows CR/LF character sequences to Unix LF sequences.
+     *
+     * @param toProcess the String to process, not null.
+     *
+     * @return the processed String, not null.
+     */
+    public String process(final String toProcess)
+    {
+        Matcher matcher = IMPORT_PATTERN.matcher(toProcess);
+        Map<String, String> importsForClasses = new HashMap<String, String>();
+        while (matcher.find())
+        {
+            String importLine = matcher.group();
+            String className = 
importLine.substring(importLine.lastIndexOf(".") + 1);
+            int indexOfSemicolon = className.lastIndexOf(';');
+            if (indexOfSemicolon != -1)
+            {
+                int endPos = matcher.end();
+                if (toProcess.length() > endPos)
+                {
+                    char followingChar = toProcess.charAt(endPos);
+                    if (followingChar == '\r'
+                        && toProcess.charAt(endPos + 1) == '\n')
+                    {
+                        importLine = importLine + followingChar + '\n';
+                    }
+                    else
+                    {
+                        importLine = importLine + followingChar;
+                    }
+                }
+                className = className.substring(0, indexOfSemicolon).trim();
+                importsForClasses.put(className, importLine);
+            }
+        }
+        String result = toProcess;
+        for (Map.Entry<String, String> importForClassEntry
+                : importsForClasses.entrySet())
+        {
+            String className = importForClassEntry.getKey();
+            if (toProcess.indexOf(className) == 
toProcess.lastIndexOf(className))
+            {
+                result = result.replace(importForClassEntry.getValue(), "");
+            }
+        }
+        return result;
+    }
+}

Added: 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/processor/string/UnixLinefeedProcessor.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/processor/string/UnixLinefeedProcessor.java?rev=1573094&view=auto
==============================================================================
--- 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/processor/string/UnixLinefeedProcessor.java
 (added)
+++ 
db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/processor/string/UnixLinefeedProcessor.java
 Fri Feb 28 22:02:39 2014
@@ -0,0 +1,39 @@
+package org.apache.torque.generator.processor.string;
+
+/*
+ * 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.
+ */
+
+/**
+ * Converts Windows CR/LF character sequences to Unix LF sequences.
+ */
+public class UnixLinefeedProcessor implements StringProcessor
+{
+    /**
+     * Converts Windows CR/LF character sequences to Unix LF sequences.
+     *
+     * @param toProcess the String to process, not null.
+     *
+     * @return the processed String, not null.
+     */
+    public String process(final String toProcess)
+    {
+
+        return toProcess.replace("\r\n", "\n");
+    }
+}

Added: 
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/processor/string/OrganizeImportsProcessorTest.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/processor/string/OrganizeImportsProcessorTest.java?rev=1573094&view=auto
==============================================================================
--- 
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/processor/string/OrganizeImportsProcessorTest.java
 (added)
+++ 
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/processor/string/OrganizeImportsProcessorTest.java
 Fri Feb 28 22:02:39 2014
@@ -0,0 +1,54 @@
+package org.apache.torque.generator.processor.string;
+
+/*
+ * 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.
+ */
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+/**
+ * Unit test for the class OrganizeImportsProcessor.
+ *
+ * @version $Id: $
+ */
+public class OrganizeImportsProcessorTest
+{
+    /** System under test. */
+    private final OrganizeImportsProcessor organizeImportsProcessor
+            = new OrganizeImportsProcessor();
+
+    @Test
+    public void testProcess()
+    {
+        String result = organizeImportsProcessor.process(
+                "package abc.def\r\n"
+              + "import a.b.c.Class1 ; \n"
+              + "import a.b.c.Class2;\r\n"
+              + "import a.b.c.Class3;\r\n"
+              + "ClaSS1\r\n"
+              + "class2\n"
+              + "Class3");
+        assertEquals("package abc.def\r\n"
+                + "import a.b.c.Class3;\r\n"
+                + "ClaSS1\r\n"
+                + "class2\n"
+                + "Class3", result);
+    }
+}

Added: 
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/processor/string/UnixLinefeedProcessorTest.java
URL: 
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/processor/string/UnixLinefeedProcessorTest.java?rev=1573094&view=auto
==============================================================================
--- 
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/processor/string/UnixLinefeedProcessorTest.java
 (added)
+++ 
db/torque/torque4/trunk/torque-generator/src/test/java/org/apache/torque/generator/processor/string/UnixLinefeedProcessorTest.java
 Fri Feb 28 22:02:39 2014
@@ -0,0 +1,43 @@
+package org.apache.torque.generator.processor.string;
+
+/*
+ * 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.
+ */
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+/**
+ * Unit test for the class UnixLinefeedProcessor.
+ *
+ * @version $Id: $
+ */
+public class UnixLinefeedProcessorTest
+{
+    /** System under test. */
+    private final UnixLinefeedProcessor unixLinefeedProcessor
+            = new UnixLinefeedProcessor();
+
+    @Test
+    public void testProcess()
+    {
+        String result = unixLinefeedProcessor.process("abc\rdef\r\nijk\nlmn");
+        assertEquals("abc\rdef\nijk\nlmn", result);
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscr...@db.apache.org
For additional commands, e-mail: torque-dev-h...@db.apache.org

Reply via email to