Source Code Cross Referenced for JWicTools.java in  » Web-Framework » jWic » de » jwic » renderer » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » jWic » de.jwic.renderer.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 <
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("&quot;");
084:                        break;
085:                    case 60:
086:                        sbHTML.append("&lt;");
087:                        break;
088:                    case 62:
089:                        sbHTML.append("&gt;");
090:                        break;
091:                    case 38:
092:                        sbHTML.append("&amp;");
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("&quot;");
126:                        break;
127:                    case 60:
128:                        sbHTML.append("&lt;");
129:                        break;
130:                    case 62:
131:                        sbHTML.append("&gt;");
132:                        break;
133:                    case 38:
134:                        sbHTML.append("&amp;");
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("&nbsp;");
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.