Return-Path: Delivered-To: apmail-xmlgraphics-commits-archive@www.apache.org Received: (qmail 68204 invoked from network); 17 Dec 2008 15:06:13 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 17 Dec 2008 15:06:13 -0000 Received: (qmail 25318 invoked by uid 500); 17 Dec 2008 15:06:26 -0000 Mailing-List: contact commits-help@xmlgraphics.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: general@xmlgraphics.apache.org Delivered-To: mailing list commits@xmlgraphics.apache.org Received: (qmail 25309 invoked by uid 99); 17 Dec 2008 15:06:26 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Dec 2008 07:06:26 -0800 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 17 Dec 2008 15:06:08 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 4E9CF238898E; Wed, 17 Dec 2008 07:05:47 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r727407 - in /xmlgraphics/commons/trunk: src/java/org/apache/xmlgraphics/ps/ src/java/org/apache/xmlgraphics/ps/dsc/ src/java/org/apache/xmlgraphics/ps/dsc/events/ src/java/org/apache/xmlgraphics/ps/dsc/tools/ test/java/org/apache/xmlgraphi... Date: Wed, 17 Dec 2008 15:05:46 -0000 To: commits@xmlgraphics.apache.org From: jeremias@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081217150547.4E9CF238898E@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: jeremias Date: Wed Dec 17 07:05:45 2008 New Revision: 727407 URL: http://svn.apache.org/viewvc?rev=727407&view=rev Log: Made DefaultNestedDocumentHandler capable of operating without a PSGenerator to delegate to. Added some new DSC comment implementations. Generalized some features in DSCParser to use a listener pattern. Makes the whole thing more powerful. ResourceTracker can now keep track of resource usage counts. Comparable implementation for PSResource. Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCListener.java (with props) xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/FilteringEventListener.java (with props) xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourceDSCComment.java (with props) xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginDocument.java (with props) xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentIncludeResource.java (with props) xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentTitle.java (with props) xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/ListenerTestCase.java (with props) xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/test1.txt (with props) Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSResource.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCCommentFactory.java 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/src/java/org/apache/xmlgraphics/ps/dsc/NestedDocumentHandler.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/ResourceTracker.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourcesDSCComment.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginResource.java xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/PageExtractor.java xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSResource.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSResource.java?rev=727407&r1=727406&r2=727407&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSResource.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSResource.java Wed Dec 17 07:05:45 2008 @@ -22,7 +22,7 @@ /** * Represents a PostScript resource (file, font, procset etc.). */ -public class PSResource { +public class PSResource implements Comparable { /** a file resource */ public static final String TYPE_FILE = "file"; @@ -36,10 +36,10 @@ public static final String TYPE_FORM = "form"; /** a procset resource */ public static final String TYPE_ENCODING = "encoding"; - + private String type; private String name; - + /** * Main constructor * @param type type of the resource @@ -49,24 +49,24 @@ this.type = type; this.name = name; } - + /** @return the type of the resource */ public String getType() { return this.type; } - + /** @return the name of the resource */ public String getName() { return this.name; } - + /** @return the specification as defined in DSC v3.0 spec. */ public String getResourceSpecification() { StringBuffer sb = new StringBuffer(); sb.append(getType()).append(" ").append(PSGenerator.convertStringToDSC(getName())); return sb.toString(); } - + /** {@inheritDoc} */ public boolean equals(Object obj) { if (obj == this) { @@ -85,7 +85,21 @@ } /** {@inheritDoc} */ + public int compareTo(Object o) { + PSResource other = (PSResource)o; + if (this == other) { + return 0; + } + int result = this.getType().compareTo(other.getType()); + if (result == 0) { + result = this.getName().compareTo(other.getName()); + } + return result; + } + + /** {@inheritDoc} */ public String toString() { return getResourceSpecification(); } + } Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCCommentFactory.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCCommentFactory.java?rev=727407&r1=727406&r2=727407&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCCommentFactory.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCCommentFactory.java Wed Dec 17 07:05:45 2008 @@ -5,9 +5,9 @@ * 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. @@ -23,6 +23,7 @@ import org.apache.xmlgraphics.ps.DSCConstants; import org.apache.xmlgraphics.ps.dsc.events.DSCComment; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBeginDocument; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBeginResource; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentBoundingBox; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentDocumentNeededResources; @@ -30,12 +31,14 @@ import org.apache.xmlgraphics.ps.dsc.events.DSCCommentEndComments; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentEndOfFile; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentHiResBoundingBox; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentIncludeResource; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentLanguageLevel; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPage; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPageBoundingBox; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPageHiResBoundingBox; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPageResources; import org.apache.xmlgraphics.ps.dsc.events.DSCCommentPages; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentTitle; /** * Factory for DSCComment subclasses. @@ -45,35 +48,41 @@ private static final Map DSC_FACTORIES = new java.util.HashMap(); static { - DSC_FACTORIES.put(DSCConstants.END_COMMENTS, + DSC_FACTORIES.put(DSCConstants.END_COMMENTS, DSCCommentEndComments.class); - DSC_FACTORIES.put(DSCConstants.BEGIN_RESOURCE, + DSC_FACTORIES.put(DSCConstants.BEGIN_RESOURCE, DSCCommentBeginResource.class); - DSC_FACTORIES.put(DSCConstants.PAGE_RESOURCES, + DSC_FACTORIES.put(DSCConstants.INCLUDE_RESOURCE, + DSCCommentIncludeResource.class); + DSC_FACTORIES.put(DSCConstants.PAGE_RESOURCES, DSCCommentPageResources.class); - DSC_FACTORIES.put(DSCConstants.PAGE, + DSC_FACTORIES.put(DSCConstants.BEGIN_DOCUMENT, + DSCCommentBeginDocument.class); + DSC_FACTORIES.put(DSCConstants.PAGE, DSCCommentPage.class); - DSC_FACTORIES.put(DSCConstants.PAGES, + DSC_FACTORIES.put(DSCConstants.PAGES, DSCCommentPages.class); - DSC_FACTORIES.put(DSCConstants.BBOX, + DSC_FACTORIES.put(DSCConstants.BBOX, DSCCommentBoundingBox.class); - DSC_FACTORIES.put(DSCConstants.HIRES_BBOX, + DSC_FACTORIES.put(DSCConstants.HIRES_BBOX, DSCCommentHiResBoundingBox.class); - DSC_FACTORIES.put(DSCConstants.PAGE_BBOX, + DSC_FACTORIES.put(DSCConstants.PAGE_BBOX, DSCCommentPageBoundingBox.class); - DSC_FACTORIES.put(DSCConstants.PAGE_HIRES_BBOX, + DSC_FACTORIES.put(DSCConstants.PAGE_HIRES_BBOX, DSCCommentPageHiResBoundingBox.class); - DSC_FACTORIES.put(DSCConstants.LANGUAGE_LEVEL, + DSC_FACTORIES.put(DSCConstants.LANGUAGE_LEVEL, DSCCommentLanguageLevel.class); - DSC_FACTORIES.put(DSCConstants.DOCUMENT_NEEDED_RESOURCES, + DSC_FACTORIES.put(DSCConstants.DOCUMENT_NEEDED_RESOURCES, DSCCommentDocumentNeededResources.class); - DSC_FACTORIES.put(DSCConstants.DOCUMENT_SUPPLIED_RESOURCES, + DSC_FACTORIES.put(DSCConstants.DOCUMENT_SUPPLIED_RESOURCES, DSCCommentDocumentSuppliedResources.class); - DSC_FACTORIES.put(DSCConstants.EOF, + DSC_FACTORIES.put(DSCConstants.TITLE, + DSCCommentTitle.class); + DSC_FACTORIES.put(DSCConstants.EOF, DSCCommentEndOfFile.class); //TODO Add additional implementations as needed } - + /** * Creates and returns new instances for DSC comments with a given name. * @param name the name of the DSCComment (without the "%%" prefix) @@ -88,12 +97,12 @@ try { return (DSCComment)clazz.newInstance(); } catch (InstantiationException e) { - throw new RuntimeException("Error instantiating instance for '" + name + "': " + throw new RuntimeException("Error instantiating instance for '" + name + "': " + e.getMessage()); } catch (IllegalAccessException e) { - throw new RuntimeException("Illegal Access error while instantiating instance for '" + throw new RuntimeException("Illegal Access error while instantiating instance for '" + name + "': " + e.getMessage()); } } - + } Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCListener.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCListener.java?rev=727407&view=auto ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCListener.java (added) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCListener.java Wed Dec 17 07:05:45 2008 @@ -0,0 +1,44 @@ +/* + * 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.dsc; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; + +/** + * Listener interface for the DSC parser. It can be used to be notified + */ +public interface DSCListener { + + /** + * Called for each DSC event. You can call methods on the DSC parser to skip comments, + * for example. But implementations need to be good citizens and take into account that + * multiple listeners can be active at the same time and that they might interfere with + * other listeners. When returning from the call, state information such as filters should + * be restored. + * @param event the DSC event + * @param parser the DSC parser + * @throws IOException if an I/O error occurs + * @throws DSCException if a DSC-specific error occurs + */ + void processEvent(DSCEvent event, DSCParser parser) throws IOException, DSCException; + +} Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCListener.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/DSCListener.java ------------------------------------------------------------------------------ svn:keywords = Id 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=727407&r1=727406&r2=727407&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 Wed Dec 17 07:05:45 2008 @@ -5,9 +5,9 @@ * 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. @@ -16,13 +16,15 @@ */ /* $Id$ */ - + package org.apache.xmlgraphics.ps.dsc; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.util.Iterator; +import java.util.List; import java.util.NoSuchElementException; import org.apache.xmlgraphics.ps.DSCConstants; @@ -38,7 +40,7 @@ /** * Parser for DSC-compliant PostScript files (DSC = Document Structuring Conventions). The parser - * is implemented as a pull parser but has the ability to act as a push parser through the + * is implemented as a pull parser but has the ability to act as a push parser through the * DSCHandler interface. */ public class DSCParser implements DSCParserConstants { @@ -49,9 +51,11 @@ private boolean checkEOF = true; private DSCEvent currentEvent; private DSCEvent nextEvent; - private DSCFilter filter; - private NestedDocumentHandler nestedDocumentHandler; - + private DSCListener nestedDocumentHandler; + private DSCListener filterListener; + private List listeners; + private boolean listenersDisabled = false; + /** * Creates a new DSC parser. * @param in InputStream to read the PostScript file from @@ -75,7 +79,7 @@ } parseNext(); } - + /** * Returns the InputStream the PostScript code is read from. * @return the InputStream the PostScript code is read from @@ -83,7 +87,7 @@ public InputStream getInputStream() { return this.in; } - + /** * This method is used to write out warning messages for the parsing process. Subclass to * override this method. The default implementation writes to System.err. @@ -92,7 +96,7 @@ protected void warn(String msg) { System.err.println(msg); } - + /** * Reads one line from the input file * @return the line or null if there are no more lines @@ -103,7 +107,7 @@ String line; line = this.reader.readLine(); checkLine(line); - + return line; } @@ -116,11 +120,11 @@ warn("Line longer than 255 characters. This file is not fully PostScript conforming."); } } - + private final boolean isWhitespace(char c) { return c == ' ' || c == '\t'; } - + private DSCComment parseDSCLine(String line) throws IOException, DSCException { int colon = line.indexOf(':'); String name; @@ -207,7 +211,7 @@ } } } - + /** * Indicates whether there are additional items. * @return true if there are additonal items, false if the end of the file has been reached @@ -227,15 +231,31 @@ if (hasNext()) { this.currentEvent = nextEvent; parseNext(); - if (this.nestedDocumentHandler != null) { - this.nestedDocumentHandler.handle(this.currentEvent, this); - } + + processListeners(); + return this.currentEvent.getEventType(); } else { throw new NoSuchElementException("There are no more events"); } } - + + private void processListeners() throws IOException, DSCException { + if (isListenersDisabled()) { + return; + } + if (this.filterListener != null) { + //Filter always comes first + this.filterListener.processEvent(this.currentEvent, this); + } + if (this.listeners != null) { + Iterator iter = this.listeners.iterator(); + while (iter.hasNext()) { + ((DSCListener)iter.next()).processEvent(this.currentEvent, this); + } + } + } + /** * Steps to the next item returning the new event. * @return the new event @@ -246,7 +266,7 @@ next(); return getCurrentEvent(); } - + /** * Returns the current event. * @return the current event @@ -254,7 +274,7 @@ public DSCEvent getCurrentEvent() { return this.currentEvent; } - + /** * Returns the next event without moving the cursor to the next event. * @return the next event @@ -262,7 +282,7 @@ public DSCEvent peek() { return this.nextEvent; } - + /** * Parses the next event. * @throws IOException In case of an I/O error @@ -287,14 +307,11 @@ } else { this.nextEvent = new PostScriptLine(line); } - if (this.filter != null && !filter.accept(this.nextEvent)) { - parseNext(); //skip - } } else { this.nextEvent = null; } } - + /** * Returns the current PostScript line. * @return the current PostScript line @@ -315,11 +332,11 @@ * @throws IOException In case of an I/O error * @throws DSCException In case of a violation of the DSC spec */ - public DSCComment nextDSCComment(String name) + public DSCComment nextDSCComment(String name) throws IOException, DSCException { return nextDSCComment(name, null); } - + /** * Advances to the next DSC comment with the given name. * @param name the name of the DSC comment @@ -328,7 +345,7 @@ * @throws IOException In case of an I/O error * @throws DSCException In case of a violation of the DSC spec */ - public DSCComment nextDSCComment(String name, PSGenerator gen) + public DSCComment nextDSCComment(String name, PSGenerator gen) throws IOException, DSCException { while (hasNext()) { DSCEvent event = nextEvent(); @@ -379,16 +396,74 @@ * @param filter the filter to use or null to disable filtering */ public void setFilter(DSCFilter filter) { - this.filter = filter; + if (filter != null) { + this.filterListener = new FilteringEventListener(filter); + } else { + this.filterListener = null; + } + } + + /** + * Adds a DSC event listener. + * @param listener the listener + */ + public void addListener(DSCListener listener) { + if (listener == null) { + throw new NullPointerException("listener must not be null"); + } + if (this.listeners == null) { + this.listeners = new java.util.ArrayList(); + } + this.listeners.add(listener); + } + + /** + * Removes a DSC event listener. + * @param listener the listener to remove + */ + public void removeListener(DSCListener listener) { + if (this.listeners != null) { + this.listeners.remove(listener); + } + } + + /** + * Allows to disable all listeners. This can be used to disable any filtering, for example in + * nested documents. + * @param value true to disable all listeners, false to re-enable them + */ + public void setListenersDisabled(boolean value) { + this.listenersDisabled = value; + } + + /** + * Indicates whether the listeners are currently disabled. + * @return true if they are disabled + */ + public boolean isListenersDisabled() { + return this.listenersDisabled; } /** * Sets a NestedDocumentHandler which is used to skip nested documents like embedded EPS files. * You can also process those parts in a special way. + *

+ * It is suggested to use the more generally usable {@link #addListener(DSCListener)} and + * {@link #removeListener(DSCListener)} instead. NestedDocumentHandler is internally + * mapped onto a {@link DSCListener}. * @param handler the NestedDocumentHandler instance or null to disable the feature */ - public void setNestedDocumentHandler(NestedDocumentHandler handler) { - this.nestedDocumentHandler = handler; + public void setNestedDocumentHandler(final NestedDocumentHandler handler) { + if (handler == null) { + removeListener(this.nestedDocumentHandler); + } else { + addListener(new DSCListener() { + public void processEvent(DSCEvent event, DSCParser parser) throws IOException, + DSCException { + handler.handle(event, parser); + } + }); + } } /** @@ -399,7 +474,7 @@ 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. 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=727407&r1=727406&r2=727407&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 Wed Dec 17 07:05:45 2008 @@ -5,9 +5,9 @@ * 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. @@ -27,13 +27,14 @@ import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; /** - * Default implementation of the NestedDocumentHandler interface which automatically skips data + * {@link DSCListener} implementation which automatically skips data * between Begin/EndDocument and Begin/EndData. */ -public class DefaultNestedDocumentHandler implements DSCParserConstants, NestedDocumentHandler { +public class DefaultNestedDocumentHandler implements DSCParserConstants, + NestedDocumentHandler, DSCListener { private PSGenerator gen; - + /** * Creates a new instance. * @param gen PSGenerator to pass through the skipped content @@ -41,37 +42,52 @@ public DefaultNestedDocumentHandler(PSGenerator gen) { this.gen = gen; } - - /** - * @see org.apache.xmlgraphics.ps.dsc.NestedDocumentHandler#handle(org.apache.xmlgraphics.ps.dsc.events.DSCEvent, org.apache.xmlgraphics.ps.dsc.DSCParser) - */ + + /** {@inheritDoc} */ public void handle(DSCEvent event, DSCParser parser) throws IOException, DSCException { + processEvent(event, parser); + } + + /** {@inheritDoc} */ + public void processEvent(DSCEvent event, DSCParser parser) throws IOException, DSCException { if (event.isDSCComment()) { DSCComment comment = event.asDSCComment(); if (DSCConstants.BEGIN_DOCUMENT.equals(comment.getName())) { - comment.generate(gen); + if (gen != null) { + comment.generate(gen); + } parser.setCheckEOF(false); + parser.setListenersDisabled(true); comment = parser.nextDSCComment(DSCConstants.END_DOCUMENT, gen); if (comment == null) { - throw new DSCException("File is not DSC-compliant: Didn't find an " + throw new DSCException("File is not DSC-compliant: Didn't find an " + DSCConstants.END_DOCUMENT); } - comment.generate(gen); + if (gen != null) { + comment.generate(gen); + } parser.setCheckEOF(true); + parser.setListenersDisabled(false); parser.next(); } else if (DSCConstants.BEGIN_DATA.equals(comment.getName())) { - comment.generate(gen); + if (gen != null) { + comment.generate(gen); + } parser.setCheckEOF(false); + parser.setListenersDisabled(true); comment = parser.nextDSCComment(DSCConstants.END_DATA, gen); if (comment == null) { - throw new DSCException("File is not DSC-compliant: Didn't find an " + throw new DSCException("File is not DSC-compliant: Didn't find an " + DSCConstants.END_DATA); } - comment.generate(gen); + if (gen != null) { + comment.generate(gen); + } parser.setCheckEOF(true); + parser.setListenersDisabled(false); parser.next(); - } + } } } - + } Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/FilteringEventListener.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/FilteringEventListener.java?rev=727407&view=auto ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/FilteringEventListener.java (added) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/FilteringEventListener.java Wed Dec 17 07:05:45 2008 @@ -0,0 +1,48 @@ +/* + * 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.dsc; + +import java.io.IOException; + +import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; + +/** + * {@code DSCListener} implementation that filters certain DSC events. + */ +public class FilteringEventListener implements DSCListener { + + private DSCFilter filter; + + /** + * Main constructor. + * @param filter the filter + */ + public FilteringEventListener(DSCFilter filter) { + this.filter = filter; + } + + /** {@inheritDoc} */ + public void processEvent(DSCEvent event, DSCParser parser) throws IOException, DSCException { + if (!filter.accept(event)) { + parser.next(); //skip + } + } + +} Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/FilteringEventListener.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/FilteringEventListener.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/NestedDocumentHandler.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/NestedDocumentHandler.java?rev=727407&r1=727406&r2=727407&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/NestedDocumentHandler.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/NestedDocumentHandler.java Wed Dec 17 07:05:45 2008 @@ -5,9 +5,9 @@ * 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. @@ -28,6 +28,9 @@ * in a PostScript document. The implementation receives a parser instance so it can step forward * until the end of the nested document is reached at which point control is given back to the * original consumer. + *

+ * It is suggested to use the more generally usable {@link DSCListener} instead. This + * interface may be deprecated in the future. */ public interface NestedDocumentHandler { Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/ResourceTracker.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/ResourceTracker.java?rev=727407&r1=727406&r2=727407&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/ResourceTracker.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/ResourceTracker.java Wed Dec 17 07:05:45 2008 @@ -5,9 +5,9 @@ * 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. @@ -23,6 +23,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; +import java.util.Map; import java.util.Set; import org.apache.xmlgraphics.ps.PSGenerator; @@ -42,7 +43,10 @@ private Set documentNeededResources; private Set usedResources; private Set pageResources; - + + //Map + private Map resourceUsageCounts; + /** * Returns the set of supplied resources. * @return the set of supplied resources @@ -54,7 +58,7 @@ return Collections.EMPTY_SET; } } - + /** * Returns the set of needed resources. * @return the set of needed resources @@ -66,7 +70,7 @@ return Collections.EMPTY_SET; } } - + /** * Notifies the resource tracker that a new page has been started and that the page resource * set can be cleared. @@ -76,7 +80,7 @@ pageResources.clear(); } } - + /** * Registers a supplied resource. If the same resources is already in the set of needed * resources, it is removed there. @@ -91,7 +95,7 @@ documentNeededResources.remove(res); } } - + /** * Registers a needed resource. If the same resources is already in the set of supplied * resources, it is ignored, i.e. it is assumed to be supplied. @@ -105,16 +109,34 @@ documentNeededResources.add(res); } } - + + private void preparePageResources() { + if (pageResources == null) { + pageResources = new java.util.HashSet(); + } + } + + private void prepareUsageCounts() { + if (resourceUsageCounts == null) { + resourceUsageCounts = new java.util.HashMap(); + } + } + /** * Notifies the resource tracker about the usage of a resource on the current page. * @param res the resource being used */ public void notifyResourceUsageOnPage(PSResource res) { - if (pageResources == null) { - pageResources = new java.util.HashSet(); - } + preparePageResources(); pageResources.add(res); + + prepareUsageCounts(); + Counter counter = (Counter)resourceUsageCounts.get(res); + if (counter == null) { + resourceUsageCounts.put(res, new Counter()); + } else { + counter.inc(); + } } /** @@ -122,10 +144,12 @@ * @param resources the resources being used */ public void notifyResourceUsageOnPage(Collection resources) { - if (pageResources == null) { - pageResources = new java.util.HashSet(); + preparePageResources(); + Iterator iter = resources.iterator(); + while (iter.hasNext()) { + PSResource res = (PSResource)iter.next(); + notifyResourceUsageOnPage(res); } - pageResources.addAll(resources); } /** @@ -140,34 +164,101 @@ /** * Writes a DSC comment for the accumulated used resources, either at page level or * at document level. - * @param pageLevel true if the DSC comment for the page level should be generated, + * @param pageLevel true if the DSC comment for the page level should be generated, * false for the document level (in the trailer) * @param gen the PSGenerator to write the DSC comments with * @exception IOException In case of an I/O problem */ public void writeResources(boolean pageLevel, PSGenerator gen) throws IOException { if (pageLevel) { - new DSCCommentPageResources(pageResources).generate(gen); - if (usedResources == null) { - usedResources = new java.util.HashSet(); - } - usedResources.addAll(pageResources); + writePageResources(gen); } else { - if (usedResources != null) { - Iterator iter = usedResources.iterator(); - while (iter.hasNext()) { - PSResource res = (PSResource)iter.next(); - if (documentSuppliedResources == null - || !documentSuppliedResources.contains(res)) { - registerNeededResource(res); - } + writeDocumentResources(gen); + } + } + + /** + * Writes a DSC comment for the accumulated used resources on the current page. Then it commits + * all those resources to the used resources on document level. + * @param gen the PSGenerator to write the DSC comments with + * @exception IOException In case of an I/O problem + */ + public void writePageResources(PSGenerator gen) throws IOException { + new DSCCommentPageResources(pageResources).generate(gen); + if (usedResources == null) { + usedResources = new java.util.HashSet(); + } + usedResources.addAll(pageResources); + } + + /** + * Writes a DSC comment for the needed and supplied resourced for the current DSC document. + * @param gen the PSGenerator to write the DSC comments with + * @exception IOException In case of an I/O problem + */ + public void writeDocumentResources(PSGenerator gen) throws IOException { + if (usedResources != null) { + Iterator iter = usedResources.iterator(); + while (iter.hasNext()) { + PSResource res = (PSResource)iter.next(); + if (documentSuppliedResources == null + || !documentSuppliedResources.contains(res)) { + registerNeededResource(res); } } - new DSCCommentDocumentNeededResources(documentNeededResources).generate(gen); - new DSCCommentDocumentSuppliedResources(documentSuppliedResources).generate(gen); + } + new DSCCommentDocumentNeededResources(documentNeededResources).generate(gen); + new DSCCommentDocumentSuppliedResources(documentSuppliedResources).generate(gen); + } + + /** + * This method declares that the given resource will be inlined and can therefore + * be removed from resource tracking. This is useful when you don't know beforehand + * if a resource will be used multiple times. If it's only used once it's better + * to inline the resource to lower the maximum memory needed inside the PostScript + * interpreter. + * @param res the resource + */ + public void declareInlined(PSResource res) { + if (this.documentNeededResources != null) { + this.documentNeededResources.remove(res); + } + if (this.documentSuppliedResources != null) { + this.documentSuppliedResources.remove(res); + } + if (this.pageResources != null) { + this.pageResources.remove(res); + } + if (this.usedResources != null) { + this.usedResources.remove(res); } } - - - + + /** + * Returns the number of times a resource has been used inside the current DSC document. + * @param res the resource + * @return the number of times the resource has been used + */ + public long getUsageCount(PSResource res) { + Counter counter = (Counter)resourceUsageCounts.get(res); + return (counter != null ? counter.getCount() : 0); + } + + private static class Counter { + + private long count = 1; + + public void inc() { + this.count++; + } + + public long getCount() { + return this.count; + } + + public String toString() { + return Long.toString(this.count); + } + } + } Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourceDSCComment.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourceDSCComment.java?rev=727407&view=auto ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourceDSCComment.java (added) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourceDSCComment.java Wed Dec 17 07:05:45 2008 @@ -0,0 +1,100 @@ +/* + * 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.dsc.events; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.PSProcSet; +import org.apache.xmlgraphics.ps.PSResource; + +/** + * Abstract base class for resource comments. + */ +public abstract class AbstractResourceDSCComment extends AbstractDSCComment { + + private PSResource resource; + + /** + * Creates a new instance + */ + public AbstractResourceDSCComment() { + } + + /** + * Creates a new instance for a given PSResource instance + * @param resource the resource + */ + public AbstractResourceDSCComment(PSResource resource) { + this.resource = resource; + } + + /** + * Returns the associated PSResource. + * @return the resource + */ + public PSResource getResource() { + return this.resource; + } + + /** {@inheritDoc} */ + public boolean hasValues() { + return true; + } + + /** {@inheritDoc} */ + public void parseValue(String value) { + List params = splitParams(value); + Iterator iter = params.iterator(); + String name = (String)iter.next(); + if (PSResource.TYPE_FONT.equals(name)) { + String fontname = (String)iter.next(); + this.resource = new PSResource(name, fontname); + } else if (PSResource.TYPE_PROCSET.equals(name)) { + String procname = (String)iter.next(); + String version = (String)iter.next(); + String revision = (String)iter.next(); + this.resource = new PSProcSet(procname, + Float.parseFloat(version), Integer.parseInt(revision)); + } else if (PSResource.TYPE_FILE.equals(name)) { + String filename = (String)iter.next(); + this.resource = new PSResource(name, filename); + } else if (PSResource.TYPE_FORM.equals(name)) { + String formname = (String)iter.next(); + this.resource = new PSResource(name, formname); + } else if (PSResource.TYPE_PATTERN.equals(name)) { + String patternname = (String)iter.next(); + this.resource = new PSResource(name, patternname); + } else if (PSResource.TYPE_ENCODING.equals(name)) { + String encodingname = (String)iter.next(); + this.resource = new PSResource(name, encodingname); + } else { + throw new IllegalArgumentException("Invalid resource type: " + name); + } + } + + /** {@inheritDoc} */ + public void generate(PSGenerator gen) throws IOException { + gen.writeDSCComment(getName(), getResource()); + } + +} Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourceDSCComment.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourceDSCComment.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourcesDSCComment.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourcesDSCComment.java?rev=727407&r1=727406&r2=727407&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourcesDSCComment.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/AbstractResourcesDSCComment.java Wed Dec 17 07:05:45 2008 @@ -5,9 +5,9 @@ * 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. @@ -31,20 +31,20 @@ import org.apache.xmlgraphics.ps.PSResource; /** - * Abstract base class for Resource DSC comments (DocumentNeededResources, + * Abstract base class for Resource DSC comments (DocumentNeededResources, * DocumentSuppliedResources and PageResources). */ public abstract class AbstractResourcesDSCComment extends AbstractDSCComment { private Set resources; - + /** * Creates a new instance. */ public AbstractResourcesDSCComment() { super(); } - + /** * Creates a new instance. * @param resources a Collection of PSResource instances @@ -52,38 +52,38 @@ public AbstractResourcesDSCComment(Collection resources) { addResources(resources); } - - /** - * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues() - */ + + /** {@inheritDoc} */ public boolean hasValues() { return true; } + private void prepareResourceSet() { + if (this.resources == null) { + this.resources = new java.util.TreeSet(); + } + } + /** * Adds a new resource. * @param res the resource */ public void addResource(PSResource res) { - if (this.resources == null) { - this.resources = new java.util.HashSet(); - } + prepareResourceSet(); this.resources.add(res); } - + /** * Adds a collection of resources. - * @param resources a Collection of PSResource instances. + * @param resources a Collection of PSResource instances. */ public void addResources(Collection resources) { if (resources != null) { - if (this.resources == null) { - this.resources = new java.util.HashSet(); - } + prepareResourceSet(); this.resources.addAll(resources); } } - + /** * Returns the set of resources associated with this DSC comment. * @return the set of resources @@ -91,12 +91,12 @@ public Set getResources() { return Collections.unmodifiableSet(this.resources); } - + /** * Defines the known resource types (font, procset, file, pattern etc.). */ protected static final Set RESOURCE_TYPES = new java.util.HashSet(); - + static { RESOURCE_TYPES.add(PSResource.TYPE_FONT); RESOURCE_TYPES.add(PSResource.TYPE_PROCSET); @@ -105,10 +105,8 @@ RESOURCE_TYPES.add(PSResource.TYPE_FORM); RESOURCE_TYPES.add(PSResource.TYPE_ENCODING); } - - /** - * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String) - */ + + /** {@inheritDoc} */ public void parseValue(String value) { List params = splitParams(value); String currentResourceType = null; @@ -132,7 +130,7 @@ String procname = (String)iter.next(); String version = (String)iter.next(); String revision = (String)iter.next(); - addResource(new PSProcSet(procname, + addResource(new PSProcSet(procname, Float.parseFloat(version), Integer.parseInt(revision))); } else if (PSResource.TYPE_FILE.equals(currentResourceType)) { String filename = (String)iter.next(); @@ -143,10 +141,7 @@ } } - /** - * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate( - * org.apache.xmlgraphics.ps.PSGenerator) - */ + /** {@inheritDoc} */ public void generate(PSGenerator gen) throws IOException { if (resources == null || resources.size() == 0) { return; Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginDocument.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginDocument.java?rev=727407&view=auto ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginDocument.java (added) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginDocument.java Wed Dec 17 07:05:45 2008 @@ -0,0 +1,131 @@ +/* + * 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.dsc.events; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; +import org.apache.xmlgraphics.ps.PSResource; + +/** + * Represents a %BeginDocument DSC comment. + */ +public class DSCCommentBeginDocument extends AbstractDSCComment { + + private PSResource resource; + private Float version; + private String type; + + /** + * Creates a new instance + */ + public DSCCommentBeginDocument() { + super(); + } + + /** + * Creates a new instance for a given PSResource instance + * @param resource the resource + */ + public DSCCommentBeginDocument(PSResource resource) { + this.resource = resource; + if (resource != null && !PSResource.TYPE_FILE.equals(resource.getType())) { + throw new IllegalArgumentException("Resource must be of type 'file'"); + } + } + + /** + * Creates a new instance for a given PSResource instance + * @param resource the resource + * @param version the version of the resource (or null) + * @param type the type of resource (or null) + */ + public DSCCommentBeginDocument(PSResource resource, Float version, String type) { + this(resource); + this.version = version; + this.type = type; + } + + /** + * Returns the resource version. + * @return the resource version (or null if not applicable) + */ + public Float getVersion() { + return this.version; + } + + /** + * Returns the resource type + * @return the resource type (or null if not applicable) + */ + public String getType() { + return this.type; + } + + /** {@inheritDoc} */ + public String getName() { + return DSCConstants.BEGIN_DOCUMENT; + } + + /** + * Returns the associated PSResource. + * @return the resource + */ + public PSResource getResource() { + return this.resource; + } + + /** {@inheritDoc} */ + public boolean hasValues() { + return true; + } + + /** {@inheritDoc} */ + public void parseValue(String value) { + List params = splitParams(value); + Iterator iter = params.iterator(); + String name = (String)iter.next(); + this.resource = new PSResource(PSResource.TYPE_FILE, name); + if (iter.hasNext()) { + this.version = new Float(iter.next().toString()); + this.type = null; + if (iter.hasNext()) { + this.type = (String)iter.next(); + } + } + } + + /** {@inheritDoc} */ + public void generate(PSGenerator gen) throws IOException { + List params = new java.util.ArrayList(); + params.add(getResource().getName()); + if (getVersion() != null) { + params.add(getVersion()); + if (getType() != null) { + params.add(getType()); + } + } + gen.writeDSCComment(getName(), params.toArray()); + } + +} Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginDocument.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginDocument.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginResource.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginResource.java?rev=727407&r1=727406&r2=727407&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginResource.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentBeginResource.java Wed Dec 17 07:05:45 2008 @@ -5,9 +5,9 @@ * 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. @@ -20,27 +20,24 @@ package org.apache.xmlgraphics.ps.dsc.events; import java.io.IOException; -import java.util.Iterator; -import java.util.List; import org.apache.xmlgraphics.ps.DSCConstants; import org.apache.xmlgraphics.ps.PSGenerator; -import org.apache.xmlgraphics.ps.PSProcSet; import org.apache.xmlgraphics.ps.PSResource; /** * Represents a %BeginResource DSC comment. */ -public class DSCCommentBeginResource extends AbstractDSCComment { +public class DSCCommentBeginResource extends AbstractResourceDSCComment { - private PSResource resource; private Integer min; private Integer max; - + /** * Creates a new instance */ public DSCCommentBeginResource() { + super(); } /** @@ -48,9 +45,9 @@ * @param resource the resource */ public DSCCommentBeginResource(PSResource resource) { - this.resource = resource; + super(resource); } - + /** * Creates a new instance for a given PSResource instance * @param resource the resource @@ -58,19 +55,11 @@ * @param max Maximum VM used by the resource */ public DSCCommentBeginResource(PSResource resource, int min, int max) { - this.resource = resource; + super(resource); this.min = new Integer(min); this.max = new Integer(max); } - - /** - * Returns the associated PSResource. - * @return the resource - */ - public PSResource getResource() { - return this.resource; - } - + /** * Returns the minimum VM used by the resource. * @return the minimum VM used by the resource @@ -78,7 +67,7 @@ public Integer getMin() { return this.min; } - + /** * Returns the maximum VM used by the resource. * @return the maximum VM used by the resource @@ -87,54 +76,18 @@ return this.max; } - /** - * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#getName() - */ + /** {@inheritDoc} */ public String getName() { return DSCConstants.BEGIN_RESOURCE; } - /** - * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#hasValues() - */ - public boolean hasValues() { - return true; - } - - /** - * @see org.apache.xmlgraphics.ps.dsc.events.DSCComment#parseValue(java.lang.String) - */ - public void parseValue(String value) { - List params = splitParams(value); - Iterator iter = params.iterator(); - String name = (String)iter.next(); - if (PSResource.TYPE_FONT.equals(name)) { - String fontname = (String)iter.next(); - this.resource = new PSResource(name, fontname); - } else if (PSResource.TYPE_PROCSET.equals(name)) { - String procname = (String)iter.next(); - String version = (String)iter.next(); - String revision = (String)iter.next(); - this.resource = new PSProcSet(procname, - Float.parseFloat(version), Integer.parseInt(revision)); - } else if (PSResource.TYPE_FILE.equals(name)) { - String filename = (String)iter.next(); - this.resource = new PSResource(name, filename); - } else { - throw new IllegalArgumentException("Invalid resource type: " + name); - } - } - - /** - * @see org.apache.xmlgraphics.ps.dsc.events.DSCEvent#generate( - * org.apache.xmlgraphics.ps.PSGenerator) - */ + /** {@inheritDoc} */ public void generate(PSGenerator gen) throws IOException { if (getMin() != null) { Object[] params = new Object[] {getResource(), getMin(), getMax()}; gen.writeDSCComment(getName(), params); } else { - gen.writeDSCComment(getName(), getResource()); + super.generate(gen); } } Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentIncludeResource.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentIncludeResource.java?rev=727407&view=auto ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentIncludeResource.java (added) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentIncludeResource.java Wed Dec 17 07:05:45 2008 @@ -0,0 +1,50 @@ +/* + * 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.dsc.events; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSResource; + +/** + * Represents a %IncludeResource DSC comment. + */ +public class DSCCommentIncludeResource extends AbstractResourceDSCComment { + + /** + * Creates a new instance + */ + public DSCCommentIncludeResource() { + super(); + } + + /** + * Creates a new instance for a given PSResource instance + * @param resource the resource + */ + public DSCCommentIncludeResource(PSResource resource) { + super(resource); + } + + /** {@inheritDoc} */ + public String getName() { + return DSCConstants.INCLUDE_RESOURCE; + } + +} Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentIncludeResource.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentIncludeResource.java ------------------------------------------------------------------------------ svn:keywords = Id Added: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentTitle.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentTitle.java?rev=727407&view=auto ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentTitle.java (added) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentTitle.java Wed Dec 17 07:05:45 2008 @@ -0,0 +1,80 @@ +/* + * 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.dsc.events; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.PSGenerator; + +/** + * Represents a %%Title DSC comment. + */ +public class DSCCommentTitle extends AbstractDSCComment { + + private String title; + + /** + * Creates a new instance. + */ + public DSCCommentTitle() { + } + + /** + * Creates a new instance. + * @param title the title text + */ + public DSCCommentTitle(String title) { + this.title = title; + } + + /** + * Returns the title. + * @return the title + */ + public String getTitle() { + return this.title; + } + + /** {@inheritDoc} */ + public String getName() { + return DSCConstants.TITLE; + } + + /** {@inheritDoc} */ + public boolean hasValues() { + return true; + } + + /** {@inheritDoc} */ + public void parseValue(String value) { + List params = splitParams(value); + Iterator iter = params.iterator(); + this.title = (String)iter.next(); + } + + /** {@inheritDoc} */ + public void generate(PSGenerator gen) throws IOException { + gen.writeDSCComment(getName(), getTitle()); + } + +} Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentTitle.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/events/DSCCommentTitle.java ------------------------------------------------------------------------------ svn:keywords = Id Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/PageExtractor.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/PageExtractor.java?rev=727407&r1=727406&r2=727407&view=diff ============================================================================== --- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/PageExtractor.java (original) +++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/dsc/tools/PageExtractor.java Wed Dec 17 07:05:45 2008 @@ -5,9 +5,9 @@ * 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. @@ -40,18 +40,18 @@ * This class can extract a certain range of pages from a DSC-compliant PostScript file. */ public class PageExtractor implements DSCParserConstants { - + /** * Parses a DSC-compliant file and pipes the content through to the OutputStream omitting * all pages not within the range. - * @param in the InputStream to parse from + * @param in the InputStream to parse from * @param out the OutputStream to write the modified file to * @param from the starting page (1-based) * @param to the last page (inclusive, 1-based) * @throws IOException In case of an I/O error * @throws DSCException In case of a violation of the DSC spec */ - public static void extractPages(InputStream in, OutputStream out, int from, int to) + public static void extractPages(InputStream in, OutputStream out, int from, int to) throws IOException, DSCException { if (from <= 0) { throw new IllegalArgumentException("'from' page number must be 1 or higher"); @@ -60,12 +60,12 @@ throw new IllegalArgumentException( "'to' page number must be equal or larger than the 'from' page number"); } - + DSCParser parser = new DSCParser(in); PSGenerator gen = new PSGenerator(out); - parser.setNestedDocumentHandler(new DefaultNestedDocumentHandler(gen)); + parser.addListener(new DefaultNestedDocumentHandler(gen)); int pageCount = 0; - + //Skip DSC header DSCHeaderComment header = DSCTools.checkAndSkipDSC30Header(parser); header.generate(gen); @@ -90,7 +90,7 @@ throw new DSCException("Page expected, but none found"); } parser.setFilter(null); //Remove filter - + //Process individual pages (and skip as necessary) while (true) { DSCCommentPage page = (DSCCommentPage)pageOrTrailer; @@ -108,12 +108,12 @@ break; } } - + //Write the rest while (parser.hasNext()) { DSCEvent event = parser.nextEvent(); event.generate(gen); } } - + } \ No newline at end of file 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?rev=727407&r1=727406&r2=727407&view=diff ============================================================================== --- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java (original) +++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/StandardTestSuite.java Wed Dec 17 07:05:45 2008 @@ -19,8 +19,12 @@ package org.apache.xmlgraphics; +import junit.framework.Test; +import junit.framework.TestSuite; + import org.apache.xmlgraphics.image.codec.png.PNGEncoderTest; import org.apache.xmlgraphics.ps.PSEscapeTestCase; +import org.apache.xmlgraphics.ps.dsc.ListenerTestCase; import org.apache.xmlgraphics.ps.dsc.events.DSCValueParserTestCase; import org.apache.xmlgraphics.ps.dsc.tools.DSCToolsTestCase; import org.apache.xmlgraphics.util.ClasspathResourceTest; @@ -30,9 +34,6 @@ import org.apache.xmlgraphics.util.io.ASCII85OutputStreamTestCase; import org.apache.xmlgraphics.util.io.Base64Test; -import junit.framework.Test; -import junit.framework.TestSuite; - /** * Test suite for basic functionality of XML Graphics Commons. */ @@ -55,6 +56,7 @@ suite.addTest(new TestSuite(PSEscapeTestCase.class)); suite.addTest(new TestSuite(DSCValueParserTestCase.class)); suite.addTest(new TestSuite(DSCToolsTestCase.class)); + suite.addTest(new TestSuite(ListenerTestCase.class)); suite.addTest(new TestSuite(UnitConvTestCase.class)); //$JUnit-END$ return suite; Added: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/ListenerTestCase.java URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/ListenerTestCase.java?rev=727407&view=auto ============================================================================== --- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/ListenerTestCase.java (added) +++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/ListenerTestCase.java Wed Dec 17 07:05:45 2008 @@ -0,0 +1,125 @@ +/* + * 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.dsc; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.commons.io.IOUtils; + +import org.apache.xmlgraphics.ps.DSCConstants; +import org.apache.xmlgraphics.ps.dsc.events.DSCComment; +import org.apache.xmlgraphics.ps.dsc.events.DSCCommentLanguageLevel; +import org.apache.xmlgraphics.ps.dsc.events.DSCEvent; + +/** + * Tests the listener functionality on the DSC parser. + */ +public class ListenerTestCase extends TestCase { + + /** + * Tests {@link DSCParser#setFilter(DSCFilter)}. + * @throws Exception if an error occurs + */ + public void testFilter() throws Exception { + InputStream in = getClass().getResourceAsStream("test1.txt"); + try { + DSCParser parser = new DSCParser(in); + parser.setFilter(new DSCFilter() { + + public boolean accept(DSCEvent event) { + //Filter out all non-DSC comments + return !event.isComment(); + } + + }); + while (parser.hasNext()) { + DSCEvent event = parser.nextEvent(); + + if (parser.getCurrentEvent().isComment()) { + fail("Filter failed. Comment found."); + } + } + } finally { + IOUtils.closeQuietly(in); + } + } + + /** + * Tests listeners on DSCParser. + * @throws Exception if an error occurs + */ + public void testListeners() throws Exception { + InputStream in = getClass().getResourceAsStream("test1.txt"); + try { + final Map results = new java.util.HashMap(); + DSCParser parser = new DSCParser(in); + + //Filter the prolog + parser.addListener(new DSCListener() { + public void processEvent(DSCEvent event, DSCParser parser) + throws IOException, DSCException { + if (event.isDSCComment()) { + DSCComment comment = event.asDSCComment(); + if (DSCConstants.BEGIN_PROLOG.equals(comment.getName())) { + //Skip until end of prolog + while (parser.hasNext()) { + DSCEvent e = parser.nextEvent(); + if (e.isDSCComment()) { + if (DSCConstants.END_PROLOG.equals( + e.asDSCComment().getName())) { + parser.next(); + break; + } + } + + } + } + } + } + }); + + //Listener for the language level + parser.addListener(new DSCListener() { + public void processEvent(DSCEvent event, DSCParser parser) + throws IOException, DSCException { + if (event instanceof DSCCommentLanguageLevel) { + DSCCommentLanguageLevel level = (DSCCommentLanguageLevel)event; + results.put("level", new Integer(level.getLanguageLevel())); + } + } + }); + int count = 0; + while (parser.hasNext()) { + DSCEvent event = parser.nextEvent(); + System.out.println(event); + count++; + } + assertEquals(12, count); + assertEquals(new Integer(1), results.get("level")); + } finally { + IOUtils.closeQuietly(in); + } + } + +} Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/ListenerTestCase.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/ListenerTestCase.java ------------------------------------------------------------------------------ svn:keywords = Id Added: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/test1.txt URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/test1.txt?rev=727407&view=auto ============================================================================== --- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/test1.txt (added) +++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/test1.txt Wed Dec 17 07:05:45 2008 @@ -0,0 +1,15 @@ +%!PS-Adobe-3.0 EPSF-3.0 +%%BoundingBox: 0 0 136 43 +%%LanguageLevel: 1 +%Custom comment +%%EndComments +%%BeginProlog +prologing +%%EndProlog +dummy postscript +dummy postscript +%Custom comment +dummy postscript +dummy postscript +dummy postscript +%%EOF Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/ps/dsc/test1.txt ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org For additional commands, e-mail: commits-help@xmlgraphics.apache.org