You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2014/01/19 20:36:13 UTC

svn commit: r1559569 - in /maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src: main/java/org/apache/maven/doxia/siterenderer/ main/resources/META-INF/plexus/ test/java/org/apache/maven/doxia/siterenderer/ test/resources/org/apache/maven/doxia/si...

Author: hboutemy
Date: Sun Jan 19 19:36:13 2014
New Revision: 1559569

URL: http://svn.apache.org/r1559569
Log:
[DOXIASITETOOLS-87] normalize newlines when reading template from skin

Added:
    maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java   (with props)
Modified:
    maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/resources/META-INF/plexus/components.xml
    maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java
    maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.expected.txt   (contents, props changed)
    maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.vm   (contents, props changed)

Added: 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=1559569&view=auto
==============================================================================
--- maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java (added)
+++ maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java Sun Jan 19 19:36:13 2014
@@ -0,0 +1,132 @@
+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.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.velocity.runtime.resource.Resource;
+import org.apache.velocity.runtime.resource.loader.ResourceLoader;
+import org.apache.velocity.exception.ResourceNotFoundException;
+import org.apache.commons.collections.ExtendedProperties;
+import org.codehaus.plexus.util.IOUtil;
+
+/**
+ * Skin resource loader: gets content from context classloader, which should contain skin artifact,
+ * and normalizes newlines. 
+ */
+public class SkinResourceLoader
+    extends ResourceLoader
+{
+    public void init( ExtendedProperties configuration )
+    {
+    }
+
+    public synchronized InputStream getResourceStream( String name )
+        throws ResourceNotFoundException
+    {
+        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+
+        if ( name.startsWith( "/" ) )
+        {
+            name = name.substring( 1 );
+        }
+
+        return normalizeNewline( classLoader.getResourceAsStream( name ) );
+    }
+
+    private InputStream normalizeNewline( InputStream in )
+        throws ResourceNotFoundException
+    {
+        if ( in == null )
+        {
+            return null;
+        }
+
+        try
+        {
+            byte[] content = IOUtil.toByteArray( in );
+
+            boolean crlf = System.getProperty( "line.separator" ).length() > 1;
+
+            if ( crlf )
+            {
+                // 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' )
+                    {
+                        cr = true;
+                        out.write( b );
+                    }
+                    else
+                    {
+                        if ( b == '\n' && !cr )
+                        {
+                            out.write( '\r' );
+                        }
+                        cr = false;
+                        out.write( b );
+                    }
+                }
+                return new ByteArrayInputStream( out.toByteArray() );
+            }
+            else
+            {
+                // remove every \r
+                final int size = content.length;
+                int j = 0;
+                for ( int i = 0; i < size; i++ )
+                {
+                    byte b = content[i];
+                    if ( b != '\r' )
+                    {
+                        content[j++] = b;
+                    }
+                }
+                return new ByteArrayInputStream( content, 0, j );
+            }
+        }
+        catch ( IOException ioe )
+        {
+            throw new ResourceNotFoundException( "cannot read resource", ioe );
+        }
+        finally
+        {
+            IOUtil.close( in );
+        }
+    }
+
+    public boolean isSourceModified( Resource resource )
+    {
+        return false;
+    }
+
+    public long getLastModified( Resource resource )
+    {
+        return 0;
+    }
+}

Propchange: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/SkinResourceLoader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/resources/META-INF/plexus/components.xml
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/resources/META-INF/plexus/components.xml?rev=1559569&r1=1559568&r2=1559569&view=diff
==============================================================================
--- maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/resources/META-INF/plexus/components.xml (original)
+++ maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/main/resources/META-INF/plexus/components.xml Sun Jan 19 19:36:13 2014
@@ -29,11 +29,11 @@ under the License.
         <properties>
           <property>
             <name>resource.loader</name>
-            <value>classpath,site</value>
+            <value>skin,site</value>
           </property>
           <property>
-            <name>classpath.resource.loader.class</name>
-            <value>org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader</value>
+            <name>skin.resource.loader.class</name>
+            <value>org.apache.maven.doxia.siterenderer.SkinResourceLoader</value>
           </property>
           <property>
             <name>site.resource.loader.class</name>

Modified: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java?rev=1559569&r1=1559568&r2=1559569&view=diff
==============================================================================
--- maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java (original)
+++ maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java Sun Jan 19 19:36:13 2014
@@ -92,6 +92,7 @@ public class DefaultSiteRendererTest
         try
         {
             IOUtil.copy( is, os );
+            os.write( "\n\n\n\r\n\r\n\r\n".getBytes( "ISO-8859-1" ) );
         }
         finally
         {
@@ -167,6 +168,7 @@ public class DefaultSiteRendererTest
         verifyMisc();
         verifyDocbookPageExists();
         verifyApt();
+        verifyNewlines();
 
         // ----------------------------------------------------------------------
         // Validate the rendering pages
@@ -191,7 +193,8 @@ public class DefaultSiteRendererTest
         assertEquals( expectedResult, renderResult );
     }
 
-    public void testVelocityToolManagerForTemplate() throws Exception
+    public void testVelocityToolManagerForTemplate()
+        throws Exception
     {
         StringWriter writer = new StringWriter();
 
@@ -211,7 +214,8 @@ public class DefaultSiteRendererTest
         assertEquals( expectedResult, renderResult );
     }
 
-    public void testVelocityToolManagerForSkin() throws Exception
+    public void testVelocityToolManagerForSkin()
+        throws Exception
     {
         StringWriter writer = new StringWriter();
 
@@ -223,7 +227,6 @@ public class DefaultSiteRendererTest
         RenderingContext context = new RenderingContext( new File( "" ), "document.html" );
         SiteRendererSink sink = new SiteRendererSink( context );
         renderer.generateDocument( writer, sink, siteRenderingContext );
-
         String renderResult = writer.toString();
         String expectedResult = IOUtils.toString( getClass().getResourceAsStream( "velocity-toolmanager.expected.txt" ) );
         assertEquals( expectedResult, renderResult );
@@ -380,12 +383,30 @@ public class DefaultSiteRendererTest
     }
 
     /**
+     * @throws Exception if something goes wrong.
+     */
+    public void verifyNewlines()
+        throws Exception
+    {
+        checkNewlines( FileUtils.fileRead( getTestFile( "target/output/head.html" ), "ISO-8859-1" ) );
+    }
+
+    private void checkNewlines( String content )
+    {
+        int cr = StringUtils.countMatches( content, "\r" );
+        int lf = StringUtils.countMatches( content, "\n" );
+        assertTrue( "Should contain only Windows or Unix newlines: cr = " + cr + ", lf = " + lf, ( cr == 0 )
+            || ( cr == lf ) );
+    }
+
+    /**
      * Validate the generated pages.
      *
      * @throws Exception if something goes wrong.
      * @since 1.1.1
      */
-    public void validatePages() throws Exception
+    public void validatePages()
+        throws Exception
     {
         new XhtmlValidatorTest().validateGeneratedPages();
     }

Modified: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.expected.txt
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.expected.txt?rev=1559569&r1=1559568&r2=1559569&view=diff
==============================================================================
--- maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.expected.txt (original)
+++ maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.expected.txt Sun Jan 19 19:36:13 2014
@@ -1,2 +1,2 @@
-Maven 2 &amp; 3
-This is...
\ No newline at end of file
+Maven 2 &amp; 3
+This is...

Propchange: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.expected.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.vm
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.vm?rev=1559569&r1=1559568&r2=1559569&view=diff
==============================================================================
--- maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.vm (original)
+++ maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.vm Sun Jan 19 19:36:13 2014
@@ -1,2 +1,2 @@
-$esc.html('Maven 2 & 3')
-$display.truncate("This is a long string.", 10)
\ No newline at end of file
+$esc.html('Maven 2 & 3')
+$display.truncate("This is a long string.", 10)

Propchange: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/org/apache/maven/doxia/siterenderer/velocity-toolmanager.vm
------------------------------------------------------------------------------
    svn:eol-style = native