001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.theme;
023:
024: import org.jboss.logging.Logger;
025:
026: /**
027: * An implementation of a <code>ThemeLink</code>. <p>The theme link is responsible for producing the markup of a link
028: * element in the markup response's head tag.</p>
029: *
030: * @author Martin Holzner
031: * @version $LastChangedRevision: 8784 $, $LastChangedDate: 2007-10-27 19:01:46 -0400 (Sat, 27 Oct 2007) $
032: * @see ThemeElement
033: */
034: public final class ThemeLink implements ThemeElement {
035: private static final Logger log = Logger.getLogger(ThemeLink.class);
036: private final String rel;
037: private final String type;
038: private final String href;
039: private final String title;
040: private final String media;
041: private final String id;
042: private final String link;
043:
044: /**
045: * Create a new ThemeLink
046: *
047: * @param contextPath the URI of the servlet context that hosts the resource that the link is pointing to
048: * @param rel the rel attributes value
049: * @param type the type attributes value
050: * @param href the href attributes value
051: * @param title the title attributes value
052: * @param media the media attributes value
053: */
054: public ThemeLink(String contextPath, String rel, String type,
055: String href, String id, String title, String media) {
056: if (log.isDebugEnabled()) {
057: log.debug("create theme link with rel=" + rel + " type="
058: + type + " href=" + href + "title=" + title
059: + " media=" + media);
060: }
061: this .rel = rel;
062: this .type = type;
063: this .href = href;
064: this .title = title;
065: this .media = media;
066: this .id = id;
067:
068: link = buildLinkMarkup(contextPath, rel, type, href, id, title,
069: media);
070: }
071:
072: /** @see org.jboss.portal.theme.ThemeLink#getLink */
073: public String getLink() {
074: return link;
075: }
076:
077: /** @see java.lang.Object#toString */
078: public String toString() {
079: return link;
080: }
081:
082: /** @see org.jboss.portal.theme.ThemeElement#getElement */
083: public String getElement() {
084: return getLink();
085: }
086:
087: /** @see org.jboss.portal.theme.ThemeElement@getAttributeValue */
088: public String getAttributeValue(String attributeName) {
089: if ("rel".equals(attributeName)) {
090: return this .rel;
091: }
092:
093: if ("type".equals(attributeName)) {
094: return this .type;
095: }
096:
097: if ("href".equals(attributeName)) {
098: return this .href;
099: }
100:
101: if ("title".equals(attributeName)) {
102: return this .title;
103: }
104:
105: if ("media".equals(attributeName)) {
106: return this .media;
107: }
108:
109: if ("id".equals(attributeName)) {
110: return this .id;
111: }
112:
113: return null;
114: }
115:
116: private static String buildLinkMarkup(String contextPath,
117: String rel, String type, String href, String id,
118: String title, String media) {
119: if (log.isDebugEnabled()) {
120: log.debug("build link markup...");
121: }
122: StringBuffer link = new StringBuffer();
123:
124: link.append("<link rel=\"").append(rel).append("\"");
125:
126: if (type != null) {
127: link.append(" type=\"").append(type).append("\"");
128: }
129:
130: if (id != null && !"".equals(id)) {
131: link.append(" id=\"").append(id).append("\"");
132: }
133:
134: if (href != null) {
135: // adopt the context and inject a theme id param for the theme
136: // servlet to be able to pick up the resource from the correct theme
137: StringBuffer correctHREF = new StringBuffer();
138: if (href.startsWith("/")) {
139: correctHREF.append(contextPath);
140: }
141: correctHREF.append(href);
142: link.append(" href=\"").append(correctHREF).append("\"");
143: }
144:
145: if (title != null && !"".equals(title)) {
146: link.append(" title=\"").append(title).append("\"");
147: }
148: if (media != null && !"".equals(media)) {
149: link.append(" media=\"").append(media).append("\"");
150: }
151:
152: link.append(" />");
153:
154: if (log.isDebugEnabled()) {
155: log.debug("returning: " + link);
156: }
157: return link.toString();
158: }
159: }
|