KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > staticexport > CmsLink


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/staticexport/CmsLink.java,v $
3  * Date : $Date: 2006/03/27 14:52:43 $
4  * Version: $Revision: 1.27 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.staticexport;
33
34 import org.opencms.site.CmsSiteManager;
35 import org.opencms.util.CmsRequestUtil;
36 import org.opencms.util.CmsUriSplitter;
37 import org.opencms.xml.page.CmsXmlPage;
38
39 import java.util.Map JavaDoc;
40 import java.util.Set JavaDoc;
41
42 import org.dom4j.Element;
43
44 /**
45  * A single link entry in the link table.<p>
46  *
47  * @author Carsten Weinholz
48  *
49  * @version $Revision: 1.27 $
50  *
51  * @since 6.0.0
52  */

53 public class CmsLink {
54
55     /** The anchor of the uri, if any. */
56     private String JavaDoc m_anchor;
57
58     /** The xml element reference. */
59     private Element m_element;
60
61     /** Indicates if the link is an internal link within the OpenCms VFS. */
62     private boolean m_internal;
63
64     /** The internal name of the link. */
65     private String JavaDoc m_name;
66
67     /** The parameters of the query , if any. */
68     private Map JavaDoc m_parameters;
69
70     /** The query, if any. */
71     private String JavaDoc m_query;
72
73     /** The site root of the (internal) link. */
74     private String JavaDoc m_siteRoot;
75
76     /** The link target (destination). */
77     private String JavaDoc m_target;
78
79     /** The type of the link. */
80     private String JavaDoc m_type;
81
82     /** The raw uri. */
83     private String JavaDoc m_uri;
84
85     /**
86      * Creates a new link object with a reference to the xml page link element.<p>
87      *
88      * @param element the xml link element reference
89      * @param name the internal name of this link
90      * @param type the type of this link
91      * @param uri the link uri
92      * @param internal indicates if the link is internal within OpenCms
93      */

94     public CmsLink(Element element, String JavaDoc name, String JavaDoc type, String JavaDoc uri, boolean internal) {
95
96         m_element = element;
97         m_name = name;
98         m_type = type;
99         m_internal = internal;
100
101         m_uri = uri;
102
103         // update component members from the uri
104
setComponents(m_uri);
105     }
106
107     /**
108      * Creates a new link object with a reference to the xml page link element.<p>
109      *
110      * @param element the xml link element reference
111      * @param name the internal name of this link
112      * @param type the type of this link
113      * @param target the link target (without anchor/query)
114      * @param anchor the anchor or null
115      * @param query the query or null
116      * @param internal indicates if the link is internal within OpenCms
117      */

118     public CmsLink(
119         Element element,
120         String JavaDoc name,
121         String JavaDoc type,
122         String JavaDoc target,
123         String JavaDoc anchor,
124         String JavaDoc query,
125         boolean internal) {
126
127         m_element = element;
128         m_name = name;
129         m_type = type;
130         m_internal = internal;
131
132         m_target = target;
133         m_anchor = anchor;
134         setQuery(query);
135
136         // update the uri from the components
137
m_uri = setUri(m_target, m_anchor, m_query);
138     }
139
140     /**
141      * Creates a new link object without a reference to the xml page link element.<p>
142      *
143      * @param name the internal name of this link
144      * @param type the type of this link
145      * @param uri the link uri
146      * @param internal indicates if the link is internal within OpenCms
147      */

148     public CmsLink(String JavaDoc name, String JavaDoc type, String JavaDoc uri, boolean internal) {
149
150         this(null, name, type, uri, internal);
151     }
152
153     /**
154      * Creates a new link object without a reference to the xml page link element.<p>
155      *
156      * @param name the internal name of this link
157      * @param type the type of this link
158      * @param target the link target (without anchor/query)
159      * @param anchor the anchor or null
160      * @param query the query or null
161      * @param internal indicates if the link is internal within OpenCms
162      */

163     public CmsLink(String JavaDoc name, String JavaDoc type, String JavaDoc target, String JavaDoc anchor, String JavaDoc query, boolean internal) {
164
165         this(null, name, type, target, anchor, query, internal);
166     }
167
168     /**
169      * Returns the anchor of this link.<p>
170      *
171      * @return the anchor or null if undefined
172      */

173     public String JavaDoc getAnchor() {
174
175         return m_anchor;
176     }
177
178     /**
179      * Returns the macro name of this link.<p>
180      *
181      * @return the macro name name of this link
182      */

183     public String JavaDoc getName() {
184
185         return m_name;
186     }
187
188     /**
189      * Returns the first parameter value for the given parameter name.<p>
190      *
191      * @param name the name of the parameter
192      * @return the first value for this name or <code>null</code>
193      */

194     public String JavaDoc getParameter(String JavaDoc name) {
195
196         String JavaDoc[] p = (String JavaDoc[])m_parameters.get(name);
197         if (p != null) {
198             return p[0];
199         }
200
201         return null;
202     }
203
204     /**
205      * Returns the map of parameters of this link.<p>
206      *
207      * @return the map of parameters (<code>Map(String[])</code>)
208      */

209     public Map JavaDoc getParameterMap() {
210
211         return m_parameters;
212     }
213
214     /**
215      * Returns the set of available parameter names for this link.<p>
216      *
217      * @return a <code>Set</code> of parameter names
218      */

219     public Set JavaDoc getParameterNames() {
220
221         return m_parameters.keySet();
222     }
223
224     /**
225      * Returns all parameter values for the given name.<p>
226      *
227      * @param name the name of the parameter
228      * @return a <code>String[]</code> of all parameter values or <code>null</code>
229      */

230     public String JavaDoc[] getParameterValues(String JavaDoc name) {
231
232         return (String JavaDoc[])m_parameters.get(name);
233     }
234
235     /**
236      * Returns the query of this link.<p>
237      *
238      * @return the query or null if undefined
239      */

240     public String JavaDoc getQuery() {
241
242         return m_query;
243     }
244
245     /**
246      * Return the site root of the target if it is internal.<p>
247      *
248      * @return the site root or null
249      */

250     public String JavaDoc getSiteRoot() {
251
252         if (m_siteRoot != null) {
253             return m_siteRoot;
254         }
255         if (m_internal) {
256             m_siteRoot = CmsSiteManager.getSiteRoot(m_target);
257             return m_siteRoot;
258         }
259         return null;
260     }
261
262     /**
263      * Returns the target (destination) of this link.<p>
264      *
265      * @return the target the target (destination) of this link
266      */

267     public String JavaDoc getTarget() {
268
269         return m_target;
270     }
271
272     /**
273      * Returns the type of this link.<p>
274      *
275      * @return the type of this link
276      */

277     public String JavaDoc getType() {
278
279         return m_type;
280     }
281
282     /**
283      * Returns the raw uri of this link.<p>
284      *
285      * @return the uri
286      */

287     public String JavaDoc getUri() {
288
289         return m_uri;
290     }
291
292     /**
293      * Returns the vfs link of the target if it is internal.<p>
294      *
295      * @return the full link destination or null if the link is not internal.
296      */

297     public String JavaDoc getVfsUri() {
298
299         if (m_internal) {
300             String JavaDoc siteRoot = getSiteRoot();
301             return m_uri.substring(siteRoot.length());
302         }
303
304         return null;
305     }
306
307     /**
308      * Returns if the link is internal.<p>
309      *
310      * @return true if the link is a local link
311      */

312     public boolean isInternal() {
313
314         return m_internal;
315     }
316
317     /**
318      * @see java.lang.Object#toString()
319      */

320     public String JavaDoc toString() {
321
322         return m_uri;
323     }
324
325     /**
326      * Updates the uri of this link with a new value.<p>
327      *
328      * Also updates the structure of the underlying XML page document this link belongs to.<p>
329      *
330      * Note that you can <b>not</b> update the "internal" or "type" values of the link,
331      * so the new link must be of same type (A, IMG) and also remain either an internal or external link.<p>
332      *
333      * @param uri the uri to update this link with <code>scheme://authority/path#anchor?query</code>
334      */

335     public void updateLink(String JavaDoc uri) {
336
337         // set the uri
338
m_uri = uri;
339
340         // update the components
341
setComponents(m_uri);
342
343         // update the xml
344
updateXml();
345     }
346
347     /**
348      * Updates the uri of this link with a new target, anchor and query.<p>
349      *
350      * If anchor and/or query are <code>null</code>, this features are not used.<p>
351      *
352      * Note that you can <b>not</b> update the "internal" or "type" values of the link,
353      * so the new link must be of same type (A, IMG) and also remain either an internal or external link.<p>
354      *
355      * Also updates the structure of the underlying XML page document this link belongs to.<p>
356      *
357      * @param target the target (destination) of this link
358      * @param anchor the anchor or null if undefined
359      * @param query the query or null if undefined
360      */

361     public void updateLink(String JavaDoc target, String JavaDoc anchor, String JavaDoc query) {
362
363         // set the components
364
m_target = target;
365         m_anchor = anchor;
366         setQuery(query);
367
368         // create the uri from the components
369
m_uri = setUri(m_target, m_anchor, m_query);
370
371         // update the xml
372
updateXml();
373     }
374
375     /**
376      * Sets the component member variables (target, anchor, query)
377      * by splitting the uri <code>scheme://authority/path#anchor?query</code>.<p>
378      *
379      * @param uri the uri to update the component members with
380      */

381     private void setComponents(String JavaDoc uri) {
382
383         CmsUriSplitter splitter = new CmsUriSplitter(uri, true);
384         m_target = splitter.getPrefix();
385         m_anchor = splitter.getAnchor();
386         setQuery(splitter.getQuery());
387
388         // initialize the parameter map
389
m_parameters = CmsRequestUtil.createParameterMap(m_query);
390     }
391
392     /**
393      * Sets the query of the link.<p>
394      *
395      * @param query the query to set.
396      */

397     private void setQuery(String JavaDoc query) {
398
399         m_query = CmsLinkProcessor.unescapeLink(query);
400     }
401
402     /**
403      * Joins the given components to one uri string.<p>
404      *
405      * @param target the link target (without anchor/query)
406      * @param anchor the anchor or null
407      * @param query the query or null
408      *
409      * @return the composed uri
410      */

411     private String JavaDoc setUri(String JavaDoc target, String JavaDoc anchor, String JavaDoc query) {
412
413         StringBuffer JavaDoc uri = new StringBuffer JavaDoc(64);
414         uri.append(target);
415         if (query != null) {
416             uri.append('?');
417             uri.append(query);
418         }
419         if (anchor != null) {
420             uri.append('#');
421             uri.append(anchor);
422         }
423
424         // initialize the parameter map
425
m_parameters = CmsRequestUtil.createParameterMap(query);
426
427         return uri.toString();
428     }
429
430     /**
431      * Update the link node in the underlying XML page document.<p>
432      */

433     private void updateXml() {
434
435         // check if this link node has a reference to the XML document
436
if (m_element != null) {
437
438             // handle <target> node in XML document
439
Element targetElement = m_element.element(CmsXmlPage.NODE_TARGET);
440             targetElement.clearContent();
441             targetElement.addCDATA(m_target);
442
443             // handle <anchor> node in XML document
444
Element anchorElement = m_element.element(CmsXmlPage.NODE_ANCHOR);
445             if (m_anchor != null) {
446                 if (anchorElement == null) {
447                     // element wasn't there before, add element and set value
448
m_element.addElement(CmsXmlPage.NODE_ANCHOR).addCDATA(m_anchor);
449                 } else {
450                     // element is there, update element value
451
anchorElement.clearContent();
452                     anchorElement.addCDATA(m_anchor);
453                 }
454             } else {
455                 // use remove method only when element exists
456
if (anchorElement != null) {
457                     // remove element
458
m_element.remove(anchorElement);
459                 }
460             }
461
462             // handle <query> node in XML document
463
Element queryElement = m_element.element(CmsXmlPage.NODE_QUERY);
464             if (m_query != null) {
465                 if (queryElement == null) {
466                     // element wasn't there before, add element and set value
467
m_element.addElement(CmsXmlPage.NODE_QUERY).addCDATA(m_query);
468                 } else {
469                     // element is there, update element value
470
queryElement.clearContent();
471                     queryElement.addCDATA(m_query);
472                 }
473             } else {
474                 // use remove method only when element exists
475
if (queryElement != null) {
476                     // remove element
477
m_element.remove(queryElement);
478                 }
479             }
480         }
481     }
482 }
Popular Tags