Source Code Cross Referenced for ParameterParser.java in  » J2EE » wicket » org » apache » wicket » util » upload » 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 » wicket » org.apache.wicket.util.upload 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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