xmlgraphics-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jerem...@apache.org
Subject svn commit: r599379 - in /xmlgraphics/commons/trunk: src/java/org/apache/xmlgraphics/ps/dsc/DSCParser.java src/java/org/apache/xmlgraphics/ps/dsc/DefaultNestedDocumentHandler.java status.xml
Date Thu, 29 Nov 2007 11:06:47 GMT
Author: jeremias
Date: Thu Nov 29 03:06:46 2007
New Revision: 599379

URL: http://svn.apache.org/viewvc?rev=599379&view=rev
Log:
PostScript: Fixed %%EOF checking for nested documents.
PostScript: Improved robustness of DSC parsing (multi-line comments with no value on the first
line don't cause crashes anymore, fallback for unparsable DSC comments).

Modified:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCParser.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DefaultNestedDocumentHandler.java
    xmlgraphics/commons/trunk/status.xml

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCParser.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCParser.java?rev=599379&r1=599378&r2=599379&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCParser.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCParser.java Thu Nov
29 03:06:46 2007
@@ -46,6 +46,7 @@
     private InputStream in;
     private BufferedReader reader;
     private boolean eofFound = false;
+    private boolean checkEOF = true;
     private DSCEvent currentEvent;
     private DSCEvent nextEvent;
     private DSCFilter filter;
@@ -122,16 +123,19 @@
     
     private DSCComment parseDSCLine(String line) throws IOException, DSCException {
         int colon = line.indexOf(':');
-        String name, value;
+        String name;
+        String value = "";
         if (colon > 0) {
             name = line.substring(2, colon);
             int startOfValue = colon + 1;
-            if (isWhitespace(line.charAt(startOfValue))) {
-                startOfValue++;
-            }
-            value = line.substring(startOfValue).trim();
-            if (value.equals(DSCConstants.ATEND.toString())) {
-                return new DSCAtend(name);
+            if (startOfValue < line.length()) {
+                if (isWhitespace(line.charAt(startOfValue))) {
+                    startOfValue++;
+                }
+                value = line.substring(startOfValue).trim();
+                if (value.equals(DSCConstants.ATEND.toString())) {
+                    return new DSCAtend(name);
+                }
             }
             String nextLine;
             while (true) {
@@ -155,13 +159,16 @@
     private DSCComment parseDSCComment(String name, String value) {
         DSCComment parsed = DSCCommentFactory.createDSCCommentFor(name);
         if (parsed != null) {
-            parsed.parseValue(value);
-            return parsed;
-        } else {
-            UnparsedDSCComment unparsed = new UnparsedDSCComment(name);
-            unparsed.parseValue(value);
-            return unparsed;
+            try {
+                parsed.parseValue(value);
+                return parsed;
+            } catch (Exception e) {
+                //ignore and fall back to unparsed DSC comment
+            }
         }
+        UnparsedDSCComment unparsed = new UnparsedDSCComment(name);
+        unparsed.parseValue(value);
+        return unparsed;
     }
 
     /**
@@ -190,7 +197,9 @@
                 handler.line(getLine());
                 break;
             case EOF:
-                this.eofFound = true;
+                if (isCheckEOF()) {
+                    this.eofFound = true;
+                }
                 handler.endDocument();
                 break;
             default:
@@ -267,7 +276,7 @@
             }
             if (line.startsWith("%%")) {
                 DSCComment comment = parseDSCLine(line);
-                if (comment.getEventType() == EOF) {
+                if (comment.getEventType() == EOF && isCheckEOF()) {
                     this.eofFound = true;
                 }
                 this.nextEvent = comment;
@@ -380,6 +389,23 @@
      */
     public void setNestedDocumentHandler(NestedDocumentHandler handler) {
         this.nestedDocumentHandler = handler;
+    }
+
+    /**
+     * Tells the parser whether to check for content after the EOF comment.
+     * This can be disabled to skip nested documents.
+     * @param value true if the check is enabled
+     */
+    public void setCheckEOF(boolean value) {
+        this.checkEOF = value;
+    }
+    
+    /**
+     * Indicates whether the parser is configured to check for content after the EOF comment.
+     * @return true if the check is enabled.
+     */
+    public boolean isCheckEOF() {
+        return this.checkEOF;
     }
 
 }

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DefaultNestedDocumentHandler.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DefaultNestedDocumentHandler.java?rev=599379&r1=599378&r2=599379&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DefaultNestedDocumentHandler.java
(original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DefaultNestedDocumentHandler.java
Thu Nov 29 03:06:46 2007
@@ -50,21 +50,25 @@
             DSCComment comment = event.asDSCComment();
             if (DSCConstants.BEGIN_DOCUMENT.equals(comment.getName())) {
                 comment.generate(gen);
+                parser.setCheckEOF(false);
                 comment = parser.nextDSCComment(DSCConstants.END_DOCUMENT, gen);
                 if (comment == null) {
                     throw new DSCException("File is not DSC-compliant: Didn't find an " 
                             + DSCConstants.END_DOCUMENT);
                 }
                 comment.generate(gen);
+                parser.setCheckEOF(true);
                 parser.next();
             } else if (DSCConstants.BEGIN_DATA.equals(comment.getName())) {
                 comment.generate(gen);
+                parser.setCheckEOF(false);
                 comment = parser.nextDSCComment(DSCConstants.END_DATA, gen);
                 if (comment == null) {
                     throw new DSCException("File is not DSC-compliant: Didn't find an " 
                             + DSCConstants.END_DATA);
                 }
                 comment.generate(gen);
+                parser.setCheckEOF(true);
                 parser.next();
             } 
         }

Modified: xmlgraphics/commons/trunk/status.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/status.xml?rev=599379&r1=599378&r2=599379&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/status.xml (original)
+++ xmlgraphics/commons/trunk/status.xml Thu Nov 29 03:06:46 2007
@@ -25,6 +25,13 @@
 	</todo>
 	<changes>
 		<release version="Trunk" date="n/a">
+		  <action context="Code" dev="JM" type="fix">
+		    PostScript: Fixed %%EOF checking for nested documents.
+		  </action>
+		  <action context="Code" dev="JM" type="fix">
+		    PostScript: Improved robustness of DSC parsing (multi-line comments with no value
+		    on the first line don't cause crashes anymore, fallback for unparsable DSC comments).
+		  </action>
 		  <action context="Code" dev="JM" type="add">
 		    PostScript: Added classes for "BoundingBox" DSC comments.
 		  </action>



---------------------------------------------------------------------
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


Mime
View raw message