001: /*
002: * $Id: PDFAction.java,v 1.2 2007/12/20 18:33:34 rbair Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
020: */
021:
022: package com.sun.pdfview.action;
023:
024: import java.io.IOException;
025:
026: import com.sun.pdfview.PDFObject;
027: import com.sun.pdfview.PDFParseException;
028:
029: /**
030: * The common super-class of all PDF actions.
031: */
032: public class PDFAction {
033: /** the type of this action */
034: private String type;
035:
036: /** the next action or array of actions */
037: private PDFObject next;
038:
039: /** Creates a new instance of PDFAction */
040: public PDFAction(String type) {
041: this .type = type;
042: }
043:
044: /**
045: * Get an action of the appropriate type from a PDFObject
046: *
047: * @param obj the PDF object containing the action to parse
048: * @param root the root of the PDF object tree
049: */
050: public static PDFAction getAction(PDFObject obj, PDFObject root)
051: throws IOException {
052: // figure out the action type
053: PDFObject typeObj = obj.getDictRef("S");
054: if (typeObj == null) {
055: throw new PDFParseException("No action type in object: "
056: + obj);
057: }
058:
059: // create the action based on the type
060: PDFAction action = null;
061: String type = typeObj.getStringValue();
062: if (type.equals("GoTo")) {
063: action = new GoToAction(obj, root);
064: } else {
065: /** [JK FIXME: Implement other action types! ] */
066: throw new PDFParseException("Unknown Action type: " + type);
067: }
068:
069: // figure out if there is a next action
070: PDFObject nextObj = obj.getDictRef("Next");
071: if (nextObj != null) {
072: action.setNext(nextObj);
073: }
074:
075: // return the action
076: return action;
077: }
078:
079: /**
080: * Get the type of this action
081: */
082: public String getType() {
083: return type;
084: }
085:
086: /**
087: * Get the next action or array of actions
088: */
089: public PDFObject getNext() {
090: return next;
091: }
092:
093: /**
094: * Set the next action or array of actions
095: */
096: public void setNext(PDFObject next) {
097: this.next = next;
098: }
099:
100: }
|