001: package com.canoo.webtest.plugins.pdftest.htmlunit.pdfbox;
002:
003: import java.io.IOException;
004: import java.util.List;
005: import java.util.ListIterator;
006: import java.util.Map;
007: import java.util.WeakHashMap;
008:
009: import org.apache.commons.collections.Predicate;
010: import org.pdfbox.cos.COSDictionary;
011: import org.pdfbox.cos.COSName;
012: import org.pdfbox.pdmodel.PDPage;
013: import org.pdfbox.pdmodel.interactive.form.PDField;
014:
015: import com.canoo.webtest.plugins.pdftest.htmlunit.PDFField;
016:
017: /**
018: * Implementation of {@link PDFField} based on
019: * <a href="http://www.pdfbox.org/">PDFBox</a>.
020: * @author Etienne Studer
021: * @author Marc Guillemot
022: */
023: public class PdfBoxPDFField implements PDFField {
024: private final PDField field_;
025:
026: private PdfBoxPDFField(final PDField field) {
027: field_ = field;
028: }
029:
030: public String toString() {
031: final StringBuffer sb = new StringBuffer();
032: sb.append("name: " + getName() + ", ");
033: sb.append("value: " + getValue());
034: return sb.toString();
035: }
036:
037: public String getValue() {
038: try {
039: final PDField parent = field_.getParent();
040: final PDField field = parent != null ? parent : field_;
041:
042: String value = field.getValue();
043: if (value == null)
044: value = PdfBoxPDFPage.stringValue(field.getDictionary()
045: .getDictionaryObject(COSName.DV));
046:
047: return value;
048: } catch (final IOException e) {
049: throw new RuntimeException("Can't read field value", e);
050: }
051: }
052:
053: protected static int getPageNumber(final PDField nativeField) {
054: int pageNumber = -1;
055: try {
056: final COSDictionary page = (COSDictionary) nativeField
057: .getWidget().getDictionary().getDictionaryObject(
058: COSName.P);
059: final List pages = nativeField.getAcroForm().getDocument()
060: .getDocumentCatalog().getAllPages();
061: for (final ListIterator iter = pages.listIterator(); iter
062: .hasNext();) {
063: final PDPage pageX = (PDPage) iter.next();
064: if (pageX.getCOSDictionary().equals(page)) {
065: pageNumber = iter.previousIndex();
066: break;
067: }
068: }
069: } catch (final IOException e) {
070: throw new RuntimeException("Failed accessing the page", e);
071: }
072:
073: if (pageNumber == -1)
074: throw new RuntimeException(
075: "Failed retrieving page number for field "
076: + nativeField);
077: return 1 + pageNumber;
078: }
079:
080: public PDField getNativeField() {
081: return field_;
082: }
083:
084: public boolean isReadOnly() {
085: return getNativeField().isReadonly();
086: }
087:
088: /**
089: * Gets the field name
090: */
091: public String getName() {
092: try {
093: return getNativeField().getFullyQualifiedName();
094: } catch (final IOException e) {
095: throw new RuntimeException("Problem extracting field name",
096: e);
097: }
098: }
099:
100: private static final Map wrappers_ = new WeakHashMap();
101:
102: static public PDFField wrap(final PDField nativeField) {
103: PDFField wrapperField = (PDFField) wrappers_.get(nativeField);
104: if (wrapperField == null) {
105: wrapperField = new PdfBoxPDFField(nativeField);
106: wrappers_.put(nativeField, wrapperField);
107: }
108: return wrapperField;
109: }
110:
111: protected abstract static class FieldPredicate implements Predicate {
112: public boolean evaluate(final Object object) {
113: try {
114: return evaluateField((PDField) object);
115: } catch (final IOException e) {
116: throw new RuntimeException(e);
117: }
118: }
119:
120: protected abstract boolean evaluateField(final PDField field)
121: throws IOException;
122:
123: /**
124: * Builds a field predicate accepting only fields with the given name
125: * @param name the name to filter with
126: * @return a predicate
127: */
128: static FieldPredicate buildNamePredicate(final String name) {
129: return new FieldPredicate() {
130: protected boolean evaluateField(final PDField field)
131: throws IOException {
132: final PDField parent = field.getParent();
133: final String fieldName;
134: if (parent != null)
135: fieldName = parent.getFullyQualifiedName();
136: else
137: fieldName = field.getFullyQualifiedName();
138: return name.equals(fieldName);
139: }
140: };
141: }
142:
143: /**
144: * Builds a field predicate accepting only fields located on the given page
145: * @param pageNumber the page on which fields should be located
146: * @return a predicate
147: */
148: static FieldPredicate buildPageNumberPredicate(
149: final int pageNumber) {
150: return new FieldPredicate() {
151: protected boolean evaluateField(final PDField field)
152: throws IOException {
153: return pageNumber == getPageNumber(field);
154: }
155: };
156: }
157: }
158: }
|