Source Code Cross Referenced for GnuParser.java in  » Library » Apache-command-line » org » apache » commons » cli » 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 » Library » Apache command line » org.apache.commons.cli 
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:         */package org.apache.commons.cli;
017:
018:        import java.util.ArrayList;
019:
020:        /**
021:         * The class GnuParser provides an implementation of the 
022:         * {@link Parser#flatten(Options,String[],boolean) flatten} method.
023:         *
024:         * @author John Keyes (john at integralsource.com)
025:         * @see Parser
026:         * @version $Revision: 542151 $
027:         */
028:        public class GnuParser extends Parser {
029:
030:            /** holder for flattened tokens */
031:            private ArrayList tokens = new ArrayList();
032:
033:            /**
034:             * <p>Resets the members to their original state i.e. remove
035:             * all of <code>tokens</code> entries.
036:             */
037:            private void init() {
038:                tokens.clear();
039:            }
040:
041:            /**
042:             * <p>This flatten method does so using the following rules:
043:             * <ol>
044:             *  <li>If an {@link Option} exists for the first character of 
045:             *  the <code>arguments</code> entry <b>AND</b> an {@link Option} 
046:             *  does not exist for the whole <code>argument</code> then
047:             *  add the first character as an option to the processed tokens
048:             *  list e.g. "-D" and add the rest of the entry to the also.</li>
049:             *  <li>Otherwise just add the token to the processed tokens list.
050:             *  </li>
051:             * </ol>
052:             * </p>
053:             *
054:             * @param options The Options to parse the arguments by.
055:             * @param arguments The arguments that have to be flattened.
056:             * @param stopAtNonOption specifies whether to stop 
057:             * flattening when a non option has been encountered
058:             * @return a String array of the flattened arguments
059:             */
060:            protected String[] flatten(Options options, String[] arguments,
061:                    boolean stopAtNonOption) {
062:                init();
063:
064:                boolean eatTheRest = false;
065:                Option currentOption = null;
066:
067:                for (int i = 0; i < arguments.length; i++) {
068:                    if ("--".equals(arguments[i])) {
069:                        eatTheRest = true;
070:                        tokens.add("--");
071:                    } else if ("-".equals(arguments[i])) {
072:                        tokens.add("-");
073:                    } else if (arguments[i].startsWith("-")) {
074:                        Option option = options.getOption(arguments[i]);
075:
076:                        // this is not an Option
077:                        if (option == null) {
078:                            // handle special properties Option
079:                            Option specialOption = options
080:                                    .getOption(arguments[i].substring(0, 2));
081:
082:                            if (specialOption != null) {
083:                                tokens.add(arguments[i].substring(0, 2));
084:                                tokens.add(arguments[i].substring(2));
085:                            } else if (stopAtNonOption) {
086:                                eatTheRest = true;
087:                                tokens.add(arguments[i]);
088:                            } else {
089:                                tokens.add(arguments[i]);
090:                            }
091:                        } else {
092:                            // WARNING: Findbugs reports major problems with the following code. 
093:                            //          As option cannot be null, currentOption cannot and 
094:                            //          much of the code below is never going to be run.
095:
096:                            currentOption = option;
097:
098:                            // special option
099:                            Option specialOption = options
100:                                    .getOption(arguments[i].substring(0, 2));
101:
102:                            if ((specialOption != null) && (option == null)) {
103:                                tokens.add(arguments[i].substring(0, 2));
104:                                tokens.add(arguments[i].substring(2));
105:                            } else if ((currentOption != null)
106:                                    && currentOption.hasArg()) {
107:                                if (currentOption.hasArg()) {
108:                                    tokens.add(arguments[i]);
109:                                    currentOption = null;
110:                                } else if (currentOption.hasArgs()) {
111:                                    tokens.add(arguments[i]);
112:                                } else if (stopAtNonOption) {
113:                                    eatTheRest = true;
114:                                    tokens.add("--");
115:                                    tokens.add(arguments[i]);
116:                                } else {
117:                                    tokens.add(arguments[i]);
118:                                }
119:                            } else if (currentOption != null) {
120:                                tokens.add(arguments[i]);
121:                            } else if (stopAtNonOption) {
122:                                eatTheRest = true;
123:                                tokens.add("--");
124:                                tokens.add(arguments[i]);
125:                            } else {
126:                                tokens.add(arguments[i]);
127:                            }
128:                        }
129:                    } else {
130:                        tokens.add(arguments[i]);
131:                    }
132:
133:                    if (eatTheRest) {
134:                        for (i++; i < arguments.length; i++) {
135:                            tokens.add(arguments[i]);
136:                        }
137:                    }
138:                }
139:
140:                return (String[]) tokens.toArray(new String[tokens.size()]);
141:            }
142:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.