2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
/* $Id: ListenerTestCase.java 727407 2008-12-17 15:05:45Z jeremias $ */
20
package org.apache.xmlgraphics.ps.dsc;
22
import java.io.IOException;
23
import java.io.InputStream;
26
import junit.framework.TestCase;
28
import org.apache.commons.io.IOUtils;
30
import org.apache.xmlgraphics.ps.DSCConstants;
31
import org.apache.xmlgraphics.ps.dsc.events.DSCComment;
32
import org.apache.xmlgraphics.ps.dsc.events.DSCCommentLanguageLevel;
33
import org.apache.xmlgraphics.ps.dsc.events.DSCEvent;
36
* Tests the listener functionality on the DSC parser.
38
public class ListenerTestCase extends TestCase {
41
* Tests {@link DSCParser#setFilter(DSCFilter)}.
42
* @throws Exception if an error occurs
44
public void testFilter() throws Exception {
45
InputStream in = getClass().getResourceAsStream("test1.txt");
47
DSCParser parser = new DSCParser(in);
48
parser.setFilter(new DSCFilter() {
50
public boolean accept(DSCEvent event) {
51
//Filter out all non-DSC comments
52
return !event.isComment();
56
while (parser.hasNext()) {
57
DSCEvent event = parser.nextEvent();
59
if (parser.getCurrentEvent().isComment()) {
60
fail("Filter failed. Comment found.");
64
IOUtils.closeQuietly(in);
69
* Tests listeners on DSCParser.
70
* @throws Exception if an error occurs
72
public void testListeners() throws Exception {
73
InputStream in = getClass().getResourceAsStream("test1.txt");
75
final Map results = new java.util.HashMap();
76
DSCParser parser = new DSCParser(in);
79
parser.addListener(new DSCListener() {
80
public void processEvent(DSCEvent event, DSCParser parser)
81
throws IOException, DSCException {
82
if (event.isDSCComment()) {
83
DSCComment comment = event.asDSCComment();
84
if (DSCConstants.BEGIN_PROLOG.equals(comment.getName())) {
85
//Skip until end of prolog
86
while (parser.hasNext()) {
87
DSCEvent e = parser.nextEvent();
88
if (e.isDSCComment()) {
89
if (DSCConstants.END_PROLOG.equals(
90
e.asDSCComment().getName())) {
102
//Listener for the language level
103
parser.addListener(new DSCListener() {
104
public void processEvent(DSCEvent event, DSCParser parser)
105
throws IOException, DSCException {
106
if (event instanceof DSCCommentLanguageLevel) {
107
DSCCommentLanguageLevel level = (DSCCommentLanguageLevel)event;
108
results.put("level", new Integer(level.getLanguageLevel()));
113
while (parser.hasNext()) {
114
DSCEvent event = parser.nextEvent();
115
System.out.println(event);
118
assertEquals(12, count);
119
assertEquals(new Integer(1), results.get("level"));
121
IOUtils.closeQuietly(in);