Author: vsiveton Date: Thu Jul 27 11:15:44 2006 New Revision: 426178 URL: http://svn.apache.org/viewvc?rev=426178&view=rev Log: PR: MSITE-168: Use underscores instead of spaces in anchor names
o Added HtmlTools#encodeId() and its testcase o Added javadoc in HtmlTools class o Used it in the XdocSink and XhtmlSink #anchor() Added: maven/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/module/HtmlToolsTest.java Modified: maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/HtmlTools.java maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/xdoc/XdocSink.java maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/xhtml/XhtmlSink.java Modified: maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/HtmlTools.java URL: http://svn.apache.org/viewvc/maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/HtmlTools.java?rev=426178&r1=426177&r2=426178&view=diff ============================================================================== --- maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/HtmlTools.java (original) +++ maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/HtmlTools.java Thu Jul 27 11:15:44 2006 @@ -1,7 +1,7 @@ package org.apache.maven.doxia.module; /* - * Copyright 2004-2005 The Apache Software Foundation. + * Copyright 2004-2006 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,12 +16,31 @@ * limitations under the License. */ -import org.apache.maven.doxia.sink.StructureSink; - import java.io.UnsupportedEncodingException; +import org.apache.maven.doxia.sink.StructureSink; + +/** + * The <code>HtmlTools</code> class defines methods to HTML handling. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a> + * @version $Id$ + */ public class HtmlTools { + /** + * Escape special characters in a text in HTML: + * + * <pre> + * < becomes <code>&</code>lt; + * > becomes <code>&</code>gt; + * & becomes <code>&</code>amp; + * " becomes <code>&</code>quot; + * </pre> + * + * @param text the String to escape, may be null + * @return the text escaped, "" if null String input + */ public static String escapeHTML( String text ) { if ( text == null ) @@ -57,16 +76,27 @@ return buffer.toString(); } - public static String encodeURL( String text ) + /** + * Encode an url + * + * @param url the String to encode, may be null + * @return the text encoded, null if null String input + */ + public static String encodeURL( String url ) { + if ( url == null ) + { + return null; + } + StringBuffer encoded = new StringBuffer(); - int length = text.length(); + int length = url.length(); char[] unicode = new char[1]; for ( int i = 0; i < length; ++i ) { - char c = text.charAt( i ); + char c = url.charAt( i ); switch ( c ) { @@ -91,7 +121,7 @@ case '\'': case '(': case ')': - case '#': // XLink mark + case '#': // XLink mark encoded.append( c ); break; default: @@ -131,8 +161,76 @@ return encoded.toString(); } + /** + * Replace all characters in a text + * + * <pre> + * HtmlTools.encodeFragment( null ) = null + * HtmlTools.encodeFragment( "" ) = "" + * HtmlTools.encodeFragment( "http://www.google.com" ) = "httpwwwgooglecom" + * </pre> + * + * @param text the String to check, may be null + * @return the text with only letter and digit, null if null String input + */ public static String encodeFragment( String text ) { + if ( text == null ) + { + return null; + } return encodeURL( StructureSink.linkToKey( text ) ); + } + + /** + * According to the <a href="http://www.w3.org/TR/html4/types.html#type-name">W3C recommandation</a>: + * <p><i> + * ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number + * of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").</i> + * <p> + * + * <pre> + * HtmlTools.encodeId( null ) = null + * HtmlTools.encodeId( "" ) = "" + * HtmlTools.encodeId( "1anchor" ) = "a1anchor" + * HtmlTools.encodeId( "_anchor" ) = "a_anchor" + * HtmlTools.encodeId( "a b-c123 " ) = "a_b-c123" + * HtmlTools.encodeId( " anchor" ) = "anchor" + * </pre> + * + * @param id an id to be format + * @return the id trimmed and well formated + */ + public static String encodeId( String id ) + { + if ( id == null ) + { + return null; + } + + id = id.trim(); + int length = id.length(); + StringBuffer buffer = new StringBuffer( length ); + + for ( int i = 0; i < length; ++i ) + { + char c = id.charAt( i ); + if ( ( i == 0 ) && ( !Character.isLetter( c ) ) ) + { + buffer.append( "a" ); + } + + if ( ( Character.isLetterOrDigit( c ) ) || ( c == '-' ) || ( c == '_' ) || ( c == ':' ) || ( c == '.' ) ) + { + buffer.append( Character.toLowerCase( c ) ); + } + // Not part of W3C recommandation, just to produce much nicer id + if ( c == ' ' ) + { + buffer.append( "_" ); + } + } + + return buffer.toString(); } } Modified: maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/xdoc/XdocSink.java URL: http://svn.apache.org/viewvc/maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/xdoc/XdocSink.java?rev=426178&r1=426177&r2=426178&view=diff ============================================================================== --- maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/xdoc/XdocSink.java (original) +++ maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/xdoc/XdocSink.java Thu Jul 27 11:15:44 2006 @@ -21,6 +21,7 @@ import org.apache.maven.doxia.sink.SinkAdapter; import org.apache.maven.doxia.sink.StructureSink; import org.apache.maven.doxia.util.LineBreaker; +import org.codehaus.plexus.util.StringUtils; import java.io.Writer; @@ -545,7 +546,7 @@ { if ( !headFlag && !titleFlag ) { - String id = StructureSink.linkToKey( name ); + String id = HtmlTools.encodeId(name); markup( "<a id=\"" + id + "\" name=\"" + id + "\">" ); } } Modified: maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/xhtml/XhtmlSink.java URL: http://svn.apache.org/viewvc/maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/xhtml/XhtmlSink.java?rev=426178&r1=426177&r2=426178&view=diff ============================================================================== --- maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/xhtml/XhtmlSink.java (original) +++ maven/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/module/xhtml/XhtmlSink.java Thu Jul 27 11:15:44 2006 @@ -628,16 +628,13 @@ write( " src=\"" + name + "\"" ); } - + public void anchor( String name ) { if ( !headFlag ) { - if (StringUtils.isEmpty( name )) - { - return; - } - write( "<a name=\"" + name + "\">" ); + String id = HtmlTools.encodeId(name); + write( "<a name=\"" + id + "\">" ); } } Added: maven/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/module/HtmlToolsTest.java URL: http://svn.apache.org/viewvc/maven/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/module/HtmlToolsTest.java?rev=426178&view=auto ============================================================================== --- maven/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/module/HtmlToolsTest.java (added) +++ maven/doxia/trunk/doxia-core/src/test/java/org/apache/maven/doxia/module/HtmlToolsTest.java Thu Jul 27 11:15:44 2006 @@ -0,0 +1,43 @@ +package org.apache.maven.doxia.module; + +/* + * Copyright 2006 The Apache Software Foundation. + * + * Licensed 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 junit.framework.TestCase; + +/** + * Test case for <code>HtmlTools</code> + * + * @author <a href="mailto:[EMAIL PROTECTED]">Vincent Siveton</a> + * @version $Id$ + */ +public class HtmlToolsTest + extends TestCase +{ + + /** + * Verify the awaited results + */ + public void testEncodeId() + { + assertTrue( HtmlTools.encodeId( null ) == null ); + assertTrue( HtmlTools.encodeId( "" ).equals( "" ) ); + assertTrue( HtmlTools.encodeId( "1anchor" ).equals( "a1anchor" ) ); + assertTrue( HtmlTools.encodeId( "_anchor" ).equals( "a_anchor" ) ); + assertTrue( HtmlTools.encodeId( "a b-c123 " ).equals( "a_b-c123" ) ); + assertTrue( HtmlTools.encodeId( " anchor" ).equals( "anchor" ) ); + } +}