Uploaded image for project: 'Batik'
  1. Batik
  2. BATIK-1026

ParsedURL fails using "data:" URI scheme when data contains style sheet including #-character

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Resolution: Unresolved
    • Affects Version/s: 1.8
    • Fix Version/s: None
    • Component/s: SVG Rasterizer
    • Labels:
      None
    • Environment:
      Operating System: Linux
      Platform: PC

      Description

      Transcoder allows to pass a user style sheet uri an a hint.
      Passing an data uri, such as "data:text/css;charset=UTF-8, .g

      { display:none;}

      " works as expected. But as soon, as the data (style sheet) contains a #-character, the style sheet can not be parsed correctly any more.
      The reason for this is in the ParsedULR class (or a class used by it - ParsedURLDataProtocolHandler:parseURL(String urlStr)) because the # character is interpreted as a URL fragment separator, not considering the used URI schema at all. As far as i know is the syntax for data URI's "data:<mediatype>[;base64],<data>". Because of this, the part after the first #-character is taken as URI fragment and is no longer part of the style sheet, resulting in an exception like this:

      org.w3c.dom.DOMException: data:text/css;charset=UTF-8,#RestrictedAreas

      { display:none;}

      :
      Invalid CSS document.
      java.lang.NullPointerException
      at org.apache.batik.css.engine.CSSEngine.parseStyleSheet(CSSEngine.java:1184)
      at org.apache.batik.css.engine.CSSEngine.parseStyleSheet(CSSEngine.java:1125)
      at org.apache.batik.bridge.BridgeContext.initializeDocument(BridgeContext.java:386)
      at org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:55)
      at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:208)
      at org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTranscoder.java:92)
      at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:142)
      at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:156)

      Reproduce:
      Passing an URI as "data:text/css;charset=UTF-8,#RestrictedAreas

      { display:none;}

      " as a user-stylsheet-uri to the transcoder hints.

      Workaround:
      a) Use a file instead of encoding data directly in the URL
      b) Encode style sheet data using based64

      String stylesheet = "#RestrictedAreas

      { display:none;}

      "
      Charset charset = Charset.forName("UTF-8");
      byte[] styleSheetBytes = Base64.encodeBase64(charset.encode(stylesheet).array());
      CharBuffer styleSheet = charset.decode(ByteBuffer.wrap(styleSheetBytes));
      String dataURL = "data:text/css;base64,"+styleSheet;
      hints.put(ImageTranscoder.KEY_USER_STYLESHEET_URI, dataURL);

      regards

        Attachments

          Activity

            People

            • Assignee:
              batik-dev@xmlgraphics.apache.org Batik Developer's Mailing list
              Reporter:
              gerber@slf.ch Matthias Gerber
            • Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: