You are viewing a plain text version of this content. The canonical link for it is here.
Posted to doxia-commits@maven.apache.org by lt...@apache.org on 2007/12/16 21:02:38 UTC

svn commit: r604682 - in /maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src: main/java/org/apache/maven/doxia/module/twiki/parser/ test/java/org/apache/maven/doxia/module/twiki/parser/

Author: ltheussl
Date: Sun Dec 16 12:02:38 2007
New Revision: 604682

URL: http://svn.apache.org/viewvc?rev=604682&view=rev
Log:
[DOXIA-191] Display images given a url
Submitted by: Juan F. Codagnone, Christian Nardi

Added:
    maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/ImageBlock.java   (with props)
Modified:
    maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/TextParser.java
    maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/test/java/org/apache/maven/doxia/module/twiki/parser/WordsTest.java

Added: maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/ImageBlock.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/ImageBlock.java?rev=604682&view=auto
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/ImageBlock.java (added)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/ImageBlock.java Sun Dec 16 12:02:38 2007
@@ -0,0 +1,97 @@
+package org.apache.maven.doxia.module.twiki.parser;
+
+/*
+ * 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 org.apache.maven.doxia.sink.Sink;
+
+
+/**
+ * Block that represents an image.
+ * 
+ * @author Christian Nardi
+ * @since Nov 6, 2007
+ */
+public class ImageBlock implements Block
+{
+    /**
+     * img reference
+     */
+    private final String reference;
+
+    /**
+     * Creates the ImageBlock.
+     *
+     * @param imgReference img reference
+     * @throws IllegalArgumentException if the argument is <code>null</code>
+     */
+    public ImageBlock( final String imgReference )
+    {
+        if ( imgReference == null )
+        {
+            throw new IllegalArgumentException( "arguments can't be null" );
+        }
+        this.reference = imgReference;
+    }
+
+    /**
+     * @see Block#traverse(org.apache.maven.doxia.sink.Sink)
+     */
+    public final void traverse( final Sink sink )
+    {
+        sink.figure();
+        sink.figureGraphics( reference );
+        sink.figure_();
+
+    }
+
+    /**
+     * @see Object#equals(Object)
+     */
+    
+    public final boolean equals( final Object obj )
+    {
+        boolean ret = false;
+
+        if ( obj == this )
+        {
+            ret = true;
+        }
+        else if ( obj instanceof ImageBlock )
+        {
+            final ImageBlock l = (ImageBlock) obj;
+            ret = reference.equals( l.reference );
+        }
+
+        return ret;
+    }
+
+    /**
+     * @see Object#hashCode()
+     */
+    
+    public final int hashCode()
+    {
+        final int magic1 = 17;
+        final int magic2 = 37;
+
+        return magic1 + magic2 * reference.hashCode();
+    }
+
+}

Propchange: maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/ImageBlock.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/ImageBlock.java
------------------------------------------------------------------------------
    svn:keywords = "Author Date Id Revision"

Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/TextParser.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/TextParser.java?rev=604682&r1=604681&r2=604682&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/TextParser.java (original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/main/java/org/apache/maven/doxia/module/twiki/parser/TextParser.java Sun Dec 16 12:02:38 2007
@@ -59,6 +59,12 @@
      */
     private static final Pattern URL_PATTERN =
         Pattern.compile( "(\\w+):[/][/][^\\s]*" );
+    
+    /**
+     *  image pattern specification
+     */
+    private static final Pattern IMAGE_PATTERN = 
+        Pattern.compile( "(.*)\\.(png|jpg|gif|bmp)" );
 
     /**
      * @param line line to parse
@@ -105,15 +111,35 @@
         return ret;
     }
 
+    /**
+     * Parses the url
+     * @param line
+     * @param ret
+     * @param urlMatcher
+     */
     private void parseUrl( final String line, final List ret,
                            final Matcher urlMatcher )
     {
         ret.addAll( parse( line.substring( 0, urlMatcher.start() ) ) );
         final String url = urlMatcher.group( 0 );
-        ret.add( new LinkBlock( url, url ) );
-        ret.addAll( parse( line.substring( urlMatcher.end(), line.length() ) ) );
+        final Matcher imageMatcher = IMAGE_PATTERN.matcher( url ); 
+        if ( imageMatcher.matches() ) 
+        {
+            ret.add( new ImageBlock( url ) );
+        } 
+        else 
+        {
+            ret.add( new LinkBlock( url, url ) );
+        }
+        ret.addAll( parse( line.substring( 
+                urlMatcher.end(), line.length() ) ) );
     }
 
+    /**
+     * @param line
+     * @param ret
+     * @param anchorMatcher
+     */
     private void parseAnchor( final String line, final List ret,
                               final Matcher anchorMatcher )
     {
@@ -122,6 +148,11 @@
         ret.addAll( parse( line.substring( anchorMatcher.end(), line.length() ) ) );
     }
 
+    /**
+     * @param line
+     * @param ret
+     * @param forcedLinkMatcher
+     */
     private void parseForcedLink( final String line, final List ret,
                                   final Matcher forcedLinkMatcher )
     {

Modified: maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/test/java/org/apache/maven/doxia/module/twiki/parser/WordsTest.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/test/java/org/apache/maven/doxia/module/twiki/parser/WordsTest.java?rev=604682&r1=604681&r2=604682&view=diff
==============================================================================
--- maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/test/java/org/apache/maven/doxia/module/twiki/parser/WordsTest.java (original)
+++ maven/doxia/doxia/trunk/doxia-modules/doxia-module-twiki/src/test/java/org/apache/maven/doxia/module/twiki/parser/WordsTest.java Sun Dec 16 12:02:38 2007
@@ -261,4 +261,22 @@
             TOARRAY );
         assertTrue( Arrays.equals( expected, blocks ) );
     }
+    
+    /**
+     * unit test
+     */
+    public final void testAutomaticImage()
+    {
+        Block [] blocks, expected;
+
+        expected = new Block[]{
+            new TextBlock( "Go to " ),
+            new ImageBlock( "http://twiki.com/image.png" ),
+            new TextBlock( " thisisnotanimage.png and ..." ),
+        };
+        blocks = (Block[]) textParser.parse( "Go to http://twiki.com/image.png " 
+                + "thisisnotanimage.png and ..." ).toArray(
+            TOARRAY );
+        assertTrue( Arrays.equals( expected, blocks ) );
+    }
 }