1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /* $Id: PDFGoToRemote.java 1610839 2014-07-15 20:25:58Z vhennebert $ */
19 
20 package org.apache.fop.pdf;
21 
22 /**
23  * Class representing a /GoToR object.
24  */
25 public class PDFGoToRemote extends PDFAction {
26 
27     /**
28      * the file specification
29      */
30     private PDFReference pdfFileSpec;
31     private int pageReference;
32     private String destination;
33     private boolean newWindow;
34 
35     /**
36      * Create an GoToR object.
37      *
38      * @param pdfFileSpec the fileSpec associated with the action
39      * @param newWindow boolean indicating whether the target should be
40      *                  displayed in a new window
41      */
PDFGoToRemote(PDFFileSpec pdfFileSpec, boolean newWindow)42     public PDFGoToRemote(PDFFileSpec pdfFileSpec, boolean newWindow) {
43         /* generic creation of object */
44         super();
45 
46         this.pdfFileSpec = pdfFileSpec.makeReference();
47         this.newWindow = newWindow;
48     }
49 
50     /**
51      * Create an GoToR object.
52      *
53      * @param pdfFileSpec the fileSpec associated with the action
54      * @param page a page reference within the remote document
55      * @param newWindow boolean indicating whether the target should be
56      *                  displayed in a new window
57      */
PDFGoToRemote(PDFFileSpec pdfFileSpec, int page, boolean newWindow)58     public PDFGoToRemote(PDFFileSpec pdfFileSpec, int page, boolean newWindow) {
59         this(pdfFileSpec.makeReference(), page, newWindow);
60     }
61 
62     /**
63      * Create an GoToR object.
64      *
65      * @param pdfFileSpec the fileSpec associated with the action
66      * @param page a page reference within the remote document
67      * @param newWindow boolean indicating whether the target should be
68      *                  displayed in a new window
69      */
PDFGoToRemote(PDFReference pdfFileSpec, int page, boolean newWindow)70     public PDFGoToRemote(PDFReference pdfFileSpec, int page, boolean newWindow) {
71         super();
72 
73         this.pdfFileSpec = pdfFileSpec;
74         this.pageReference = page;
75         this.newWindow = newWindow;
76     }
77 
78     /**
79      * create an GoToR object.
80      *
81      * @param pdfFileSpec the fileSpec associated with the action
82      * @param dest a named destination within the remote document
83      * @param newWindow boolean indicating whether the target should be
84      *                  displayed in a new window
85      */
PDFGoToRemote(PDFFileSpec pdfFileSpec, String dest, boolean newWindow)86     public PDFGoToRemote(PDFFileSpec pdfFileSpec, String dest, boolean newWindow) {
87         /* generic creation of object */
88         super();
89 
90         this.pdfFileSpec = pdfFileSpec.makeReference();
91         this.destination = dest;
92         this.newWindow = newWindow;
93     }
94 
95     /**
96      * return the action string which will reference this object
97      *
98      * @return the action String
99      */
getAction()100     public String getAction() {
101         return this.referencePDF();
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
toPDFString()107     public String toPDFString() {
108         StringBuffer sb = new StringBuffer(64);
109         sb.append("<<\n/S /GoToR\n/F ");
110         sb.append(pdfFileSpec.toString());
111         sb.append("\n");
112 
113         if (destination != null) {
114             sb.append("/D (").append(this.destination).append(")");
115         } else {
116             sb.append("/D [ ").append(this.pageReference).append(" /XYZ null null null ]");
117         }
118 
119         if (newWindow) {
120             sb.append("/NewWindow true");
121         }
122 
123         sb.append("\n>>");
124 
125         return sb.toString();
126     }
127 
128 
129     /*
130      * example
131      * 28 0 obj
132      * <<
133      * /S /GoToR
134      * /F 29 0 R
135      * /D [ 0 /XYZ -6 797 null ]
136      * >>
137      * endobj
138      */
139 
140     /** {@inheritDoc} */
contentEquals(PDFObject obj)141     protected boolean contentEquals(PDFObject obj) {
142         if (this == obj) {
143             return true;
144         }
145 
146         if (obj == null || !(obj instanceof PDFGoToRemote)) {
147             return false;
148         }
149 
150         PDFGoToRemote remote = (PDFGoToRemote)obj;
151 
152         if (!remote.pdfFileSpec.toString().equals(pdfFileSpec.toString())) {
153             return false;
154         }
155 
156         if (destination != null) {
157             if (!destination.equals(remote.destination)) {
158                 return false;
159             }
160         } else {
161             if (pageReference != remote.pageReference) {
162                 return false;
163             }
164         }
165 
166         return (this.newWindow == remote.newWindow);
167     }
168 }
169 
170