001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.pd;
031:
032: import de.intarsys.pdf.cos.COSBasedObject;
033: import de.intarsys.pdf.cos.COSName;
034: import de.intarsys.pdf.cos.COSObject;
035:
036: /**
037: * A logical signature field within an AcroForm.
038: *
039: */
040: public class PDAFSignatureField extends PDAcroFormField {
041: /*
042: * protected PDSignatureLock cosGetLock() { COSObject cosObject =
043: * cosGetField(DK_Lock); if (cosObject != null) { return (PDSignatureLock)
044: * PDSignatureLock.createFromCos( this, (COSDictionary) cosObject ); }
045: * return null; }
046: *
047: * protected PDSeedValue cosGetSeedValue() { COSObject cosObject =
048: * cosGetField(DK_SV); if (cosObject != null) { return (PDSeedValue)
049: * PDSeedValue.createFromCos( this, (COSDictionary) cosObject ); } return
050: * null; }
051: *
052: * protected void cosSetLock(PDSignatureLock newLock) { if (newLock != null) {
053: * cosSetField(DK_Lock, newLock.cosSerialize()); } else {
054: * cosSetField(DK_Lock, null); } }
055: *
056: * protected void cosSetSeedValue(PDSeedValue newSeedValue) { if
057: * (newSeedValue != null) { cosSetField(DK_SV, newSeedValue.cosSerialize()); }
058: * else { cosSetField(DK_SV, null); } }
059: */
060: static public class MetaClass extends PDAcroFormField.MetaClass {
061: protected MetaClass(Class instanceClass) {
062: super (instanceClass);
063: }
064:
065: protected COSBasedObject doCreateCOSBasedObject(COSObject object) {
066: return new PDAFSignatureField(object);
067: }
068: }
069:
070: static public final COSName DK_Lock = COSName.constant("Lock"); //$NON-NLS-1$
071:
072: static public final COSName DK_SV = COSName.constant("SV"); //$NON-NLS-1$
073:
074: /** The meta class instance */
075: static public final MetaClass META = new MetaClass(MetaClass.class
076: .getDeclaringClass());
077:
078: private PDSignature cachedSignature;
079:
080: protected PDAFSignatureField(COSObject object) {
081: super (object);
082: }
083:
084: /*
085: * (non-Javadoc)
086: *
087: * @see de.intarsys.pdf.pd.PDAcroFormField#cosGetExpectedFieldType()
088: */
089: public COSName cosGetExpectedFieldType() {
090: return CN_FT_Sig;
091: }
092:
093: public COSObject cosSetValue(COSObject newValue) {
094: COSObject result = super .cosSetValue(newValue);
095: AcroFormSigFlags sigFlags = getAcroForm().getSigFlags();
096: sigFlags.setAppendOnly(getAcroForm().isSigned());
097: return result;
098: }
099:
100: /**
101: * The associated {@link PDSignature} if available.
102: *
103: * @return The associated {@link PDSignature} if available.
104: */
105: public PDSignature getSignature() {
106: if (cachedSignature == null) {
107: cachedSignature = (PDSignature) PDSignature.META
108: .createFromCos(cosGetValue());
109: if (cachedSignature != null) {
110: cachedSignature.setAcroFormField(this );
111: }
112: }
113: return cachedSignature;
114: }
115:
116: /**
117: * <code>true</code> if this field is already signed.
118: *
119: * @return <code>true</code> if this field is already signed.
120: */
121: public boolean isSigned() {
122: return !cosGetValue().isNull();
123: }
124:
125: /*
126: * (non-Javadoc)
127: *
128: * @see de.intarsys.pdf.pd.PDAcroFormField#isTypeSig()
129: */
130: public boolean isTypeSig() {
131: return true;
132: }
133:
134: /**
135: * Assign a new signature value.
136: *
137: * @param newSignature
138: * The new signature value.
139: */
140: public void setSignature(PDSignature newSignature) {
141: if (cachedSignature != null) {
142: cachedSignature.setAcroFormField(null);
143: }
144: cachedSignature = newSignature;
145:
146: if (newSignature != null) {
147: newSignature.setAcroFormField(this );
148: cosSetValue(newSignature.cosGetObject());
149: } else {
150: cosSetValue(null);
151: }
152: }
153:
154: public void invalidateCaches() {
155: super .invalidateCaches();
156: cachedSignature = null;
157: }
158:
159: public void clearSignature() {
160: setSignature(null);
161: }
162: }
|