001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.postscript;
031:
032: import java.text.MessageFormat;
033: import java.util.ArrayList;
034: import java.util.Iterator;
035: import java.util.List;
036: import java.util.Stack;
037:
038: /**
039: * <em>Very</em> simple class to execute postscript code as it it parsed
040: * (which means to execute it again you have to parse it again)
041: */
042: public class Handler {
043: Stack stack;
044:
045: public Handler() {
046: super ();
047:
048: stack = new Stack();
049: }
050:
051: public List popResult() {
052: List result;
053: Iterator iterator;
054:
055: result = new ArrayList(stack.size());
056: iterator = stack.iterator();
057: while (iterator.hasNext()) {
058: result.add(iterator.next());
059: }
060: stack.removeAllElements();
061: return result;
062: }
063:
064: public void processEndArray() {
065: throw new UnsupportedOperationException();
066: }
067:
068: public void processEndProcedure() {
069: throw new UnsupportedOperationException();
070: }
071:
072: public void processIdentifier(String string) throws ParseException {
073: String name;
074: Class clazz;
075: IOperator operator;
076:
077: name = MessageFormat.format(
078: "de.intarsys.pdf.postscript.Operator_{0}", //$NON-NLS-1$
079: new Object[] { string });
080: try {
081: clazz = Class.forName(name);
082: } catch (ClassNotFoundException ex) {
083: throw new ParseException(ex);
084: }
085:
086: // all operator classes should have an "Instance" field by convention
087: try {
088: operator = (IOperator) clazz.getField("Instance").get(null); //$NON-NLS-1$
089: } catch (IllegalArgumentException ex) {
090: throw new ParseException(ex);
091: } catch (SecurityException ex) {
092: throw new ParseException(ex);
093: } catch (IllegalAccessException ex) {
094: throw new ParseException(ex);
095: } catch (NoSuchFieldException ex) {
096: throw new ParseException(ex);
097: }
098: operator.execute(stack);
099: }
100:
101: public void processImmediateIdentifier(String string) {
102: throw new UnsupportedOperationException();
103: }
104:
105: public void processKeyIdentifier(String string) {
106: throw new UnsupportedOperationException();
107: }
108:
109: public void processLiteral(double d) {
110: stack.push(new Double(d));
111: }
112:
113: public void processLiteral(int i) {
114: stack.push(new Integer(i));
115: }
116:
117: public void processLiteral(String string) {
118: stack.push(string);
119: }
120:
121: public void processStartArray() {
122: throw new UnsupportedOperationException();
123: }
124:
125: public void processStartProcedure() {
126: throw new UnsupportedOperationException();
127: }
128:
129: public void pushArgs(List args) {
130: stack.addAll(args);
131: }
132: }
|