Source Code Cross Referenced for Getopt.java in  » Portal » Open-Portal » com » sun » portal » search » util » 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 » Open Portal » com.sun.portal.search.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2001 Sun Microsystems, Inc.  All rights reserved.
003:         * PROPRIETARY/CONFIDENTIAL.  Use of this product is subject to license terms.
004:         */
005:
006:        package com.sun.portal.search.util;
007:
008:        import java.io.*;
009:
010:        public class Getopt {
011:
012:            String argList[] = null; // the argument List to be parsed
013:            String optStr = null; // the string of arguments in the form "ab:" see getopt man
014:            public String optArg = null; // argument of an option
015:            public int optInd = 0; // index of the option
016:            int nbArgs = 0; // number of args in the argList
017:            int optPos = 1; // position of option letter in current argument scanned
018:
019:            static final int EOF = -1; // returned when no option left
020:            static final int ERROR = -2; // returned on error
021:
022:            public Getopt(String[] args, String opts) {
023:                argList = args;
024:                nbArgs = argList.length;
025:                optStr = opts;
026:                optInd = 0;
027:                optPos = 1;
028:                optArg = null;
029:            }
030:
031:            private void optError(String msg, char optLetter) {
032:                //System.err.println("Getopt: " + msg + " / " + optLetter);
033:            }
034:
035:            public char getOptLetter() {
036:                return argList[optInd].charAt(optPos);
037:            }
038:
039:            public int getopt() {
040:                optArg = null;
041:
042:                // checking boundaries cases
043:                if (argList == null || optStr == null)
044:                    return EOF;
045:                if (optInd < 0 || optInd >= nbArgs)
046:                    return EOF;
047:
048:                // get current Arg out of the argList
049:                String currentArg = argList[optInd];
050:                int argLength = currentArg.length();
051:
052:                // checking special cases
053:                // if arg starts with optLetter "a", "xyz"..
054:                // if arg only 1 char in length "a", "-"
055:                if (argLength <= 1 || currentArg.charAt(0) != '-') {
056:                    return EOF;
057:                } else if (currentArg.equals("--")) { // end of options case
058:                    optInd++;
059:                    return EOF;
060:                }
061:
062:                // get next "letter" from option argument
063:                char optLetter = currentArg.charAt(optPos);
064:
065:                // find position of option in optStr
066:                int pos = optStr.indexOf(optLetter);
067:                if (pos == -1 || optLetter == ':') {
068:                    optError("illegal option", optLetter);
069:                    return ERROR;
070:                } else { // check if option requires argument
071:                    if (pos < optStr.length() - 1
072:                            && optStr.charAt(pos + 1) == ':') {
073:                        // if remain characters after the current option in currentArg
074:                        if (optPos != argLength - 1) {
075:                            // take rest of current arg as the option argument
076:                            optArg = currentArg.substring(optPos + 1);
077:                            optPos = argLength - 1; // go to next arg below in argList
078:                        } else { // take next arg as optArg
079:                            optInd++;
080:                            if (optInd < nbArgs
081:                                    && (argList[optInd].charAt(0) != '-' || argList[optInd]
082:                                            .length() >= 2
083:                                            && (optStr.indexOf(argList[optInd]
084:                                                    .charAt(1)) == -1 || argList[optInd]
085:                                                    .charAt(1) == ':'))) {
086:                                optArg = argList[optInd];
087:                            } else {
088:                                optError("option '" + optLetter
089:                                        + "' requires an argument", optLetter);
090:                                optArg = null;
091:                                optLetter = '?';
092:                            }
093:                        }
094:                    }
095:                }
096:
097:                // next option argument,
098:                // either in currentArg or next arg in argList
099:                optPos++;
100:
101:                // if no more option in currentArg
102:                if (optPos >= argLength) {
103:                    optInd++;
104:                    optPos = 1; // reset postion of opt letter
105:                }
106:                return optLetter;
107:            }
108:
109:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.