You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ni...@apache.org on 2004/02/14 03:56:54 UTC

cvs commit: avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl URLSource.java

niclas      2004/02/13 18:56:54

  Modified:    sourceresolve/src/java/org/apache/excalibur/source
                        SourceResolver.java SourceUtil.java
               sourceresolve/src/java/org/apache/excalibur/source/impl
                        URLSource.java
  Log:
  Applied patch from http://nagoya.apache.org/bugzilla/show_bug.cgi?id=26923
  
  Revision  Changes    Path
  1.3       +13 -1     avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/SourceResolver.java
  
  Index: SourceResolver.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/SourceResolver.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SourceResolver.java	22 Nov 2003 11:30:40 -0000	1.2
  +++ SourceResolver.java	14 Feb 2004 02:56:54 -0000	1.3
  @@ -97,6 +97,12 @@
        */
       String URI_PARAMETERS = "org.apache.excalibur.source.Source.uri.parameters";
   
  +    /** With this parameter you can specify the encoding to use for encoding
  +     * the additional request parameters the URI. It is up to the protocol
  +     * implementation ({@link SourceFactory}) to support this or not.
  +     */
  +    String URI_ENCODING = "org.apache.excalibur.source.Source.uri.encoding";
  +	
       /**
        * Get a {@link Source} object. This is a shortcut for {@link #resolveURI
        * (String, String, Map)}.
  @@ -108,6 +114,12 @@
       Source resolveURI( String location )
           throws MalformedURLException, IOException;
   
  +    /** With this parameter you can specify the encoding to use for encoding
  +     * the additional request parameters the URI. It is up to the protocol
  +     * implementation ({@link SourceFactory}) to support this or not.
  +     */
  +    String URI_ENCODING = "org.apache.excalibur.source.Source.uri.encoding";
  +	
       /**
        * Get a {@link Source} object.
        * @param location - the URI to resolve. If this is relative it is either
  
  
  
  1.2       +61 -1     avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/SourceUtil.java
  
  Index: SourceUtil.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/SourceUtil.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- SourceUtil.java	9 Nov 2003 12:46:57 -0000	1.1
  +++ SourceUtil.java	14 Feb 2004 02:56:54 -0000	1.2
  @@ -308,6 +308,66 @@
       }
   
       /**
  +     * Translates a string into <code>x-www-form-urlencoded</code> format
  +     * with specified encoding
  +     *
  +     * @param   s   <code>String</code> to be translated.
  +     * @param	enc The name of a supported charset
  +     * @return  the translated <code>String</code>.
  +     * @throws UnsupportedEncodingException
  +     */
  +    public static String encode( String s, String enc ) throws UnsupportedEncodingException
  +    {
  +        // Why not use the java.net.URLEncoder for this purpose?
  +        final StringBuffer out = new StringBuffer( s.length() );
  +        final ByteArrayOutputStream buf = new ByteArrayOutputStream( 32 );
  +        final OutputStreamWriter writer = new OutputStreamWriter( buf, enc );
  +        for( int i = 0; i < s.length(); i++ )
  +        {
  +            int c = s.charAt( i );
  +            if( charactersDontNeedingEncoding.get( c ) )
  +            {
  +                out.append( (char)c );
  +            }
  +            else
  +            {
  +                try
  +                {
  +                    writer.write( c );
  +                    writer.flush();
  +                }
  +                catch( IOException e )
  +                {
  +                    buf.reset();
  +                    continue;
  +                }
  +                byte[] ba = buf.toByteArray();
  +                for( int j = 0; j < ba.length; j++ )
  +                {
  +                    out.append( '%' );
  +                    char ch = Character.forDigit( ( ba[ j ] >> 4 ) & 0xF, 16 );
  +                    // converting to use uppercase letter as part of
  +                    // the hex value if ch is a letter.
  +                    if( Character.isLetter( ch ) )
  +                    {
  +                        ch -= characterCaseDiff;
  +                    }
  +                    out.append( ch );
  +                    ch = Character.forDigit( ba[ j ] & 0xF, 16 );
  +                    if( Character.isLetter( ch ) )
  +                    {
  +                        ch -= characterCaseDiff;
  +                    }
  +                    out.append( ch );
  +                }
  +                buf.reset();
  +            }
  +        }
  +
  +        return out.toString();
  +    }
  +
  +    /**
        * Return a <code>File</code> object associated with the <code>Source</code> object.
        *
        * @return The corresponding <code>File</code> object or null if the
  
  
  
  1.2       +12 -3     avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSource.java
  
  Index: URLSource.java
  ===================================================================
  RCS file: /home/cvs/avalon-excalibur/sourceresolve/src/java/org/apache/excalibur/source/impl/URLSource.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- URLSource.java	9 Nov 2003 12:46:57 -0000	1.1
  +++ URLSource.java	14 Feb 2004 02:56:54 -0000	1.2
  @@ -90,6 +90,9 @@
       /** The <code>SourceParameters</code> used for a post*/
       protected SourceParameters m_parameters;
   
  +    /** The encoding of the <code>SourceParameters</code>*/
  +    protected String m_encoding;
  +
       /** Is this a post? */
       protected boolean m_isPost = false;
   
  @@ -123,6 +126,8 @@
   
           m_url = url;
           m_isPost = false;
  +        // get the default system encoding in case no encoding is specified
  +        m_encoding = System.getProperties().getProperty("file.property", "ISO-8859-1");
   
           if (null != parameters)
           {
  @@ -131,6 +136,10 @@
               
               if ("POST".equalsIgnoreCase(method))
                   m_isPost = true;
  +
  +            final String encoding = (String) parameters.get(SourceResolver.URI_ENCODING);
  +            if (encoding != null && !"".equals(encoding))
  +                m_encoding = encoding;
           }
           
           if (null != m_parameters && m_parameters.hasParameters() && !m_isPost)
  @@ -149,7 +158,7 @@
                   values = m_parameters.getParameterValues(key);
                   while (values.hasNext() == true)
                   {
  -                    value = SourceUtil.encode((String) values.next());
  +                    value = SourceUtil.encode((String) values.next(), m_encoding);
                       if (first == false)
                           urlBuffer.append('&');
                       first = false;
  @@ -248,7 +257,7 @@
                       values = m_parameters.getParameterValues(key);
                       while (values.hasNext() == true)
                       {
  -                        value = SourceUtil.encode((String) values.next());
  +                        value = SourceUtil.encode((String) values.next(), m_encoding);
                           if (first == false)
                               buffer.append('&');
                           first = false;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: cvs-unsubscribe@avalon.apache.org
For additional commands, e-mail: cvs-help@avalon.apache.org