KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > decorator > CmsDecorationObject


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/decorator/CmsDecorationObject.java,v $
3  * Date : $Date: 2006/03/27 14:52:30 $
4  * Version: $Revision: 1.2 $
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.jsp.decorator;
33
34 import org.opencms.util.CmsMacroResolver;
35
36 import java.util.Locale JavaDoc;
37
38 /**
39  * The CmsDecorationObject defines a single text decoration.<p>
40  *
41  * It uses the information of a <code>{@link CmsDecorationDefintion}</code> to create the
42  * pre- and postfix for a text decoration.
43  
44  * @author Michael Emmerich
45  *
46  * @version $Revision: 1.2 $
47  *
48  * @since 6.1.3
49  */

50 public class CmsDecorationObject {
51
52     /** Macro for the decoration. */
53     public static final String JavaDoc MACRO_DECORATION = "decoration";
54
55     /** Macro for the decoration key. */
56     public static final String JavaDoc MACRO_DECORATIONKEY = "decorationkey";
57
58     /** Macro for the locale. */
59     public static final String JavaDoc MACRO_LOCALE = "locale";
60
61     /** The decoration. */
62     private String JavaDoc m_decoration;
63
64     /** The CmsDecorationDefintion to be used by this decoration object. */
65     private CmsDecorationDefintion m_decorationDefinition;
66
67     /** The key for this decoration. */
68     private String JavaDoc m_decorationKey;
69
70     /** The locale of this decoration. */
71     private Locale JavaDoc m_locale;
72
73     /**
74      * Constructor, creates a new, empty decoration object.<p>
75      */

76     public CmsDecorationObject() {
77
78         m_decorationDefinition = new CmsDecorationDefintion();
79     }
80
81     /**
82      * Constructor, creates a new decoration object with given values.<p>
83      *
84      * @param decorationKey the decoration key
85      * @param decoration the decoration for this decoration key
86      * @param decDef the decoration defintion to be used
87      * @param locale the locale of this decoration object
88      */

89     public CmsDecorationObject(String JavaDoc decorationKey, String JavaDoc decoration, CmsDecorationDefintion decDef, Locale JavaDoc locale) {
90
91         m_decorationKey = decorationKey;
92         m_decoration = decoration;
93         m_decorationDefinition = decDef;
94         m_locale = locale;
95     }
96
97     /**
98      * Gets the decorated content for this decoration object.<p>
99      *
100      * @param config the configuration used
101      * @return decorated content
102      */

103     public String JavaDoc getContentDecoration(CmsDecoratorConfiguration config) {
104
105         StringBuffer JavaDoc content = new StringBuffer JavaDoc();
106         // TODO: we have to handle with word phrases, too
107

108         // add the pretext
109
if (!config.hasUsed(m_decorationKey) && m_decorationDefinition.isMarkFirst()) {
110             content.append(m_decorationDefinition.getPreTextFirst());
111         } else {
112             content.append(m_decorationDefinition.getPreText());
113         }
114         // now add the original word
115
content.append(m_decorationKey);
116
117         // add the posttext
118
if (!config.hasUsed(m_decorationKey) && m_decorationDefinition.isMarkFirst()) {
119             content.append(m_decorationDefinition.getPostTextFirst());
120             config.markAsUsed(m_decorationKey);
121         } else {
122             content.append(m_decorationDefinition.getPostText());
123         }
124
125         // replace the occurance of the ${decoration} makro in the decorated text
126
return replaceMacros(content.toString());
127     }
128
129     /**
130      * Returns the decoration.<p>
131      *
132      * @return the decoration
133      */

134     public String JavaDoc getDecoration() {
135
136         return m_decoration;
137     }
138
139     /**
140      * Returns the decorationDefinition.<p>
141      *
142      * @return the decorationDefinition
143      */

144     public CmsDecorationDefintion getDecorationDefinition() {
145
146         return m_decorationDefinition;
147     }
148
149     /**
150      * Returns the decorationKey.<p>
151      *
152      * @return the decorationKey
153      */

154     public String JavaDoc getDecorationKey() {
155
156         return m_decorationKey;
157     }
158
159     /**
160      * @see java.lang.Object#toString()
161      */

162     public String JavaDoc toString() {
163
164         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
165         buf.append(this.getClass().getName());
166         buf.append(" [name = '");
167         buf.append(m_decorationKey);
168         buf.append("', decoration = '");
169         buf.append(m_decoration);
170         buf.append("', locale = '");
171         buf.append(m_locale);
172         buf.append("' decorationDefinition ='");
173         buf.append(m_decorationDefinition);
174         buf.append("']");
175         return buf.toString();
176     }
177
178     /**
179      * Replaces the macros in the given message.<p>
180      *
181      * @param msg the message in which the macros are replaced
182      *
183      * @return the message with the macros replaced
184      */

185     private String JavaDoc replaceMacros(String JavaDoc msg) {
186
187         CmsMacroResolver resolver = CmsMacroResolver.newInstance();
188         resolver.addMacro(MACRO_DECORATION, m_decoration);
189         resolver.addMacro(MACRO_DECORATIONKEY, m_decorationKey);
190         if (m_locale != null) {
191             resolver.addMacro(MACRO_LOCALE, m_locale.toString());
192         }
193         return resolver.resolveMacros(msg);
194     }
195
196 }
197
Popular Tags