KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > wsdl > codegen > CommandLineOptionParser


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis2.wsdl.codegen;
18
19 import java.util.*;
20
21 /**
22  * @author chathura@opensource.lk
23  *
24  */

25 public class CommandLineOptionParser implements CommandLineOptionConstants {
26
27     private Map commandLineOptions;
28
29     public CommandLineOptionParser(Map commandLineOptions){
30         this.commandLineOptions = commandLineOptions;
31     }
32     public CommandLineOptionParser(String JavaDoc[] args) {
33         this.commandLineOptions = this.parse(args);
34
35     }
36
37     /**
38      * Return a list with <code>CommandLineOption</code> objects
39      *
40      * @param args
41      * @return CommandLineOption List
42      */

43     private Map parse(String JavaDoc[] args){
44         Map commandLineOptions = new HashMap();
45         
46         if(0 == args.length)
47             return commandLineOptions;
48         
49         //State 0 means started
50
//State 1 means earlier one was a new -option
51
//State 2 means earlier one was a sub param of a -option
52

53         int state = 0;
54         ArrayList optionBundle = null;
55         String JavaDoc optionType = null;
56         CommandLineOption commandLineOption ;
57         
58         for(int i=0; i< args.length ; i++){
59             
60             if(args[i].substring(0,1).equals("-")){
61                 if(0 == state){
62                     // fresh one
63
state = 1;
64                     optionType = args[i];
65                 }else if(2 == state || 1 == state){
66                     // new one but old one should be saved
67
commandLineOption = new CommandLineOption(optionType, optionBundle);
68                     commandLineOptions.put(commandLineOption.getType(), commandLineOption);
69                     state = 1;
70                     optionType = args[i];
71                     optionBundle = null;
72                     
73                 }
74             }else{
75                 if(0 == state){
76                     commandLineOption = new CommandLineOption(CommandLineOptionConstants.SOLE_INPUT, args);
77                     commandLineOptions.put(commandLineOption.getType(), commandLineOption);
78                     return commandLineOptions;
79                     
80                 }else if(1 == state){
81                     optionBundle = new ArrayList();
82                     optionBundle.add(args[i]);
83                     state =2;
84                     
85                 }else if(2 == state){
86                     optionBundle.add(args[i]);
87                 }
88                 
89             }
90             
91             
92         }
93         
94         commandLineOption = new CommandLineOption(optionType, optionBundle);
95         commandLineOptions.put(commandLineOption.getType(), commandLineOption);
96         return commandLineOptions;
97
98     }
99     public Map getAllOptions() {
100         return this.commandLineOptions;
101     }
102
103     public List getInvalidOptions() {
104         List faultList = new ArrayList();
105         Iterator iterator = this.commandLineOptions.values().iterator();
106         while (iterator.hasNext()) {
107             CommandLineOption commandLineOption = ((CommandLineOption) (iterator
108                     .next()));
109             if (commandLineOption.isInvalid()) {
110                 faultList.add(commandLineOption);
111             }
112         }
113
114         return faultList;
115     }
116
117     
118
119 }
Popular Tags