cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sylv...@apache.org
Subject cvs commit: xml-cocoon2/src/org/apache/cocoon/util NetUtils.java
Date Fri, 28 Dec 2001 16:04:08 GMT
sylvain     01/12/28 08:04:08

  Modified:    documentation/stylesheets book2menu.xsl site2xhtml.xsl
               src/org/apache/cocoon/util NetUtils.java
  Log:
  Add RFC1738-conformant encoding to generated URLs so that side-menu images are displayed
correctly by Netscape 4.x
  
  Revision  Changes    Path
  1.7       +11 -6     xml-cocoon2/documentation/stylesheets/book2menu.xsl
  
  Index: book2menu.xsl
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/documentation/stylesheets/book2menu.xsl,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- book2menu.xsl	2001/10/11 21:00:49	1.6
  +++ book2menu.xsl	2001/12/28 16:04:08	1.7
  @@ -1,6 +1,7 @@
   <?xml version="1.0"?>
   
   <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  +                xmlns:NetUtils="org.apache.cocoon.util.NetUtils"
                   version="1.0">
   
     <xsl:param name="resource"/>
  @@ -19,12 +20,14 @@
     </xsl:template>
   
     <xsl:template match="menu">
  +     <!-- Encode label to escape any reserved characters such as space -->
  +     <xsl:variable name="encLabel" select="NetUtils:encodePath(@label)"/>
        <tr>
          <td align="left" valign="top">
            <img border="0"
                 height="20"
                 hspace="0"
  -              src="graphics/{@label}-separator.jpg"
  +              src="graphics/{$encLabel}-separator.jpg"
                 vspace="0"
                 width="120"
                 alt="{@label}"/>
  @@ -37,12 +40,13 @@
   
     <xsl:template match="menu-item">
       <xsl:if test="not(@type) or @type!='hidden'">
  +     <xsl:variable name="encLabel" select="NetUtils:encodePath(@label)"/>
        <tr>
         <td align="left" valign="top">
          <xsl:choose>
            <xsl:when test="@href=$resource">
             <img alt="{@label}"
  -               src="graphics/{@label}-label_select.jpg"
  +               src="graphics/{$encLabel}-label_select.jpg"
                  border="0"
                  height="12"
                  width="120"
  @@ -54,14 +58,14 @@
             <a href="{@href}" onMouseOut="rolloverOff('{@label}')"
                               onMouseOver="rolloverOn('{@label}')">
              <img alt="{@label}"
  -                src="graphics/{@label}-label.jpg"
  +                src="graphics/{$encLabel}-label.jpg"
                   border="0"
                   height="12"
                   width="120"
                   name="{@label}"
                   hspace="0"
                   vspace="0"
  -                onLoad="rolloverLoad('{@label}', 'graphics/{@label}-label_over.jpg', 'graphics/{@label}-label.jpg');"/>
  +                onLoad="rolloverLoad('{@label}', 'graphics/{$encLabel}-label_over.jpg',
'graphics/{$encLabel}-label.jpg');"/>
            </a>
           </xsl:otherwise>
          </xsl:choose>
  @@ -72,20 +76,21 @@
   
     <xsl:template match="external">
       <xsl:if test="not(@type) or @type!='hidden'">
  +     <xsl:variable name="encLabel" select="NetUtils:encodePath(@label)"/>
        <tr>
   	<td align="left" valign="top">
           <a href="{@href}" target="new"
                             onMouseOut="rolloverOff('{@label}')"
                             onMouseOver="rolloverOn('{@label}')">
             <img alt="{@label}"
  -               src="graphics/{@label}-label.jpg"
  +               src="graphics/{$encLabel}-label.jpg"
                  border="0"
                  height="12"
                  hspace="0"
                  name="{@label}"
                  vspace="0"
                  width="120"
  -               onLoad="rolloverLoad('{@label}', 'graphics/{@label}-label_over.jpg', 'graphics/{@label}-label.jpg');"/>
  +               onLoad="rolloverLoad('{@label}', 'graphics/{$encLabel}-label_over.jpg',
'graphics/{$encLabel}-label.jpg');"/>
           </a>
   	</td>
        </tr>
  
  
  
  1.8       +2 -1      xml-cocoon2/documentation/stylesheets/site2xhtml.xsl
  
  Index: site2xhtml.xsl
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/documentation/stylesheets/site2xhtml.xsl,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- site2xhtml.xsl	2001/10/11 21:00:49	1.7
  +++ site2xhtml.xsl	2001/12/28 16:04:08	1.8
  @@ -2,6 +2,7 @@
   
   <xsl:stylesheet
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  +    xmlns:URLEncoder="java.net.URLEncoder"
       version="1.0">
   
   	<xsl:param name="header"/>
  @@ -31,7 +32,7 @@
             </tr>
             <tr>
               <td width="100%" height="35" valign="top" align="right" colspan="2" bgcolor="#0086b2">
  -              <img src="{$header}?label={/site/document/title}" hspace="0" vspace="0"
border="0" alt="{/site/document/title}" align="right"/>           
  +              <img src="{$header}?label={URLEncoder:encode(/site/document/title)}" hspace="0"
vspace="0" border="0" alt="{/site/document/title}" align="right"/>           
               </td>
             </tr>
             <tr>
  
  
  
  1.10      +114 -1    xml-cocoon2/src/org/apache/cocoon/util/NetUtils.java
  
  Index: NetUtils.java
  ===================================================================
  RCS file: /home/cvs/xml-cocoon2/src/org/apache/cocoon/util/NetUtils.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- NetUtils.java	2001/10/30 09:06:50	1.9
  +++ NetUtils.java	2001/12/28 16:04:08	1.10
  @@ -8,6 +8,11 @@
   
   package org.apache.cocoon.util;
   
  +import java.io.ByteArrayOutputStream;
  +import java.io.IOException;
  +import java.io.OutputStreamWriter;
  +
  +import java.util.BitSet;
   import java.util.Iterator;
   import java.util.Map;
   
  @@ -16,10 +21,118 @@
    * utility methods
    *
    * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
  - * @version CVS $Revision: 1.9 $ $Date: 2001/10/30 09:06:50 $
  + * @version CVS $Revision: 1.10 $ $Date: 2001/12/28 16:04:08 $
    */
   
   public class NetUtils {
  +
  +    /**
  +     * Array containing the safe characters set as defined by RFC 1738
  +     */
  +    private static BitSet safeCharacters;
  +
  +
  +    private static final char[] hexadecimal =
  +    {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  +     'A', 'B', 'C', 'D', 'E', 'F'};
  +
  +    static {
  +        safeCharacters = new BitSet(256);
  +        int i;
  +        // 'lowalpha' rule
  +        for (i = 'a'; i <= 'z'; i++) {
  +            safeCharacters.set(i);
  +        }
  +        // 'hialpha' rule
  +        for (i = 'A'; i <= 'Z'; i++) {
  +            safeCharacters.set(i);
  +        }
  +        // 'digit' rule
  +        for (i = '0'; i <= '9'; i++) {
  +            safeCharacters.set(i);
  +        }
  +        
  +        // 'safe' rule
  +        safeCharacters.set('$');
  +        safeCharacters.set('-');
  +        safeCharacters.set('_');
  +        safeCharacters.set('.');
  +        safeCharacters.set('+');
  +        
  +        // 'extra' rule
  +        safeCharacters.set('!');
  +        safeCharacters.set('*');
  +        safeCharacters.set('\'');
  +        safeCharacters.set('(');
  +        safeCharacters.set(')');
  +        safeCharacters.set(',');
  +        
  +        // special characters common to http: file: and ftp: URLs ('fsegment' and 'hsegment'
rules)
  +        safeCharacters.set('/');
  +        safeCharacters.set(':');
  +        safeCharacters.set('@');
  +        safeCharacters.set('&');
  +        safeCharacters.set('=');
  +    }
  +
  +    /**
  +     * Encode a path as required by the URL specificatin (<a href="http://www.ietf.org/rfc/rfc1738.txt">
  +     * RFC 1738</a>). This differs from <code>java.net.URLEncoder.encode()</code>
which encodes according
  +     * to the <code>x-www-form-urlencoded</code> MIME format.
  +     *
  +     * @param path the path to encode
  +     * @return the encoded path
  +     */
  +    public static String encodePath(String path) {
  +       // stolen from org.apache.catalina.servlets.DefaultServlet ;)
  +
  +        /**
  +         * Note: This code portion is very similar to URLEncoder.encode.
  +         * Unfortunately, there is no way to specify to the URLEncoder which
  +         * characters should be encoded. Here, ' ' should be encoded as "%20"
  +         * and '/' shouldn't be encoded.
  +         */
  +
  +        int maxBytesPerChar = 10;
  +        StringBuffer rewrittenPath = new StringBuffer(path.length());
  +        ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
  +        OutputStreamWriter writer = null;
  +        try {
  +            writer = new OutputStreamWriter(buf, "UTF8");
  +        } catch (Exception e) {
  +            e.printStackTrace();
  +            writer = new OutputStreamWriter(buf);
  +        }
  +
  +        for (int i = 0; i < path.length(); i++) {
  +            int c = (int) path.charAt(i);
  +            if (safeCharacters.get(c)) {
  +                rewrittenPath.append((char)c);
  +            } else {
  +                // convert to external encoding before hex conversion
  +                try {
  +                    writer.write(c);
  +                    writer.flush();
  +                } catch(IOException e) {
  +                    buf.reset();
  +                    continue;
  +                }
  +                byte[] ba = buf.toByteArray();
  +                for (int j = 0; j < ba.length; j++) {
  +                    // Converting each byte in the buffer
  +                    byte toEncode = ba[j];
  +                    rewrittenPath.append('%');
  +                    int low = (int) (toEncode & 0x0f);
  +                    int high = (int) ((toEncode & 0xf0) >> 4);
  +                    rewrittenPath.append(hexadecimal[high]);
  +                    rewrittenPath.append(hexadecimal[low]);
  +                }
  +                buf.reset();
  +            }
  +        }
  +
  +        return rewrittenPath.toString();
  +    }
   
       /**
        * Returns the path of the given resource.
  
  
  

----------------------------------------------------------------------
In case of troubles, e-mail:     webmaster@xml.apache.org
To unsubscribe, e-mail:          cocoon-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: cocoon-cvs-help@xml.apache.org


Mime
View raw message