summaryrefslogtreecommitdiffstats
path: root/sandbox/tools/xmlfromxsd/src/main/java/org/apache/tuscany/tools/xmlfromxsd/generate/CmdLineArgsProcessor.java
blob: bdfd1bbe307f68d3ab9da401f6a89c0440d7a2f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
 *
 * Copyright 2006 The Apache Software Foundation or its licensors as applicable
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.tools.xmlfromxsd.generate;

/**
 * This class provides a generic command line arguments processing utility.  The processArgs
 * method of this class processes the command line arguments that could contain option flags, 
 * options and values and calls a ArgumentHanlder instance for handling the agruments. 
 *
 */
public class CmdLineArgsProcessor
{
	private String printUsageMessage = "No hints available on Usage!";
	private CmdLineArgsHandler argsHandler = null;
	public static final String HYPEN = "-";
	
	public void processArgs(String[] args)
	{
		try
		{
			 if (args.length == 0) 
			 {
				 printUsage();
		     }
			 else
			 {
				 parseAndHandleArgs(args);
			 }
		}
		catch ( Exception e )
		{
			System.out.println("Exception in processing argument - " + e);
			printUsage();
		}
		
	}
	
	public void parseAndHandleArgs(String[] args) throws Exception
	{
		int index = 0;
		while ( index < args.length - 1 )
		{
			if ( args[index].startsWith(HYPEN) && !args[ index + 1 ].startsWith(HYPEN) )
			{
				argsHandler.handleArgument(args[index].substring(1), args[index + 1]);
				index = index + 2;
			}
			else
			{
				throw new IllegalArgumentException("Wrong Usage of options!");
			}
		}
	}
	
	protected void printUsage()
	{
		
	}

	public CmdLineArgsHandler getArgsHandler()
	{
		return argsHandler;
	}

	public void setArgsHandler(CmdLineArgsHandler argsHandler)
	{
		this.argsHandler = argsHandler;
	}

	public String getPrintUsageMessage()
	{
		return printUsageMessage;
	}

	public void setPrintUsageMessage(String printUsageMessage)
	{
		this.printUsageMessage = printUsageMessage;
	}
}