You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2014/01/24 22:53:37 UTC

svn commit: r1561205 - in /maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src: main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java test/java/org/apache/maven/doxia/siterenderer/SkinResourceLoaderTest.java

Author: rfscholte
Date: Fri Jan 24 21:53:37 2014
New Revision: 1561205

URL: http://svn.apache.org/r1561205
Log:
Use strategy of AbstractSink.unifyEOLs to unify EOLs. Small bugfix: also respect MacOS9 EOLs

Added:
    maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/SkinResourceLoaderTest.java
Modified:
    maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java

Modified: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java?rev=1561205&r1=1561204&r2=1561205&view=diff
==============================================================================
--- maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java (original)
+++ maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java Fri Jan 24 21:53:37 2014
@@ -54,7 +54,7 @@ public class SkinResourceLoader
         return normalizeNewline( classLoader.getResourceAsStream( name ) );
     }
 
-    private InputStream normalizeNewline( InputStream in )
+    InputStream normalizeNewline( InputStream in )
         throws ResourceNotFoundException
     {
         if ( in == null )
@@ -66,49 +66,38 @@ public class SkinResourceLoader
         {
             byte[] content = IOUtil.toByteArray( in );
 
-            boolean crlf = System.getProperty( "line.separator" ).length() > 1;
+            // following code based on org.apache.maven.doxia.sink.AbstractSink.unifyEOLs(String)
+            
+            byte[] EOL = System.getProperty( "line.separator" ).getBytes();
 
-            if ( crlf )
+            final int size = content.length;
+
+            ByteArrayOutputStream out = new ByteArrayOutputStream( size );
+
+            for ( int i = 0; i < size; i++ )
             {
-                // ensure every \n is preceded by \r
-                final int size = content.length;
-                ByteArrayOutputStream out = new ByteArrayOutputStream( size );
-                boolean cr = false;
-                for ( int i = 0; i < size; i++ )
+                byte b = content[i];
+
+                if ( b == '\r' )
                 {
-                    byte b = content[i];
-                    if ( b == '\r' )
+                    if ( ( i + 1 ) < size && content[i + 1] == '\n' )
                     {
-                        cr = true;
-                        out.write( b );
-                    }
-                    else
-                    {
-                        if ( b == '\n' && !cr )
-                        {
-                            out.write( '\r' );
-                        }
-                        cr = false;
-                        out.write( b );
+                        i++;
                     }
+
+                    out.write( EOL );
                 }
-                return new ByteArrayInputStream( out.toByteArray() );
-            }
-            else
-            {
-                // remove every \r
-                final int size = content.length;
-                int j = 0;
-                for ( int i = 0; i < size; i++ )
+                else if ( b == '\n' )
                 {
-                    byte b = content[i];
-                    if ( b != '\r' )
-                    {
-                        content[j++] = b;
-                    }
+                    out.write( EOL );
+                }
+                else
+                {
+                    out.write( b );
                 }
-                return new ByteArrayInputStream( content, 0, j );
             }
+
+            return new ByteArrayInputStream( out.toByteArray() );
         }
         catch ( IOException ioe )
         {

Added: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/SkinResourceLoaderTest.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/SkinResourceLoaderTest.java?rev=1561205&view=auto
==============================================================================
--- maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/SkinResourceLoaderTest.java (added)
+++ maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/SkinResourceLoaderTest.java Fri Jan 24 21:53:37 2014
@@ -0,0 +1,62 @@
+package org.apache.maven.doxia.siterenderer;
+
+/*
+ * 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.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.maven.doxia.sink.AbstractSink;
+import org.codehaus.plexus.util.IOUtil;
+
+public class SkinResourceLoaderTest
+    extends TestCase
+{
+    private SkinResourceLoader skinResourceLoader = new SkinResourceLoader();
+
+    public void testNormalizeNewline() throws Exception
+    {
+        String EOL = AbstractSink.EOL;
+        String EOL_MACOS9 = "\r";
+        String EOL_UNIX = "\n";
+        String EOL_WIN = "\r\n";
+        
+        assertEquals( "Hello " + EOL + " world", normalizeNewline( "Hello " + EOL_MACOS9 + " world" ) );
+        assertEquals( "Hello " + EOL + " world", normalizeNewline( "Hello " + EOL_UNIX + " world" ) );
+        assertEquals( "Hello " + EOL + " world", normalizeNewline( "Hello " + EOL_WIN + " world" ) );
+
+        assertEquals( "Hello world" + EOL, normalizeNewline( "Hello world" + EOL_MACOS9 ) );
+        assertEquals( "Hello world" + EOL, normalizeNewline( "Hello world" + EOL_UNIX ) );
+        assertEquals( "Hello world" + EOL, normalizeNewline( "Hello world" + EOL_WIN ) );
+
+        assertEquals( EOL + "Hello world", normalizeNewline( EOL_MACOS9 + "Hello world" ) );
+        assertEquals( EOL + "Hello world", normalizeNewline( EOL_UNIX + "Hello world" ) );
+        assertEquals( EOL + "Hello world", normalizeNewline( EOL_WIN + "Hello world" ) );
+    }
+    
+    private String normalizeNewline( String  text ) throws IOException
+    {
+        InputStream in = new ByteArrayInputStream( text.getBytes() ); 
+        InputStream out = skinResourceLoader.normalizeNewline( in );
+        return IOUtil.toString( out );
+    }
+}