You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2011/07/24 16:02:33 UTC
svn commit: r1150373 [8/12] - in /pdfbox/trunk/preflight: ./ src/ src/main/
src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/padaf/ src/main/java/org/apache/padaf/preflight/
src/main/java/org/apache/padaf/preflight/a...
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjImageValidator.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjImageValidator.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjImageValidator.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjImageValidator.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,162 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.graphics;
+
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_MISSING_FIELD;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_UNEXPECTED_KEY;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY;
+import static org.apache.padaf.preflight.ValidationConstants.XOBJECT_DICTIONARY_KEY_COLOR_SPACE;
+
+import java.util.List;
+
+
+import org.apache.padaf.preflight.DocumentHandler;
+import org.apache.padaf.preflight.ValidationConstants;
+import org.apache.padaf.preflight.ValidationException;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+import org.apache.padaf.preflight.graphics.color.ColorSpaceHelper;
+import org.apache.padaf.preflight.graphics.color.ColorSpaceHelperFactory;
+import org.apache.padaf.preflight.graphics.color.ColorSpaceHelperFactory.ColorSpaceRestriction;
+import org.apache.padaf.preflight.utils.COSUtils;
+import org.apache.padaf.preflight.utils.RenderingIntents;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSBoolean;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSStream;
+
+/**
+ * This class validates XObject with the Image subtype.
+ */
+public class XObjImageValidator extends AbstractXObjValidator {
+
+ public XObjImageValidator(DocumentHandler handler, COSStream xobj) {
+ super(handler, xobj);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seenet.awl.edoc.pdfa.validation.graphics.AbstractXObjValidator#
+ * checkMandatoryFields(java.util.List)
+ */
+ @Override
+ protected boolean checkMandatoryFields(List<ValidationError> result) {
+ boolean res = this.xobject.getItem(COSName.getPDFName("Width")) != null;
+ res = res && this.xobject.getItem(COSName.getPDFName("Height")) != null;
+ // type and subtype checked before to create the Validator.
+ if (!res) {
+ result.add(new ValidationError(ERROR_GRAPHIC_MISSING_FIELD));
+ }
+ return res;
+ }
+
+ /*
+ * 6.2.4 no Alternates
+ */
+ protected void checkAlternates(List<ValidationError> result) throws ValidationException {
+ if (this.xobject.getItem(COSName.getPDFName("Alternates")) != null) {
+ result.add(new ValidationError(
+ ValidationConstants.ERROR_GRAPHIC_UNEXPECTED_KEY,
+ "Unexpected 'Alternates' Key"));
+ }
+ }
+
+ /*
+ * 6.2.4 if interpolates, value = false
+ */
+ protected void checkInterpolate(List<ValidationError> result) throws ValidationException {
+ if (this.xobject.getItem(COSName.getPDFName("Interpolate")) != null) {
+ if (this.xobject.getBoolean(COSName.getPDFName("Interpolate"), true)) {
+ result.add(new ValidationError(
+ ValidationConstants.ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY,
+ "Unexpected 'true' value for 'Interpolate' Key"));
+ }
+ }
+ }
+
+ /*
+ * 6.2.4 Intent has specific values
+ */
+ protected void checkIntent(List<ValidationError> result) throws ValidationException {
+ if (this.xobject.getItem(COSName.getPDFName("Intent")) != null) {
+ String s = this.xobject.getNameAsString("Intent");
+ if (!RenderingIntents.contains(s)) {
+ result.add(new ValidationError(ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY,
+ "Unexpected value '" + s + "' for Intent key in image"));
+ }
+ }
+ }
+
+ /*
+ * According to the PDF Reference file, there are some specific rules on following fields
+ * ColorSpace, Mask, ImageMask and BitsPerComponent.
+ * If ImageMask is set to true, ColorSpace and Mask entries are forbidden.
+ */
+ protected void checkColorSpaceAndImageMask(List<ValidationError> result) throws ValidationException {
+
+ COSBase csImg = this.xobject.getItem(COSName.getPDFName(XOBJECT_DICTIONARY_KEY_COLOR_SPACE));
+ COSBase bitsPerComp = this.xobject.getItem(COSName.getPDFName("BitsPerComponent"));
+ COSBase mask = this.xobject.getItem(COSName.getPDFName("Mask"));
+
+ if (isImageMaskTrue()) {
+ if ( csImg != null || mask != null) {
+ result.add(new ValidationError(ERROR_GRAPHIC_UNEXPECTED_KEY, "ImageMask entry is true, ColorSpace and Mask are forbidden."));
+ }
+
+ Integer bitsPerCompValue = COSUtils.getAsInteger(bitsPerComp, cosDocument);
+ if (bitsPerCompValue != 1 ) {
+ result.add(new ValidationError(ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY, "ImageMask entry is true, BitsPerComponent must be 1."));
+ }
+
+ } else {
+ ColorSpaceHelper csh = ColorSpaceHelperFactory.getColorSpaceHelper(csImg, handler, ColorSpaceRestriction.NO_PATTERN);
+ csh.validate(result);
+ }
+ }
+
+ private boolean isImageMaskTrue() {
+ COSBase imgMask = this.xobject.getItem(COSName.getPDFName("ImageMask"));
+ if (imgMask != null && imgMask instanceof COSBoolean) {
+ return ((COSBoolean) imgMask).getValue();
+ } else {
+ return false;
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.awl.edoc.pdfa.validation.graphics.AbstractXObjValidator#validate()
+ */
+ @Override
+ public List<ValidationError> validate() throws ValidationException {
+ List<ValidationError> result = super.validate();
+
+ checkAlternates(result);
+ checkInterpolate(result);
+ checkIntent(result);
+
+ checkColorSpaceAndImageMask(result);
+
+ return result;
+ }
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjImageValidator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjPostscriptValidator.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjPostscriptValidator.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjPostscriptValidator.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjPostscriptValidator.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,61 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.graphics;
+
+import java.util.List;
+
+
+import org.apache.padaf.preflight.DocumentHandler;
+import org.apache.padaf.preflight.ValidationException;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+import org.apache.pdfbox.cos.COSStream;
+
+public class XObjPostscriptValidator extends AbstractXObjValidator {
+
+ public XObjPostscriptValidator(DocumentHandler handler, COSStream xobj) {
+ super(handler, xobj);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see net.awl.edoc.pdfa.validation.graphics.AbstractXObjValidator#validate()
+ */
+ @Override
+ public List<ValidationError> validate() throws ValidationException {
+ return super.validate();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @seenet.awl.edoc.pdfa.validation.graphics.AbstractXObjValidator#
+ * checkMandatoryFields(java.util.List)
+ */
+ @Override
+ protected boolean checkMandatoryFields(List<ValidationError> result) {
+ // PostScript XObjects are forbidden. Whatever the result of this function,
+ // the validation will fail
+ return true;
+ }
+
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjPostscriptValidator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjectValidator.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjectValidator.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjectValidator.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjectValidator.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,39 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.graphics;
+
+import java.util.List;
+
+import org.apache.padaf.preflight.ValidationException;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+
+
+public interface XObjectValidator {
+ /**
+ * Process the validation of the XObject.
+ *
+ * @return A list of validation errors. This list is empty if the validation
+ * succeed.
+ * @throws ValidationException
+ */
+ public abstract List<ValidationError> validate() throws ValidationException;
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/XObjectValidator.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaceHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaceHelper.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaceHelper.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaceHelper.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,41 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.graphics.color;
+
+import java.util.List;
+
+import org.apache.padaf.preflight.ValidationException;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+
+
+public interface ColorSpaceHelper {
+ /**
+ * Process the ColorSpace validation.
+ *
+ * @param result
+ * @return
+ * @throws ValidationException
+ */
+ public boolean validate(List<ValidationError> result)
+ throws ValidationException;
+
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaceHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaceHelperFactory.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaceHelperFactory.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaceHelperFactory.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaceHelperFactory.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,100 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.graphics.color;
+
+
+import org.apache.padaf.preflight.DocumentHandler;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
+
+/**
+ * This factory create the right Helper according to the owner of the ColorSpace
+ * entry.
+ */
+public class ColorSpaceHelperFactory {
+
+ /**
+ * Return an instance of ColorSpaceHelper according to the
+ * ColorSpaceRestiction value.
+ * <UL>
+ * <li>ColorSpaceRestiction.NO_PATTERN : returns NoPatternColorSpaceHelper
+ * <li>ColorSpaceRestiction.ONLY_DEVICE : returns DeviceColorSpaceHelper
+ * <li>default : returns StandardColorSpaceHelper
+ * </UL>
+ *
+ * @param csObj
+ * the COSBase which represents the ColorSpace (COSName or COSArray)
+ * @param handler
+ * the DocumentHandler to access useful data
+ * @param csr
+ * the color space restriction
+ * @return
+ */
+ public static ColorSpaceHelper getColorSpaceHelper(COSBase csObj,
+ DocumentHandler handler, ColorSpaceRestriction csr) {
+ switch (csr) {
+ case NO_PATTERN:
+ return new NoPatternColorSpaceHelper(csObj, handler);
+ case ONLY_DEVICE:
+ return new DeviceColorSpaceHelper(csObj, handler);
+ default:
+ return new StandardColorSpaceHelper(csObj, handler);
+ }
+ }
+
+ /**
+ * Return an instance of ColorSpaceHelper according to the
+ * ColorSpaceRestiction value.
+ * <UL>
+ * <li>ColorSpaceRestiction.NO_PATTERN : returns NoPatternColorSpaceHelper
+ * <li>ColorSpaceRestiction.ONLY_DEVICE : returns DeviceColorSpaceHelper
+ * <li>default : returns StandardColorSpaceHelper
+ * </UL>
+ *
+ * @param cs
+ * the High level PDFBox object which represents the ColorSpace
+ * @param handler
+ * the DocumentHandler to access useful data
+ * @param csr
+ * the color space restriction
+ * @return
+ */
+ public static ColorSpaceHelper getColorSpaceHelper(PDColorSpace cs,
+ DocumentHandler handler, ColorSpaceRestriction csr) {
+ switch (csr) {
+ case NO_PATTERN:
+ return new NoPatternColorSpaceHelper(cs, handler);
+ case ONLY_DEVICE:
+ return new DeviceColorSpaceHelper(cs, handler);
+ default:
+ return new StandardColorSpaceHelper(cs, handler);
+ }
+ }
+
+ /**
+ * Enum used as argument of methods of this factory to return the right
+ * Helper.
+ */
+ public enum ColorSpaceRestriction {
+ NO_RESTRICTION, NO_PATTERN, ONLY_DEVICE;
+ }
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaceHelperFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaces.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaces.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaces.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaces.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,62 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.graphics.color;
+
+/**
+ * This enum makes ColorSpaces validation easier. Labels represent ColorSpace
+ * names as defined in the "PDF Reference 1.4". Enum entries with the "_SHORT"
+ * suffix are used to represent color spaces (DeviceGray, DeviceRGB, DeviceCMYK
+ * and Indexed) using the InlinedImage notation.
+ */
+public enum ColorSpaces {
+
+ Lab("Lab"), CalRGB("CalRGB"), CalGray("CalGray"), DeviceN("DeviceN"), Indexed(
+ "Indexed"), Indexed_SHORT("I"), Pattern("Pattern"), ICCBased("ICCBased"), DeviceRGB(
+ "DeviceRGB"), DeviceRGB_SHORT("RGB"), DeviceGray("DeviceGray"), DeviceGray_SHORT(
+ "G"), DeviceCMYK("DeviceCMYK"), DeviceCMYK_SHORT("CMYK"), Separation(
+ "Separation");
+
+ /**
+ * Name of the ColorSpace
+ */
+ public String label;
+
+ private ColorSpaces(String _label) {
+ label = _label;
+ }
+
+ /**
+ * @return the label
+ */
+ public String getLabel() {
+ return label;
+ }
+
+ /**
+ * @param label
+ * the label to set
+ */
+ public void setLabel(String label) {
+ this.label = label;
+ }
+
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/ColorSpaces.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/DeviceColorSpaceHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/DeviceColorSpaceHelper.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/DeviceColorSpaceHelper.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/DeviceColorSpaceHelper.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,143 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.graphics.color;
+
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_COLOR_SPACE;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_COLOR_SPACE_FORBIDDEN;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING;
+
+import java.io.IOException;
+import java.util.List;
+
+
+import org.apache.padaf.preflight.DocumentHandler;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
+import org.apache.pdfbox.pdmodel.graphics.color.PDIndexed;
+
+/**
+ * This class defines restrictions on Color Spaces. It checks the consistency of
+ * the Color space with the DestOutputIntent, if the color space isn't a Device
+ * Color space or a Indexed color space using Device the validation will fail.
+ */
+public class DeviceColorSpaceHelper extends StandardColorSpaceHelper {
+
+ DeviceColorSpaceHelper(COSBase object, DocumentHandler _handler) {
+ super(object, _handler);
+ }
+
+ DeviceColorSpaceHelper(PDColorSpace object, DocumentHandler _handler) {
+ super(object, _handler);
+ }
+
+ /**
+ * This method updates the given list with a ValidationError
+ * (ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN) and returns false.
+ */
+ protected boolean processPatternColorSpace(List<ValidationError> result) {
+ result
+ .add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_FORBIDDEN, "Pattern ColorSpace is forbidden"));
+ return false;
+ }
+
+ /**
+ * This method updates the given list with a ValidationError
+ * (ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN) and returns false.
+ */
+ protected boolean processCalibratedColorSpace(List<ValidationError> result) {
+ result
+ .add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_FORBIDDEN, "Calibrated ColorSpace is forbidden"));
+ return false;
+ }
+
+ /**
+ * This method updates the given list with a ValidationError
+ * (ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN) and returns false.
+ */
+ protected boolean processICCBasedColorSpace(PDColorSpace pdcs,
+ List<ValidationError> result) {
+ result
+ .add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_FORBIDDEN, "ICCBased ColorSpace is forbidden"));
+ return false;
+ }
+
+ /**
+ * This method updates the given list with a ValidationError
+ * (ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN) and returns false.
+ */
+ protected boolean processDeviceNColorSpace(PDColorSpace pdcs,
+ List<ValidationError> result) {
+ result
+ .add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_FORBIDDEN, "DeviceN ColorSpace is forbidden"));
+ return false;
+ }
+
+ /**
+ * Indexed color space is authorized only if the BaseColorSpace is a DeviceXXX
+ * color space. In all other cases the given list is updated with a
+ * ValidationError (ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN) and
+ * returns false.
+ */
+ protected boolean processIndexedColorSpace(PDColorSpace pdcs,
+ List<ValidationError> result) {
+ PDIndexed indexed = (PDIndexed) pdcs;
+ try {
+ if (iccpw == null) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING, "DestOutputProfile is missing"));
+ return false;
+ }
+
+ PDColorSpace based = indexed.getBaseColorSpace();
+ ColorSpaces cs = ColorSpaces.valueOf(based.getName());
+ switch (cs) {
+ case DeviceCMYK:
+ case DeviceCMYK_SHORT:
+ case DeviceRGB:
+ case DeviceRGB_SHORT:
+ case DeviceGray:
+ case DeviceGray_SHORT:
+ return processAllColorSpace(based, result);
+ default:
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_INVALID_COLOR_SPACE_FORBIDDEN, cs.getLabel() + " ColorSpace is forbidden"));
+ return false;
+ }
+
+ } catch (IOException e) {
+ result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE, "Unable to read Indexed Color Space : " + e.getMessage()));
+ return false;
+ }
+ }
+
+ /**
+ * This method updates the given list with a ValidationError
+ * (ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN) and returns false.
+ */
+ protected boolean processSeparationColorSpace(PDColorSpace pdcs,
+ List<ValidationError> result) {
+ result
+ .add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_FORBIDDEN, "Separation ColorSpace is forbidden"));
+ return false;
+ }
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/DeviceColorSpaceHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/NoPatternColorSpaceHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/NoPatternColorSpaceHelper.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/NoPatternColorSpaceHelper.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/NoPatternColorSpaceHelper.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,58 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.graphics.color;
+
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN;
+
+import java.util.List;
+
+
+import org.apache.padaf.preflight.DocumentHandler;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
+
+/**
+ * This class defines restrictions on Pattern ColorSpace. It checks the
+ * consistency of the Color space with the DestOutputIntent, if the color space
+ * is a Pattern the validation will fail.
+ */
+public class NoPatternColorSpaceHelper extends StandardColorSpaceHelper {
+
+ NoPatternColorSpaceHelper(COSBase _csObject, DocumentHandler _handler) {
+ super(_csObject, _handler);
+ }
+
+ NoPatternColorSpaceHelper(PDColorSpace _csObject, DocumentHandler _handler) {
+ super(_csObject, _handler);
+ }
+
+ /**
+ * This method updates the given list with a ValidationError
+ * (ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN) and returns false.
+ */
+ protected boolean processPatternColorSpace(List<ValidationError> result) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN, "Pattern color space is forbidden"));
+ return false;
+ }
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/NoPatternColorSpaceHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/StandardColorSpaceHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/StandardColorSpaceHelper.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/StandardColorSpaceHelper.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/StandardColorSpaceHelper.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,439 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.graphics.color;
+
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_COLOR_SPACE;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_COLOR_SPACE_ALTERNATE;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_COLOR_SPACE_CMYK;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_COLOR_SPACE_INDEXED;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_COLOR_SPACE_RGB;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_COLOR_SPACE_TOO_MANY_COMPONENTS_DEVICEN;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN;
+import static org.apache.padaf.preflight.ValidationConstants.ERROR_GRAPHIC_INVALID_UNKNOWN_COLOR_SPACE;
+import static org.apache.padaf.preflight.ValidationConstants.MAX_DEVICE_N_LIMIT;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+
+
+import org.apache.padaf.preflight.DocumentHandler;
+import org.apache.padaf.preflight.ValidationException;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+import org.apache.padaf.preflight.graphics.ICCProfileWrapper;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSObject;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace;
+import org.apache.pdfbox.pdmodel.graphics.color.PDColorSpaceFactory;
+import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceN;
+import org.apache.pdfbox.pdmodel.graphics.color.PDICCBased;
+import org.apache.pdfbox.pdmodel.graphics.color.PDIndexed;
+import org.apache.pdfbox.pdmodel.graphics.color.PDSeparation;
+
+/**
+ * This class doesn't define restrictions on ColorSpace. It checks only the
+ * consistency of the Color space with the DestOutputIntent.
+ */
+public class StandardColorSpaceHelper implements ColorSpaceHelper {
+ /**
+ * The color space object to check, this object is used to instantiate the
+ * pdcs object.
+ */
+ protected COSBase csObject = null;
+ /**
+ * The document handler which contains useful information to process the
+ * validation.
+ */
+ protected DocumentHandler handler = null;
+ /**
+ * The ICCProfile contained in the DestOutputIntent
+ */
+ protected ICCProfileWrapper iccpw = null;
+ /**
+ * High level object which represents the colors space to check.
+ */
+ protected PDColorSpace pdcs = null;
+
+ StandardColorSpaceHelper(COSBase _csObject, DocumentHandler _handler) {
+ this.csObject = _csObject;
+ this.handler = _handler;
+ this.iccpw = this.handler.getIccProfileWrapper();
+ }
+
+ StandardColorSpaceHelper(PDColorSpace _cs, DocumentHandler _handler) {
+ this.handler = _handler;
+ this.pdcs = _cs;
+ this.iccpw = this.handler.getIccProfileWrapper();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * net.awl.edoc.pdfa.validation.graphics.color.ColorSpaceHelper#validate(java
+ * .util.List)
+ */
+ public final boolean validate(List<ValidationError> result) throws ValidationException {
+ // ---- Create a PDFBox ColorSpace object
+ if (pdcs == null && csObject != null) {
+ try {
+ if (csObject instanceof COSObject) {
+ pdcs = PDColorSpaceFactory.createColorSpace(((COSObject)csObject).getObject());
+ } else {
+ pdcs = PDColorSpaceFactory.createColorSpace(csObject);
+ }
+ } catch (IOException e) {
+ throw new ValidationException("Unable to create a PDColorSpace : "
+ + e.getMessage(), e);
+ }
+ }
+
+ if ( pdcs == null ) {
+ throw new ValidationException(
+ "Unable to create a PDColorSpace with the value null");
+ }
+
+ return processAllColorSpace(pdcs, result);
+ }
+
+ /**
+ * Method called by the validate method. According to the ColorSpace, a
+ * specific ColorSpace method is called.
+ *
+ * @param pdcs
+ * the color space object to check.
+ * @param result
+ * the list of error to update if the validation fails.
+ * @return true if the validation succeed, false otherwise.
+ */
+ protected final boolean processAllColorSpace(PDColorSpace pdcs,
+ List<ValidationError> result) {
+ ColorSpaces cs = ColorSpaces.valueOf(pdcs.getName());
+ switch (cs) {
+ case DeviceRGB:
+ case DeviceRGB_SHORT:
+ return processRGBColorSpace(result);
+
+ case DeviceCMYK:
+ case DeviceCMYK_SHORT:
+ return processCYMKColorSpace(result);
+
+ case CalRGB:
+ case CalGray:
+ case Lab:
+ return processCalibratedColorSpace(result);
+
+ case DeviceGray:
+ case DeviceGray_SHORT:
+ return processGrayColorSpace(result);
+
+ case ICCBased:
+ return processICCBasedColorSpace(pdcs, result);
+
+ case DeviceN:
+ return processDeviceNColorSpace(pdcs, result);
+
+ case Indexed:
+ case Indexed_SHORT:
+ return processIndexedColorSpace(pdcs, result);
+
+ case Separation:
+ return processSeparationColorSpace(pdcs, result);
+
+ case Pattern:
+ return processPatternColorSpace(result);
+
+ default:
+ result
+ .add(new ValidationError(ERROR_GRAPHIC_INVALID_UNKNOWN_COLOR_SPACE, cs.getLabel() + " is unknown as ColorSpace"));
+ return false;
+ }
+ }
+
+ /**
+ * Method called by the processAllColorSpace if the ColorSpace to check is
+ * DeviceRGB.
+ *
+ * @param result
+ * the list of error to update if the validation fails.
+ * @return true if the color space is valid, false otherwise.
+ */
+ protected boolean processRGBColorSpace(List<ValidationError> result) {
+ // ---- ICCProfile must contain a RGB Color Space
+ if (iccpw == null || !iccpw.isRGBColorSpace()) {
+ result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_RGB, "DestOutputProfile is missing"));
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Method called by the processAllColorSpace if the ColorSpace to check is
+ * DeviceCYMK.
+ *
+ * @param result
+ * the list of error to update if the validation fails.
+ * @return true if the color space is valid, false otherwise.
+ */
+ protected boolean processCYMKColorSpace(List<ValidationError> result) {
+ // ---- ICCProfile must contain a CYMK Color Space
+ if (iccpw == null || !iccpw.isCMYKColorSpace()) {
+ result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_CMYK, "DestOutputProfile is missing"));
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Method called by the processAllColorSpace if the ColorSpace to check is a
+ * Pattern.
+ *
+ * @param result
+ * the list of error to update if the validation fails.
+ * @return true if the color space is valid, false otherwise.
+ */
+ protected boolean processPatternColorSpace(List<ValidationError> result) {
+ if (iccpw == null) {
+ result
+ .add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING, "DestOutputProfile is missing"));
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Method called by the processAllColorSpace if the ColorSpace to check is
+ * DeviceGray.
+ *
+ * @param result
+ * the list of error to update if the validation fails.
+ * @return true if the color space is valid, false otherwise.
+ */
+ protected boolean processGrayColorSpace(List<ValidationError> result) {
+ // ---- OutputIntent is mandatory
+ if (iccpw == null) {
+ result
+ .add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING, "DestOutputProfile is missing"));
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Method called by the processAllColorSpace if the ColorSpace to check is a
+ * Clibrated Color (CalGary, CalRGB, Lab).
+ *
+ * @param result
+ * the list of error to update if the validation fails.
+ * @return true if the color space is valid, false otherwise.
+ */
+ protected boolean processCalibratedColorSpace(List<ValidationError> result) {
+ // ---- OutputIntent isn't mandatory
+ return true;
+ }
+
+ /**
+ * Method called by the processAllColorSpace if the ColorSpace to check is a
+ * ICCBased color space. Because this kind of ColorSpace can have alternate
+ * color space, the processAllColorSpace is called to check this alternate
+ * color space. (Pattern is forbidden as Alternate Color Space)
+ *
+ * @param pdcs
+ * the color space object to check.
+ * @param result
+ * the list of error to update if the validation fails.
+ * @return true if the color space is valid, false otherwise.
+ */
+ protected boolean processICCBasedColorSpace(PDColorSpace pdcs,
+ List<ValidationError> result) {
+ PDICCBased iccBased = (PDICCBased) pdcs;
+ try {
+ if (iccpw == null) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING, "DestOutputProfile is missing"));
+ return false;
+ }
+
+ List<PDColorSpace> altCs = iccBased.getAlternateColorSpaces();
+ for (PDColorSpace altpdcs : altCs) {
+ if (altpdcs != null) {
+
+ ColorSpaces altCsId = ColorSpaces.valueOf(altpdcs.getName());
+ if (altCsId == ColorSpaces.Pattern) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN, "Pattern is forbidden as AlternateColorSpace of a ICCBased"));
+ return false;
+ }
+
+ if (!processAllColorSpace(altpdcs, result)) {
+ return false;
+ }
+ }
+ }
+ } catch (IOException e) {
+ result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE, "Unable to read ICCBase color space : " + e.getMessage()));
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Method called by the processAllColorSpace if the ColorSpace to check is
+ * DeviceN. Because this kind of ColorSpace can have alternate color space,
+ * the processAllColorSpace is called to check this alternate color space.
+ * (There are no restrictions on the Alternate Color space)
+ *
+ * @param pdcs
+ * the color space object to check.
+ * @param result
+ * the list of error to update if the validation fails.
+ * @return true if the color space is valid, false otherwise.
+ */
+ protected boolean processDeviceNColorSpace(PDColorSpace pdcs,
+ List<ValidationError> result) {
+ PDDeviceN deviceN = (PDDeviceN) pdcs;
+ try {
+ if (iccpw == null) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING, "DestOutputProfile is missing"));
+ return false;
+ }
+
+ PDColorSpace altColor = deviceN.getAlternateColorSpace();
+ boolean res = true;
+ if (altColor != null) {
+ res = processAllColorSpace(altColor, result);
+ }
+
+ Map colorants = deviceN.getAttributes().getColorants();
+ int numberOfColorants = 0;
+ if (colorants != null) {
+ numberOfColorants = colorants.size();
+ for (Object col : colorants.values()) {
+ if (col != null) {
+ res = res && processAllColorSpace((PDColorSpace) col, result);
+ }
+ }
+ }
+
+ int numberOfComponents = deviceN.getNumberOfComponents();
+ if (numberOfColorants > MAX_DEVICE_N_LIMIT || numberOfComponents > MAX_DEVICE_N_LIMIT ) {
+ result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_TOO_MANY_COMPONENTS_DEVICEN, "DeviceN has too many tint components or colorants"));
+ res = false;
+ }
+ return res;
+ } catch (IOException e) {
+ result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE, "Unable to read DeviceN color space : " + e.getMessage()));
+ return false;
+ }
+ }
+
+ /**
+ * Method called by the processAllColorSpace if the ColorSpace to check is
+ * Indexed. Because this kind of ColorSpace can have a Base color space, the
+ * processAllColorSpace is called to check this base color space. (Indexed and
+ * Pattern can't be a Base color space)
+ *
+ * @param pdcs
+ * the color space object to check.
+ * @param result
+ * the list of error to update if the validation fails.
+ * @return true if the color space is valid, false otherwise.
+ */
+ protected boolean processIndexedColorSpace(PDColorSpace pdcs,
+ List<ValidationError> result) {
+ PDIndexed indexed = (PDIndexed) pdcs;
+ try {
+ if (iccpw == null) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING, "DestOutputProfile is missing"));
+ return false;
+ }
+
+ PDColorSpace based = indexed.getBaseColorSpace();
+ ColorSpaces cs = ColorSpaces.valueOf(based.getName());
+ if (cs == ColorSpaces.Indexed || cs == ColorSpaces.Indexed_SHORT) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_INVALID_COLOR_SPACE_INDEXED,"Indexed color space can't be used as Base color space"));
+ return false;
+ }
+ if (cs == ColorSpaces.Pattern) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_INVALID_COLOR_SPACE_INDEXED,"Pattern color space can't be used as Base color space"));
+ return false;
+ }
+ return processAllColorSpace(based, result);
+ } catch (IOException e) {
+ result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE, "Unable to read Indexed color space : " + e.getMessage()));
+ return false;
+ }
+ }
+
+ /**
+ * Method called by the processAllColorSpace if the ColorSpace to check is
+ * Separation. Because this kind of ColorSpace can have an alternate color
+ * space, the processAllColorSpace is called to check this alternate color
+ * space. (Indexed, Separation, DeviceN and Pattern can't be a Base color
+ * space)
+ *
+ * @param pdcs
+ * the color space object to check.
+ * @param result
+ * the list of error to update if the validation fails.
+ * @return true if the color space is valid, false otherwise.
+ */
+ protected boolean processSeparationColorSpace(PDColorSpace pdcs,
+ List<ValidationError> result) {
+ PDSeparation separation = (PDSeparation) pdcs;
+ try {
+ if (iccpw == null) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING,"DestOutputProfile is missing"));
+ return false;
+ }
+
+ PDColorSpace altCol = separation.getAlternateColorSpace();
+ if (altCol != null) {
+ ColorSpaces acs = ColorSpaces.valueOf(altCol.getName());
+ switch (acs) {
+ case Separation:
+ case DeviceN:
+ case Pattern:
+ case Indexed:
+ case Indexed_SHORT:
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_INVALID_COLOR_SPACE_ALTERNATE, acs.getLabel() + " color space can't be used as alternate color space"));
+ return false;
+ default:
+ return processAllColorSpace(altCol, result);
+ }
+ }
+
+ return true;
+ } catch (IOException e) {
+ result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE, "Unable to read Separation color space : " + e.getMessage()));
+ return false;
+ }
+ }
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/graphics/color/StandardColorSpaceHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/AbstractValidationHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/AbstractValidationHelper.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/AbstractValidationHelper.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/AbstractValidationHelper.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,110 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.helpers;
+
+import java.util.List;
+
+import org.apache.padaf.preflight.DocumentHandler;
+import org.apache.padaf.preflight.ValidationConstants;
+import org.apache.padaf.preflight.ValidationException;
+import org.apache.padaf.preflight.ValidatorConfig;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+import org.apache.padaf.preflight.actions.ActionManagerFactory;
+import org.apache.padaf.preflight.annotation.AnnotationValidatorFactory;
+
+
+/**
+ * Abstract class for ValidationHelper. A validation helper is an object which
+ * check the PDF/A-1x compliance of a specific PDF element.
+ */
+public abstract class AbstractValidationHelper implements ValidationConstants {
+ protected ValidatorConfig valConfig = null;
+
+ protected AnnotationValidatorFactory annotFact = null;
+ protected ActionManagerFactory actionFact = null;
+
+ public AbstractValidationHelper(ValidatorConfig cfg) throws ValidationException {
+ valConfig = cfg;
+
+ try {
+ this.actionFact = cfg.getActionFact().newInstance();
+ } catch ( IllegalAccessException e) {
+ throw new ValidationException("Unable to instatiate action factory : " + e.getMessage(), e);
+ } catch ( InstantiationException e) {
+ throw new ValidationException("Unable to instatiate action factory : " + e.getMessage(), e);
+ }
+
+ try {
+ this.annotFact = cfg.getAnnotFact().newInstance();
+ this.annotFact.setActionFact(this.actionFact);
+ } catch ( IllegalAccessException e) {
+ throw new ValidationException("Unable to instatiate annotation factory : " + e.getMessage(), e);
+ } catch ( InstantiationException e) {
+ throw new ValidationException("Unable to instatiate annotation factory : " + e.getMessage(), e);
+ }
+
+ }
+
+ /**
+ * Validation process of all inherited classes.
+ *
+ * @param handler
+ * the object which contains the PDF Document
+ * @return A list of validation error. If there are no error, the list is
+ * empty.
+ * @throws ValidationException
+ */
+ public abstract List<ValidationError> innerValidate(DocumentHandler handler)
+ throws ValidationException;
+
+ /**
+ * Process the validation of specific elements contained in the PDF document.
+ *
+ * @param handler
+ * the object which contains the PDF Document
+ * @return A list of validation error. If there are no error, the list is
+ * empty.
+ * @throws ValidationException
+ */
+ public final List<ValidationError> validate(DocumentHandler handler)
+ throws ValidationException {
+ checkHandler(handler);
+ return innerValidate(handler);
+ }
+
+ /**
+ * Check if the Handler isn't null and if it is complete.
+ *
+ * @param handler
+ * @throws ValidationException
+ */
+ protected void checkHandler(DocumentHandler handler)
+ throws ValidationException {
+ if (handler == null) {
+ throw new ValidationException("DocumentHandler can't be null");
+ }
+ if (!handler.isComplete()) {
+ throw new ValidationException("DocumentHandler error : missing element");
+ }
+ }
+
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/AbstractValidationHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/AcroFormValidationHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/AcroFormValidationHelper.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/AcroFormValidationHelper.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/AcroFormValidationHelper.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,173 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.helpers;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+
+import org.apache.padaf.preflight.DocumentHandler;
+import org.apache.padaf.preflight.ValidationException;
+import org.apache.padaf.preflight.ValidatorConfig;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+import org.apache.padaf.preflight.annotation.AnnotationValidator;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
+import org.apache.pdfbox.pdmodel.interactive.action.PDFormFieldAdditionalActions;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
+import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
+import org.apache.pdfbox.pdmodel.interactive.form.PDField;
+
+/**
+ * This helper validates AcroFrom (Interactive Form)
+ */
+public class AcroFormValidationHelper extends AbstractValidationHelper {
+
+ /**
+ *
+ * @param cfg
+ * @throws ValidationException
+ */
+ public AcroFormValidationHelper(ValidatorConfig cfg) throws ValidationException {
+ super(cfg);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * net.awl.edoc.pdfa.validation.helpers.AbstractValidationHelper#innerValidate
+ * (net.awl.edoc.pdfa.validation.DocumentHandler)
+ */
+ @Override
+ public List<ValidationError> innerValidate(DocumentHandler handler)
+ throws ValidationException {
+
+ List<ValidationError> result = new ArrayList<ValidationError>(0);
+
+ PDDocumentCatalog catalog = handler.getDocument().getDocumentCatalog();
+ if (catalog != null) {
+ PDAcroForm acroForm = catalog.getAcroForm();
+ if (acroForm != null) {
+ checkNeedAppearences(handler, acroForm, result);
+ try {
+ exploreFields(handler, acroForm.getFields(), result);
+ } catch (IOException e) {
+ throw new ValidationException("Unable to get the list of fields : "
+ + e.getMessage(), e);
+ }
+ }
+ } else {
+ throw new ValidationException(
+ "There are no Catalog entry in the Document.");
+ }
+
+ return result;
+ }
+
+ /**
+ * This method checks if the NeedAppearances entry is present. If it is, the
+ * value must be false.
+ *
+ * If the entry is invalid, the ERROR_SYNTAX_DICT_INVALID (1.2.3) error is
+ * return.
+ *
+ * @param handler
+ * @param acroForm
+ * @param result
+ */
+ protected void checkNeedAppearences(DocumentHandler handler,
+ PDAcroForm acroForm, List<ValidationError> error) {
+ if (acroForm.getDictionary().getBoolean(
+ ACROFORM_DICTIONARY_KEY_NEED_APPEARANCES, false)) {
+ error.add(new ValidationError(ERROR_SYNTAX_DICT_INVALID,
+ "NeedAppearance is present with the value \"true\""));
+ }
+ }
+
+ /**
+ * This function explores all fields and their children to check if the A or
+ * AA entry is present.
+ *
+ * @param handler
+ * @param acroForm
+ * @param result
+ * @throws IOException
+ */
+ protected boolean exploreFields(DocumentHandler handler, List<?> lFields,
+ List<ValidationError> error) throws IOException, ValidationException {
+ if (lFields != null) {
+ // ---- the list can be null is the Field doesn't have child
+ for (Object obj : lFields) {
+ if (!valideField((PDField) obj, handler, error)) {
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+
+ /**
+ * A and AA field are forbidden, this method checks if they are present and
+ * checks all child of this field. If the an Additional Action is present the
+ * error code ERROR_ACTION_FORBIDDEN_ADDITIONAL_ACTIONS_FIELD (6.2.3) is added
+ * to the error list If the an Action is present (in the Widget Annotation)
+ * the error ERROR_ACTION_FORBIDDEN_WIDGET_ACTION_FIELD (6.2.4) is added to
+ * the error list. (Remark : The widget validation will be done by the
+ * AnnotationValidationHelper, but some actions are authorized in a standard
+ * Widget)
+ *
+ * @param aField
+ * @param handler
+ * @param result
+ * @return
+ * @throws IOException
+ */
+ protected boolean valideField(PDField aField, DocumentHandler handler,
+ List<ValidationError> error) throws IOException, ValidationException {
+ boolean res = true;
+ PDFormFieldAdditionalActions aa = aField.getActions();
+ if (aa != null) {
+ error.add(new ValidationError(ERROR_ACTION_FORBIDDEN_ADDITIONAL_ACTIONS_FIELD, "\"AA\" must not be used in a Field dictionary"));
+ res = false;
+ }
+
+ // ---- The widget validation will be done by the widget annotation, a
+ // widget contained in a Field can't have action.
+ PDAnnotationWidget widget = aField.getWidget();
+ if (res && widget != null) {
+ AnnotationValidator widgetVal = annotFact.getAnnotationValidator( widget.getDictionary(), handler, error);
+ widgetVal.validate(error);
+
+ COSBase act = widget.getDictionary().getDictionaryObject(DICTIONARY_KEY_ACTION);
+ if (act != null) {
+ error.add(new ValidationError(
+ ERROR_ACTION_FORBIDDEN_WIDGET_ACTION_FIELD, "\"A\" must not be used in a Field dictionary"));
+ res = false;
+ }
+ }
+
+ res = res && exploreFields(handler, aField.getKids(), error);
+ return res;
+ }
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/AcroFormValidationHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/BookmarkValidationHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/BookmarkValidationHelper.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/BookmarkValidationHelper.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/BookmarkValidationHelper.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,187 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.helpers;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+import org.apache.padaf.preflight.DocumentHandler;
+import org.apache.padaf.preflight.ValidationException;
+import org.apache.padaf.preflight.ValidatorConfig;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+import org.apache.padaf.preflight.actions.AbstractActionManager;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
+
+/**
+ * This helper validates the book mark object (Outline Items)
+ */
+public class BookmarkValidationHelper extends AbstractValidationHelper {
+
+ /**
+ *
+ * @param cfg
+ * @throws ValidationException
+ */
+ public BookmarkValidationHelper(ValidatorConfig cfg) throws ValidationException {
+ super(cfg);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * net.awl.edoc.pdfa.validation.helpers.AbstractValidationHelper#innerValidate
+ * (net.awl.edoc.pdfa.validation.DocumentHandler)
+ */
+ @Override
+ public List<ValidationError> innerValidate(DocumentHandler handler)
+ throws ValidationException {
+ List<ValidationError> result = new ArrayList<ValidationError>(0);
+
+ PDDocumentCatalog catalog = handler.getDocument().getDocumentCatalog();
+ if (catalog != null) {
+ PDDocumentOutline outlineHierarchy = catalog.getDocumentOutline();
+ if (outlineHierarchy != null) {
+ if (outlineHierarchy.getFirstChild() == null
+ || outlineHierarchy.getLastChild() == null) {
+ result.add(new ValidationError(ERROR_SYNTAX_TRAILER_OUTLINES_INVALID,
+ "Outline Hierarchy doesn't have First and/or Last entry(ies)"));
+ } else {
+ // ---- Count entry is mandatory if there are childrens
+ if (!isCountEntryPresent(outlineHierarchy.getCOSDictionary())) {
+ result.add(new ValidationError(
+ ERROR_SYNTAX_TRAILER_OUTLINES_INVALID,
+ "Outline Hierarchy doesn't have Count entry"));
+ } else {
+ exploreOutlineLevel(outlineHierarchy.getFirstChild(), handler,
+ result);
+ }
+ }
+ }
+ } else {
+ throw new ValidationException(
+ "There are no Catalog entry in the Document.");
+ }
+ return result;
+ }
+
+ /**
+ * Return true if the Count entry is present in the given dictionary.
+ *
+ * @param outline
+ * @return
+ */
+ private boolean isCountEntryPresent(COSDictionary outline) {
+ return outline.getItem(COSName.getPDFName("Count")) != null;
+ }
+
+ /**
+ * This method explores the Outline Item Level and call a validation method on
+ * each Outline Item. If an invalid outline item is found, the result list is
+ * updated.
+ *
+ * @param inputItem
+ * The first outline item of the level
+ * @param handler
+ * The document handler which provides useful data for the level
+ * exploration (ex : access to the PDDocument)
+ * @param result
+ * @return true if all items are valid in this level.
+ * @throws ValidationException
+ */
+ protected boolean exploreOutlineLevel(PDOutlineItem inputItem,
+ DocumentHandler handler, List<ValidationError> result)
+ throws ValidationException {
+ PDOutlineItem currentItem = inputItem;
+ int oiValided = 0;
+ while (currentItem != null) {
+ if (!validateItem(currentItem, handler, result)) {
+ return false;
+ }
+ oiValided++;
+ currentItem = currentItem.getNextSibling();
+ }
+ return true;
+ }
+
+ /**
+ * This method checks the inputItem dictionary and call the
+ * exploreOutlineLevel method on the first child if it is not null.
+ *
+ * @param inputItem
+ * outline item to validate
+ * @param handler
+ * The document handler which provides useful data for the level
+ * exploration (ex : access to the PDDocument)
+ * @param result
+ * @return
+ * @throws ValidationException
+ */
+ protected boolean validateItem(PDOutlineItem inputItem,
+ DocumentHandler handler, List<ValidationError> result)
+ throws ValidationException {
+ boolean isValid = true;
+ // ---- Dest entry isn't permitted if the A entry is present
+ // A entry isn't permitted if the Dest entry is present
+ // If the A enntry is present, the referenced actions is validated
+ COSDictionary dictionary = inputItem.getCOSDictionary();
+ COSBase dest = dictionary.getItem(COSName
+ .getPDFName(DICTIONARY_KEY_DESTINATION));
+ COSBase action = dictionary.getItem(COSName
+ .getPDFName(DICTIONARY_KEY_ACTION));
+
+ if (action != null && dest != null) {
+ result.add(new ValidationError(ERROR_SYNTAX_TRAILER_OUTLINES_INVALID,
+ "Dest entry isn't permitted if the A entry is present"));
+ return false;
+ } else if (action != null) {
+ List<AbstractActionManager> actions = this.actionFact.getActions(dictionary, handler
+ .getDocument().getDocument());
+ for (AbstractActionManager act : actions) {
+ isValid = isValid && act.valid(result);
+ }
+ } // else no specific validation
+
+ // ---- check children
+ PDOutlineItem fChild = inputItem.getFirstChild();
+ if (fChild != null) {
+ if (!isCountEntryPresent(inputItem.getCOSDictionary())) {
+ result
+ .add(new ValidationError(ERROR_SYNTAX_TRAILER_OUTLINES_INVALID,
+ "Outline item doesn't have Count entry but has at least one descendant."));
+ isValid = false;
+ } else {
+ // ---- there are some descendants, so dictionary must have a Count
+ // entry
+ isValid = isValid && exploreOutlineLevel(fChild, handler, result);
+ }
+ }
+
+ return isValid;
+ }
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/BookmarkValidationHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/CatalogValidationHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/CatalogValidationHelper.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/CatalogValidationHelper.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/CatalogValidationHelper.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,349 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.helpers;
+
+import java.awt.color.ICC_Profile;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+import org.apache.padaf.preflight.DocumentHandler;
+import org.apache.padaf.preflight.ValidationException;
+import org.apache.padaf.preflight.ValidatorConfig;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+import org.apache.padaf.preflight.actions.AbstractActionManager;
+import org.apache.padaf.preflight.graphics.ICCProfileWrapper;
+import org.apache.padaf.preflight.utils.COSUtils;
+import org.apache.pdfbox.cos.COSArray;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSDocument;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSObject;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
+import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
+import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
+import org.apache.pdfbox.pdmodel.common.PDStream;
+import org.apache.pdfbox.persistence.util.COSObjectKey;
+
+/**
+ * This helper validates the PDF file catalog
+ */
+public class CatalogValidationHelper extends AbstractValidationHelper {
+
+ public CatalogValidationHelper(ValidatorConfig cfg)
+ throws ValidationException {
+ super(cfg);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * net.awl.edoc.pdfa.validation.helpers.AbstractValidationHelper#innerValidate
+ * (net.awl.edoc.pdfa.validation.DocumentHandler)
+ */
+ @Override
+ public List<ValidationError> innerValidate(DocumentHandler handler)
+ throws ValidationException {
+ List<ValidationError> result = new ArrayList<ValidationError>(0);
+ PDDocument pdfbox = handler.getDocument();
+ PDDocumentCatalog catalog = pdfbox.getDocumentCatalog();
+ if (catalog != null) {
+ validateActions(handler, catalog, result);
+ validateLang(handler, catalog, result);
+ validateNames(handler, catalog, result);
+ validateOCProperties(handler, catalog, result);
+ } else {
+ throw new ValidationException(
+ "There are no Catalog entry in the Document.");
+ }
+
+ // ---- Check OutputIntent to know the ICC Profile
+ result.addAll(validateOutputIntent(handler));
+
+ return result;
+ }
+
+ /**
+ * This method validates if OpenAction entry contains forbidden action type.
+ * It checks too if an Additional Action is present.
+ *
+ * @param handler
+ * @param catalog
+ * @param result
+ * @throws ValidationException
+ */
+ protected void validateActions(DocumentHandler handler,
+ PDDocumentCatalog catalog, List<ValidationError> result)
+ throws ValidationException {
+ // ---- get OpenAction and Additional Action if these entries are present
+ List<AbstractActionManager> lActions = this.actionFact.getActions(catalog
+ .getCOSDictionary(), handler.getDocument().getDocument());
+ for (AbstractActionManager action : lActions) {
+ if (!action.valid(result)) {
+ return;
+ }
+ }
+ }
+
+ /**
+ * The Lang element is optional but it is recommended. This method check the
+ * Syntax of the Lang if this entry is present.
+ *
+ * @param handler
+ * @param catalog
+ * @param result
+ * @throws ValidationException
+ */
+ protected void validateLang(DocumentHandler handler,
+ PDDocumentCatalog catalog, List<ValidationError> result)
+ throws ValidationException {
+ String lang = catalog.getLanguage();
+ if (lang != null && !lang.matches("[A-Za-z]{1,8}(-[A-Za-z]{1,8})*")) {
+ result.add(new ValidationError(ERROR_SYNTAX_LANG_NOT_RFC1766));
+ }
+ }
+
+ /**
+ * A Catalog shall not contain the EmbeddedFiles entry.
+ *
+ * @param handler
+ * @param catalog
+ * @param result
+ * @throws ValidationException
+ */
+ protected void validateNames(DocumentHandler handler,
+ PDDocumentCatalog catalog, List<ValidationError> result)
+ throws ValidationException {
+ PDDocumentNameDictionary names = catalog.getNames();
+ if (names != null) {
+ PDEmbeddedFilesNameTreeNode efs = names.getEmbeddedFiles();
+ if (efs != null) {
+ result.add(new ValidationError(
+ ERROR_SYNTAX_TRAILER_CATALOG_EMBEDDEDFILES,"EmbeddedFile entry is present in the Names dictionary"));
+ }
+ }
+ }
+
+ /**
+ * A Catalog shall not contain the OCPProperties (Optional Content Properties)
+ * entry.
+ *
+ * @param handler
+ * @param catalog
+ * @param result
+ * @throws ValidationException
+ */
+ protected void validateOCProperties(DocumentHandler handler,
+ PDDocumentCatalog catalog, List<ValidationError> result)
+ throws ValidationException {
+ COSBase ocp = catalog.getCOSDictionary().getItem(
+ COSName.getPDFName(DOCUMENT_DICTIONARY_KEY_OPTIONAL_CONTENTS));
+ if (ocp != null) {
+ result
+ .add(new ValidationError(ERROR_SYNTAX_TRAILER_CATALOG_OCPROPERTIES, "A Catalog shall not contain the OCPProperties entry."));
+ }
+ }
+
+ /**
+ * This method checks the content of each OutputIntent. The S entry must
+ * contain GTS_PDFA1. The DestOuputProfile must contain a valid ICC Profile
+ * Stream.
+ *
+ * If there are more than one OutputIntent, they have to use the same ICC
+ * Profile.
+ *
+ * This method returns a list of ValidationError. It is empty if no errors
+ * have been found.
+ *
+ * @param handler
+ * @return
+ * @throws ValidationException
+ */
+ public List<ValidationError> validateOutputIntent(DocumentHandler handler)
+ throws ValidationException {
+ List<ValidationError> result = new ArrayList<ValidationError>(0);
+ PDDocument pdDocument = handler.getDocument();
+ PDDocumentCatalog catalog = pdDocument.getDocumentCatalog();
+ COSDocument cDoc = pdDocument.getDocument();
+
+ COSBase cBase = catalog.getCOSDictionary().getItem(
+ COSName.getPDFName(DOCUMENT_DICTIONARY_KEY_OUTPUT_INTENTS));
+ COSArray outputIntents = COSUtils.getAsArray(cBase, cDoc);
+
+ Map<COSObjectKey, Boolean> tmpDestOutputProfile = new HashMap<COSObjectKey, Boolean>();
+
+ for (int i = 0; outputIntents != null && i < outputIntents.size(); ++i) {
+ COSDictionary dictionary = COSUtils.getAsDictionary(outputIntents.get(i),
+ cDoc);
+
+ if (dictionary == null) {
+
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY,
+ "OutputIntent object is null or isn't a dictionary"));
+
+ } else {
+ // ---- S entry is mandatory and must be equals to GTS_PDFA1
+ String sValue = dictionary.getNameAsString(COSName
+ .getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_S));
+ if (!OUTPUT_INTENT_DICTIONARY_VALUE_GTS_PDFA1.equals(sValue)) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_OUTPUT_INTENT_S_VALUE_INVALID,"The S entry of the OutputIntent isn't GTS_PDFA1"));
+ continue;
+ }
+
+ // ---- OutputConditionIdentifier is a mandatory field
+ String outputConditionIdentifier = dictionary
+ .getString(COSName
+ .getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_OUTPUT_CONDITION_IDENTIFIER));
+ if (outputConditionIdentifier == null
+ || "".equals(outputConditionIdentifier)) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY,
+ "The OutputIntentCondition is missing"));
+ continue;
+ }
+
+ // ---- If OutputConditionIdentifier is "Custom" :
+ // ---- DestOutputProfile and Info are mandatory
+ // ---- DestOutputProfile must be a ICC Profile
+
+ // ---- Because of PDF/A conforming file needs to specify the color
+ // characteristics, the DestOutputProfile
+ // is checked even if the OutputConditionIdentifier isn't "Custom"
+ COSBase dop = dictionary.getItem(COSName
+ .getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_DEST_OUTPUT_PROFILE));
+ ValidationError valer = validateICCProfile(dop, cDoc,
+ tmpDestOutputProfile, handler);
+ if (valer != null) {
+ result.add(valer);
+ continue;
+ }
+
+ if (OUTPUT_INTENT_DICTIONARY_VALUE_OUTPUT_CONDITION_IDENTIFIER_CUSTOM
+ .equals(outputConditionIdentifier)) {
+ String info = dictionary.getString(COSName
+ .getPDFName(OUTPUT_INTENT_DICTIONARY_KEY_INFO));
+ if (info == null || "".equals(info)) {
+ result.add(new ValidationError(
+ ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY,
+ "The Info entry of a OutputIntent dictionary is missing"));
+ continue;
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * This method checks the destOutputProfile which must be a valid ICCProfile.
+ *
+ * If an other ICCProfile exists in the mapDestOutputProfile, a
+ * ValdiationError (ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_MULTIPLE) is
+ * returned because of only one profile is authorized. If the ICCProfile
+ * already exist in the mapDestOutputProfile, the method returns null. If the
+ * destOutputProfile contains an invalid ICCProfile, a ValidationError
+ * (ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID) is returned If the
+ * destOutputProfile is an empty stream, a
+ * ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY) is returned.
+ *
+ * If the destOutputFile is valid, mapDestOutputProfile is updated, the
+ * ICCProfile is added to the document handler and null is returned.
+ *
+ * @param destOutputProfile
+ * @param cDoc
+ * @param tmpDestOutputProfile
+ * @param handler
+ * @return
+ * @throws ValidationException
+ */
+ protected ValidationError validateICCProfile(COSBase destOutputProfile,
+ COSDocument cDoc, Map<COSObjectKey, Boolean> mapDestOutputProfile,
+ DocumentHandler handler) throws ValidationException {
+ try {
+ if (destOutputProfile == null) {
+ return new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY,
+ "OutputIntent object uses a NULL Object");
+ }
+
+ // ---- destOutputProfile should be an instance of COSObject because of
+ // this is a object reference
+ if (destOutputProfile instanceof COSObject) {
+ if (mapDestOutputProfile.containsKey(new COSObjectKey(
+ (COSObject) destOutputProfile))) {
+ // ---- the profile is already checked. continue
+ return null;
+ } else if (!mapDestOutputProfile.isEmpty()) {
+ // ---- A DestOutputProfile exits but it isn't the same, error
+ return new ValidationError(
+ ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_MULTIPLE, "More than one ICCProfile is defined");
+ }
+ // else the profile will be kept in the tmpDestOutputProfile if it is valid
+ }
+
+ PDStream stream = PDStream.createFromCOS(COSUtils.getAsStream(
+ destOutputProfile, cDoc));
+ if (stream == null) {
+ return new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY,
+ "OutputIntent object uses a NULL Object");
+ }
+
+ ICC_Profile iccp = ICC_Profile.getInstance(stream.getByteArray());
+ // check the ICC Profile version (6.2.2)
+ if (iccp.getMajorVersion() == 2) {
+ if (iccp.getMinorVersion() > 0x20) {
+ // in PDF 1.4, max version is 02h.20h (meaning V 3.5)
+ return new ValidationError(
+ ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_TOO_RECENT, "Invalid version of the ICCProfile");
+ } // else OK
+ } else if (iccp.getMajorVersion() > 2) {
+ // in PDF 1.4, max version is 02h.20h (meaning V 3.5)
+ return new ValidationError(
+ ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_TOO_RECENT, "Invalid version of the ICCProfile");
+ } // else seems less than 2, so correct
+
+ if (handler.getIccProfileWrapper() == null) {
+ handler.setIccProfileWrapper(new ICCProfileWrapper(iccp));
+ }
+
+ // ---- keep reference to avoid multiple profile definition
+ mapDestOutputProfile.put(new COSObjectKey((COSObject) destOutputProfile),
+ true);
+
+ } catch (IllegalArgumentException e) {
+ // ---- this is not a ICC_Profile
+ return new ValidationError(
+ ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID, "DestOutputProfile isn't a ICCProfile");
+ } catch (IOException e) {
+ throw new ValidationException("Unable to parse the ICC Profile", e);
+ }
+
+ return null;
+ }
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/CatalogValidationHelper.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/FileSpecificationValidationHelper.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/FileSpecificationValidationHelper.java?rev=1150373&view=auto
==============================================================================
--- pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/FileSpecificationValidationHelper.java (added)
+++ pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/FileSpecificationValidationHelper.java Sun Jul 24 14:02:12 2011
@@ -0,0 +1,107 @@
+/*****************************************************************************
+ *
+ * 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.padaf.preflight.helpers;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+import org.apache.padaf.preflight.DocumentHandler;
+import org.apache.padaf.preflight.ValidationException;
+import org.apache.padaf.preflight.ValidatorConfig;
+import org.apache.padaf.preflight.ValidationResult.ValidationError;
+import org.apache.pdfbox.cos.COSBase;
+import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSDocument;
+import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSObject;
+import org.apache.pdfbox.pdmodel.PDDocument;
+
+/**
+ * This helper validates FileSpec dictionaries
+ */
+public class FileSpecificationValidationHelper extends AbstractValidationHelper {
+
+ public FileSpecificationValidationHelper(ValidatorConfig cfg)
+ throws ValidationException {
+ super(cfg);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * net.awl.edoc.pdfa.validation.helpers.AbstractValidationHelper#innerValidate
+ * (net.awl.edoc.pdfa.validation.DocumentHandler)
+ */
+ @Override
+ public List<ValidationError> innerValidate(DocumentHandler handler)
+ throws ValidationException {
+ List<ValidationError> result = new ArrayList<ValidationError>(0);
+ PDDocument pdfDoc = handler.getDocument();
+ COSDocument cDoc = pdfDoc.getDocument();
+
+ List<?> lCOSObj = cDoc.getObjects();
+ for (Object o : lCOSObj) {
+ COSObject cObj = (COSObject) o;
+
+ // ---- If this object represents a Stream
+ // The Dictionary must contain the Length key
+ COSBase cBase = cObj.getObject();
+ if (cBase instanceof COSDictionary) {
+ COSDictionary dic = (COSDictionary) cBase;
+ String type = dic.getNameAsString(COSName
+ .getPDFName(DICTIONARY_KEY_TYPE));
+ if (FILE_SPECIFICATION_VALUE_TYPE.equals(type)) {
+ // ---- It is a file specification
+ result.addAll(validateFileSpecification(handler, cObj));
+ }
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Validate a FileSpec dictionary, a FileSpec dictionary mustn't have the EF
+ * (EmbeddedFile) entry.
+ *
+ * @param handler
+ * The document handler
+ * @param cObj
+ * the FileSpec Dictionary
+ * @return
+ */
+ public List<ValidationError> validateFileSpecification(
+ DocumentHandler handler, COSObject cObj) {
+ List<ValidationError> result = new ArrayList<ValidationError>(0);
+ COSDictionary fileSpec = (COSDictionary) cObj.getObject();
+
+ // ---- Check dictionary entries
+ // ---- Only the EF entry is forbidden
+ if (fileSpec.getItem(COSName
+ .getPDFName(FILE_SPECIFICATION_KEY_EMBEDDED_FILE)) != null) {
+ result.add(new ValidationError(ERROR_SYNTAX_EMBEDDED_FILES,"EmbeddedFile entry is present in a FileSpecification dictionary"));
+ }
+
+ return result;
+ }
+}
Propchange: pdfbox/trunk/preflight/src/main/java/org/apache/padaf/preflight/helpers/FileSpecificationValidationHelper.java
------------------------------------------------------------------------------
svn:eol-style = native