You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by je...@apache.org on 2007/06/20 16:27:04 UTC

svn commit: r549118 - in /xmlgraphics/commons/trunk: src/java/org/apache/xmlgraphics/ps/PSGenerator.java status.xml test/java/org/apache/xmlgraphics/StandardTestSuite.java test/java/org/apache/xmlgraphics/ps/PSEscapeTestCase.java

Author: jeremias
Date: Wed Jun 20 07:27:03 2007
New Revision: 549118

URL: http://svn.apache.org/viewvc?view=rev&rev=549118
Log:
Fixed literal text string escaping in PSGenerator. Some characters were not properly escaped.
Reported by: Manuel Polling

Added:
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/PSEscapeTestCase.java   (with props)
Modified:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java
    xmlgraphics/commons/trunk/status.xml
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java?view=diff&rev=549118&r1=549117&r2=549118
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSGenerator.java Wed Jun 20 07:27:03 2007
@@ -202,7 +202,6 @@
         out.flush();
     }
 
-
     /**
      * Escapes a character conforming to the rules established in the PostScript
      * Language Reference (Search for "Literal Text Strings").
@@ -210,38 +209,45 @@
      * @param target target StringBuffer to write the escaped character to
      */
     public static final void escapeChar(char c, StringBuffer target) {
-        if (c > 127) {
-            target.append("\\");
-            target.append(Integer.toOctalString(c));
-        } else {
-            switch (c) {
-                case '\n':
-                    target.append("\\n");
-                    break;
-                case '\r':
-                    target.append("\\r");
-                    break;
-                case '\t':
-                    target.append("\\t");
-                    break;
-                case '\b':
-                    target.append("\\b");
-                    break;
-                case '\f':
-                    target.append("\\f");
-                    break;
-                case '\\':
-                    target.append("\\\\");
-                    break;
-                case '(':
-                    target.append("\\(");
-                    break;
-                case ')':
-                    target.append("\\)");
-                    break;
-                default:
+        switch (c) {
+            case '\n':
+                target.append("\\n");
+                break;
+            case '\r':
+                target.append("\\r");
+                break;
+            case '\t':
+                target.append("\\t");
+                break;
+            case '\b':
+                target.append("\\b");
+                break;
+            case '\f':
+                target.append("\\f");
+                break;
+            case '\\':
+                target.append("\\\\");
+                break;
+            case '(':
+                target.append("\\(");
+                break;
+            case ')':
+                target.append("\\)");
+                break;
+            default:
+                if (c > 255) {
+                    //Ignoring non Latin-1 characters
+                    target.append('?');
+                } else if (c < 32 || c > 127) {
+                    target.append('\\');
+                   
+                    target.append((char)('0' + (c >> 6)));
+                    target.append((char)('0' + ((c >> 3) % 8)));
+                    target.append((char)('0' + (c % 8)));
+                    //Integer.toOctalString(i)
+                } else {
                     target.append(c);
-            }
+                }
         }
     }
 
@@ -278,10 +284,7 @@
             int initialSize = text.length();
             initialSize += initialSize / 2;
             StringBuffer sb = new StringBuffer(initialSize);
-            if ((Long.getLong(text) != null) 
-                    || (text.indexOf(' ') >= 0) 
-                    || forceParentheses) {
-                        
+            if ((text.indexOf(' ') >= 0) || forceParentheses) {
                 sb.append('(');
                 for (int i = 0; i < text.length(); i++) {
                     final char c = text.charAt(i);
@@ -290,7 +293,11 @@
                 sb.append(')');
                 return sb.toString();
             } else {
-                return text;
+                for (int i = 0; i < text.length(); i++) {
+                    final char c = text.charAt(i);
+                    escapeChar(c, sb);
+                }
+                return sb.toString();
             }
         }
     }

Modified: xmlgraphics/commons/trunk/status.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/status.xml?view=diff&rev=549118&r1=549117&r2=549118
==============================================================================
--- xmlgraphics/commons/trunk/status.xml (original)
+++ xmlgraphics/commons/trunk/status.xml Wed Jun 20 07:27:03 2007
@@ -28,6 +28,10 @@
 
   <changes>
     <release version="Trunk" date="n/a">
+      <action context="Code" dev="JM" type="fix">
+        Fixed literal text string escaping in PSGenerator. Some characters were not properly
+        escaped.
+      </action>
       <action context="Code" dev="JM" type="add" fixes-bug="42507" due-to="Adrian Cumiskey">
         Added support for CMYK and GRAY color spaces for PSGenerator
         (moved there from PSGraphics2D).

Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java?view=diff&rev=549118&r1=549117&r2=549118
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java Wed Jun 20 07:27:03 2007
@@ -20,6 +20,7 @@
 package org.apache.xmlgraphics;
 
 import org.apache.xmlgraphics.image.codec.png.PNGEncoderTest;
+import org.apache.xmlgraphics.ps.PSEscapeTestCase;
 import org.apache.xmlgraphics.ps.dsc.events.DSCValueParserTestCase;
 import org.apache.xmlgraphics.ps.dsc.tools.DSCToolsTestCase;
 import org.apache.xmlgraphics.util.ServiceTest;
@@ -48,6 +49,7 @@
         suite.addTest(new TestSuite(ASCII85OutputStreamTestCase.class));
         suite.addTest(new TestSuite(PNGEncoderTest.class));
         suite.addTest(new TestSuite(ServiceTest.class));
+        suite.addTest(new TestSuite(PSEscapeTestCase.class));
         suite.addTest(new TestSuite(DSCValueParserTestCase.class));
         suite.addTest(new TestSuite(DSCToolsTestCase.class));
         //$JUnit-END$

Added: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/PSEscapeTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/PSEscapeTestCase.java?view=auto&rev=549118
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/PSEscapeTestCase.java (added)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/PSEscapeTestCase.java Wed Jun 20 07:27:03 2007
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/* $Id$ */
+ 
+package org.apache.xmlgraphics.ps;
+
+import org.apache.xmlgraphics.ps.PSGenerator;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests literal text string escaping.
+ */
+public class PSEscapeTestCase extends TestCase {
+
+    public void testBasics() throws Exception {
+        StringBuffer sb = new StringBuffer();
+        
+        PSGenerator.escapeChar('a', sb);
+        PSGenerator.escapeChar('b', sb);
+        PSGenerator.escapeChar('c', sb);
+        PSGenerator.escapeChar('!', sb);
+        assertEquals("abc!", sb.toString());
+        
+        sb.setLength(0);
+        PSGenerator.escapeChar('0', sb);
+        PSGenerator.escapeChar('\t', sb);
+        PSGenerator.escapeChar('(', sb);
+        PSGenerator.escapeChar('x', sb);
+        PSGenerator.escapeChar(')', sb);
+        PSGenerator.escapeChar('\n', sb);
+        PSGenerator.escapeChar('\u001E', sb); //<RS>
+        PSGenerator.escapeChar('ä', sb);
+        PSGenerator.escapeChar('\u20AC', sb); //EURO Sign
+        assertEquals("0\\t\\(x\\)\\n\\036\\344?", sb.toString());
+    }
+    
+    public void testStringToDSC() throws Exception {
+        String escaped;
+        escaped = PSGenerator.convertStringToDSC("0\t(x)\n\u001Eä\u20AC");
+        assertEquals("0\\t\\(x\\)\\n\\036\\344?", escaped);
+        escaped = PSGenerator.convertStringToDSC("0\t(x)\n\u001Eä \u20AC");
+        assertEquals("(0\\t\\(x\\)\\n\\036\\344 ?)", escaped);
+        escaped = PSGenerator.convertStringToDSC("0\t(x)\n\u001Eä\u20AC", true);
+        assertEquals("(0\\t\\(x\\)\\n\\036\\344?)", escaped);
+    }
+    
+}

Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/PSEscapeTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org