Source Code Cross Referenced for HtmlComponent.java in  » J2EE » Sofia » com » salmonllc » html » 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 » J2EE » Sofia » com.salmonllc.html 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.salmonllc.html;
002:
003:        /////////////////////////
004:        //$Archive: /SOFIA/SourceCode/com/salmonllc/html/HtmlComponent.java $
005:        //$Author: Dan $
006:        //$Revision: 54 $
007:        //$Modtime: 1/19/04 2:36p $
008:        /////////////////////////
009:
010:        import com.salmonllc.jsp.tags.PageTag;
011:        import com.salmonllc.jsp.JspForm;
012:        import com.salmonllc.personalization.Nameable;
013:        import com.salmonllc.sitemap.SiteMap;
014:
015:        import java.io.*;
016:        import java.util.*;
017:        import javax.servlet.http.*;
018:
019:        /**
020:         * This component is the base class for every HTML component in the framework.
021:         */
022:        public abstract class HtmlComponent implements  Serializable, Nameable {
023:
024:            public static final int EVENT_NONE = 0;
025:            public static final int EVENT_SUBMIT = 1;
026:            public static final int EVENT_OTHER = 2;
027:
028:            public static final int DISABLED_ATTRIBUTE_USE_ON_SUPPORTED_BROWSERS = 0;
029:            public static final int DISABLED_ATTRIBUTE_USE_NEVER = 1;
030:            public static final int DISABLED_ATTRIBUTE_USE_SYSTEM_DEFAULT = 2;
031:
032:            private HtmlPage _page;
033:            protected HtmlComponent _parent;
034:            String _name = "";
035:            String _fullName = null;
036:            protected boolean _visible = true;
037:            /** Write a newline, or not, at the end of the generated tag. */
038:            protected boolean _generateNewline = true;
039:            /** DataSource to bind */
040:            private String _dataSource;
041:            protected String _class;
042:            private String _form;
043:            private int _formNo = -1;
044:            private int _useDisabledAttribute = DISABLED_ATTRIBUTE_USE_SYSTEM_DEFAULT;
045:
046:            protected static final boolean debug = false;
047:
048:            /**
049:             * This method constructs a new HtmlComponent with the specified name.
050:             * @param name The name of the component. Each component in a page must have a unique name.
051:             */
052:            public HtmlComponent(String name, HtmlPage p) {
053:                _name = name;
054:                _page = p;
055:            }
056:
057:            /**
058:             * This method should be implemented by subclasses of this component that will have autobinding.
059:             * This method is called by the framework and should not be called directly.
060:             */
061:            public void doBinding() throws Exception {
062:            }
063:
064:            /**
065:             * This method will encode a url to support URL rewriting
066:             */
067:            public String encodeURL(String url) {
068:                if (url == null)
069:                    return null;
070:                if (url.toLowerCase().startsWith("javascript:"))
071:                    return url;
072:                if (!getPage().getEncodeURLs()) {
073:                    String logicalName = getSiteMapEntryName(url);
074:                    if (logicalName == null)
075:                        return url;
076:                    else {
077:                        url = translateSiteMapURL(url);
078:                        SiteMap m = getPage().getSiteMap();
079:                        if (m != null)
080:                            return m.addJavaScriptToUrl(logicalName, url);
081:                        else
082:                            return url;
083:                    }
084:                }
085:                if (!getPage().isWMLMaintained()) {
086:                    HttpServletRequest req = getPage().getCurrentRequest();
087:                    HttpServletResponse res = getPage().getCurrentResponse();
088:                    String logicalName = getSiteMapEntryName(url);
089:                    if (logicalName == null)
090:                        return HttpServletResponseWrapper.encodeURL(url, req,
091:                                res);
092:                    else {
093:                        String translatedURL = translateSiteMapURL(url);
094:                        String encodedURL = HttpServletResponseWrapper
095:                                .encodeURL(translatedURL, req, res);
096:                        SiteMap m = getPage().getSiteMap();
097:                        if (m != null)
098:                            return m
099:                                    .addJavaScriptToUrl(logicalName, encodedURL);
100:                        else
101:                            return encodedURL;
102:                    }
103:                } else {
104:                    url = translateSiteMapURL(url);
105:                    HttpServletRequest req = getPage().getCurrentRequest();
106:                    HttpServletResponse res = getPage().getCurrentResponse();
107:                    if (HttpServletResponseWrapper.encodeURL(url, req, res)
108:                            .indexOf(';') >= 0) {
109:                        if (url.indexOf('?') >= 0)
110:                            if (url.indexOf(PageTag.getSessionIdentifier()
111:                                    + "="
112:                                    + PageTag.getWmlSessId(getPage()
113:                                            .getSession())) < 0) {
114:                                if (url.endsWith("&amp;") || url.endsWith("?"))
115:                                    return url
116:                                            + PageTag.getSessionIdentifier()
117:                                            + "="
118:                                            + PageTag.getWmlSessId(getPage()
119:                                                    .getSession());
120:                                else
121:                                    return url
122:                                            + "&amp;"
123:                                            + PageTag.getSessionIdentifier()
124:                                            + "="
125:                                            + PageTag.getWmlSessId(getPage()
126:                                                    .getSession());
127:                            } else
128:                                return url;
129:                        else
130:                            return url
131:                                    + "?"
132:                                    + PageTag.getSessionIdentifier()
133:                                    + "="
134:                                    + PageTag.getWmlSessId(getPage()
135:                                            .getSession());
136:                    } else
137:                        return url;
138:                }
139:            }
140:
141:            protected String getSiteMapEntryName(String url) {
142:                if (url == null)
143:                    return null;
144:                if (url.startsWith("%")) {
145:                    int pos = url.length();
146:                    for (int i = 0; i < pos; i++) {
147:                        char c = url.charAt(i);
148:                        if (c == '?' || c == '#' || c == ' ' || c == ';'
149:                                || c == '/') {
150:                            pos = i;
151:                            break;
152:                        }
153:                    }
154:                    return url.substring(1, pos);
155:                } else
156:                    return null;
157:            }
158:
159:            /**
160:             * Translates a site map url (preceeded by a % sign) to an actual URL
161:             * @param url
162:             * @return
163:             */
164:            public String translateSiteMapURL(String url) {
165:                if (url == null)
166:                    return null;
167:                String logical = getSiteMapEntryName(url);
168:                if (logical != null) {
169:                    int pos = logical.length() + 1;
170:                    String extraParms = null;
171:                    if (pos < url.length())
172:                        extraParms = url.substring(pos);
173:                    String u = getPage().getSiteMapURL(logical, extraParms,
174:                            false);
175:                    if (u != null)
176:                        url = u;
177:                }
178:                return url;
179:            }
180:
181:            /**
182:             * This method will encode a url to support URL rewriting
183:             */
184:            public StringBuffer encodeURL(StringBuffer url) {
185:                if (url == null)
186:                    return null;
187:                return new StringBuffer(encodeURL(url.toString()));
188:            }
189:
190:            /**
191:             * This method should be implemented by subclasses of this component that will respond to events. This method should notify each of the component's listeners that an event occured.
192:             * This method is called by the framework and should not be called directly.
193:             * @param eventType valid Types are EVENT_SUBMIT and EVENT_OTHER
194:             */
195:            public boolean executeEvent(int eventType) throws Exception {
196:                if (eventType == -999)
197:                    eventType = 0;
198:                return true;
199:            }
200:
201:            /**
202:             * A utility method used to replace special HTML characters <, >, &, " with their HTML representations.
203:             */
204:            public static String fixSpecialHTMLCharacters(String input) {
205:                if (input == null)
206:                    return null;
207:
208:                int inputLength = input.length();
209:                StringBuffer sb = new StringBuffer(inputLength);
210:                for (int i = 0; i < inputLength; i++) {
211:                    char c = input.charAt(i);
212:                    if (c == '<')
213:                        sb.append("&lt;");
214:                    else if (c == '&')
215:                        sb.append("&amp;");
216:                    else if (c == '>')
217:                        sb.append("&gt;");
218:                    else if (c == '>')
219:                        sb.append("&gt;");
220:                    else if (c == '"')
221:                        sb.append("&quot;");
222:                    else if (c == '\'')
223:                        sb.append("&#39;");
224:                    else
225:                        sb.append(c);
226:                }
227:                return sb.toString();
228:            }
229:
230:            /**
231:             * The purpose of this method is to generate the HTML a particular component needs to send back to the browser for its visual representation and must be implemented by each subclass.
232:             * The method is called by the framework and should not be called directly.
233:             */
234:            public abstract void generateHTML(PrintWriter p, int rowNo)
235:                    throws Exception;
236:
237:            /**
238:             * The purpose of this method is to generate the HTML a particular component needs to send back to the browser for its visual representation.
239:             * This version of the method is used for components that must process multiple rows in a DataStore in one generate (like computing a total for several rows).
240:             * The method is called by the framework and should not be called directly.
241:             */
242:            public void generateHTML(PrintWriter p, int rowStart, int rowEnd)
243:                    throws Exception {
244:                generateHTML(p, rowStart);
245:            }
246:
247:            /**
248:             * This method generates a URL for a dynamically generated image created by the component.
249:             * The method is called by the framework and should not be called directly.
250:             */
251:            protected String generateImageURL() {
252:                HtmlPage p = getPage();
253:
254:                StringBuffer buf = new StringBuffer(
255:                        com.salmonllc.util.URLGenerator.generateObjectstoreURL(
256:                                p.getCurrentRequest(), getPage()
257:                                        .getApplicationName()));
258:                buf.append("/");
259:
260:                if (p instanceof  com.salmonllc.jsp.JspController) {
261:                    buf.append("JSP/");
262:                    buf.append(((com.salmonllc.jsp.JspController) p)
263:                            .getSessionKey());
264:                } else {
265:                    buf.append(p.getOrigApplicationName());
266:                    buf.append("/");
267:                    buf.append(p.getPageName());
268:                }
269:
270:                buf.append("/");
271:                buf.append(getFullName());
272:                buf.append("/");
273:
274:                return buf.toString();
275:            }
276:
277:            /**
278:             * This method should be implemented by containers that need to iterate through data in a datastore. The generateHtml method
279:             * is called once for each row. This method is called once before the rows are processed.
280:             */
281:            public void generateInitialHTML(PrintWriter p) throws Exception {
282:            }
283:
284:            /**
285:             * This method returns the class name used by the component (for DSS sytle sheets) .
286:             */
287:            public String getClassName() {
288:                return _class;
289:            }
290:
291:            /**
292:             * This method returns the Data Source the component is bound to.
293:             */
294:            public String getDataSource() {
295:                return _dataSource;
296:            }
297:
298:            /**
299:             * This will return the full name of the component. The full name is the name of the component appended to the names of its parent components and seperated by underscores. For example if this is a button named "button1" inside a table named "table1" the full name for this component would be "table1_button1".
300:             */
301:            public String getFullName() {
302:                if (_fullName == null) {
303:                    HtmlComponent parent = _parent;
304:                    String name = _name;
305:                    // no need to create a new string for every time through the loop
306:                    String parentName = null;
307:                    //
308:                    while (parent != null) {
309:                        parentName = parent.getName();
310:                        if (parentName != null) {
311:                            if (parentName != "") {
312:                                name = parentName + "_" + name;
313:                            }
314:                        }
315:                        parent = parent.getParent();
316:                    }
317:                    _fullName = getPortletNameSpace() + name;
318:                }
319:
320:                return _fullName;
321:            }
322:
323:            /**
324:             * This method returns the name of the component.
325:             */
326:            public String getName() {
327:                return _name;
328:            }
329:
330:            /**
331:             * This method return the page that the component is a member of.
332:             */
333:            public HtmlPage getPage() {
334:                return _page;
335:            }
336:
337:            /**
338:             * This method returns the parent container that this component is in.
339:             * @return The parent component or null if there is no parent.
340:             */
341:            public HtmlComponent getParent() {
342:                return _parent;
343:            }
344:
345:            /**
346:             * This method returns whether or not the component is visible. If it is not visible, no HTML will be generated.
347:             */
348:            public boolean getVisible() {
349:                return getVisible(false);
350:            }
351:
352:            /**
353:             * This method returns whether or not the component is visible. If it is not visible, no HTML will be generated.
354:             * @param checkParents A boolean value, true mean that this component is considered visible if it and all it's parents are visible. A false value only checks the component itself.
355:             */
356:            public boolean getVisible(boolean checkParents) {
357:                if (!_visible)
358:                    return false;
359:                boolean bVisible = _visible;
360:                if (checkParents) {
361:                    HtmlComponent hc = getParent();
362:                    while (hc != null) {
363:                        if (hc.getVisible() == false) {
364:                            bVisible = false;
365:                            break;
366:                        }
367:                        hc = hc.getParent();
368:                    }
369:                }
370:                return bVisible;
371:            }
372:
373:            /**
374:             * This method will process the parmeters from a HTTP post that are relavent to this component or components in it.
375:             * This is a framework method and should not be called directly.
376:             * @param parms a HashTable containing all the parameters for the servlet.
377:             * @param rowNo - int row number to act on
378:             * @return true if this component or a child is the one that submitted the page and false if not.
379:             */
380:            public boolean processParms(Hashtable parms, int rowNo)
381:                    throws Exception {
382:                return false;
383:            }
384:
385:            /**
386:             * This method will clear all pending events from the event queue for this component and components it contains.
387:             */
388:            public void reset() {
389:            }
390:
391:            /**
392:             * This method sets the property class (for DSS - style sheets) for the component.
393:             * @param sClass The class to use.
394:             */
395:            public void setClassName(String sClass) {
396:                _class = sClass;
397:            }
398:
399:            /**
400:             * This method sets the Data Source the component should be bound to.
401:             */
402:            public void setDataSource(String dataSource) {
403:                _dataSource = dataSource;
404:                if (_dataSource == null)
405:                    return;
406:
407:                //search and replace &QUOT; with a single quote. For Engines that have trouble with single quotes inside a tag
408:                String test = _dataSource.toUpperCase();
409:                int pos = test.indexOf("&QUOT;");
410:                if (pos == -1)
411:                    return;
412:
413:                StringBuffer work = new StringBuffer(_dataSource);
414:                while (pos > -1) {
415:                    work.replace(pos, pos + 6, "'");
416:                    test = work.toString().toUpperCase();
417:                    pos = test.indexOf("&QUOT;");
418:                }
419:                _dataSource = work.toString();
420:            }
421:
422:            /**
423:             * Enables or disables the newline at the end of the generated HTML tag.
424:             * @param enable If true, generate a newline after the generated HTML.
425:             */
426:            public void setGenerateNewline(boolean enable) {
427:                _generateNewline = enable;
428:            }
429:
430:            /**
431:             * This method sets the name of the component.
432:             */
433:            public void setName(String name) {
434:                _name = name;
435:            }
436:
437:            /**
438:             * This method sets the page the component is in
439:             */
440:            public void setPage(HtmlPage p) {
441:                _page = p;
442:            }
443:
444:            /**
445:             * This method sets the parent component of this component. If a component is inside of a container, the container is its parent.
446:             */
447:            public void setParent(HtmlComponent parent) {
448:                _parent = parent;
449:                _form = null;
450:            }
451:
452:            /**
453:             * This method sets the visible property for the component. If it is not visible, no html will be generated.
454:             */
455:            public void setVisible(boolean visible) {
456:                _visible = visible;
457:            }
458:
459:            public String toString() {
460:                String ret = super .toString();
461:                ret += "\n" + getFullName();
462:                return ret;
463:            }
464:
465:            public String getFormString() {
466:                if (_form != null)
467:                    return _form;
468:                HtmlComponent comp = getParent();
469:
470:                while (comp != null) {
471:                    if (comp instanceof  JspForm) {
472:                        _form = "document.forms['" + getPortletNameSpace()
473:                                + comp.getName() + "'].";
474:                        break;
475:                    }
476:                    comp = comp.getParent();
477:                }
478:                if (_form == null)
479:                    if (_formNo == -1)
480:                        _form = "document.forms[0].";
481:                    else
482:                        _form = "document.forms[" + _formNo + "].";
483:                return _form;
484:            }
485:
486:            /**
487:             * Set the form that this component is in. This can be used if a page uses more than one form and the component is in an html form tag instead of the <sofia:form> tag.
488:             */
489:            public void setFormIndex(int formIndex) {
490:                _formNo = formIndex;
491:                _form = null;
492:            }
493:
494:            /**
495:             * Get the form that this component is in. This can be used if a page uses more than one form and the component is in an html form tag instead of the <sofia:form> tag.
496:             */
497:            public int getFormIndex() {
498:                return _formNo;
499:            }
500:
501:            protected boolean useDisabledAttribute() {
502:                HtmlPageBase p = getPage();
503:                if (_useDisabledAttribute == DISABLED_ATTRIBUTE_USE_SYSTEM_DEFAULT) {
504:                    if (!p.getUseDisabledAttribute())
505:                        return false;
506:                } else if (_useDisabledAttribute == DISABLED_ATTRIBUTE_USE_NEVER)
507:                    return false;
508:
509:                int browser = p.getBrowserType();
510:                int ver = p.getBrowserVersion();
511:                if (browser == HtmlPage.BROWSER_NETSCAPE && ver <= 4)
512:                    return false;
513:                if (browser == HtmlPage.BROWSER_MICROSOFT && ver <= 4)
514:                    return false;
515:                return true;
516:            }
517:
518:            /**
519:             * This method sets the property theme for the component.
520:             */
521:            public void setTheme(String theme) {
522:                //override this in subclasses
523:            }
524:
525:            /**
526:             * Returns the theme for this component or null if it doesn't have one
527:             * @return
528:             */
529:            public String getTheme() {
530:                //override this in subclasses
531:                return null;
532:            }
533:
534:            /**
535:             * @return how the component will handle the disabled HTML attribute. DISABLED_ATTRIBUTE_USE_ON_SUPPORTED_BROWSERS (use disabled html attribute on browsers that support it) , DISABLED_ATTRIBUTE_USE_NEVER (Never use the disabled attribute for this component) or DISABLED_ATTRIBUTE_USE_SYSTEM_DEFAULT (Use the attribute if the system default is to and the browser supports it)
536:             */
537:            public int getUseDisabledAttribute() {
538:                return _useDisabledAttribute;
539:            }
540:
541:            /**
542:             * Sets how the component will handle the disabled HTML attribute. DISABLED_ATTRIBUTE_USE_ON_SUPPORTED_BROWSERS (use disabled html attribute on browsers that support it) , DISABLED_ATTRIBUTE_USE_NEVER (Never use the disabled attribute for this component) or DISABLED_ATTRIBUTE_USE_SYSTEM_DEFAULT (Use the attribute if the system default is to and the browser supports it)
543:             */
544:            public void setUseDisabledAttribute(int i) {
545:                _useDisabledAttribute = i;
546:            }
547:
548:            protected String getPortletNameSpace() {
549:                if (getPage().isRequestFromPortlet())
550:                    return getPage().getPortletInfo().getNameSpace();
551:                else
552:                    return "";
553:            }
554:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.