KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfContentParser


1 /*
2  * $Id: PdfContentParser.java 2382 2006-09-15 23:37:38Z xlv $
3  *
4  * Copyright 2005 by Paulo Soares.
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49
50 package com.lowagie.text.pdf;
51
52 import java.io.IOException JavaDoc;
53 import java.util.ArrayList JavaDoc;
54 /**
55  * Parses the page or template content.
56  * @author Paulo Soares (psoares@consiste.pt)
57  */

58 public class PdfContentParser {
59     
60     /**
61      * Commands have this type.
62      */

63     public static final int COMMAND_TYPE = 200;
64     /**
65      * Holds value of property tokeniser.
66      */

67     private PRTokeniser tokeniser;
68     
69     /**
70      * Creates a new instance of PdfContentParser
71      * @param tokeniser the tokeniser with the content
72      */

73     public PdfContentParser(PRTokeniser tokeniser) {
74         this.tokeniser = tokeniser;
75     }
76     
77     /**
78      * Parses a single command from the content. Each command is output as an array of arguments
79      * having the command itself as the last element. The returned array will be empty if the
80      * end of content was reached.
81      * @param ls an <CODE>ArrayList</CODE> to use. It will be cleared before using. If it's
82      * <CODE>null</CODE> will create a new <CODE>ArrayList</CODE>
83      * @return the same <CODE>ArrayList</CODE> given as argument or a new one
84      * @throws IOException on error
85      */

86     public ArrayList JavaDoc parse(ArrayList JavaDoc ls) throws IOException JavaDoc {
87         if (ls == null)
88             ls = new ArrayList JavaDoc();
89         else
90             ls.clear();
91         PdfObject ob = null;
92         while ((ob = readPRObject()) != null) {
93             ls.add(ob);
94             if (ob.type() == COMMAND_TYPE)
95                 break;
96         }
97         return ls;
98     }
99     
100     /**
101      * Gets the tokeniser.
102      * @return the tokeniser.
103      */

104     public PRTokeniser getTokeniser() {
105         return this.tokeniser;
106     }
107     
108     /**
109      * Sets the tokeniser.
110      * @param tokeniser the tokeniser
111      */

112     public void setTokeniser(PRTokeniser tokeniser) {
113         this.tokeniser = tokeniser;
114     }
115     
116     /**
117      * Reads a dictionary. The tokeniser must be positioned past the "&lt;&lt;" token.
118      * @return the dictionary
119      * @throws IOException on error
120      */

121     public PdfDictionary readDictionary() throws IOException JavaDoc {
122         PdfDictionary dic = new PdfDictionary();
123         while (true) {
124             if (!nextValidToken())
125                 throw new IOException JavaDoc("Unexpected end of file.");
126                 if (tokeniser.getTokenType() == PRTokeniser.TK_END_DIC)
127                     break;
128                 if (tokeniser.getTokenType() != PRTokeniser.TK_NAME)
129                     throw new IOException JavaDoc("Dictionary key is not a name.");
130                 PdfName name = new PdfName(tokeniser.getStringValue(), false);
131                 PdfObject obj = readPRObject();
132                 int type = obj.type();
133                 if (-type == PRTokeniser.TK_END_DIC)
134                     throw new IOException JavaDoc("Unexpected '>>'");
135                 if (-type == PRTokeniser.TK_END_ARRAY)
136                     throw new IOException JavaDoc("Unexpected ']'");
137                 dic.put(name, obj);
138         }
139         return dic;
140     }
141     
142     /**
143      * Reads an array. The tokeniser must be positioned past the "[" token.
144      * @return an array
145      * @throws IOException on error
146      */

147     public PdfArray readArray() throws IOException JavaDoc {
148         PdfArray array = new PdfArray();
149         while (true) {
150             PdfObject obj = readPRObject();
151             int type = obj.type();
152             if (-type == PRTokeniser.TK_END_ARRAY)
153                 break;
154             if (-type == PRTokeniser.TK_END_DIC)
155                 throw new IOException JavaDoc("Unexpected '>>'");
156             array.add(obj);
157         }
158         return array;
159     }
160     
161     /**
162      * Reads a pdf object.
163      * @return the pdf object
164      * @throws IOException on error
165      */

166     public PdfObject readPRObject() throws IOException JavaDoc {
167         if (!nextValidToken())
168             return null;
169         int type = tokeniser.getTokenType();
170         switch (type) {
171             case PRTokeniser.TK_START_DIC: {
172                 PdfDictionary dic = readDictionary();
173                 return dic;
174             }
175             case PRTokeniser.TK_START_ARRAY:
176                 return readArray();
177             case PRTokeniser.TK_STRING:
178                 PdfString str = new PdfString(tokeniser.getStringValue(), null).setHexWriting(tokeniser.isHexString());
179                 return str;
180             case PRTokeniser.TK_NAME:
181                 return new PdfName(tokeniser.getStringValue(), false);
182             case PRTokeniser.TK_NUMBER:
183                 return new PdfNumber(tokeniser.getStringValue());
184             case PRTokeniser.TK_OTHER:
185                 return new PdfLiteral(COMMAND_TYPE, tokeniser.getStringValue());
186             default:
187                 return new PdfLiteral(-type, tokeniser.getStringValue());
188         }
189     }
190     
191     /**
192      * Reads the next token skipping over the comments.
193      * @return <CODE>true</CODE> if a token was read, <CODE>false</CODE> if the end of content was reached
194      * @throws IOException on error
195      */

196     public boolean nextValidToken() throws IOException JavaDoc {
197         while (tokeniser.nextToken()) {
198             if (tokeniser.getTokenType() == PRTokeniser.TK_COMMENT)
199                 continue;
200             return true;
201         }
202         return false;
203     }
204 }
Popular Tags