📄 prtokeniser.java
字号:
/* * $Id: PRTokeniser.java,v 1.16 2002/11/19 08:33:35 blowagie Exp $ * $Name: $ * * Copyright 2001, 2002 by Paulo Soares. * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above. If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text.pdf;import java.io.*;/** * * @author Paulo Soares (psoares@consiste.pt) */public class PRTokeniser { public static final int TK_NUMBER = 1; public static final int TK_STRING = 2; public static final int TK_NAME = 3; public static final int TK_COMMENT = 4; public static final int TK_START_ARRAY = 5; public static final int TK_END_ARRAY = 6; public static final int TK_START_DIC = 7; public static final int TK_END_DIC = 8; public static final int TK_REF = 9; public static final int TK_OTHER = 10; static final String EMPTY = ""; protected RandomAccessFileOrArray file; protected int type; protected String stringValue; protected int reference; protected int generation; private static final int LINE_SEGMENT_SIZE = 256; public PRTokeniser(String filename) throws IOException { file = new RandomAccessFileOrArray(filename); } public PRTokeniser(byte pdfIn[]) { file = new RandomAccessFileOrArray(pdfIn); } public void seek(int pos) throws IOException { file.seek(pos); } public int getFilePointer() throws IOException { return file.getFilePointer(); } public void close() throws IOException { file.close(); } public int length() throws IOException { return file.length(); } public int read() throws IOException { return file.read(); } public RandomAccessFileOrArray getSafeFile() { return new RandomAccessFileOrArray(file); } public String readString(int size) throws IOException { StringBuffer buf = new StringBuffer(); int ch; while ((size--) > 0) { ch = file.read(); if (ch == -1) break; buf.append((char)ch); } return buf.toString(); } public static final boolean isWhitespace(int ch) { return (ch == 0 || ch == 9 || ch == 10 || ch == 12 || ch == 13 || ch == 32); } public static final boolean isDelimiter(int ch) { return (ch == '(' || ch == ')' || ch == '<' || ch == '>' || ch == '[' || ch == ']' || ch == '/' || ch == '%'); } public int getTokenType() { return type; } public String getStringValue() { return stringValue; } public int getReference() { return reference; } public int getGeneration() { return generation; } public void backOnePosition() throws IOException { file.seek(file.getFilePointer() - 1); } public void throwError(String error) throws IOException { throw new IOException(error + " at file pointer " + file.getFilePointer()); } public void checkPdfHeader() throws IOException { String str = readString(7); if (!str.equals("%PDF-1.")) throw new IOException("PDF header signature not found."); } public void checkFdfHeader() throws IOException { String str = readString(8); if (!str.equals("%FDF-1.2")) throw new IOException("FDF header signature not found."); } public int getStartxref() throws IOException { int size = Math.min(1024, file.length()); int pos = file.length() - size; file.seek(pos); String str = readString(1024); int idx = str.lastIndexOf("startxref"); if (idx < 0) throw new IOException("PDF startxref not found."); return pos + idx; } public static int getHex(int v) { if (v >= '0' && v <= '9') return v - '0'; if (v >= 'A' && v <= 'F') return v - 'A' + 10; if (v >= 'a' && v <= 'f') return v - 'a' + 10; return -1; } public void nextValidToken() throws IOException { int level = 0; String n1 = null; String n2 = null; int ptr = 0; while (nextToken()) { if (type == TK_COMMENT) continue; switch (level) { case 0: { if (type != TK_NUMBER) return; ptr = file.getFilePointer(); n1 = stringValue; ++level; break; } case 1: { if (type != TK_NUMBER) { file.seek(ptr); type = TK_NUMBER; stringValue = n1; return; } n2 = stringValue; ++level; break; } default: { if (type != TK_OTHER || !stringValue.equals("R")) { file.seek(ptr); type = TK_NUMBER; stringValue = n1; return; } type = TK_REF; reference = Integer.valueOf(n1).intValue(); generation = Integer.valueOf(n2).intValue(); return; } } } throwError("Unexpected end of file"); } public boolean nextToken() throws IOException { StringBuffer outBuf = null; stringValue = EMPTY; int ch = 0; do { ch = file.read(); } while (ch != -1 && isWhitespace(ch)); if (ch == -1) return false; switch (ch) { case '[': type = TK_START_ARRAY; break; case ']': type = TK_END_ARRAY; break; case '/': { outBuf = new StringBuffer(); type = TK_NAME; while (true) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -