001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id$ */
019:
020: package org.apache.xmlgraphics.ps.dsc;
021:
022: import java.io.IOException;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.xmlgraphics.ps.dsc.events.DSCComment;
027:
028: /**
029: * DSCHandler implementation that records DSC events.
030: */
031: public class EventRecorder implements DSCHandler {
032:
033: private List events = new java.util.ArrayList();
034:
035: /**
036: * Replays the recorded events to a specified DSCHandler instance.
037: * @param handler the DSCHandler to send the recorded events to
038: * @throws IOException In case of an I/O error
039: */
040: public void replay(DSCHandler handler) throws IOException {
041: Iterator iter = events.iterator();
042: while (iter.hasNext()) {
043: Object obj = iter.next();
044: if (obj instanceof PSLine) {
045: handler.line(((PSLine) obj).getLine());
046: } else if (obj instanceof PSComment) {
047: handler.comment(((PSComment) obj).getComment());
048: } else if (obj instanceof DSCComment) {
049: handler.handleDSCComment((DSCComment) obj);
050: } else {
051: throw new IllegalStateException(
052: "Unsupported class type");
053: }
054: }
055: }
056:
057: /**
058: * @see org.apache.xmlgraphics.ps.dsc.DSCHandler#comment(java.lang.String)
059: */
060: public void comment(String comment) throws IOException {
061: events.add(new PSComment(comment));
062: }
063:
064: /**
065: * @see org.apache.xmlgraphics.ps.dsc.DSCHandler#handleDSCComment(
066: * org.apache.xmlgraphics.ps.dsc.events.DSCComment)
067: */
068: public void handleDSCComment(DSCComment comment) throws IOException {
069: events.add(comment);
070: }
071:
072: /**
073: * @see org.apache.xmlgraphics.ps.dsc.DSCHandler#line(java.lang.String)
074: */
075: public void line(String line) throws IOException {
076: events.add(new PSLine(line));
077: }
078:
079: /**
080: * @see org.apache.xmlgraphics.ps.dsc.DSCHandler#startDocument(java.lang.String)
081: */
082: public void startDocument(String header) throws IOException {
083: throw new UnsupportedOperationException(getClass().getName()
084: + " is only used to handle parts of a document");
085: }
086:
087: /**
088: * @see org.apache.xmlgraphics.ps.dsc.DSCHandler#endDocument()
089: */
090: public void endDocument() throws IOException {
091: throw new UnsupportedOperationException(getClass().getName()
092: + " is only used to handle parts of a document");
093: }
094:
095: private static class PSComment {
096:
097: private String comment;
098:
099: public PSComment(String comment) {
100: this .comment = comment;
101: }
102:
103: public String getComment() {
104: return this .comment;
105: }
106: }
107:
108: private static class PSLine {
109:
110: private String line;
111:
112: public PSLine(String line) {
113: this .line = line;
114: }
115:
116: public String getLine() {
117: return this.line;
118: }
119: }
120:
121: }
|