001: /*
002: * de.jwic.renderer.velocity.JWicTools
003: * $Id: JWicTools.java,v 1.7 2006/10/06 08:52:02 lordsam Exp $
004: */
005: package de.jwic.renderer.util;
006:
007: import java.text.DateFormat;
008: import java.text.MessageFormat;
009: import java.text.SimpleDateFormat;
010: import java.util.Calendar;
011: import java.util.Date;
012: import java.util.Locale;
013:
014: import de.jwic.base.JWicRuntime;
015: import de.jwic.controls.IHTMLElement;
016: import de.jwic.util.XMLTool;
017:
018: /**
019: * JWicTools are used as a velocity context object to provide usefull functions
020: * like formatting within velocity templates. The JWicTools are placed as the
021: * context object "jwic".
022: *
023: * @author Florian Lippisch
024: * @version $Revision: 1.7 $
025: */
026: public class JWicTools {
027:
028: protected Locale locale = null;
029:
030: /**
031: * JWicTools.
032: */
033: public JWicTools(Locale locale) {
034: super ();
035: setLocale(locale);
036: }
037:
038: /**
039: * @return Returns the locale.
040: */
041: public Locale getLocale() {
042: return locale;
043: }
044:
045: /**
046: * @param locale The locale to set.
047: */
048: public void setLocale(Locale locale) {
049: this .locale = locale;
050: }
051:
052: /**
053: * Transforms a text into UTF-8 format. Required by renderers who
054: * create XML files in UTF-8 format.
055: * @param text
056: * @return
057: */
058: public String toUtf8(String text) {
059: return XMLTool.toUtf8(text);
060: }
061:
062: /**
063: * Transforms HTML code into code that can
064: * be placed into a TEXTAREA field.
065: * i.e. transforming < character into &lt;
066: * <p>Returns an empty string if the argument is <code>null</code>.
067: * @return java.lang.String
068: * @param text java.lang.String
069: */
070: public String formatInp(String text) {
071:
072: if (text == null) {
073: return "";
074: }
075:
076: StringBuffer sbHTML = new StringBuffer("");
077: //boolean newline = true;
078:
079: for (int i = 0; i < text.length(); i++) {
080: char c = text.charAt(i);
081: switch (c) {
082: case 34:
083: sbHTML.append(""");
084: break;
085: case 60:
086: sbHTML.append("<");
087: break;
088: case 62:
089: sbHTML.append(">");
090: break;
091: case 38:
092: sbHTML.append("&");
093: break;
094: default:
095: sbHTML.append(c);
096: }
097:
098: }
099:
100: return sbHTML.toString();
101: }
102:
103: /**
104: * Transforms text into HTML format. Used to transform
105: * user inputed text.
106: * <p>Returns an empty string if the argument is <code>null</code>.
107: * @return java.lang.String
108: * @param text java.lang.String
109: */
110: public String formatHtml(String text) {
111:
112: if (text == null) {
113: return "";
114: }
115:
116: StringBuffer sbHTML = new StringBuffer("");
117: //boolean newline = true;
118: boolean newline = true;
119: boolean wascr = false;
120:
121: for (int i = 0; i < text.length(); i++) {
122: char c = text.charAt(i);
123: switch (c) {
124: case 34:
125: sbHTML.append(""");
126: break;
127: case 60:
128: sbHTML.append("<");
129: break;
130: case 62:
131: sbHTML.append(">");
132: break;
133: case 38:
134: sbHTML.append("&");
135: break;
136: case 13:
137: // do some
138: sbHTML.append("<BR>");
139: wascr = true;
140: break;
141:
142: case 32:
143: // space
144: if (newline)
145: sbHTML.append(" ");
146: else
147: sbHTML.append(c);
148: wascr = false;
149: break;
150:
151: case 10:
152: if (!wascr) {
153: sbHTML.append("<BR>");
154: }
155: wascr = false;
156: break;
157:
158: default:
159: sbHTML.append(c);
160: wascr = false;
161: newline = false;
162:
163: }
164:
165: }
166:
167: return sbHTML.toString();
168: }
169:
170: /**
171: * Returns a String MessageFormat(ed) with the pattern and the object in current Locale.
172: * @param pattern
173: * @param object An Object or an array of objects
174: * @return
175: */
176: public String format(String pattern, Object object) {
177: if (object == null) {
178: return null;
179: }
180: if (!(object instanceof Object[])) {
181: object = new Object[] { object };
182: }
183: MessageFormat format = new MessageFormat(pattern, locale);
184: return format.format(object);
185: }
186:
187: /**
188: * Returns the time in short format. If time is 0:0:0.0 an empty String is returned.
189: * @param time
190: * @return
191: */
192: public String formatTime(Date time) {
193: if (time == null) {
194: return "";
195: }
196: Calendar cal = Calendar.getInstance(locale);
197: cal.setTime(time);
198: if (cal.get(Calendar.HOUR_OF_DAY) == 0
199: && cal.get(Calendar.MINUTE) == 0
200: && cal.get(Calendar.SECOND) == 0
201: && cal.get(Calendar.MILLISECOND) == 0) {
202: // if time is exactly 0:0:0.0 return ""
203: return "";
204: }
205: // by default return time like "12:16"
206: DateFormat format = SimpleDateFormat.getTimeInstance(
207: SimpleDateFormat.SHORT, locale);
208: return format.format(time);
209: }
210:
211: /**
212: * Returns the time in short format. If time is 0:0:0.0 an empty String is returned.
213: * @param time
214: * @return
215: */
216: public String formatDate(Date date) {
217: if (date == null) {
218: return "";
219: }
220: DateFormat format = SimpleDateFormat.getDateInstance(
221: SimpleDateFormat.SHORT, locale);
222: return format.format(date);
223: }
224:
225: /**
226: * Returns the time in short format. If time is 0:0:0.0 an empty String is returned.
227: * @param time
228: * @return
229: */
230: public String formatDateTime(Date date) {
231: if (date == null) {
232: return "";
233: }
234: // by default return time like "12:16"
235: DateFormat format = SimpleDateFormat.getDateTimeInstance(
236: SimpleDateFormat.SHORT, SimpleDateFormat.SHORT, locale);
237: return format.format(date);
238: }
239:
240: /**
241: * Returns the context path of the webapp. Used to build links with an absolut path.
242: * @return
243: */
244: public String getContextPath() {
245: return JWicRuntime.getJWicRuntime().getContextPath();
246: }
247:
248: /**
249: * Creates a URL to the specified resource. Works only if the ClasspathResourceServlet is
250: * activated.
251: * @param control
252: * @param resourceName
253: * @return
254: */
255: public static String linkResource(Object control,
256: String resourceName) {
257:
258: String packageName = control.getClass().getPackage().getName();
259:
260: return JWicRuntime.getJWicRuntime().getContextPath() + "/cp/"
261: + packageName.replace('.', '/') + "/" + resourceName;
262:
263: }
264:
265: /**
266: * Generates the class and style property based upon the IHTMLElement object.
267: * @param element
268: * @return
269: */
270: public String generateCssProperty(IHTMLElement element) {
271: return generateCssProperty(element, "");
272: }
273:
274: /**
275: * Generates the class and style property based upon the IHTMLElement object. The extraStyles
276: * parameter is added to the style property.</p>
277: *
278: * Sample Result: class="myClass" style="width: 98px;"
279: *
280: * @param element
281: * @return
282: */
283: public String generateCssProperty(IHTMLElement element,
284: String extraStyles) {
285:
286: StringBuffer sb = new StringBuffer();
287: if (element.getCssClass() != null
288: && element.getCssClass().length() != 0) {
289: sb.append("class=\"").append(element.getCssClass()).append(
290: "\" ");
291: }
292:
293: if (element.getWidth() != 0 || element.getHeight() != 0
294: || element.isFillWidth() || extraStyles.length() != 0) {
295:
296: sb.append("style=\"");
297: if (element.isFillWidth()) {
298: sb.append("width: 100%;");
299: } else if (element.getWidth() != 0) {
300: sb.append("width: ").append(element.getWidth()).append(
301: "px;");
302: }
303: if (element.getHeight() != 0) {
304: sb.append("height: ").append(element.getHeight())
305: .append("px;");
306: }
307: sb.append(extraStyles);
308: sb.append("\"");
309: }
310:
311: return sb.toString();
312:
313: }
314:
315: }
|