1 /*
2  * $Id: Anchor.java 3373 2008-05-12 16:21:24Z xlv $
3  *
4  * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above.  If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */

49
50 package com.lowagie.text;
51
52 import java.net.MalformedURLException;
53 import java.net.URL;
54 import java.util.ArrayList;
55 import java.util.Iterator;
56
57 /**
58  * An <CODE>Anchor</CODE> can be a reference or a destination of a reference.
59  * <P>
60  * An <CODE>Anchor</CODE> is a special kind of <CODE>Phrase</CODE>.
61  * It is constructed in the same way.
62  * <P>
63  * Example:
64  * <BLOCKQUOTE><PRE>
65  * <STRONG>Anchor anchor = new Anchor("this is a link");</STRONG>
66  * <STRONG>anchor.setName("LINK");</STRONG>
67  * <STRONG>anchor.setReference("http://www.lowagie.com");</STRONG>
68  * </PRE></BLOCKQUOTE>
69  *
70  * @see        Element
71  * @see        Phrase
72  */

73
74 public class Anchor extends Phrase {
75
76     // constant
77     private static final long serialVersionUID = -852278536049236911L;
78     
79     // membervariables
80     
81     /** This is the name of the <CODE>Anchor</CODE>. */
82     protected String name = null;
83     
84     /** This is the reference of the <CODE>Anchor</CODE>. */
85     protected String reference = null;
86     
87     // constructors
88     
89     /**
90      * Constructs an <CODE>Anchor</CODE> without specifying a leading.
91      */

92     public Anchor() {
93         super(16);
94     }
95     
96     /**
97      * Constructs an <CODE>Anchor</CODE> with a certain leading.
98      *
99      * @param    leading        the leading
100      */

101     
102     public Anchor(float leading) {
103         super(leading);
104     }
105     
106     /**
107      * Constructs an <CODE>Anchor</CODE> with a certain <CODE>Chunk</CODE>.
108      *
109      * @param    chunk        a <CODE>Chunk</CODE>
110      */

111     public Anchor(Chunk chunk) {
112         super(chunk);
113     }
114     
115     /**
116      * Constructs an <CODE>Anchor</CODE> with a certain <CODE>String</CODE>.
117      *
118      * @param    string        a <CODE>String</CODE>
119      */

120     public Anchor(String string) {
121         super(string);
122     }
123     
124     /**
125      * Constructs an <CODE>Anchor</CODE> with a certain <CODE>String</CODE>
126      * and a certain <CODE>Font</CODE>.
127      *
128      * @param    string        a <CODE>String</CODE>
129      * @param    font        a <CODE>Font</CODE>
130      */

131     public Anchor(String string, Font font) {
132         super(string, font);
133     }
134     
135     /**
136      * Constructs an <CODE>Anchor</CODE> with a certain <CODE>Chunk</CODE>
137      * and a certain leading.
138      *
139      * @param    leading        the leading
140      * @param    chunk        a <CODE>Chunk</CODE>
141      */

142     public Anchor(float leading, Chunk chunk) {
143         super(leading, chunk);
144     }
145     
146     /**
147      * Constructs an <CODE>Anchor</CODE> with a certain leading
148      * and a certain <CODE>String</CODE>.
149      *
150      * @param    leading        the leading
151      * @param    string        a <CODE>String</CODE>
152      */

153     public Anchor(float leading, String string) {
154         super(leading, string);
155     }
156     
157     /**
158      * Constructs an <CODE>Anchor</CODE> with a certain leading,
159      * a certain <CODE>String</CODE> and a certain <CODE>Font</CODE>.
160      *
161      * @param    leading        the leading
162      * @param    string        a <CODE>String</CODE>
163      * @param    font        a <CODE>Font</CODE>
164      */

165     public Anchor(float leading, String string, Font font) {
166         super(leading, string, font);
167     }
168     
169     /**
170      * Constructs an <CODE>Anchor</CODE> with a certain <CODE>Phrase</CODE>.
171      *
172      * @param    phrase        a <CODE>Phrase</CODE>
173      */
    
174     public Anchor(Phrase phrase) {
175         super(phrase);
176         if (phrase instanceof Anchor) {
177             Anchor a = (Anchor) phrase;
178             setName(a.name);
179             setReference(a.reference);
180         }
181     }
182     
183     // implementation of the Element-methods
184     
185     /**
186      * Processes the element by adding it (or the different parts) to an
187      * <CODE>ElementListener</CODE>.
188      *
189      * @param    listener    an <CODE>ElementListener</CODE>
190      * @return    <CODE>true</CODE> if the element was processed successfully
191      */

192     public boolean process(ElementListener listener) {
193         try {
194             Chunk chunk;
195             Iterator i = getChunks().iterator();
196             boolean localDestination = (reference != null && reference.startsWith("#"));
197             boolean notGotoOK = true;
198             while (i.hasNext()) {
199                 chunk = (Chunk) i.next();
200                 if (name != null && notGotoOK && !chunk.isEmpty()) {
201                     chunk.setLocalDestination(name);
202                     notGotoOK = false;
203                 }
204                 if (localDestination) {
205                     chunk.setLocalGoto(reference.substring(1));
206                 }
207                 listener.add(chunk);
208             }
209             return true;
210         }
211         catch(DocumentException de) {
212             return false;
213         }
214     }
215     
216     /**
217      * Gets all the chunks in this element.
218      *
219      * @return    an <CODE>ArrayList</CODE>
220      */

221     public ArrayList getChunks() {
222         ArrayList tmp = new ArrayList();
223         Chunk chunk;
224         Iterator i = iterator();
225         boolean localDestination = (reference != null && reference.startsWith("#"));
226         boolean notGotoOK = true;
227         while (i.hasNext()) {
228             chunk = (Chunk) i.next();
229             if (name != null && notGotoOK && !chunk.isEmpty()) {
230                 chunk.setLocalDestination(name);
231                 notGotoOK = false;
232             }
233             if (localDestination) {
234                 chunk.setLocalGoto(reference.substring(1));
235             }
236             else if (reference != null)
237                 chunk.setAnchor(reference);
238             tmp.add(chunk);
239         }
240         return tmp;
241     }
242     
243     /**
244      * Gets the type of the text element.
245      *
246      * @return    a type
247      */

248     public int type() {
249         return Element.ANCHOR;
250     }
251     
252     // methods
253     
254     /**
255      * Sets the name of this <CODE>Anchor</CODE>.
256      *
257      * @param    name        a new name
258      */

259     public void setName(String name) {
260         this.name = name;
261     }
262     
263     /**
264      * Sets the reference of this <CODE>Anchor</CODE>.
265      *
266      * @param    reference        a new reference
267      */

268     public void setReference(String reference) {
269         this.reference = reference;
270     }
271     
272     // methods to retrieve information
273
274     /**
275      * Returns the name of this <CODE>Anchor</CODE>.
276      *
277      * @return    a name
278      */
   
279     public String getName() {
280         return name;
281     }
282
283     /**
284      * Gets the reference of this <CODE>Anchor</CODE>.
285      *
286      * @return    a reference
287      */

288     public String getReference() {
289         return reference;
290     }
291
292     /**
293      * Gets the reference of this <CODE>Anchor</CODE>.
294      *
295      * @return    an <CODE>URL</CODE>
296      */

297     public URL getUrl() {
298         try {
299             return new URL(reference);
300         }
301         catch(MalformedURLException mue) {
302             return null;
303         }
304     }
305
306 }
307