KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > pdf > PDFOutline


1 /*
2  * $Id: PDFOutline.java,v 1.3.2.3 2003/02/25 14:29:37 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.pdf;
52
53 // Java
54
import java.io.UnsupportedEncodingException JavaDoc;
55 import java.util.ArrayList JavaDoc;
56
57
58 /**
59  * This represents a single Outline object in a PDF, including the root Outlines
60  * object. Outlines provide the bookmark bar, usually rendered to the right of
61  * a PDF document in user agents such as Acrobat Reader
62  *
63  * @author Kelly A. Campbell
64  *
65  */

66
67 public class PDFOutline extends PDFObject {
68
69     /**
70      * list of sub-entries (outline objects)
71      */

72     private ArrayList JavaDoc _subentries;
73
74     /**
75      * parent outline object. Root Outlines parent is null
76      */

77     private PDFOutline _parent;
78
79     private PDFOutline _prev;
80     private PDFOutline _next;
81
82     private PDFOutline _first;
83     private PDFOutline _last;
84
85     private int _count;
86
87
88     /**
89      * title to display for the bookmark entry
90      */

91     private String JavaDoc _title;
92
93     String JavaDoc _actionRef;
94
95
96
97     /**
98      * @param number the object id number
99      * @param title the title of the outline entry (can only be null for root Outlines obj)
100      * @param page the page which this outline refers to.
101      */

102     public PDFOutline(int number, String JavaDoc title, String JavaDoc action) {
103         super(number);
104         _subentries = new ArrayList JavaDoc();
105         _count = 0;
106         _parent = null;
107         _prev = null;
108         _next = null;
109         _first = null;
110         _last = null;
111         _title = title;
112         _actionRef = action;
113
114
115     }
116
117     public void setTitle(String JavaDoc title) {
118         _title = title;
119     }
120
121     /**
122      * Add a sub element to this outline
123      */

124     public void addOutline(PDFOutline outline) {
125         if (_subentries.size() > 0) {
126             outline._prev =
127                 (PDFOutline)_subentries.get(_subentries.size() - 1);
128             outline._prev._next = outline;
129         } else {
130             _first = outline;
131         }
132
133         _subentries.add(outline);
134         outline._parent = this;
135
136         incrementCount(); // note: count is not just the immediate children
137

138         _last = outline;
139
140     }
141
142     private void incrementCount() {
143         // count is a total of our immediate subentries and all descendent subentries
144
_count++;
145         if (_parent != null) {
146             _parent.incrementCount();
147         }
148     }
149
150
151     /**
152      * represent the object in PDF
153      */

154     protected byte[] toPDF() {
155         StringBuffer JavaDoc result = new StringBuffer JavaDoc(this.number + " "
156                                                + this.generation
157                                                + " obj\n<<\n");
158         if (_parent == null) {
159             // root Outlines object
160
if (_first != null && _last != null) {
161                 result.append(" /First " + _first.referencePDF() + "\n");
162                 result.append(" /Last " + _last.referencePDF() + "\n");
163                 // no count... we start with the outline completely closed for now
164
}
165         } else {
166             // subentry Outline object
167
result.append(" /Title (" + escapeString(_title) + ")\n");
168             result.append(" /Parent " + _parent.referencePDF() + "\n");
169             if (_first != null && _last != null) {
170                 result.append(" /First " + _first.referencePDF() + "\n");
171                 result.append(" /Last " + _last.referencePDF() + "\n");
172             }
173             if (_prev != null) {
174                 result.append(" /Prev " + _prev.referencePDF() + "\n");
175             }
176             if (_next != null) {
177                 result.append(" /Next " + _next.referencePDF() + "\n");
178             }
179             if (_count > 0) {
180                 result.append(" /Count -" + _count + "\n");
181             }
182
183             if (_actionRef != null) {
184                 result.append(" /A " + _actionRef + "\n");
185             }
186
187
188         }
189         result.append(">> endobj\n");
190
191         try {
192             return result.toString().getBytes(PDFDocument.ENCODING);
193         } catch (UnsupportedEncodingException JavaDoc ue) {
194             return result.toString().getBytes();
195         }
196     }
197
198     /**
199      * escape string (see 3.8.1 in PDF reference 2nd edition)
200      */

201     private String JavaDoc escapeString(String JavaDoc s) {
202         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
203         if (s != null) {
204             int l = s.length();
205
206             // byte order marker (0xfeff)
207
result.append("\\376\\377");
208
209             for (int i = 0; i < l; i++) {
210                 char ch = s.charAt(i);
211                 int high = (ch & 0xff00) >>> 8;
212                 int low = ch & 0xff;
213                 result.append("\\");
214                 result.append(Integer.toOctalString(high));
215                 result.append("\\");
216                 result.append(Integer.toOctalString(low));
217             }
218         }
219
220         return result.toString();
221     }
222
223 }
224
Popular Tags