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>ThemeScript</code>. <p>The script element is responsible for creating the markup that
028: * represents a single script tag in the response markup.</p>
029: *
030: * @author Martin Holzner
031: * @author <a href="mailto:roy@jboss.org">Roy Russo</a>
032: * @version $LastChangedRevision: 8784 $, $LastChangedDate: 2007-10-27 19:01:46 -0400 (Sat, 27 Oct 2007) $
033: * @see org.jboss.portal.theme.ThemeElement
034: */
035: public final class ThemeScript implements ThemeElement {
036: private static final Logger log = Logger
037: .getLogger(ThemeScript.class);
038:
039: private final String src;
040: private final String type;
041: private final String id;
042: private final String charset;
043: private final String script;
044:
045: /**
046: * Create a theme script.
047: *
048: * @param contextPath the URI of the servlet context that hosts the resource that the script tag is pointing to
049: * @param src the value of the src attribute
050: * @param type the value of the type attribute
051: */
052: public ThemeScript(String contextPath, String src, String id,
053: String type, String bodyContent, String charset) {
054: if (log.isDebugEnabled()) {
055: log.debug("creating theme script with src=" + src
056: + " type=" + type);
057: }
058: this .src = src;
059: this .id = id;
060: this .type = type;
061: this .charset = charset;
062: script = buildScriptMarkup(contextPath, src, id, type,
063: bodyContent, charset);
064: }
065:
066: /** @see org.jboss.portal.theme.ThemeScript#getScript */
067: public String getScript() {
068: return script;
069: }
070:
071: /** @see java.lang.Object#toString() */
072: public String toString() {
073: return script;
074: }
075:
076: /** @see org.jboss.portal.theme.ThemeElement#getElement */
077: public String getElement() {
078: return getScript();
079: }
080:
081: /** @see org.jboss.portal.theme.ThemeElement@getAttributeValue */
082: public String getAttributeValue(String attributeName) {
083: if ("type".equals(attributeName)) {
084: return this .type;
085: }
086:
087: if ("id".equals(attributeName)) {
088: return this .id;
089: }
090:
091: if ("src".equals(attributeName)) {
092: return this .src;
093: }
094:
095: if ("charset".equals(attributeName)) {
096: return this .charset;
097: }
098:
099: return null;
100: }
101:
102: private static String buildScriptMarkup(String contextPath,
103: String src, String id, String type, String bodyContent,
104: String charset) {
105: if (log.isDebugEnabled()) {
106: log.debug("build script markup...");
107: }
108: StringBuffer script = new StringBuffer();
109: script.append("<script");
110: // adopt the context and inject a theme id param for the theme
111: // servlet to be able to pick up the resource from the correct theme
112: if (src != null && !"".equals(src)) {
113: StringBuffer correctSRC = new StringBuffer();
114: if (src.startsWith("/")) {
115: correctSRC.append(contextPath);
116: }
117: correctSRC.append(src);
118: script.append(" src=\"").append(correctSRC).append("\"");
119: }
120:
121: if (id != null && !"".equals(id)) {
122: script.append(" id=\"").append(id).append("\"");
123: }
124:
125: if (type != null && !"".equals(type)) {
126: script.append(" type=\"").append(type).append("\"");
127: }
128:
129: if (charset != null && !"".equals(charset)) {
130: script.append(" charset=\"").append(charset).append("\"");
131: }
132:
133: if (bodyContent == null || "".equals(bodyContent)) {
134: //script.append(" />");
135: script.append("></script>");
136: } else {
137: script.append(">").append(bodyContent).append("</script>");
138: }
139:
140: if (log.isDebugEnabled()) {
141: log.debug("returning script : " + script);
142: }
143:
144: return script.toString();
145: }
146: }
|