Source Code Cross Referenced for ParameterParser.java in  » Forum » JForum-2.1.8 » net » jforum » util » legacy » commons » fileupload » 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 » Forum » JForum 2.1.8 » net.jforum.util.legacy.commons.fileupload 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001-2004 The Apache Software Foundation
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package net.jforum.util.legacy.commons.fileupload;
017:
018:        import java.util.HashMap;
019:        import java.util.Map;
020:
021:        /**
022:         * A simple parser intended to parse sequences of name/value pairs.
023:         * Parameter values are exptected to be enclosed in quotes if they
024:         * contain unsafe characters, such as '=' characters or separators.
025:         * Parameter values are optional and can be omitted.
026:         *
027:         * <p>
028:         *  <code>param1 = value; param2 = "anything goes; really"; param3</code>
029:         * </p>
030:         *
031:         * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
032:         */
033:
034:        public class ParameterParser {
035:            /**
036:             * String to be parsed.
037:             */
038:            private char[] chars = null;
039:
040:            /**
041:             * Current position in the string.
042:             */
043:            private int pos = 0;
044:
045:            /**
046:             * Maximum position in the string.
047:             */
048:            private int len = 0;
049:
050:            /**
051:             * Start of a token.
052:             */
053:            private int i1 = 0;
054:
055:            /**
056:             * End of a token.
057:             */
058:            private int i2 = 0;
059:
060:            /**
061:             * Whether names stored in the map should be converted to lower case.
062:             */
063:            private boolean lowerCaseNames = false;
064:
065:            /**
066:             * Default ParameterParser constructor.
067:             */
068:            public ParameterParser() {
069:                super ();
070:            }
071:
072:            /**
073:             * Are there any characters left to parse?
074:             *
075:             * @return <tt>true</tt> if there are unparsed characters,
076:             *         <tt>false</tt> otherwise.
077:             */
078:            private boolean hasChar() {
079:                return this .pos < this .len;
080:            }
081:
082:            /**
083:             * A helper method to process the parsed token. This method removes
084:             * leading and trailing blanks as well as enclosing quotation marks,
085:             * when necessary.
086:             *
087:             * @param quoted <tt>true</tt> if quotation marks are expected,
088:             *               <tt>false</tt> otherwise.
089:             * @return the token
090:             */
091:            private String getToken(boolean quoted) {
092:                // Trim leading white spaces
093:                while ((i1 < i2) && (Character.isWhitespace(chars[i1]))) {
094:                    i1++;
095:                }
096:                // Trim trailing white spaces
097:                while ((i2 > i1) && (Character.isWhitespace(chars[i2 - 1]))) {
098:                    i2--;
099:                }
100:                // Strip away quotation marks if necessary
101:                if (quoted) {
102:                    if (((i2 - i1) >= 2) && (chars[i1] == '"')
103:                            && (chars[i2 - 1] == '"')) {
104:                        i1++;
105:                        i2--;
106:                    }
107:                }
108:                String result = null;
109:                if (i2 > i1) {
110:                    result = new String(chars, i1, i2 - i1);
111:                }
112:                return result;
113:            }
114:
115:            /**
116:             * Tests if the given character is present in the array of characters.
117:             *
118:             * @param ch the character to test for presense in the array of characters
119:             * @param charray the array of characters to test against
120:             *
121:             * @return <tt>true</tt> if the character is present in the array of
122:             *   characters, <tt>false</tt> otherwise.
123:             */
124:            private boolean isOneOf(char ch, final char[] charray) {
125:                boolean result = false;
126:                for (int i = 0; i < charray.length; i++) {
127:                    if (ch == charray[i]) {
128:                        result = true;
129:                        break;
130:                    }
131:                }
132:                return result;
133:            }
134:
135:            /**
136:             * Parses out a token until any of the given terminators
137:             * is encountered.
138:             *
139:             * @param terminators the array of terminating characters. Any of these
140:             * characters when encountered signify the end of the token
141:             *
142:             * @return the token
143:             */
144:            private String parseToken(final char[] terminators) {
145:                char ch;
146:                i1 = pos;
147:                i2 = pos;
148:                while (hasChar()) {
149:                    ch = chars[pos];
150:                    if (isOneOf(ch, terminators)) {
151:                        break;
152:                    }
153:                    i2++;
154:                    pos++;
155:                }
156:                return getToken(false);
157:            }
158:
159:            /**
160:             * Parses out a token until any of the given terminators
161:             * is encountered outside the quotation marks.
162:             *
163:             * @param terminators the array of terminating characters. Any of these
164:             * characters when encountered outside the quotation marks signify the end
165:             * of the token
166:             *
167:             * @return the token
168:             */
169:            private String parseQuotedToken(final char[] terminators) {
170:                char ch;
171:                i1 = pos;
172:                i2 = pos;
173:                boolean quoted = false;
174:                boolean charEscaped = false;
175:                while (hasChar()) {
176:                    ch = chars[pos];
177:                    if (!quoted && isOneOf(ch, terminators)) {
178:                        break;
179:                    }
180:                    if (!charEscaped && ch == '"') {
181:                        quoted = !quoted;
182:                    }
183:                    charEscaped = (!charEscaped && ch == '\\');
184:                    i2++;
185:                    pos++;
186:
187:                }
188:                return getToken(true);
189:            }
190:
191:            /**
192:             * Returns <tt>true</tt> if parameter names are to be converted to lower
193:             * case when name/value pairs are parsed.
194:             *
195:             * @return <tt>true</tt> if parameter names are to be
196:             * converted to lower case when name/value pairs are parsed.
197:             * Otherwise returns <tt>false</tt>
198:             */
199:            public boolean isLowerCaseNames() {
200:                return this .lowerCaseNames;
201:            }
202:
203:            /**
204:             * Sets the flag if parameter names are to be converted to lower case when
205:             * name/value pairs are parsed.
206:             *
207:             * @param b <tt>true</tt> if parameter names are to be
208:             * converted to lower case when name/value pairs are parsed.
209:             * <tt>false</tt> otherwise.
210:             */
211:            public void setLowerCaseNames(boolean b) {
212:                this .lowerCaseNames = b;
213:            }
214:
215:            /**
216:             * Extracts a map of name/value pairs from the given string. Names are
217:             * expected to be unique.
218:             *
219:             * @param str the string that contains a sequence of name/value pairs
220:             * @param separator the name/value pairs separator
221:             *
222:             * @return a map of name/value pairs
223:             */
224:            public Map parse(final String str, char separator) {
225:                if (str == null) {
226:                    return new HashMap();
227:                }
228:                return parse(str.toCharArray(), separator);
229:            }
230:
231:            /**
232:             * Extracts a map of name/value pairs from the given array of
233:             * characters. Names are expected to be unique.
234:             *
235:             * @param chars the array of characters that contains a sequence of
236:             * name/value pairs
237:             * @param separator the name/value pairs separator
238:             *
239:             * @return a map of name/value pairs
240:             */
241:            public Map parse(final char[] chars, char separator) {
242:                if (chars == null) {
243:                    return new HashMap();
244:                }
245:                return parse(chars, 0, chars.length, separator);
246:            }
247:
248:            /**
249:             * Extracts a map of name/value pairs from the given array of
250:             * characters. Names are expected to be unique.
251:             *
252:             * @param chars the array of characters that contains a sequence of
253:             * name/value pairs
254:             * @param offset - the initial offset.
255:             * @param length - the length.
256:             * @param separator the name/value pairs separator
257:             *
258:             * @return a map of name/value pairs
259:             */
260:            public Map parse(final char[] chars, int offset, int length,
261:                    char separator) {
262:
263:                if (chars == null) {
264:                    return new HashMap();
265:                }
266:                HashMap params = new HashMap();
267:                this .chars = chars;
268:                this .pos = offset;
269:                this .len = length;
270:
271:                String paramName = null;
272:                String paramValue = null;
273:                while (hasChar()) {
274:                    paramName = parseToken(new char[] { '=', separator });
275:                    paramValue = null;
276:                    if (hasChar() && (chars[pos] == '=')) {
277:                        pos++; // skip '='
278:                        paramValue = parseQuotedToken(new char[] { separator });
279:                    }
280:                    if (hasChar() && (chars[pos] == separator)) {
281:                        pos++; // skip separator
282:                    }
283:                    if ((paramName != null) && (paramName.length() > 0)) {
284:                        if (this.lowerCaseNames) {
285:                            paramName = paramName.toLowerCase();
286:                        }
287:                        params.put(paramName, paramValue);
288:                    }
289:                }
290:                return params;
291:            }
292:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.