/*
 * 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.
 */
package org.apache.pdfbox.examples;

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.input.DefaultPDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.junit.Test;

/**
 * @author Andrea Vacondio
 *
 */
public class TestParse
{
    private static final String FILEPATH = "/home/torakiki/Scaricati/Responsive Web Design by Example [eBook].pdf";
    private static final String ENC_FILEPATH = "/home/torakiki/repos/sejda/sejda-core/src/test/resources/pdf/enc_usr_own_same_pwd.pdf";
    @Test
    public void testParse() throws IOException
    {
        File file = new File(FILEPATH);
        PDDocument document = DefaultPDFParser.parse(file);
        document.writeTo("/home/torakiki/Scrivania/test_incremental_xref/Responsive_sambox.pdf");
    }

    @Test
    public void testParseEnc() throws IOException
    {
        File file = new File(ENC_FILEPATH);
        PDDocument document = DefaultPDFParser.parse(file, "test");
        print(document);
    }

    @Test(expected = InvalidPasswordException.class)
    public void testWrongParseEnc() throws IOException
    {
        File file = new File(ENC_FILEPATH);
        PDDocument document = DefaultPDFParser.parse(file, "banana");
        print(document);
    }

    private void print(PDDocument document)
    {
        PDDocumentCatalog catalog = document.getDocumentCatalog();
        System.out.println("Got catalog");
        PDPageTree pages = catalog.getPages();
        for (PDPage page : pages)
        {
            page.getCOSObject();
            System.out.println(page.getMediaBox());
        }
    }

}

@@ -914,23 +914,16 @@
 x.input.
-Default
 PDFParse
@@ -954,119 +954,81 @@
 box.
-pdmodel.PDDocument;%0Aimport org.apache.pdfbox.pdmodel.PDDocumentCatalog;%0Aimport org.apache.pdfbox.pdmodel.PDPage
+input.source.SeekableSources;%0Aimport org.apache.pdfbox.output.WriteOption
 ;%0Aim
@@ -1060,24 +1060,24 @@
 model.PD
-PageTree
+Document
 ;%0Aimport
@@ -1292,56 +1292,56 @@
 i/Sc
-aricati/Responsive Web Design by Example %5BeBook%5D
+rivania/test_incremental_xref/Responsive_sambox3
 .pdf
@@ -1475,24 +1475,25 @@
 e_pwd.pdf%22;%0A
+%0A
     @Test%0A  
@@ -1583,32 +1583,37 @@
 EPATH);%0A        
+try (
 PDDocument docum
@@ -1610,39 +1610,32 @@
 ment document = 
-Default
 PDFParser.parse(
@@ -1638,15 +1638,65 @@
 rse(
-file);%0A
+SeekableSources.seekableSourceFrom(file)))%0A        %7B%0A    
     
@@ -1716,16 +1716,37 @@
 writeTo(
+%0A                    
 %22/home/t
@@ -1806,15 +1806,101 @@
 mbox
+5
 .pdf%22
-);
+,%0A                    WriteOption.XREF_STREAM, WriteOption.SYNC_BODY_WRITE);%0A        %7D%0A
 %0A   
@@ -2013,32 +2013,37 @@
 EPATH);%0A        
+try (
 PDDocument docum
@@ -2040,39 +2040,32 @@
 ment document = 
-Default
 PDFParser.parse(
@@ -2057,27 +2057,80 @@
 DFParser
-.parse
+%0A                .parse(SeekableSources.seekableSourceFrom
 (file
+)
 , %22test%22
@@ -2134,34 +2134,124 @@
 st%22)
-;%0A        print(document);
+)%0A        %7B%0A            document.writeTo(%22/home/torakiki/Scrivania/test_incremental_xref/enc_sambox.pdf%22);%0A        %7D
 %0A   
@@ -2412,32 +2412,37 @@
 EPATH);%0A        
+try (
 PDDocument docum
@@ -2451,15 +2451,8 @@
 t = 
-Default
 PDFP
@@ -2460,397 +2460,185 @@
 rser
-.parse(file, %22banana%22);%0A        print(document);%0A
+%0A        
     
-%7D%0A%0A
     
-private void print(PDDocument document)%0A    %7B%0A        PDDocumentCatalog catalog = document.getDocumentCatalog();%0A        System.out.println(%22Got catalog%22);%0A        PDPageTree pages = catalog.getPages();%0A        for (PDPage page : pages)%0A        %7B%0A            page.getCOSObject();%0A            System.out.println(page.getMediaBox()
+.parse(SeekableSources.seekableSourceFrom(file), %22test%22))%0A        %7B%0A            document.writeTo(%22/home/torakiki/Scrivania/test_incremental_xref/enc_sambox.pdf%22
 );%0A 

Test for parse. To be removed.