//-------------------------------------------------------------------------------
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.
// 
//-------------------------------------------------------------------------------

options {
  JAVA_UNICODE_ESCAPE=false;
  STATIC=false;
  DEBUG_PARSER=false;
  DEBUG_TOKEN_MANAGER=false;
  BUILD_PARSER=false;
}

// Parser is useless.
// Only the TokenManager is used to get useful objects (as Trailers) in the 
// order of appearance
PARSER_BEGIN(Extractor)

package org.apache.pdfbox.preflight.javacc.extractor;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
	
	public class Extractor {}
	PARSER_END(Extractor)
	
	TOKEN_MGR_DECLS: {
		private List<String> lTrailers = new ArrayList<String>(1);
		private StringBuilder aTrailer = null;
		private boolean alreadyParsed = false;
	
	    public static void main (String [] args) throws FileNotFoundException {
    	FileInputStream sr = new FileInputStream(args[0]);
    	SimpleCharStream scs = new SimpleCharStream(sr);
    	ExtractorTokenManager extractor = new ExtractorTokenManager(scs);
        
   		for (Token t = extractor.getNextToken();t.kind != EOF;t = extractor.getNextToken()) {
	    	System.out.println(t.image);
	    }

        for  (String s : extractor.lTrailers){
	        System.err.println(s);
        }
    }

	public void parse() {
		if (alreadyParsed) { return; }
		for (Token t = getNextToken();t.kind != EOF;t = getNextToken()) {

	    }
	    alreadyParsed = true;
	}

	public List<String> getAllTrailers() {
		return this.lTrailers;
	}
}

// --------------------------------------------------
// ---- COMMON TOKENS
// --------------------------------------------------

<DEFAULT, WithinTrailer> TOKEN :
{
	< PDF_EOF : "%%EOF" > |
	< EOL : (<CR>|<LF>|(<CR><LF>)) > |
	<#CR : "\r"> |
	<#LF : "\n"> 
}

// ---- Even if the "ISO190005 App Notes" says that only PDF-1.[1-4] should be alloewd,
//		the "ISO 190005-1:2005" says : "The version number in the header of a PDF file nor 
//		the value of the Version key in the document catalog dictionaryy shall be used in 
//		determining whether a file is in accordance with this part of ISO 190005 " 
TOKEN :
{
	< PERCENT: "%" > |
	< PDFA_HEADER: "PDF-1."["1"-"6"] >
}

MORE :
{
	< ~[] >
}

// --------------------------------------------------
// ---- TRAILER TOKENS
// --------------------------------------------------
TOKEN :
{
	< START_TRAILER : (<EOL>)"trailer"(<EOL>) > { aTrailer = new StringBuilder(50);} : WithinTrailer
}

<WithinTrailer> MORE :
{
	<TRAILER_DICT : ~[] > { aTrailer.append(image.charAt(image.length()-1));} 
}

<WithinTrailer> TOKEN : 
{
	< END_TRAILER : "startxref"(<EOL>)  > {  lTrailers.add(aTrailer.toString()); aTrailer = null; } : DEFAULT
}