Source Code Cross Referenced for Parser.java in  » EJB-Server-resin-3.1.5 » examples » example » 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 » EJB Server resin 3.1.5 » examples » example 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package example;
002:
003:        import com.caucho.util.L10N;
004:        import java.util.logging.Logger;
005:        import java.util.logging.Level;
006:
007:        import com.caucho.vfs.ReadStream;
008:        import java.io.IOException;
009:        import java.util.HashMap;
010:
011:        /**
012:         * Parse a request made using the magic8ball protocol, returning
013:         * appropriate objects that are instances of AbstractCommand.
014:         *
015:         * This class is both a parser and a command factory.
016:         */
017:        public class Parser {
018:            static protected final Logger log = Logger.getLogger(Parser.class
019:                    .getName());
020:            static final L10N L = new L10N(Parser.class);
021:
022:            // set in init() for each new parse
023:            private ReadStream _readStream;
024:            String _error;
025:
026:            // parsing buffer
027:            private StringBuffer _buffer = new StringBuffer();
028:
029:            // commands
030:            private HashMap _commands = new HashMap();
031:
032:            public Parser() {
033:                _commands.put("set-prophet", new SetProphetCommand());
034:                _commands.put("ask", new AskCommand());
035:            }
036:
037:            public void init(ReadStream readStream) {
038:                _readStream = readStream;
039:                _error = null;
040:            }
041:
042:            /**
043:             * Parse one command out of the ReadStream.
044:             * Once the last command is encountered, null is returned. 
045:             *
046:             * @return null if there were no more commands to parse, or there is a parse
047:             * error.
048:             */
049:            public AbstractCommand nextCommand() throws IOException {
050:                String cmd = parseToken();
051:                if (cmd == null) {
052:                    return null;
053:                }
054:                AbstractCommand command = (AbstractCommand) _commands.get(cmd
055:                        .toLowerCase());
056:
057:                if (command == null) {
058:                    _error = "Unknown command `" + cmd + "'";
059:                } else {
060:                    command.parse(this );
061:                    if (command.isError()) {
062:                        _error = command.getError();
063:                        command = null;
064:                    }
065:                }
066:
067:                return command;
068:            }
069:
070:            public boolean isError() {
071:                return _error != null;
072:            }
073:
074:            public String getError() {
075:                return _error;
076:            }
077:
078:            /**
079:             * @return true if ch is an indication that the end-of-stream was reached
080:             */
081:            boolean isEOS(int ch) {
082:                return ch < 0;
083:            }
084:
085:            /**
086:             * @return true if ch is a whitespace character or end-of-stream indicator
087:             */
088:            boolean isWhitespace(int ch) {
089:                return isEOS(ch) || Character.isWhitespace((char) ch);
090:            }
091:
092:            /**
093:             * Eat whitespace characters out of readStream
094:             *
095:             * @return the first non-whitespace character (which
096:             * is still in the _readStream), or -1 if end of stream encountered.
097:             */
098:            int eatWhitespace() throws IOException {
099:                int ch;
100:                while (!isEOS(ch = _readStream.read())) {
101:                    if (!isWhitespace(ch)) {
102:                        _readStream.unread();
103:                        break;
104:                    }
105:                }
106:                return ch;
107:            }
108:
109:            /**
110:             * Parse optional whitespace then a token containing no whitespace.
111:             *
112:             * @return the token, or null if there are no tokens left on the stream
113:             */
114:
115:            String parseToken() throws IOException {
116:                String token = null;
117:
118:                int ch = eatWhitespace();
119:
120:                if (ch >= 0) {
121:                    _buffer.setLength(0);
122:
123:                    while (!isEOS(ch = _readStream.read())) {
124:                        if (isWhitespace(ch)) {
125:                            _readStream.unread();
126:                            break;
127:                        }
128:                        _buffer.append((char) ch);
129:                    }
130:                    token = _buffer.toString();
131:                }
132:
133:                return token;
134:            }
135:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.