01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /* $Id$ */
19:
20: package org.apache.xmlgraphics.ps.dsc;
21:
22: import java.io.IOException;
23:
24: import org.apache.xmlgraphics.ps.DSCConstants;
25: import org.apache.xmlgraphics.ps.PSGenerator;
26: import org.apache.xmlgraphics.ps.dsc.events.DSCComment;
27: import org.apache.xmlgraphics.ps.dsc.events.DSCEvent;
28:
29: /**
30: * Default implementation of the NestedDocumentHandler interface which automatically skips data
31: * between Begin/EndDocument and Begin/EndData.
32: */
33: public class DefaultNestedDocumentHandler implements
34: DSCParserConstants, NestedDocumentHandler {
35:
36: private PSGenerator gen;
37:
38: /**
39: * Creates a new instance.
40: * @param gen PSGenerator to pass through the skipped content
41: */
42: public DefaultNestedDocumentHandler(PSGenerator gen) {
43: this .gen = gen;
44: }
45:
46: /**
47: * @see org.apache.xmlgraphics.ps.dsc.NestedDocumentHandler#handle(org.apache.xmlgraphics.ps.dsc.events.DSCEvent, org.apache.xmlgraphics.ps.dsc.DSCParser)
48: */
49: public void handle(DSCEvent event, DSCParser parser)
50: throws IOException, DSCException {
51: if (event.isDSCComment()) {
52: DSCComment comment = event.asDSCComment();
53: if (DSCConstants.BEGIN_DOCUMENT.equals(comment.getName())) {
54: comment.generate(gen);
55: comment = parser.nextDSCComment(
56: DSCConstants.END_DOCUMENT, gen);
57: if (comment == null) {
58: throw new DSCException(
59: "File is not DSC-compliant: Didn't find an "
60: + DSCConstants.END_DOCUMENT);
61: }
62: comment.generate(gen);
63: parser.next();
64: } else if (DSCConstants.BEGIN_DATA
65: .equals(comment.getName())) {
66: comment.generate(gen);
67: comment = parser.nextDSCComment(DSCConstants.END_DATA,
68: gen);
69: if (comment == null) {
70: throw new DSCException(
71: "File is not DSC-compliant: Didn't find an "
72: + DSCConstants.END_DATA);
73: }
74: comment.generate(gen);
75: parser.next();
76: }
77: }
78: }
79:
80: }
|