001: /**
002: * Copyright (c) 2004-2005, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. 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: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.interactive.documentnavigation.destination;
031:
032: import java.io.IOException;
033:
034: import org.pdfbox.cos.COSArray;
035: import org.pdfbox.cos.COSBase;
036: import org.pdfbox.cos.COSName;
037: import org.pdfbox.cos.COSString;
038:
039: import org.pdfbox.pdmodel.common.PDDestinationOrAction;
040:
041: /**
042: * This represents a destination in a PDF document.
043: *
044: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
045: * @version $Revision: 1.6 $
046: */
047: public abstract class PDDestination implements PDDestinationOrAction {
048:
049: /**
050: * This will create a new destination depending on the type of COSBase
051: * that is passed in.
052: *
053: * @param base The base level object.
054: *
055: * @return A new destination.
056: *
057: * @throws IOException If the base cannot be converted to a Destination.
058: */
059: public static PDDestination create(COSBase base) throws IOException {
060: PDDestination retval = null;
061: if (base == null) {
062: //this is ok, just return null.
063: } else if (base instanceof COSArray
064: && ((COSArray) base).size() > 0) {
065: COSArray array = (COSArray) base;
066: COSName type = (COSName) array.getObject(1);
067: String typeString = type.getName();
068: if (typeString.equals(PDPageFitDestination.TYPE)
069: || typeString
070: .equals(PDPageFitDestination.TYPE_BOUNDED)) {
071: retval = new PDPageFitDestination(array);
072: } else if (typeString
073: .equals(PDPageFitHeightDestination.TYPE)
074: || typeString
075: .equals(PDPageFitHeightDestination.TYPE_BOUNDED)) {
076: retval = new PDPageFitHeightDestination(array);
077: } else if (typeString
078: .equals(PDPageFitRectangleDestination.TYPE)) {
079: retval = new PDPageFitRectangleDestination(array);
080: } else if (typeString
081: .equals(PDPageFitWidthDestination.TYPE)
082: || typeString
083: .equals(PDPageFitWidthDestination.TYPE_BOUNDED)) {
084: retval = new PDPageFitWidthDestination(array);
085: } else if (typeString.equals(PDPageXYZDestination.TYPE)) {
086: retval = new PDPageXYZDestination(array);
087: } else {
088: throw new IOException("Unknown destination type:"
089: + type);
090: }
091: } else if (base instanceof COSString) {
092: retval = new PDNamedDestination((COSString) base);
093: } else if (base instanceof COSName) {
094: retval = new PDNamedDestination((COSName) base);
095: } else {
096: throw new IOException(
097: "Error: can't convert to Destination " + base);
098: }
099: return retval;
100: }
101:
102: /**
103: * Return a string representation of this class.
104: *
105: * @return A human readable string.
106: */
107: public String toString() {
108: return super.toString();
109: }
110: }
|