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.tools;
021:
022: import java.io.IOException;
023:
024: import org.apache.xmlgraphics.ps.DSCConstants;
025: import org.apache.xmlgraphics.ps.PSGenerator;
026: import org.apache.xmlgraphics.ps.dsc.DSCException;
027: import org.apache.xmlgraphics.ps.dsc.DSCParser;
028: import org.apache.xmlgraphics.ps.dsc.DSCParserConstants;
029: import org.apache.xmlgraphics.ps.dsc.events.DSCComment;
030: import org.apache.xmlgraphics.ps.dsc.events.DSCEvent;
031: import org.apache.xmlgraphics.ps.dsc.events.DSCHeaderComment;
032: import org.apache.xmlgraphics.ps.dsc.events.PostScriptComment;
033:
034: /**
035: * Helper methods commonly used when dealing with DSC-compliant PostScript files.
036: */
037: public class DSCTools implements DSCParserConstants {
038:
039: /**
040: * Indicates whether the given event ends a header comment section according to the rules in
041: * DSC 3.0, chapter 4.4.
042: * @param event the event to check
043: * @return true if a header comment section would be ended either explicitely or implicitely
044: * by the given event
045: */
046: public static boolean headerCommentsEndHere(DSCEvent event) {
047: switch (event.getEventType()) {
048: case DSC_COMMENT:
049: DSCComment comment = event.asDSCComment();
050: return (comment.getName().equals(DSCConstants.END_COMMENTS));
051: case COMMENT:
052: String s = ((PostScriptComment) event).getComment();
053: if (s == null || s.length() == 0) {
054: return true;
055: } else {
056: char c = s.charAt(0);
057: return ("\n\t ".indexOf(c) >= 0);
058: }
059: default:
060: return true;
061: }
062: }
063:
064: /**
065: * Verifies that the file being parsed is a DSC 3.0 file.
066: * @param parser the DSC parser
067: * @return the header comment event
068: * @throws DSCException In case of a violation of the DSC spec
069: * @throws IOException In case of an I/O problem
070: */
071: public static DSCHeaderComment checkAndSkipDSC30Header(
072: DSCParser parser) throws DSCException, IOException {
073: if (!parser.hasNext()) {
074: throw new DSCException("File has no content");
075: }
076: DSCEvent event = parser.nextEvent();
077: if (event.getEventType() == HEADER_COMMENT) {
078: DSCHeaderComment header = (DSCHeaderComment) event;
079: if (!header.isPSAdobe30()) {
080: throw new DSCException(
081: "PostScript file does not start with '"
082: + DSCConstants.PS_ADOBE_30 + "'");
083: }
084: return header;
085: } else {
086: throw new DSCException(
087: "PostScript file does not start with '"
088: + DSCConstants.PS_ADOBE_30 + "'");
089: }
090: }
091:
092: /**
093: * Advances the parser to the next page or to the trailer or the end of file comment.
094: * @param parser the DSC parser
095: * @param gen the PSGenerator instance to pass the skipped events through to
096: * @return the DSC comment found (Page, Trailer or EOF)
097: * @throws IOException In case of an I/O error
098: * @throws DSCException In case of a violation of the DSC spec
099: */
100: public static DSCComment nextPageOrTrailer(DSCParser parser,
101: PSGenerator gen) throws IOException, DSCException {
102: while (parser.hasNext()) {
103: DSCEvent event = parser.nextEvent();
104: if (event.getEventType() == DSC_COMMENT) {
105: DSCComment comment = event.asDSCComment();
106: if (DSCConstants.PAGE.equals(comment.getName())) {
107: return comment;
108: } else if (DSCConstants.TRAILER.equals(comment
109: .getName())) {
110: return comment;
111: }
112: } else if (event.getEventType() == EOF) {
113: //The Trailer may be missing
114: return event.asDSCComment();
115: }
116: if (gen != null) {
117: event.generate(gen); //Pipe through to PSGenerator
118: }
119: }
120: return null;
121: }
122:
123: }
|