You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by de...@apache.org on 2002/07/11 18:26:20 UTC

cvs commit: xml-batik/test-sources/org/apache/batik/util ParsedURLDataTest.java ParsedURLTest.java

deweese     2002/07/11 09:26:20

  Modified:    samples  mapSpain.svg
               sources/org/apache/batik/util ParsedURL.java
                        ParsedURLData.java
                        ParsedURLDataProtocolHandler.java
               test-resources/org/apache/batik/util unitTesting.xml
               test-sources/org/apache/batik/util ParsedURLTest.java
  Added:       test-sources/org/apache/batik/util ParsedURLDataTest.java
  Log:
  1) Can now request contentType, contentEncoding prior to 'opening the stream'.
     There are some issues with accept headers here still...
  2) Data urls are now fully conformant with the RFC rather than only
     supporting base64 encoded data.  They also now expose the contentType
     and content encoding portions of the data URL.
  3) Added unit tests for the new functionality.
  PR: 10580
  
  Revision  Changes    Path
  1.4       +18 -16    xml-batik/samples/mapSpain.svg
  
  Index: mapSpain.svg
  ===================================================================
  RCS file: /home/cvs/xml-batik/samples/mapSpain.svg,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- mapSpain.svg	14 Mar 2002 11:20:00 -0000	1.3
  +++ mapSpain.svg	11 Jul 2002 16:26:20 -0000	1.4
  @@ -1,24 +1,26 @@
   <?xml version="1.0" encoding="iso-8859-1"?>
  -<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" >
  +<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" 
  +          "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd" >
   
  -<!-- ========================================================================= -->
  -<!-- Copyright (C) The Apache Software Foundation. All rights reserved.        -->
  -<!--                                                                           -->
  -<!-- This software is published under the terms of the Apache Software License -->
  -<!-- version 1.1, a copy of which has been included with this distribution in  -->
  -<!-- the LICENSE file.                                                         -->
  -<!-- ========================================================================= -->
  +<!-- ====================================================================== -->
  +<!-- Copyright (C) The Apache Software Foundation. All rights reserved.     -->
  +<!--                                                                        -->
  +<!-- This software is published under the terms of the Apache Software      -->
  +<!-- License version 1.1, a copy of which has been included with this       -->
  +<!-- distribution in  the LICENSE file.                                     -->
  +<!-- ====================================================================== -->
   
  -<!-- ========================================================================= -->
  -<!-- Map example                                                               -->
  -<!--                                                                           -->
  -<!-- @author Andreas Neuman                                                    -->
  -<!-- @version $Id$            -->
  -<!-- Translated from German to English by Bert Bos, 21 Nov 2001                -->
  -<!-- ========================================================================= -->
  +<!-- ====================================================================== -->
  +<!-- Map example                                                            -->
  +<!--                                                                        -->
  +<!-- @author Andreas Neuman                                                 -->
  +<!-- @version $Id$     -->
  +<!-- Translated from German to English by Bert Bos, 21 Nov 2001             -->
  +<!-- ====================================================================== -->
   
   <!-- Map created by Andreas Neumann, 1998 -->
  -<svg viewBox="0 0 600 420" width="600" height="420" xmlns="http://www.w3.org/2000/svg"
  +<svg viewBox="0 0 600 420" width="600" height="420" 
  +     xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink">
       <svg xml:space="preserve" x="25" y="25" width="550" height="382.36"
            style="shape-rendering:geometricPrecision; text-rendering:auto; image-rendering:optimizeSpeed"
  
  
  
  1.13      +3 -3      xml-batik/sources/org/apache/batik/util/ParsedURL.java
  
  Index: ParsedURL.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/util/ParsedURL.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- ParsedURL.java	5 Jun 2002 21:14:48 -0000	1.12
  +++ ParsedURL.java	11 Jul 2002 16:26:20 -0000	1.13
  @@ -330,7 +330,7 @@
        * for some protocols.
        */
       public String getContentType() {
  -        return data.getContentType();
  +        return data.getContentType(userAgent);
       }
   
       /**
  @@ -338,7 +338,7 @@
        * for some protocols.
        */
       public String getContentEncoding() {
  -        return data.getContentEncoding();
  +        return data.getContentEncoding(userAgent);
       }
   
       /**
  
  
  
  1.7       +36 -5     xml-batik/sources/org/apache/batik/util/ParsedURLData.java
  
  Index: ParsedURLData.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/util/ParsedURLData.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ParsedURLData.java	5 Jun 2002 21:14:48 -0000	1.6
  +++ ParsedURLData.java	11 Jul 2002 16:26:20 -0000	1.7
  @@ -86,6 +86,10 @@
       public String ref             = null;
       public String contentType     = null;
       public String contentEncoding = null;
  +
  +    public InputStream stream     = null;
  +    public boolean     hasBeenOpened  = false;
  +
       /**
        * Void constructor
        */
  @@ -216,7 +220,16 @@
        * Returns the content type if available.  This is only available
        * for some protocols.
        */
  -    public String getContentType() {
  +    public String getContentType(String userAgent) {
  +        if (contentType != null)
  +            return contentType;
  +
  +        if (!hasBeenOpened) {
  +            try {
  +                openStreamInternal(userAgent, null,  null);
  +            } catch (IOException ioe) { /* nothing */ }
  +        }
  +
           return contentType;
       }
   
  @@ -224,7 +237,16 @@
        * Returns the content encoding if available.  This is only available
        * for some protocols.
        */
  -    public String getContentEncoding() {
  +    public String getContentEncoding(String userAgent) {
  +        if (contentEncoding != null)
  +            return contentEncoding;
  +
  +        if (!hasBeenOpened) {
  +            try {
  +                openStreamInternal(userAgent, null,  null);
  +            } catch (IOException ioe) { /* nothing */ }
  +        }
  +
           return contentEncoding;
       }
   
  @@ -258,6 +280,7 @@
                                                acceptedEncodings.iterator());
           if (raw == null)
               return null;
  +        stream = null;
                   
           return checkGZIP(raw);
       }
  @@ -273,13 +296,21 @@
        */
       public InputStream openStreamRaw(String userAgent, Iterator mimeTypes) 
           throws IOException {
  -        return openStreamInternal(userAgent, mimeTypes, null);
  +        
  +        InputStream ret = openStreamInternal(userAgent, mimeTypes, null);
  +        stream = null;
  +        return ret;
       }
   
       protected InputStream openStreamInternal(String userAgent,
                                                Iterator mimeTypes,
                                                Iterator encodingTypes) 
           throws IOException {
  +        if (stream != null)
  +            return stream;
  +        
  +        hasBeenOpened = true;
  +
           URL url = null;
           try {
               url = buildURL();
  @@ -321,7 +352,7 @@
               contentEncoding = urlC.getContentEncoding();
           }
   
  -        return urlC.getInputStream();
  +        return (stream = urlC.getInputStream());
       }
   
       /**
  
  
  
  1.4       +101 -8    xml-batik/sources/org/apache/batik/util/ParsedURLDataProtocolHandler.java
  
  Index: ParsedURLDataProtocolHandler.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/util/ParsedURLDataProtocolHandler.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ParsedURLDataProtocolHandler.java	27 Sep 2001 20:09:11 -0000	1.3
  +++ ParsedURLDataProtocolHandler.java	11 Jul 2002 16:26:20 -0000	1.4
  @@ -12,9 +12,12 @@
   import java.io.InputStream;
   import java.io.IOException;
   import java.util.Iterator;
  +import java.net.URLDecoder;
   
   /**
    * Protocol Handler for the 'data' protocol.
  + * RFC: 2397
  + * http://www.ietf.org/rfc/rfc2397.txt
    *
    * @author <a href="mailto:deweese@apache.org">Thomas DeWeese</a>
    * @version $Id$ 
  @@ -22,8 +25,12 @@
   public class ParsedURLDataProtocolHandler 
       extends AbstractParsedURLProtocolHandler {
   
  +    static final String DATA_PROTOCOL = "data";
  +    static final String BASE64 = "base64";
  +    static final String CHARSET = "charset";
  +
       public ParsedURLDataProtocolHandler() {
  -        super("data");
  +        super(DATA_PROTOCOL);
       }
   
       public ParsedURLData parseURL(ParsedURL baseURL, String urlStr) {
  @@ -32,7 +39,7 @@
       }
   
       public ParsedURLData parseURL(String urlStr) {
  -        ParsedURLData ret = new DataParsedURLData();
  +        DataParsedURLData ret = new DataParsedURLData();
   
           int pidx=0, idx;
           int len = urlStr.length();
  @@ -52,10 +59,42 @@
           }
   
           idx = urlStr.indexOf(',',pidx);
  -        if (idx != -1) {
  +        if ((idx != -1) && (idx != pidx)) {
               ret.host = urlStr.substring(pidx, idx);
               pidx = idx+1;
  +
  +            int aidx = ret.host.lastIndexOf(';');
  +            if ((aidx == -1) || (aidx==ret.host.length())) {
  +                ret.contentType = ret.host;
  +            } else {
  +                String enc = ret.host.substring(aidx+1);
  +                idx = enc.indexOf('=');
  +                if (idx == -1) {
  +                    // It is an encoding.
  +                    ret.contentEncoding = enc;
  +                    ret.contentType = ret.host.substring(0, aidx);
  +                } else {
  +                    ret.contentType = ret.host;
  +                }
  +                // if theres a charset pull it out.
  +                aidx = 0;
  +                idx = ret.contentType.indexOf(';', aidx);
  +                if (idx != -1) {
  +                    aidx = idx+1;
  +                    while (aidx < ret.contentType.length()) {
  +                        idx = ret.contentType.indexOf(';', aidx);
  +                        if (idx == -1) idx = ret.contentType.length();
  +                        String param = ret.contentType.substring(aidx, idx);
  +                        int eqIdx = param.indexOf('=');
  +                        if ((eqIdx != -1) &&
  +                            (CHARSET.equals(param.substring(0,eqIdx)))) 
  +                            ret.charset = param.substring(eqIdx+1);
  +                        aidx = idx+1;
  +                    }
  +                }
  +            }
           }
  +        
           if (pidx != urlStr.length()) 
               ret.path = urlStr.substring(pidx);
   
  @@ -66,6 +105,7 @@
        * Overrides some of the methods to support data protocol weirdness
        */
       static class DataParsedURLData extends ParsedURLData {
  +        String charset= null;
   
           public boolean complete() {
               return (path != null);
  @@ -84,14 +124,67 @@
               return ret;
           }
   
  +        /**
  +         * Returns the content type if available.  This is only available
  +         * for some protocols.
  +         */
  +        public String getContentType(String userAgent) {
  +            return contentType;
  +        }
  +
  +        /**
  +         * Returns the content encoding if available.  This is only available
  +         * for some protocols.
  +         */
  +        public String getContentEncoding(String userAgent) {
  +            return contentEncoding;
  +        }
  +
           protected InputStream openStreamInternal
               (String userAgent, Iterator mimeTypes, Iterator encodingTypes)
               throws IOException {
  -            byte [] data = path.getBytes();
  -            InputStream is = new ByteArrayInputStream(data);
  -            return new Base64DecodeStream(is);
  +            if (BASE64.equals(contentEncoding)) {
  +                byte [] data = path.getBytes();
  +                stream = new ByteArrayInputStream(data);
  +                stream = new Base64DecodeStream(stream);
  +            } else {
  +                stream = decode(path);
  +            }
  +            return stream;
           }
  -    }
   
  +        public static InputStream decode(String s) {
  +            int len = s.length();
  +            byte [] data = new byte[len];
  +            int j=0;
  +            for(int i=0; i<len; i++) {
  +                char c = s.charAt(i);
  +                switch (c) {
  +                default : data[j++]= (byte)c;   break;
  +                case '%': {
  +                    if (i+2 < len) {
  +                        i += 2;
  +                        byte b; 
  +                        char c1 = s.charAt(i-1);
  +                        if      (c1 >= '0' && c1 <= '9') b=(byte)(c1-'0');
  +                        else if (c1 >= 'a' && c1 <= 'z') b=(byte)(c1-'a'+10);
  +                        else if (c1 >= 'A' && c1 <= 'Z') b=(byte)(c1-'A'+10);
  +                        else break;
  +                        b*=16;
  +
  +                        char c2 = s.charAt(i);
  +                        if      (c2 >= '0' && c2 <= '9') b+=(byte)(c2-'0');
  +                        else if (c2 >= 'a' && c2 <= 'z') b+=(byte)(c2-'a'+10);
  +                        else if (c2 >= 'A' && c2 <= 'Z') b+=(byte)(c2-'A'+10);
  +                        else break;
  +                        data[j++] = b;
  +                    }
  +                }
  +                break;
  +                }
  +            }
  +            return new ByteArrayInputStream(data, 0, j);
  +        }
  +    }
   }
   
  
  
  
  1.5       +49 -1     xml-batik/test-resources/org/apache/batik/util/unitTesting.xml
  
  Index: unitTesting.xml
  ===================================================================
  RCS file: /home/cvs/xml-batik/test-resources/org/apache/batik/util/unitTesting.xml,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- unitTesting.xml	3 May 2002 09:34:20 -0000	1.4
  +++ unitTesting.xml	11 Jul 2002 16:26:20 -0000	1.5
  @@ -305,4 +305,52 @@
                value="jar:file:dir/file.jar!/b/a/t/i/k/new.svg#bar" />
       </test>
   
  +    <test id="ParsedURLData.1" class="org.apache.batik.util.ParsedURLDataTest">
  +        <!-- Test basic image base 64 data -->
  +        <arg class="java.lang.String" 
  +             value="data:image/png;base64,R0lGODdhMAAw" />
  +        <arg class="java.lang.String" 
  +             value="CT: image/png CE: base64 DATA: 47 49 46 38 37 URL: data:image/png;base64,R0lGODdhMAAw" />
  +    </test>
  +
  +    <test id="ParsedURLData.2" class="org.apache.batik.util.ParsedURLDataTest">
  +        <!-- Test text encoded as base 64 data -->
  +        <arg class="java.lang.String" 
  +             value="data:text/plain;charset=US-ASCII;base64,R0lGODdhMAAw" />
  +        <arg class="java.lang.String" 
  +             value="CT: text/plain;charset=US-ASCII CE: base64 DATA: 47 49 46 38 37 URL: data:text/plain;charset=US-ASCII;base64,R0lGODdhMAAw" />
  +    </test>
  +
  +    <test id="ParsedURLData.3" class="org.apache.batik.util.ParsedURLDataTest">
  +        <!-- Test basic image base 64 data -->
  +        <arg class="java.lang.String" 
  +             value="data:image/gif;base64,R0lGODdhMAAw" />
  +        <arg class="java.lang.String" 
  +             value="CT: image/gif CE: base64 DATA: 47 49 46 38 37 URL: data:image/gif;base64,R0lGODdhMAAw" />
  +    </test>
  +
  +    <test id="ParsedURLData.4" class="org.apache.batik.util.ParsedURLDataTest">
  +        <!-- Test text using octect encoding. -->
  +        <arg class="java.lang.String" 
  +             value="data:text/plain;charset=iso-8859-7,%be%fb%be" />
  +        <arg class="java.lang.String" 
  +             value="CT: text/plain;charset=iso-8859-7 CE: null DATA: be fb be URL: data:text/plain;charset=iso-8859-7,%be%fb%be" />
  +    </test>
  +
  +    <test id="ParsedURLData.5" class="org.apache.batik.util.ParsedURLDataTest">
  +        <!-- Test Minimalest data url -->
  +        <arg class="java.lang.String" 
  +             value="data:,A%20brief%20note" />
  +        <arg class="java.lang.String" 
  +             value="CT: null CE: null DATA: 2c 41 20 62 72 URL: data:,,A%20brief%20note" />
  +    </test>
  +
  +    <test id="ParsedURLData.6" class="org.apache.batik.util.ParsedURLDataTest">
  +        <!-- Test tricky data url -->
  +        <arg class="java.lang.String" 
  +             value="data:;=;,A%20brief%20note" />
  +        <arg class="java.lang.String" 
  +             value="CT: ;= CE:  DATA: 41 20 62 72 69 URL: data:;=;,A%20brief%20note" />
  +    </test>
  +
   </testSuite>
  
  
  
  1.2       +3 -3      xml-batik/test-sources/org/apache/batik/util/ParsedURLTest.java
  
  Index: ParsedURLTest.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/test-sources/org/apache/batik/util/ParsedURLTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ParsedURLTest.java	5 Jul 2001 16:54:36 -0000	1.1
  +++ ParsedURLTest.java	11 Jul 2002 16:26:20 -0000	1.2
  @@ -98,8 +98,8 @@
                       (Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
                        Messages.formatMessage
                        (ERROR_CANNOT_PARSE_URL,
  -                      new String[]{sub == null? "null" : base, 
  -                                   sub,
  +                      new String[]{base, 
  +                                   (sub == null) ? "null" : sub,
                                      trace.toString()}))
                       });
               report.setPassed(false);
  
  
  
  1.1                  xml-batik/test-sources/org/apache/batik/util/ParsedURLDataTest.java
  
  Index: ParsedURLDataTest.java
  ===================================================================
  /*****************************************************************************
   * Copyright (C) The Apache Software Foundation. All rights reserved.        *
   * ------------------------------------------------------------------------- *
   * This software is published under the terms of the Apache Software License *
   * version 1.1, a copy of which has been included with this distribution in  *
   * the LICENSE file.                                                         *
   *****************************************************************************/
  
  package org.apache.batik.util;
  
  import org.apache.batik.test.AbstractTest;
  import org.apache.batik.test.DefaultTestReport;
  import org.apache.batik.test.TestReport;
  
  import java.io.StringWriter;
  import java.io.PrintWriter;
  import java.io.InputStream;
  import java.io.IOException;
  /**
   * This test validates that the ParsedURL class properly parses and
   * cascades URLs.
   *
   * @author <a href="mailto:deweese@apache.org">Thomas DeWeese</a>
   * @version $Id: ParsedURLDataTest.java,v 1.1 2002/07/11 16:26:20 deweese Exp $
   */
  public class ParsedURLDataTest extends AbstractTest {
      /**
       * Error when unable to parse URL
       * {0} = 'Base URL' or NULL
       * {1} = Sub URL
       * {2} = exception stack trace.
       */
      public static final String ERROR_CANNOT_PARSE_URL
          = "ParsedURLTest.error.cannot.parse.url";
  
      /**
       * Result didn't match expected result.
       * {0} = result
       * {1} = expected result
       */
      public static final String ERROR_WRONG_RESULT
          = "ParsedURLTest.error.wrong.result";
  
      public static final String ENTRY_KEY_ERROR_DESCRIPTION
          = "ParsedURLTest.entry.key.error.description";
  
      protected String base = null;
      protected String ref  = null;
      /**
       * Constructor
       * @param url The url to parse
       * @param ref The expected result.
       */
      public ParsedURLDataTest(String url, String ref ){
          this.base = url;
          this.ref  = ref;
      }
  
      /**
       * Returns this Test's name
       */
      public String getName() {
          return ref + " -- " + super.getName();
      }
  
      /**
       * This method will only throw exceptions if some aspect
       * of the test's internal operation fails.
       */
      public TestReport runImpl() throws Exception {
          DefaultTestReport report
              = new DefaultTestReport(this);
  
          ParsedURL purl;
          try {
              purl  = new ParsedURL(base);
          } catch(Exception e) {
              StringWriter trace = new StringWriter();
              e.printStackTrace(new PrintWriter(trace));
              report.setErrorCode(ERROR_CANNOT_PARSE_URL);
              report.setDescription(new TestReport.Entry[] {
                  new TestReport.Entry
                      (Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
                       Messages.formatMessage
                       (ERROR_CANNOT_PARSE_URL,
                        new String[]{"null", 
                                     base,
                                     trace.toString()}))
                      });
              report.setPassed(false);
              return report;
          }
  
          byte[] data = new byte[5];
          int num = 0;
          try {
              InputStream is = purl.openStream();
              num = is.read(data);
          } catch (IOException ioe) {
              ioe.printStackTrace();
          }
          StringBuffer sb = new StringBuffer();
          for (int i=0; i<num; i++) {
              int val = ((int)data[i])&0xFF;
              if (val < 16) {
                  sb.append("0");
              }
              sb.append(Integer.toHexString(val) + " ");
          }
          
          String info = ( "CT: " + purl.getContentType() + 
                         " CE: " + purl.getContentEncoding() + 
                         " DATA: " + sb +
                         "URL: " + purl);
  
          if (ref.equals(info)) {
              report.setPassed(true);
              return report;
          }
  
          report.setErrorCode(ERROR_WRONG_RESULT);
          report.setDescription(new TestReport.Entry[] {
            new TestReport.Entry
              (Messages.formatMessage(ENTRY_KEY_ERROR_DESCRIPTION, null),
               Messages.formatMessage
               (ERROR_WRONG_RESULT, new String[]{info, ref }))
              });
          report.setPassed(false);
          return report;
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org