KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/staticexport/CmsLinkProcessor.java,v $
3  * Date : $Date: 2006/09/22 16:19:12 $
4  * Version: $Revision: 1.47 $
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.file.CmsObject;
35 import org.opencms.file.CmsPropertyDefinition;
36 import org.opencms.file.CmsRequestContext;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.OpenCms;
39 import org.opencms.util.CmsHtmlParser;
40 import org.opencms.util.CmsMacroResolver;
41 import org.opencms.util.CmsStringUtil;
42
43 import java.util.Vector JavaDoc;
44
45 import org.htmlparser.Attribute;
46 import org.htmlparser.Tag;
47 import org.htmlparser.tags.ImageTag;
48 import org.htmlparser.tags.LinkTag;
49 import org.htmlparser.util.ParserException;
50
51 /**
52  * Implements the HTML parser node visitor pattern to
53  * exchange all links on the page.<p>
54  *
55  * @author Alexander Kandzior
56  *
57  * @version $Revision: 1.47 $
58  *
59  * @since 6.0.0
60  */

61 public class CmsLinkProcessor extends CmsHtmlParser {
62
63     /** HTML end. */
64     public static final String JavaDoc HTML_END = "</body></html>";
65
66     /** HTML start. */
67     public static final String JavaDoc HTML_START = "<html><body>";
68
69     /** Processing mode "process links". */
70     private static final int PROCESS_LINKS = 1;
71
72     /** Processing mode "replace links". */
73     private static final int REPLACE_LINKS = 0;
74
75     /** The current users cms instance, containing the users permission and site root context. */
76     private CmsObject m_cms;
77
78     /** The selected encoding to use for parsing the HTML. */
79     private String JavaDoc m_encoding;
80
81     /** The link table used for link macro replacements. */
82     private CmsLinkTable m_linkTable;
83
84     /** Current processing mode. */
85     private int m_mode;
86
87     /** Indicates if links should be generated for editing purposes. */
88     private boolean m_processEditorLinks;
89
90     /** The relative path for relative links, if not set, relative links are treated as external links. */
91     private String JavaDoc m_relativePath;
92
93     /** Another cms instance based on the current users cms instance, but with the site root set to '/'. */
94     private CmsObject m_rootCms;
95
96     /**
97      * Creates a new link processor.<p>
98      *
99      * @param cms the cms object
100      * @param linkTable the link table to use
101      * @param encoding the encoding to use for parsing the HTML content
102      * @param relativePath additional path for links with relative path (only used in "replace" mode)
103      */

104     public CmsLinkProcessor(CmsObject cms, CmsLinkTable linkTable, String JavaDoc encoding, String JavaDoc relativePath) {
105
106         // echo mode must be on for link processor
107
super(true);
108
109         m_cms = cms;
110         if (m_cms != null) {
111             try {
112                 m_rootCms = OpenCms.initCmsObject(cms);
113                 m_rootCms.getRequestContext().setSiteRoot("/");
114             } catch (CmsException e) {
115                 // this should not happen
116
m_rootCms = null;
117             }
118         }
119         m_linkTable = linkTable;
120         m_encoding = encoding;
121         m_processEditorLinks = ((null != m_cms) && (null != m_cms.getRequestContext().getAttribute(
122             CmsRequestContext.ATTRIBUTE_EDITOR)));
123         m_relativePath = relativePath;
124     }
125
126     /**
127      * Escapes all <code>&</code>, e.g. replaces them with a <code>&amp;</code>.<p>
128      *
129      * @param source the String to escape
130      * @return the escaped String
131      */

132     public static String JavaDoc escapeLink(String JavaDoc source) {
133
134         if (source == null) {
135             return null;
136         }
137         StringBuffer JavaDoc result = new StringBuffer JavaDoc(source.length() * 2);
138         int terminatorIndex;
139         for (int i = 0; i < source.length(); ++i) {
140             char ch = source.charAt(i);
141             switch (ch) {
142                 case '&':
143                     // don't escape already escaped &amps;
144
terminatorIndex = source.indexOf(';', i);
145                     if (terminatorIndex > 0) {
146                         String JavaDoc substr = source.substring(i + 1, terminatorIndex);
147                         if ("amp".equals(substr)) {
148                             result.append(ch);
149                         } else {
150                             result.append("&amp;");
151                         }
152                     } else {
153                         result.append("&amp;");
154                     }
155                     break;
156                 default:
157                     result.append(ch);
158             }
159         }
160         return new String JavaDoc(result);
161     }
162
163     /**
164      * Unescapes all <code>&amp;</code>, that is replaces them with a <code>&</code>.<p>
165      *
166      * @param source the String to unescape
167      * @return the unescaped String
168      */

169     public static String JavaDoc unescapeLink(String JavaDoc source) {
170
171         if (source == null) {
172             return null;
173         }
174         return CmsStringUtil.substitute(source, "&amp;", "&");
175
176     }
177
178     /**
179      * Returns the link table this link processor was initialized with.<p>
180      *
181      * @return the link table this link processor was initialized with
182      */

183     public CmsLinkTable getLinkTable() {
184
185         return m_linkTable;
186     }
187
188     /**
189      * Starts link processing for the given content in processing mode.<p>
190      *
191      * Macros are replaced by links.<p>
192      *
193      * @param content the content to process
194      * @return the processed content with replaced macros
195      *
196      * @throws ParserException if something goes wrong
197      */

198     public String JavaDoc processLinks(String JavaDoc content) throws ParserException {
199
200         m_mode = PROCESS_LINKS;
201         return process(content, m_encoding);
202     }
203
204     /**
205      * Starts link processing for the given content in replacement mode.<p>
206      *
207      * Links are replaced by macros.<p>
208      *
209      * @param content the content to process
210      * @return the processed content with replaced links
211      *
212      * @throws ParserException if something goes wrong
213      */

214     public String JavaDoc replaceLinks(String JavaDoc content) throws ParserException {
215
216         m_mode = REPLACE_LINKS;
217         return process(content, m_encoding);
218     }
219
220     /**
221      * Visitor method to process a tag (start).<p>
222      *
223      * @param tag the tag to process
224      */

225     public void visitTag(Tag tag) {
226
227         if (tag instanceof LinkTag) {
228             processLinkTag((LinkTag)tag);
229         } else if (tag instanceof ImageTag) {
230             processImageTag((ImageTag)tag);
231         }
232         // append text content of the tag (may have been changed by above methods)
233
super.visitTag(tag);
234     }
235
236     /**
237      * Process an image tag.<p>
238      *
239      * @param tag the tag to process
240      */

241     protected void processImageTag(ImageTag tag) {
242
243         if (tag.getAttribute("src") != null) {
244
245             CmsLink link;
246             switch (m_mode) {
247
248                 case PROCESS_LINKS:
249                     // macros are replaced with links
250
link = m_linkTable.getLink(CmsMacroResolver.stripMacro(tag.getImageURL()));
251                     if (link != null) {
252                         tag.setImageURL(processLink(link));
253                     }
254                     break;
255
256                 case REPLACE_LINKS:
257                     // links are replaced with macros
258
String JavaDoc targetUri = tag.getImageURL();
259                     if (CmsStringUtil.isNotEmpty(targetUri)) {
260                         String JavaDoc internalUri = CmsLinkManager.getSitePath(m_cms, m_relativePath, targetUri);
261                         if (internalUri != null) {
262                             // this is an internal link
263
link = m_linkTable.addLink(tag.getTagName(), internalUri, true);
264                         } else {
265                             // this is an external link
266
link = m_linkTable.addLink(tag.getTagName(), targetUri, false);
267                         }
268                         tag.setImageURL(CmsMacroResolver.formatMacro(link.getName()));
269
270                         // now ensure the image has the "alt" attribute set
271
boolean hasAltAttrib = (tag.getAttribute("alt") != null);
272                         if (!hasAltAttrib) {
273                             String JavaDoc value = null;
274                             if ((internalUri != null) && (m_rootCms != null)) {
275                                 // internal image: try to read the alt text from the "Title" property
276
try {
277                                     value = m_rootCms.readPropertyObject(
278                                         internalUri,
279                                         CmsPropertyDefinition.PROPERTY_TITLE,
280                                         false).getValue();
281                                 } catch (CmsException e) {
282                                     // property can't be read, ignore
283
}
284                             }
285                             // some editors add a "/" at the end of the tag, we must make sure to insert before that
286
Vector JavaDoc attrs = tag.getAttributesEx();
287                             // first element is always the tag name
288
attrs.add(1, new Attribute(" "));
289                             attrs.add(2, new Attribute("alt", value == null ? "" : value, '"'));
290                         }
291                     }
292                     break;
293
294                 default: // noop
295
}
296         }
297     }
298
299     /**
300      * Process a link tag.<p>
301      *
302      * @param tag the tag to process
303      */

304     protected void processLinkTag(LinkTag tag) {
305
306         if (tag.getAttribute("href") != null) {
307             // href attribute is required
308

309             CmsLink link;
310             switch (m_mode) {
311
312                 case PROCESS_LINKS:
313                     // macros are replaced with links
314
link = m_linkTable.getLink(CmsMacroResolver.stripMacro(tag.getLink()));
315                     if (link != null) {
316                         tag.setLink(escapeLink(processLink(link)));
317                     }
318                     break;
319
320                 case REPLACE_LINKS:
321                     // links are replaced with macros
322
String JavaDoc targetUri = tag.extractLink();
323                     if (CmsStringUtil.isNotEmpty(targetUri)) {
324                         String JavaDoc internalUri = CmsLinkManager.getSitePath(m_cms, m_relativePath, targetUri);
325                         if (internalUri != null) {
326                             // this is an internal link
327
link = m_linkTable.addLink(tag.getTagName(), internalUri, true);
328                         } else {
329                             // this is an external link
330
link = m_linkTable.addLink(tag.getTagName(), targetUri, false);
331                         }
332                         tag.setLink(CmsMacroResolver.formatMacro(link.getName()));
333                     }
334                     break;
335
336                 default: // noop
337
}
338         }
339     }
340
341     /**
342      * Returns the processed link of a given link.<p>
343      *
344      * @param link the link
345      * @return processed link
346      */

347     private String JavaDoc processLink(CmsLink link) {
348
349         if (link.isInternal()) {
350
351             // if we have a local link, leave it unchanged
352
// cms may be null for unit tests
353
if ((m_cms == null) || (link.getUri().length() == 0) || (link.getUri().charAt(0) == '#')) {
354                 return link.getUri();
355             }
356
357             // Explanation why the "m_processEditorLinks" variable is required:
358
// If the VFS is browsed in the root site, this indicates that a user has switched
359
// the context to the / in the Workplace. In this case the workplace site must be
360
// the active site. If normal link processing would be used, the site root in the link
361
// would be replaced with server name / port for the other sites. But if a user clicks
362
// on such a link he would leave the workplace site and loose his session.
363
// A result is that the "direct edit" mode does not work since he in not longer logged in.
364
// Therefore if the user is NOT in the editor, but in the root site, the links are generated
365
// without server name / port. However, if the editor is opened, the links are generated
366
// _with_ server name / port so that the source code looks identical to code
367
// that would normally created when running in a regular site.
368

369             // we are in the opencms root site but not in edit mode - use link as stored
370
if (!m_processEditorLinks && (m_cms.getRequestContext().getSiteRoot().length() == 0)) {
371                 return OpenCms.getLinkManager().substituteLink(m_cms, link.getUri());
372             }
373
374             // otherwise get the desired site root from the stored link
375
// if there is no site root, we have a /system link (or the site was deleted),
376
// return the link prefixed with the opencms context
377
String JavaDoc siteRoot = link.getSiteRoot();
378             if (siteRoot == null) {
379                 return OpenCms.getLinkManager().substituteLink(m_cms, link.getUri());
380             }
381
382             // return the link with the server prefix, if necessary
383
return OpenCms.getLinkManager().substituteLink(m_cms, link.getVfsUri(), siteRoot);
384         } else {
385
386             // don't touch external links
387
return link.getUri();
388         }
389     }
390 }
Popular Tags