1 49 50 package com.lowagie.text.pdf; 51 52 import java.io.IOException ; 53 import java.util.ArrayList ; 54 58 public class PdfContentParser { 59 60 63 public static final int COMMAND_TYPE = 200; 64 67 private PRTokeniser tokeniser; 68 69 73 public PdfContentParser(PRTokeniser tokeniser) { 74 this.tokeniser = tokeniser; 75 } 76 77 86 public ArrayList parse(ArrayList ls) throws IOException { 87 if (ls == null) 88 ls = new ArrayList (); 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 104 public PRTokeniser getTokeniser() { 105 return this.tokeniser; 106 } 107 108 112 public void setTokeniser(PRTokeniser tokeniser) { 113 this.tokeniser = tokeniser; 114 } 115 116 121 public PdfDictionary readDictionary() throws IOException { 122 PdfDictionary dic = new PdfDictionary(); 123 while (true) { 124 if (!nextValidToken()) 125 throw new IOException ("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 ("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 ("Unexpected '>>'"); 135 if (-type == PRTokeniser.TK_END_ARRAY) 136 throw new IOException ("Unexpected ']'"); 137 dic.put(name, obj); 138 } 139 return dic; 140 } 141 142 147 public PdfArray readArray() throws IOException { 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 ("Unexpected '>>'"); 156 array.add(obj); 157 } 158 return array; 159 } 160 161 166 public PdfObject readPRObject() throws IOException { 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 196 public boolean nextValidToken() throws IOException { 197 while (tokeniser.nextToken()) { 198 if (tokeniser.getTokenType() == PRTokeniser.TK_COMMENT) 199 continue; 200 return true; 201 } 202 return false; 203 } 204 } | Popular Tags |