Source Code Cross Referenced for CNumberGuess.java in  » Portal » uPortal_rel-2-6-1-GA » org » jasig » portal » channels » 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 » Portal » uPortal_rel 2 6 1 GA » org.jasig.portal.channels 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Copyright 2001 The JA-SIG Collaborative.  All rights reserved.
002:         *  See license distributed with this file and
003:         *  available online at http://www.uportal.org/license.html
004:         */
005:
006:        package org.jasig.portal.channels;
007:
008:        import java.io.StringWriter;
009:        import java.util.ResourceBundle;
010:        import java.text.MessageFormat;
011:        import java.lang.String;
012:
013:        import org.jasig.portal.ChannelRuntimeData;
014:        import org.jasig.portal.ChannelRuntimeProperties;
015:        import org.jasig.portal.ChannelStaticData;
016:        import org.jasig.portal.IChannel;
017:        import org.jasig.portal.PortalEvent;
018:        import org.jasig.portal.PortalException;
019:        import org.apache.commons.logging.Log;
020:        import org.apache.commons.logging.LogFactory;
021:        import org.jasig.portal.utils.XSLT;
022:        import org.xml.sax.ContentHandler;
023:
024:        /** <p>A number guessing game which asks the user to enter a number within
025:         * a certain range as determined by this channel's parameters.</p>
026:         * @author Ken Weiner, kweiner@unicon.net
027:         * @version $Revision: 35982 $
028:         */
029:        public class CNumberGuess implements  IChannel {
030:            private static final Log log = LogFactory
031:                    .getLog(CNumberGuess.class);
032:            ChannelStaticData staticData = null;
033:            ChannelRuntimeData runtimeData = null;
034:
035:            private static final String sslLocation = "CNumberGuess/CNumberGuess.ssl";
036:            private static final String bundleLocation = "/org/jasig/portal/channels/CNumberGuess/CNumberGuess";
037:            private int iMinNum = 0;
038:            private int iMaxNum = 0;
039:            private int iGuess = 0;
040:            private int iGuesses = 0;
041:            private int iAnswer = 0;
042:            private boolean bFirstTime = true;
043:
044:            /** Constructs a CNumberGuess.
045:             */
046:            public CNumberGuess() {
047:                this .staticData = new ChannelStaticData();
048:                this .runtimeData = new ChannelRuntimeData();
049:            }
050:
051:            /** Returns channel runtime properties
052:             * @return handle to runtime properties
053:             */
054:            public ChannelRuntimeProperties getRuntimeProperties() {
055:                // Channel will always render, so the default values are ok
056:                return new ChannelRuntimeProperties();
057:            }
058:
059:            /** Processes layout-level events coming from the portal
060:             * @param ev a portal layout event
061:             */
062:            public void receiveEvent(PortalEvent ev) {
063:                // no events for this channel
064:            }
065:
066:            /** Receive static channel data from the portal
067:             * @param sd static channel data
068:             */
069:            public void setStaticData(ChannelStaticData sd) {
070:                this .staticData = sd;
071:
072:                String sMinNum = null;
073:                String sMaxNum = null;
074:
075:                try {
076:                    if ((sMinNum = sd.getParameter("minNum")) != null)
077:                        iMinNum = Integer.parseInt(sMinNum);
078:
079:                    if ((sMaxNum = sd.getParameter("maxNum")) != null)
080:                        iMaxNum = Integer.parseInt(sMaxNum);
081:
082:                    iAnswer = getRandomNumber(iMinNum, iMaxNum);
083:                } catch (NumberFormatException nfe) {
084:                    iMinNum = 0;
085:                    iMaxNum = 100;
086:
087:                    if (log.isWarnEnabled())
088:                        log
089:                                .warn("CNumberGuess::setStaticData() : either "
090:                                        + sMinNum
091:                                        + " or "
092:                                        + sMaxNum
093:                                        + " (minNum, maxNum) is not a valid integer. Defaults "
094:                                        + iMinNum + " and " + iMaxNum
095:                                        + " will be used instead.");
096:                }
097:            }
098:
099:            /** Receives channel runtime data from the portal and processes actions
100:             * passed to it.  The names of these parameters are entirely up to the channel.
101:             * @param rd handle to channel runtime data
102:             */
103:            public void setRuntimeData(ChannelRuntimeData rd) {
104:                this .runtimeData = rd;
105:
106:                String sGuess = runtimeData.getParameter("guess");
107:
108:                if (sGuess != null) {
109:                    try {
110:                        iGuess = Integer.parseInt(sGuess);
111:                    } catch (NumberFormatException nfe) {
112:                        // Assume that the guess was the same as last time
113:                    }
114:
115:                    bFirstTime = false;
116:                    iGuesses++;
117:                }
118:            }
119:
120:            /** Output channel content to the portal
121:             * @param out a sax document handler
122:             */
123:            public void renderXML(ContentHandler out) throws PortalException {
124:                String sSuggest = null;
125:
126:                ResourceBundle l10n = ResourceBundle.getBundle(bundleLocation,
127:                        runtimeData.getLocales()[0]);
128:
129:                if (iGuess < iAnswer)
130:                    sSuggest = l10n.getString("HIGHER");
131:                else if (iGuess > iAnswer)
132:                    sSuggest = l10n.getString("LOWER");
133:
134:                String GUESS_SUGGEST = MessageFormat.format(l10n
135:                        .getString("GUESS_SUGGEST"), new Object[] { sSuggest });
136:                String THE_ANSWER_WAS_X = MessageFormat.format(l10n
137:                        .getString("THE_ANSWER_WAS_X"), new Object[] { String
138:                        .valueOf(iAnswer) });
139:                String YOU_GOT_IT_AFTER_X_TRIES = MessageFormat.format(l10n
140:                        .getString("YOU_GOT_IT_AFTER_X_TRIES"),
141:                        new Object[] { String.valueOf(iGuesses) });
142:                String YOU_HAVE_MADE_X_GUESSES = MessageFormat.format(l10n
143:                        .getString("YOU_HAVE_MADE_X_GUESSES"),
144:                        new Object[] { String.valueOf(iGuesses) });
145:                String YOUR_GUESS_OF_GUESS_WAS_INCORRECT = MessageFormat
146:                        .format(
147:                                l10n
148:                                        .getString("YOUR_GUESS_OF_GUESS_WAS_INCORRECT"),
149:                                new Object[] { String.valueOf(iGuess) });
150:                String I_AM_THINKING_OF_A_NUMBER_BETWEEN_X_AND_Y = MessageFormat
151:                        .format(
152:                                l10n
153:                                        .getString("I_AM_THINKING_OF_A_NUMBER_BETWEEN_X_AND_Y"),
154:                                new Object[] { String.valueOf(iMinNum),
155:                                        String.valueOf(iMaxNum) });
156:
157:                StringWriter w = new StringWriter();
158:                w.write("<?xml version='1.0'?>\n");
159:                w.write("<content>\n");
160:                w.write("  <minNum>" + iMinNum + "</minNum>\n");
161:                w.write("  <maxNum>" + iMaxNum + "</maxNum>\n");
162:                w.write("  <guesses>" + iGuesses + "</guesses>\n");
163:                w.write("  <guess>" + iGuess + "</guess>\n");
164:
165:                if (bFirstTime)
166:                    ; // Do nothing
167:                else if (iGuess == iAnswer) {
168:                    w.write("  <answer>" + iAnswer + "</answer>\n");
169:                    bFirstTime = true;
170:                    iGuesses = 0;
171:                    iAnswer = getRandomNumber(iMinNum, iMaxNum);
172:                } else
173:                    w.write("  <suggest>" + sSuggest + "</suggest>\n");
174:
175:                w.write("</content>\n");
176:
177:                XSLT xslt = XSLT.getTransformer(this );
178:                xslt.setResourceBundle(l10n);
179:                xslt.setXML(w.toString());
180:                xslt.setXSL(sslLocation, "main", runtimeData.getBrowserInfo());
181:                xslt.setTarget(out);
182:                xslt.setStylesheetParameter("baseActionURL", runtimeData
183:                        .getBaseActionURL());
184:                xslt.setStylesheetParameter("guessSuggest", GUESS_SUGGEST);
185:                xslt.setStylesheetParameter("theAnswerWasX", THE_ANSWER_WAS_X);
186:                xslt.setStylesheetParameter("youHaveMadeXGuesses",
187:                        YOU_HAVE_MADE_X_GUESSES);
188:                xslt.setStylesheetParameter("youGotItAfterXTries",
189:                        YOU_GOT_IT_AFTER_X_TRIES);
190:                xslt.setStylesheetParameter("YourGuessOfGuessWasIncorrect",
191:                        YOUR_GUESS_OF_GUESS_WAS_INCORRECT);
192:                xslt.setStylesheetParameter("IAmThinkingOfANumberBetweenXAndY",
193:                        I_AM_THINKING_OF_A_NUMBER_BETWEEN_X_AND_Y);
194:                xslt.transform();
195:            }
196:
197:            private int getRandomNumber(int min, int max) {
198:                return new Double((max - min) * Math.random() + min).intValue();
199:            }
200:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.