001: /*
002: * This file is part of "SnipSnap Radeox Rendering Engine".
003: *
004: * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
005: * All Rights Reserved.
006: *
007: * Please visit http://radeox.org/ for updates and contact.
008: *
009: * --LICENSE NOTICE--
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * --LICENSE NOTICE--
022: */
023: package org.radeox.macro;
024:
025: import java.io.IOException;
026: import java.io.Writer;
027:
028: import org.radeox.Messages;
029: import org.radeox.api.engine.ImageRenderEngine;
030: import org.radeox.api.engine.RenderEngine;
031: import org.radeox.api.engine.context.RenderContext;
032: import org.radeox.api.macro.MacroParameter;
033: import org.radeox.util.Encoder;
034:
035: /*
036: * Macro for displaying external links with a name. The normal UrlFilter takes
037: * the url as a name. @author stephan @team sonicteam
038: *
039: * @version $Id: LinkMacro.java 29159 2007-04-19 01:46:15Z ajpoland@iupui.edu $
040: */
041:
042: public class LinkMacro extends BaseLocaleMacro {
043: private static String[] paramDescription = {
044: Messages.getString("LinkMacro.0"), //$NON-NLS-1$
045: Messages.getString("LinkMacro.1"), //$NON-NLS-1$
046: Messages.getString("LinkMacro.2"), //$NON-NLS-1$
047: Messages.getString("LinkMacro.3"), //$NON-NLS-1$
048: Messages.getString("LinkMacro.4") }; //$NON-NLS-1$
049:
050: private static String description = Messages
051: .getString("LinkMacro.5"); //$NON-NLS-1$
052:
053: public String[] getParamDescription() {
054: return paramDescription;
055: }
056:
057: /*
058: * (non-Javadoc)
059: *
060: * @see org.radeox.macro.Macro#getDescription()
061: */
062: public String getDescription() {
063: return description;
064: }
065:
066: public String getLocaleKey() {
067: return "macro.link"; //$NON-NLS-1$
068: }
069:
070: public void execute(Writer writer, MacroParameter params)
071: throws IllegalArgumentException, IOException {
072:
073: RenderContext context = params.getContext();
074: RenderEngine engine = context.getRenderEngine();
075:
076: String text = params.get("text", 0); //$NON-NLS-1$
077: String url = params.get("url", 1); //$NON-NLS-1$
078: String img = params.get("img", 2); //$NON-NLS-1$
079: String target = params.get("target", 3); //$NON-NLS-1$
080:
081: // check for single url argument (text == url)
082: if (params.getLength() == 1) {
083: url = text;
084: text = Encoder.toEntity(text.charAt(0))
085: + Encoder.escape(text.substring(1));
086: }
087:
088: if (url != null && text != null) {
089: if (target == null) {
090: if (url.indexOf("://") >= 0 && url.indexOf("://") < 6) //$NON-NLS-1$ //$NON-NLS-2$
091: {
092: target = "rwikiexternal"; //$NON-NLS-1$
093: } else {
094: target = "none"; //$NON-NLS-1$
095: }
096:
097: }
098: writer.write("<span class=\"nobr\">"); //$NON-NLS-1$
099: if (!"none".equals(img) && engine instanceof ImageRenderEngine) //$NON-NLS-1$
100: {
101: writer.write(((ImageRenderEngine) engine)
102: .getExternalImageLink());
103: }
104: writer.write("<a href=\""); //$NON-NLS-1$
105: writer.write(url);
106: writer.write("\""); //$NON-NLS-1$
107: if (!"none".equals(target)) //$NON-NLS-1$
108: {
109: writer.write(" target=\""); //$NON-NLS-1$
110: writer.write(target);
111: writer.write("\" "); //$NON-NLS-1$
112: }
113: writer.write(">"); //$NON-NLS-1$
114: writer.write(text);
115: writer.write("</a></span>"); //$NON-NLS-1$
116: } else {
117: throw new IllegalArgumentException(Messages
118: .getString("LinkMacro.23")); //$NON-NLS-1$
119: }
120: return;
121: }
122: }
|