View Javadoc

1   /*$Id: CommandLineParameterDescription.java,v 1.6 2004/12/18 15:43:57 jdt Exp $
2    * Created on 17-Jun-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.applications.commandline;
12  
13  import org.astrogrid.applications.beans.v1.parameters.types.ParameterTypes;
14  import org.astrogrid.applications.beans.v1.types.SwitchTypes;
15  import org.astrogrid.applications.description.base.BaseParameterDescription;
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  /*** extended parameter description for commandline - contains further fields loaded from config - flag format, etc.
21   *
22   */
23  public class CommandLineParameterDescription extends BaseParameterDescription {
24      /*** Construct a new CommandLineParameterDescription
25       * 
26       */
27      public CommandLineParameterDescription() {
28          super();
29      }
30  
31     
32      /***
33       * The string that makes up the command switch. If this is not specified then it is assumed that the switch is the same as the name
34       */
35      private String commandSwitch=null;
36      /***
37       * The switchtype can be "normal" i.e. it is a -switch form or "keyword" where is is of the form switch=par
38       */
39      private SwitchTypes switchType = SwitchTypes.NORMAL;
40      /***
41       * The commandPosition indicates where on the command line the parameter is to be placed - The first parameter position is 1. If this value is specified then it means that no switch will be output, but the parameter value will be placed directly on the command line at that position.
42       */
43      private int commandPosition = -1;
44     
45  
46      /*** flag that indicates application expects a file-reference containing this parameter in the arguments, rather than the parameter value itself. NB This default should match the schema default*/ 
47      private boolean fileRef = true;
48      
49      /*** if non-null, then the application will always write output to a file with this name */
50      private String localFileName = null;
51  
52      /***
53       * Adds any necessary switches to the commandline parameter. This is controlled by the @link #commandPosition, @link #commandSwitch and @link #switchType fields. 
54       * If the commandPosition is anything other than -1 then no adornment is added. If a switch string is to be added then the style is controlled by switchType and the
55       * string for the switch is given by commandSwitch, or if that is null the parameter name is used. 
56       * @param val
57       * @return stringbuffer containing original value, plus any required adornments.
58       */
59      public List addCmdlineAdornment(String val)
60      {
61         List cmdarg = new ArrayList();
62        
63         if (commandPosition == -1) {
64            // if not a command position type parameter then we need to add a switch
65            String sw = name;
66            if(commandSwitch != null)
67            {
68               sw = commandSwitch;
69            }
70            
71            if (!sw.equals("---")) //TODO perhaps do this in a nicer way in the schema
72            {
73              if (switchType.equals(SwitchTypes.NORMAL)) {
74                 cmdarg.add("-" + sw);
75                 cmdarg.add(val);
76  
77              }
78              else if (switchType.equals(SwitchTypes.KEYWORD)) {
79                 cmdarg.add(sw + "=" + val);
80              }
81           }
82           else
83           {
84            // do nothing - do not put the parameter on the commandline if the switch name is ---
85           }
86         }
87         else
88         {
89            cmdarg.add(val);
90         }
91        
92         return cmdarg;
93      }
94     
95      /***
96       * @return
97       */
98      public int getCommandPosition() {
99         return commandPosition;
100     }
101 
102     /***
103      * @return
104      */
105     public String getCommandSwitch() {
106        return commandSwitch;
107     }
108 
109     /***
110      * Returns the SwitchType as a enumerated type. 
111      * @TODO Daft name...
112      * @return
113      */
114     public SwitchTypes getSwitchTypeType() {
115        return switchType;
116     }
117 
118     /***
119      * @param i
120      *
121      */
122     public void setCommandPosition(int i) {
123        commandPosition = i;
124     }
125 
126     /***
127      * @param string
128      * 
129      */
130     public void setCommandSwitch(String string) {
131        commandSwitch = string;
132     }
133 
134     /***
135      * Set the switchType.
136      * To allow the digester to work we need the argument to be a string (and the pseudo getter needs to be present), even though internally this is set to @link SwitchTypes.
137      * @param string
138      * 
139      */
140     public void setSwitchType(String string) {
141        try {
142         switchType = SwitchTypes.valueOf(string);
143     }
144     catch (RuntimeException e) {
145         // catch any problem with the enumeration - hopefully this should not happen if the config has been verified by an xml parser
146         logger.warn("Invalid SwitchType - " + string + "defaulting to NORMAL for parameter "+ name , e);
147         switchType = SwitchTypes.NORMAL;
148     }
149     }
150     public String getSwitchType()
151     {
152         return switchType.toString();
153     }
154 
155    
156    /*** setter method to set superclasses 'type' field from a string - used within the digester parsing of the configuration file */
157     public void setTypeString(String arg0) {
158         super.setType(ParameterTypes.valueOf(arg0));
159     }
160 
161     public boolean isFileRef() {
162         return fileRef;
163     } 
164     
165     public void setFileRef(boolean isFile) {
166         this.fileRef = isFile;
167     }
168 
169    public String getLocalFileName() {
170       return localFileName;
171    }
172    public void setLocalFileName(String localFileName) {
173       this.localFileName = localFileName;
174    }
175 }
176 
177 
178 /* 
179 $Log: CommandLineParameterDescription.java,v $
180 Revision 1.6  2004/12/18 15:43:57  jdt
181 merge from  cea_pah_561b
182 
183 Revision 1.5.2.1  2004/12/07 07:32:27  pah
184 update to pass band information properly
185 
186 Revision 1.5  2004/11/27 13:20:02  pah
187 result of merge of pah_cea_bz561 branch
188 
189 Revision 1.4.26.1  2004/11/15 16:53:56  pah
190 enable pickup of locally generated file name
191 
192 Revision 1.4  2004/09/09 12:22:12  pah
193 change the fileref default to match schema
194 
195 Revision 1.3  2004/08/28 07:17:34  pah
196 commandline parameter passing - unit tests ok
197 
198 Revision 1.2  2004/07/01 11:07:59  nw
199 merged in branch
200 nww-itn06-componentization
201 
202 Revision 1.1.2.1  2004/07/01 01:43:39  nw
203 final version, before merge
204  
205 */