/*****************************************************************************
 * 
 * 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.preflight;

import java.io.IOException;
import java.util.Collection;

import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.preflight.ValidationResult.ValidationError;
import org.apache.pdfbox.preflight.exception.ValidationException;
import org.apache.pdfbox.preflight.utils.ContextHelper;

public class PreflightDocument extends PDDocument
{

    protected ValidationResult result = new ValidationResult(true);

    protected PreflightConfiguration config;

    protected PreflightContext context;

    protected final Format specification;

    /**
     * Create an empty preflight document and load the default configuration for the given format.
     * 
     * @param format
     * @throws IOException
     */
    public PreflightDocument(Format format) throws IOException
    {
        this(format, null);
    }

    /**
     * Create a preflight document based on the COSDocument and load the default configuration for the given format.
     * 
     * @param doc
     * @param format
     */
    public PreflightDocument(COSDocument doc, Format format)
    {
        this(doc, format, null);
    }

    /**
     * Create an empty preflight document that will use the given configuration bean to process the validation. if the
     * configuration is null, a default configuration will be load using the given format.
     * 
     * @param format
     * @param cfg
     * @throws IOException
     */
    public PreflightDocument(Format format, PreflightConfiguration cfg) throws IOException
    {
        this(new COSDocument(), format, cfg);
    }

    /**
     * Create a preflight document based on the COSDocument that will use the given configuration bean to process the
     * validation. if the configuration is null, a default configuration will be load using the given format.
     * 
     * @param doc
     * @param format
     * @param cfg
     * @throws IOException
     */
    public PreflightDocument(COSDocument doc, Format format, PreflightConfiguration cfg)
    {
        super(doc);
        this.specification = format;
        this.config = cfg;
        if (this.config == null)
        {
            initConfiguration(format);
        }
    }

    private void initConfiguration(Format format)
    {
        switch (format)
        {
        default: // default is PDF/A1-b
            this.config = PreflightConfiguration.createPdfA1BConfiguration();
            break;
        }

    }

    public ValidationResult getResult()
    {
        return result;
    }

    public void setResult(ValidationResult _result)
    {
        if (this.result != null)
        {
            this.result.mergeResult(_result);
        }
        else if (_result != null)
        {
            this.result = _result;
        }
        else
        {
            this.result = new ValidationResult(true);
        }
    }

    public void addValidationError(ValidationError error)
    {
        if (error != null)
        {
            if (result == null)
            {
                this.result = new ValidationResult(error.isWarning());
            }
            this.result.addError(error);
        }
    }

    public PreflightContext getContext()
    {
        return this.context;
    }

    public void setContext(PreflightContext context)
    {
        this.context = context;
    }

    /**
     * Check that PDDocument is a valid file according to the format given during the object creation.
     * 
     * @throws ValidationException
     */
    public void validate() throws ValidationException
    {
        context.setConfig(config);
        Collection<String> processes = config.getProcessNames();
        for (String name : processes)
        {
            ContextHelper.validateElement(context, name);
        }
    }

    public Format getSpecification()
    {
        return specification;
    }

}

@@ -1395,23 +1395,21 @@
 %0A%0A    pr
-otec
+iva
 te
-d
  Validat
@@ -1454,39 +1454,37 @@
 t(true);%0A%0A    pr
-otec
+iva
 te
-d
  PreflightConfig
@@ -1506,23 +1506,21 @@
 %0A%0A    pr
-otec
+iva
 te
-d
  Preflig
@@ -1549,15 +1549,13 @@
   pr
-otec
+iva
 te
-d
  fin
@@ -2894,37 +2894,13 @@
 am c
-fg%0A     * @throws IOException
+onfig
 %0A   
@@ -2989,17 +2989,20 @@
 ration c
-f
+onfi
 g)%0A    %7B
@@ -3082,17 +3082,20 @@
 nfig = c
-f
+onfi
 g;%0A     
@@ -3552,17 +3552,16 @@
 nResult 
-_
 result)%0A
@@ -3645,17 +3645,16 @@
 eResult(
-_
 result);
@@ -3681,17 +3681,16 @@
 lse if (
-_
 result !
@@ -3737,9 +3737,8 @@
 t = 
-_
 resu

make fields that have accessors private; make field private that isn't used outside; rename variable