KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > codec > postscript > PAEngine


1 /*
2  * Copyright 1998 by Sun Microsystems, Inc.,
3  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
4  * All rights reserved.
5  *
6  * This software is the confidential and proprietary information
7  * of Sun Microsystems, Inc. ("Confidential Information"). You
8  * shall not disclose such Confidential Information and shall use
9  * it only in accordance with the terms of the license agreement
10  * you entered into with Sun.
11  */

12
13 package com.lowagie.text.pdf.codec.postscript;
14
15 import java.lang.*;
16 import java.lang.reflect.*;
17 import java.util.*;
18 import java.awt.*;
19 import java.awt.geom.*;
20 import java.awt.color.*;
21 import java.awt.font.*;
22
23
24 public class PAEngine extends Object {
25
26     static public final int MODE_STACK = 0;
27     static public final int MODE_PROCEDURE = 1;
28     static public final int MODE_ARRAY = 2;
29
30     protected PAContext context;
31     protected int mode;
32     protected Stack procedure;
33     protected int innerProcedures;
34
35     public PAEngine(PAContext context){
36         super();
37         this.context = context;
38         this.mode = PAEngine.MODE_STACK;
39     }
40
41     public void startProcedure() throws PainterException {
42         this.procedure = new Stack();
43         this.mode = PAEngine.MODE_PROCEDURE;
44         this.innerProcedures = 0;
45     }
46
47     public void endProcedure() throws PainterException {
48         this.context.operands.push(new PAToken(this.procedure, PAToken.PROCEDURE));
49         this.mode = PAEngine.MODE_STACK;
50     }
51
52     public void bindProcedure(PAToken patoken){
53         Stack oldStack = (Stack) patoken.value;
54         Stack newStack = new Stack();
55         int i,n;
56         n = oldStack.size();
57         for(i = 0; i < n; i++){
58             Object token = oldStack.elementAt(i);
59             if((token instanceof PAToken) && ((PAToken) token).type == PAToken.IDENTIFIER){
60                 Object foundToken = this.context.findIdentifier(((PAToken) token).value);
61                 if(foundToken == null){
62                     newStack.push(token);
63                 } else {
64                     newStack.push(foundToken);
65                 }
66             } else {
67                 newStack.push(token);
68             }
69         }
70         patoken.value = newStack;
71     }
72
73     public void process(Object token) throws PainterException {
74         if(token == null){
75             throw new IllegalStateException("Null token encountered; last unknown identifier was " + this.context.getLastUnknownIdentifier());
76         }
77        // System.out.println("==>"+token.toString());
78
if(token instanceof PAToken && ((PAToken) token).type == PAToken.IMMEDIATE){
79             Object foundValue = this.context.findIdentifier(((PAToken) token).value);
80             this.process(foundValue);
81             return;
82         }
83         if(this.mode == MODE_STACK){
84             if(token instanceof PACommand){
85                 ((PACommand) token).execute(this.context);
86             } else if(token instanceof PAToken){
87                 PAToken patoken = (PAToken) token;
88
89                 switch(patoken.type){
90                 case PAToken.IDENTIFIER:
91                     this.process(this.context.findIdentifier(patoken.value));
92                     break;
93                 case PAToken.KEY:
94                 case PAToken.MARK:
95                 case PAToken.START_ARRAY:
96                     this.context.operands.push(token);
97                     break;
98                 case PAToken.PROCEDURE:
99                     Enumeration enumx = ((Vector) patoken.value).elements();
100                     while(enumx.hasMoreElements()){
101                         this.process(enumx.nextElement());
102                     }
103                     break;
104                 case PAToken.START_PROCEDURE:
105                     this.startProcedure();
106                     break;
107                 case PAToken.END_ARRAY:
108                     this.context.collectArray();
109                     break;
110                 default:
111                     throw new IllegalStateException("Unknown token encountered" + token);
112                 }
113             } else {
114                 this.context.operands.push(token);
115             }
116         } else if(this.mode == MODE_PROCEDURE){
117             if(token instanceof PAToken){
118                 PAToken patoken = (PAToken) token;
119
120                 switch(patoken.type){
121                 case PAToken.START_PROCEDURE:
122                     this.innerProcedures++;
123                     this.procedure.push(token);
124                     break;
125                 case PAToken.END_PROCEDURE:
126                     if(this.innerProcedures > 0){
127                         this.innerProcedures--;
128                         this.procedure.push(token);
129                     } else {
130                         this.endProcedure();
131                     }
132                     break;
133                 default:
134                     this.procedure.push(token);
135                 }
136             } else {
137                 this.procedure.push(token);
138             }
139         }
140     }
141
142 }
143
144
Popular Tags