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 java.util.ArrayList;
033: import java.util.List;
034: import de.intarsys.pdf.cos.COSArray;
035: import de.intarsys.pdf.cos.COSBasedObject;
036: import de.intarsys.pdf.cos.COSDictionary;
037: import de.intarsys.pdf.cos.COSName;
038: import de.intarsys.pdf.cos.COSObject;
039:
040: /**
041: * A PDF document can be interative by defining actions for different events.
042: * This is an abstract superclass for the implementation of the various action
043: * types.
044: *
045: */
046: abstract public class PDAction extends PDObject {
047: /**
048: * The meta class implementation
049: */
050: static public class MetaClass extends PDObject.MetaClass {
051: protected MetaClass(Class instanceClass) {
052: super (instanceClass);
053: }
054:
055: public Class getRootClass() {
056: return PDAction.class;
057: }
058:
059: /*
060: * (non-Javadoc)
061: *
062: * @see de.intarsys.pdf.cos.COSBasedObject.MetaClass#doDetermineClass(de.intarsys.pdf.cos.COSObject)
063: */
064: protected COSBasedObject.MetaClass doDetermineClass(
065: COSObject object) {
066: if (object instanceof COSArray) {
067: return PDActionGoTo.META;
068: }
069: COSName actionType = ((COSDictionary) object).get(DK_S)
070: .asName();
071: if (actionType == null) {
072: throw new IllegalArgumentException(
073: "action type not specified");
074: }
075: if (actionType
076: .equals(PDActionSubmitForm.CN_ActionType_SubmitForm)) {
077: return PDActionSubmitForm.META;
078: }
079: if (actionType
080: .equals(PDActionResetForm.CN_ActionType_ResetForm)) {
081: return PDActionResetForm.META;
082: }
083: if (actionType.equals(PDActionGoTo.CN_ActionType_GoTo)) {
084: return PDActionGoTo.META;
085: }
086: if (actionType.equals(PDActionGoToR.CN_ActionType_GoToR)) {
087: return PDActionGoToR.META;
088: }
089: if (actionType
090: .equals(PDActionJavaScript.CN_ActionType_JavaScript)) {
091: return PDActionJavaScript.META;
092: }
093: if (actionType.equals(PDActionNamed.CN_ActionType_Named)) {
094: return PDActionNamed.META;
095: }
096: if (actionType.equals(PDActionURI.CN_ActionType_URI)) {
097: return PDActionURI.META;
098: }
099: if (actionType.equals(PDActionLaunch.CN_ActionType_Launch)) {
100: return PDActionLaunch.META;
101: }
102: return PDActionAny.META;
103: }
104: }
105:
106: /** The meta class instance */
107: static public final MetaClass META = new MetaClass(MetaClass.class
108: .getDeclaringClass());
109:
110: static public final COSName DK_S = COSName.constant("S");
111:
112: static public final COSName DK_Next = COSName.constant("Next");
113:
114: static public final COSName CN_Type_Action = COSName
115: .constant("Action");
116:
117: protected PDAction(COSObject object) {
118: super (object);
119: }
120:
121: /**
122: * The action type expected for a {@link PDAction} of the instantiated
123: * class.
124: *
125: * @return The action type expected for a {@link PDAction} of the
126: * instantiated class.
127: */
128: abstract public COSName cosGetExpectedActionType();
129:
130: /**
131: * The real action type.
132: *
133: * @return The real action type.
134: */
135: public COSName cosGetActionType() {
136: return cosGetField(DK_S).asName();
137: }
138:
139: /**
140: * The {@link List} of {@link PDAction} instances that must be executed
141: * after this.
142: *
143: * @return The {@link List} of {@link PDAction} instances that must be
144: * executed after this.
145: */
146: public List getNext() {
147: List result = null;
148: if (!(cosGetObject() instanceof COSDictionary)) {
149: return null;
150: }
151: COSObject dictNext = cosGetField(DK_Next); // can be Array or Dict
152: if (dictNext instanceof COSDictionary) {
153: result = new ArrayList();
154: result.add(PDAction.META.createFromCos(dictNext));
155: } else if (dictNext instanceof COSArray) {
156: result = getPDObjects(DK_Next, PDAction.META, false);
157: }
158: return result;
159: }
160:
161: /*
162: * (non-Javadoc)
163: *
164: * @see de.intarsys.pdf.pd.PDObject#cosGetExpectedType()
165: */
166: protected COSName cosGetExpectedType() {
167: return CN_Type_Action;
168: }
169:
170: /**
171: * Add a new {@link PDAction} to be executed after this.
172: *
173: * @param action
174: * The new {@link PDAction}
175: */
176: public void addNext(PDAction action) {
177: COSArray nextArray = null;
178: COSObject dictNext = cosGetField(DK_Next); // can be Array or Dict
179:
180: if (dictNext.isNull()) {
181: nextArray = COSArray.create(1);
182: cosSetField(DK_Next, nextArray);
183: } else {
184: if (dictNext instanceof COSArray) {
185: nextArray = (COSArray) dictNext;
186: } else if (dictNext instanceof COSDictionary) {
187: nextArray = COSArray.create(2);
188: nextArray.add(dictNext);
189: cosSetField(DK_Next, nextArray);
190: }
191: }
192: nextArray.add(action.cosGetDict());
193: }
194:
195: /**
196: * Remove a {@link PDAction} from the actions to be executed after this.
197: *
198: * @param action
199: * The {@link PDAction} to be removed
200: */
201: public void removeNext(PDAction action) {
202: COSArray nextArray = null;
203: COSObject dictNext = cosGetField(DK_Next); // can be Array or Dict
204:
205: if (dictNext.isNull()) {
206: //
207: } else {
208: if (dictNext instanceof COSArray) {
209: nextArray = (COSArray) dictNext;
210: nextArray.remove(action.cosGetObject());
211: } else if (dictNext instanceof COSDictionary) {
212: if (dictNext == action.cosGetObject()) {
213: cosRemoveField(DK_Next);
214: }
215: }
216: }
217: }
218:
219: protected void initializeFromScratch() {
220: super .initializeFromScratch();
221: //
222: COSName actionType = cosGetExpectedActionType();
223: if (actionType != null) {
224: cosSetField(DK_S, cosGetExpectedActionType().copyShallow());
225: }
226: }
227: }
|