001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.tags; 020 021import java.io.IOException; 022 023import javax.servlet.jsp.JspWriter; 024 025import org.apache.wiki.WikiContext; 026import org.apache.wiki.WikiPage; 027import org.apache.wiki.attachment.Attachment; 028 029/** 030 * Writes a link to a Wiki page. Body of the link becomes the actual text. 031 * The link is written regardless to whether the page exists or not. 032 * 033 * <P><B>Attributes</B></P> 034 * <UL> 035 * <LI>page - Page name to refer to. Default is the current page. 036 * <LI>format - either "anchor" or "url" to output either an <A>... or just the HREF part of one. 037 * <LI>template - Which template should we link to. 038 * <LI>title - Is used in page actions to display hover text (tooltip) 039 * <LI>accesskey - Set an accesskey (ALT+[Char]) 040 * </UL> 041 * 042 * @since 2.0 043 */ 044public class LinkToTag 045 extends WikiLinkTag 046{ 047 private static final long serialVersionUID = 0L; 048 049 private String m_version = null; 050 public String m_title = ""; 051 public String m_accesskey = ""; 052 053 public void initTag() 054 { 055 super.initTag(); 056 m_version = null; 057 } 058 059 public String getVersion() 060 { 061 return m_version; 062 } 063 064 public void setVersion( String arg ) 065 { 066 m_version = arg; 067 } 068 069 public void setTitle( String title ) 070 { 071 m_title = title; 072 } 073 074 public void setAccesskey( String access ) 075 { 076 m_accesskey = access; 077 } 078 079 080 public int doWikiStartTag() 081 throws IOException 082 { 083 String pageName = m_pageName; 084 boolean isattachment = false; 085 086 if( m_pageName == null ) 087 { 088 WikiPage p = m_wikiContext.getPage(); 089 090 if( p != null ) 091 { 092 pageName = p.getName(); 093 094 isattachment = p instanceof Attachment; 095 } 096 else 097 { 098 return SKIP_BODY; 099 } 100 } 101 102 JspWriter out = pageContext.getOut(); 103 String url; 104 String linkclass; 105 106 if( isattachment ) 107 { 108 url = m_wikiContext.getURL(WikiContext.ATTACH,pageName, 109 (getVersion() != null) ? "version="+getVersion() : null ); 110 linkclass = "attachment"; 111 } 112 else 113 { 114 StringBuilder params = new StringBuilder(); 115 if( getVersion() != null ) params.append( "version="+getVersion() ); 116 if( getTemplate() != null ) params.append( (params.length()>0?"&":"") + "skin="+getTemplate() ); 117 118 url = m_wikiContext.getURL( WikiContext.VIEW, pageName, 119 params.toString() ); 120 linkclass = "wikipage"; 121 } 122 123 switch( m_format ) 124 { 125 case ANCHOR: 126 out.print("<a class=\""+linkclass+"\" href=\""+url+"\" accesskey=\"" 127 + m_accesskey + "\" title=\"" + m_title + "\">"); 128 break; 129 case URL: 130 out.print( url ); 131 break; 132 } 133 134 return EVAL_BODY_INCLUDE; 135 } 136 137}