Method from org.apache.pdfbox.pdfparser.BaseParser Detail: |
protected boolean isClosing() throws IOException {
return isClosing(pdfSource.peek());
}
This will tell if the next character is a closing brace( close of PDF array ). |
protected boolean isClosing(int c) {
return c == ']';
}
This will tell if the next character is a closing brace( close of PDF array ). |
protected boolean isEOL() throws IOException {
return isEOL(pdfSource.peek());
}
This will tell if the next byte to be read is an end of line byte. |
protected boolean isEOL(int c) {
return c == 10 || c == 13;
}
This will tell if the next byte to be read is an end of line byte. |
protected boolean isEndOfName(char ch) {
return (ch == ' ' || ch == 13 || ch == 10 || ch == 9 || ch == ' >' || ch == '< '
|| ch == '[' || ch =='/' || ch ==']' || ch ==')' || ch =='(' ||
ch == -1 //EOF
);
}
Determine if a character terminates a PDF name. |
protected boolean isWhitespace() throws IOException {
return isWhitespace( pdfSource.peek() );
}
This will tell if the next byte is whitespace or not. |
protected boolean isWhitespace(int c) {
return c == 0 || c == 9 || c == 12 || c == 10
|| c == 13 || c == 32;
}
This will tell if the next byte is whitespace or not. |
protected COSBoolean parseBoolean() throws IOException {
COSBoolean retval = null;
char c = (char)pdfSource.peek();
if( c == 't' )
{
String trueString = new String( pdfSource.readFully( 4 ) );
if( !trueString.equals( "true" ) )
{
throw new IOException( "Error parsing boolean: expected='true' actual='" + trueString + "'" );
}
else
{
retval = COSBoolean.TRUE;
}
}
else if( c == 'f' )
{
String falseString = new String( pdfSource.readFully( 5 ) );
if( !falseString.equals( "false" ) )
{
throw new IOException( "Error parsing boolean: expected='true' actual='" + falseString + "'" );
}
else
{
retval = COSBoolean.FALSE;
}
}
else
{
throw new IOException( "Error parsing boolean expected='t or f' actual='" + c + "'" );
}
return retval;
}
This will parse a boolean object from the stream. |
protected COSArray parseCOSArray() throws IOException {
char ch = (char)pdfSource.read();
if( ch != '[')
{
throw new IOException( "expected='[' actual='" + ch + "'" );
}
COSArray po = new COSArray();
COSBase pbo = null;
skipSpaces();
int i = 0;
while( ((i = pdfSource.peek()) > 0) && ((char)i != ']') )
{
pbo = parseDirObject();
if( pbo instanceof COSObject )
{
// We have to check if the expected values are there or not PDFBOX-385
if (po.get(po.size()-1) instanceof COSInteger)
{
COSInteger genNumber = (COSInteger)po.remove( po.size() -1 );
if (po.get(po.size()-1) instanceof COSInteger)
{
COSInteger number = (COSInteger)po.remove( po.size() -1 );
COSObjectKey key = new COSObjectKey(number.intValue(), genNumber.intValue());
pbo = document.getObjectFromPool(key);
}
else
{
// the object reference is somehow wrong
pbo = null;
}
}
else
{
pbo = null;
}
}
if( pbo != null )
{
po.add( pbo );
}
else
{
log.warn("Corrupt object reference" );
//it could be a bad object in the array which is just skipped
}
skipSpaces();
}
pdfSource.read(); //read ']'
skipSpaces();
return po;
}
This will parse a PDF array object. |
protected COSDictionary parseCOSDictionary() throws IOException {
char c = (char)pdfSource.read();
if( c != '< ')
{
throw new IOException( "expected='< ' actual='" + c + "'" );
}
c = (char)pdfSource.read();
if( c != '< ')
{
throw new IOException( "expected='< ' actual='" + c + "' " + pdfSource );
}
skipSpaces();
COSDictionary obj = new COSDictionary();
boolean done = false;
while( !done )
{
skipSpaces();
c = (char)pdfSource.peek();
if( c == ' >')
{
done = true;
}
else
if(c != '/')
{
//an invalid dictionary, we are expecting
//the key, read until we can recover
log.warn("Invalid dictionary, found:" + (char)c + " but expected:\''");
int read = pdfSource.read();
while(read != -1 && read != '/' && read != ' >')
{
read = pdfSource.read();
}
if(read != -1)
{
pdfSource.unread(read);
}
else
{
return obj;
}
}
else
{
COSName key = parseCOSName();
COSBase value = parseCOSDictionaryValue();
skipSpaces();
if( ((char)pdfSource.peek()) == 'd' )
{
//if the next string is 'def' then we are parsing a cmap stream
//and want to ignore it, otherwise throw an exception.
String potentialDEF = readString();
if( !potentialDEF.equals( DEF ) )
{
pdfSource.unread( potentialDEF.getBytes() );
}
else
{
skipSpaces();
}
}
if( value == null )
{
log.warn("Bad Dictionary Declaration " + pdfSource );
}
else
{
obj.setItem( key, value );
}
}
}
char ch = (char)pdfSource.read();
if( ch != ' >' )
{
throw new IOException( "expected=' >' actual='" + ch + "'" );
}
ch = (char)pdfSource.read();
if( ch != ' >' )
{
throw new IOException( "expected=' >' actual='" + ch + "'" );
}
return obj;
}
This will parse a PDF dictionary. |
protected COSName parseCOSName() throws IOException {
COSName retval = null;
int c = pdfSource.read();
if( (char)c != '/')
{
throw new IOException("expected='/' actual='" + (char)c + "'-" + c + " " + pdfSource );
}
// costruisce il nome
StringBuffer buffer = new StringBuffer();
c = pdfSource.read();
while( c != -1 )
{
char ch = (char)c;
if(ch == '#')
{
char ch1 = (char)pdfSource.read();
char ch2 = (char)pdfSource.read();
// Prior to PDF v1.2, the # was not a special character. Also,
// it has been observed that various PDF tools do not follow the
// spec with respect to the # escape, even though they report
// PDF versions of 1.2 or later. The solution here is that we
// interpret the # as an escape only when it is followed by two
// valid hex digits.
//
if (isHexDigit(ch1) && isHexDigit(ch2))
{
String hex = "" + ch1 + ch2;
try
{
buffer.append( (char) Integer.parseInt(hex, 16));
}
catch (NumberFormatException e)
{
throw new IOException("Error: expected hex number, actual='" + hex + "'");
}
c = pdfSource.read();
}
else
{
pdfSource.unread(ch2);
c = ch1;
buffer.append( ch );
}
}
else if (isEndOfName(ch))
{
break;
}
else
{
buffer.append( ch );
c = pdfSource.read();
}
}
if (c != -1)
{
pdfSource.unread(c);
}
retval = COSName.getPDFName( buffer.toString() );
return retval;
}
This will parse a PDF name from the stream. |
protected COSStream parseCOSStream(COSDictionary dic,
RandomAccess file) throws IOException {
COSStream stream = new COSStream( dic, file );
OutputStream out = null;
try
{
String streamString = readString();
//long streamLength;
if (!streamString.equals("stream"))
{
throw new IOException("expected='stream' actual='" + streamString + "'");
}
//PDF Ref 3.2.7 A stream must be followed by either
//a CRLF or LF but nothing else.
int whitespace = pdfSource.read();
//see brother_scan_cover.pdf, it adds whitespaces
//after the stream but before the start of the
//data, so just read those first
while (whitespace == 0x20)
{
whitespace = pdfSource.read();
}
if( whitespace == 0x0D )
{
whitespace = pdfSource.read();
if( whitespace != 0x0A )
{
pdfSource.unread( whitespace );
//The spec says this is invalid but it happens in the real
//world so we must support it.
}
}
else if (whitespace == 0x0A)
{
//that is fine
}
else
{
//we are in an error.
//but again we will do a lenient parsing and just assume that everything
//is fine
pdfSource.unread( whitespace );
}
/*This needs to be dic.getItem because when we are parsing, the underlying object
* might still be null.
*/
COSBase streamLength = dic.getItem(COSName.LENGTH);
//Need to keep track of the
out = stream.createFilteredStream( streamLength );
String endStream = null;
readUntilEndStream(out);
skipSpaces();
endStream = readString();
if (!endStream.equals("endstream"))
{
/*
* Sometimes stream objects don't have an endstream tag so readUntilEndStream(out)
* also can stop on endobj tags. If that's the case we need to make sure to unread
* the endobj so parseObject() can handle that case normally.
*/
if (endStream.startsWith("endobj"))
{
byte[] endobjarray = endStream.getBytes();
pdfSource.unread(endobjarray);
}
/*
* Some PDF files don't contain a new line after endstream so we
* need to make sure that the next object number is getting read separately
* and not part of the endstream keyword. Ex. Some files would have "endstream8"
* instead of "endstream"
*/
else if(endStream.startsWith("endstream"))
{
String extra = endStream.substring(9, endStream.length());
endStream = endStream.substring(0, 9);
byte[] array = extra.getBytes();
pdfSource.unread(array);
}
else
{
/*
* If for some reason we get something else here, Read until we find the next
* "endstream"
*/
readUntilEndStream( out );
endStream = readString();
if( !endStream.equals( "endstream" ) )
{
throw new IOException("expected='endstream' actual='" + endStream + "' " + pdfSource);
}
}
}
}
finally
{
if( out != null )
{
out.close();
}
}
return stream;
}
This will read a COSStream from the input stream. |
protected COSString parseCOSString() throws IOException {
char nextChar = (char)pdfSource.read();
COSString retval = new COSString();
char openBrace;
char closeBrace;
if( nextChar == '(' )
{
openBrace = '(';
closeBrace = ')';
}
else if( nextChar == '< ' )
{
openBrace = '< ';
closeBrace = ' >';
}
else
{
throw new IOException( "parseCOSString string should start with '(' or '< ' and not '" +
nextChar + "' " + pdfSource );
}
//This is the number of braces read
//
int braces = 1;
int c = pdfSource.read();
while( braces > 0 && c != -1)
{
char ch = (char)c;
int nextc = -2; // not yet read
//if( log.isDebugEnabled() )
//{
// log.debug( "Parsing COSString character '" + c + "' code=" + (int)c );
//}
if(ch == closeBrace)
{
braces--;
byte[] nextThreeBytes = new byte[3];
int amountRead = pdfSource.read(nextThreeBytes);
//lets handle the special case seen in Bull River Rules and Regulations.pdf
//The dictionary looks like this
// 2 0 obj
// < <
// /Type /Info
// /Creator (PaperPort http://www.scansoft.com)
// /Producer (sspdflib 1.0 http://www.scansoft.com)
// /Title ( (5)
// /Author ()
// /Subject ()
//
// Notice the /Title, the braces are not even but they should
// be. So lets assume that if we encounter an this scenario
// < end_brace >< new_line >< opening_slash > then that
// means that there is an error in the pdf and assume that
// was the end of the document.
if( amountRead == 3 )
{
if( nextThreeBytes[0] == 0x0d &&
nextThreeBytes[1] == 0x0a &&
nextThreeBytes[2] == 0x2f )
{
braces = 0;
}
}
if (amountRead > 0)
{
pdfSource.unread( nextThreeBytes, 0, amountRead );
}
if( braces != 0 )
{
retval.append( ch );
}
}
else if( ch == openBrace )
{
braces++;
retval.append( ch );
}
else if( ch == '\\' )
{
//patched by ram
char next = (char)pdfSource.read();
switch(next)
{
case 'n':
retval.append( '\n' );
break;
case 'r':
retval.append( '\r' );
break;
case 't':
retval.append( '\t' );
break;
case 'b':
retval.append( '\b' );
break;
case 'f':
retval.append( '\f' );
break;
case '(':
case ')':
case '\\':
retval.append( next );
break;
case 10:
case 13:
//this is a break in the line so ignore it and the newline and continue
c = pdfSource.read();
while( isEOL(c) && c != -1)
{
c = pdfSource.read();
}
nextc = c;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
{
StringBuffer octal = new StringBuffer();
octal.append( next );
c = pdfSource.read();
char digit = (char)c;
if( digit >= '0' && digit < = '7' )
{
octal.append( digit );
c = pdfSource.read();
digit = (char)c;
if( digit >= '0' && digit < = '7' )
{
octal.append( digit );
}
else
{
nextc = c;
}
}
else
{
nextc = c;
}
int character = 0;
try
{
character = Integer.parseInt( octal.toString(), 8 );
}
catch( NumberFormatException e )
{
throw new IOException( "Error: Expected octal character, actual='" + octal + "'" );
}
retval.append( character );
break;
}
default:
{
retval.append( '\\' );
retval.append( next );
//another ficken problem with PDF's, sometimes the \ doesn't really
//mean escape like the PDF spec says it does, sometimes is should be literal
//which is what we will assume here.
//throw new IOException( "Unexpected break sequence '" + next + "' " + pdfSource );
}
}
}
else
{
if( openBrace == '< ' )
{
if( isHexDigit(ch) )
{
retval.append( ch );
}
}
else
{
retval.append( ch );
}
}
if (nextc != -2)
{
c = nextc;
}
else
{
c = pdfSource.read();
}
}
if (c != -1)
{
pdfSource.unread(c);
}
if( openBrace == '< ' )
{
retval = COSString.createFromHexString( retval.getString() );
}
return retval;
}
This will parse a PDF string. |
protected COSBase parseDirObject() throws IOException {
COSBase retval = null;
skipSpaces();
int nextByte = pdfSource.peek();
char c = (char)nextByte;
switch(c)
{
case '< ':
{
int leftBracket = pdfSource.read();//pull off first left bracket
c = (char)pdfSource.peek(); //check for second left bracket
pdfSource.unread( leftBracket );
if(c == '< ')
{
retval = parseCOSDictionary();
skipSpaces();
}
else
{
retval = parseCOSString();
}
break;
}
case '[': // array
{
retval = parseCOSArray();
break;
}
case '(':
retval = parseCOSString();
break;
case '/': // name
retval = parseCOSName();
break;
case 'n': // null
{
String nullString = readString();
if( !nullString.equals( "null") )
{
throw new IOException("Expected='null' actual='" + nullString + "'");
}
retval = COSNull.NULL;
break;
}
case 't':
{
String trueString = new String( pdfSource.readFully(4) );
if( trueString.equals( "true" ) )
{
retval = COSBoolean.TRUE;
}
else
{
throw new IOException( "expected true actual='" + trueString + "' " + pdfSource );
}
break;
}
case 'f':
{
String falseString = new String( pdfSource.readFully(5) );
if( falseString.equals( "false" ) )
{
retval = COSBoolean.FALSE;
}
else
{
throw new IOException( "expected false actual='" + falseString + "' " + pdfSource );
}
break;
}
case 'R':
pdfSource.read();
retval = new COSObject(null);
break;
case (char)-1:
return null;
default:
{
if( Character.isDigit(c) || c == '-' || c == '+' || c == '.')
{
StringBuffer buf = new StringBuffer();
int ic = pdfSource.read();
c = (char)ic;
while( Character.isDigit( c )||
c == '-' ||
c == '+' ||
c == '.' ||
c == 'E' ||
c == 'e' )
{
buf.append( c );
ic = pdfSource.read();
c = (char)ic;
}
if( ic != -1 )
{
pdfSource.unread( ic );
}
retval = COSNumber.get( buf.toString() );
}
else
{
//This is not suppose to happen, but we will allow for it
//so we are more compatible with POS writers that don't
//follow the spec
String badString = readString();
//throw new IOException( "Unknown dir object c='" + c +
//"' peek='" + (char)pdfSource.peek() + "' " + pdfSource );
if( badString == null || badString.length() == 0 )
{
int peek = pdfSource.peek();
// we can end up in an infinite loop otherwise
throw new IOException( "Unknown dir object c='" + c +
"' cInt=" + (int)c + " peek='" + (char)peek + "' peekInt=" + peek + " " + pdfSource );
}
}
}
}
return retval;
}
This will parse a directory object from the stream. |
protected String readExpectedString(String theString) throws IOException {
int c = pdfSource.read();
while( isWhitespace(c) && c != -1)
{
c = pdfSource.read();
}
StringBuffer buffer = new StringBuffer( theString.length() );
int charsRead = 0;
while( !isEOL(c) && c != -1 && charsRead < theString.length() )
{
char next = (char)c;
buffer.append( next );
if( theString.charAt( charsRead ) == next )
{
charsRead++;
}
else
{
pdfSource.unread(buffer.toString().getBytes());
throw new IOException( "Error: Expected to read '" + theString +
"' instead started reading '" +buffer.toString() + "'" );
}
c = pdfSource.read();
}
while( isEOL(c) && c != -1 )
{
c = pdfSource.read();
}
if (c != -1)
{
pdfSource.unread(c);
}
return buffer.toString();
}
This will read bytes until the end of line marker occurs. |
protected int readInt() throws IOException {
skipSpaces();
int retval = 0;
int lastByte = 0;
StringBuffer intBuffer = new StringBuffer();
while( (lastByte = pdfSource.read() ) != 32 &&
lastByte != 10 &&
lastByte != 13 &&
lastByte != 60 && //see sourceforge bug 1714707
lastByte != 0 && //See sourceforge bug 853328
lastByte != -1 )
{
intBuffer.append( (char)lastByte );
}
if( lastByte != -1 )
{
pdfSource.unread( lastByte );
}
try
{
retval = Integer.parseInt( intBuffer.toString() );
}
catch( NumberFormatException e )
{
pdfSource.unread(intBuffer.toString().getBytes());
throw new IOException( "Error: Expected an integer type, actual='" + intBuffer + "'" );
}
return retval;
}
This will read an integer from the stream. |
protected String readLine() throws IOException {
if (pdfSource.isEOF())
{
throw new IOException( "Error: End-of-File, expected line");
}
StringBuffer buffer = new StringBuffer( 11 );
int c;
while ((c = pdfSource.read()) != -1)
{
if (isEOL(c))
{
break;
}
buffer.append( (char)c );
}
return buffer.toString();
}
This will read bytes until the first end of line marker occurs.
Note: if you later unread the results of this function, you'll
need to add a newline character to the end of the string. |
protected String readString() throws IOException {
skipSpaces();
StringBuffer buffer = new StringBuffer();
int c = pdfSource.read();
while( !isEndOfName((char)c) && !isClosing(c) && c != -1 )
{
buffer.append( (char)c );
c = pdfSource.read();
}
if (c != -1)
{
pdfSource.unread(c);
}
return buffer.toString();
}
This will read the next string from the stream. |
protected String readString(int length) throws IOException {
skipSpaces();
int c = pdfSource.read();
//average string size is around 2 and the normal string buffer size is
//about 16 so lets save some space.
StringBuffer buffer = new StringBuffer(length);
while( !isWhitespace(c) && !isClosing(c) && c != -1 && buffer.length() < length &&
c != '[' &&
c != '< ' &&
c != '(' &&
c != '/' )
{
buffer.append( (char)c );
c = pdfSource.read();
}
if (c != -1)
{
pdfSource.unread(c);
}
return buffer.toString();
}
This will read the next string from the stream up to a certain length. |
public void setDocument(COSDocument doc) {
document = doc;
}
Set the document for this stream. |
protected void skipSpaces() throws IOException {
//log( "skipSpaces() " + pdfSource );
int c = pdfSource.read();
// identical to, but faster as: isWhiteSpace(c) || c == 37
while(c == 0 || c == 9 || c == 12 || c == 10
|| c == 13 || c == 32 || c == 37)//37 is the % character, a comment
{
if ( c == 37 )
{
// skip past the comment section
c = pdfSource.read();
while(!isEOL(c) && c != -1)
{
c = pdfSource.read();
}
}
else
{
c = pdfSource.read();
}
}
if (c != -1)
{
pdfSource.unread(c);
}
//log( "skipSpaces() done peek='" + (char)pdfSource.peek() + "'" );
}
This will skip all spaces and comments that are present. |