View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
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.gwe.integration.slicer.model;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.gwe.utils.IOUtils;
23  import org.gwe.utils.VelocityUtils;
24  import org.gwe.utils.cmd.OptionTemplate;
25  import org.gwe.utils.cmd.OptionableAppTemplate;
26  
27  /**
28   * @author Marco Ruiz
29   * @since Dec 14, 2007
30   */
31  public class ExecutableModel extends OptionableAppTemplate {
32  
33  	public static String getXMLHeader() {
34  		return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
35  	}
36  	
37  	public final OptionTemplate OPT_XML = new OptionTemplate("xml", "XML. Displays the CLM schema.");
38  	public final OptionTemplate OPT_LOGO = new OptionTemplate("logo", "Logo. Returns a stream with a custom logo image for this CLM.");
39  	
40  	private String category;
41  	private String title;
42  	private String description;
43  	private String version = "1.0-Alpha";
44  	private String url = "";
45  	private String license = "http://www.apache.org/licenses/LICENSE-2.0.txt";
46  	private String contributor = "Jeff Grethe, Marco Ruiz";
47  	private String acknowledgements = "BIRN CC - 'Grid Wizard Enterprise' (GWE) Project";
48  	private List<GroupModel> groups = new ArrayList<GroupModel>();
49  
50  	private String xmlTemplate;
51  
52  	public ExecutableModel(String title, String category, String description) {
53  		this(title, category, description, "standardExecutionModel.vm");
54  	}
55  	
56  	public ExecutableModel(String title, String category, String description, String templateFilename) {
57  		this.category = category;
58  		this.title = title;
59  		this.description = description;
60  		setXMLTemplate(IOUtils.readClassPathFile(templateFilename));
61  	}
62  	
63  	public void setXMLTemplate(String xmlTemplate) {
64  		this.xmlTemplate = xmlTemplate;
65  	}
66  
67  	public void loadArgs(String[] args) {
68  		loadArgs(args, false);
69  	}
70  	
71  	public void loadArgs(String[] args, boolean processXML) {
72      	// Register the specific application options
73      	for (GroupModel group : groups)
74  			for (OptionTemplate param : group.getParameters()) addArg(param);
75  
76      	// Register the system XML option
77      	addArg(OPT_XML);
78      	addArg(OPT_LOGO);
79      	
80      	// Invoke the super implementation of this method
81      	super.loadArgs(args);
82  
83      	if (processXML) outputXMLIfRequested();
84      }
85  
86  	private void outputXMLIfRequested() {
87  		// Print XML representation and leave if XML option selected
88      	if (isXMLSelected()) {
89      		System.out.println(getXML());
90      		System.exit(0);
91      	}
92  	}
93  
94  	public boolean isXMLSelected() {
95  		return getArg(OPT_XML) != null;
96  	}
97  	
98  	public boolean isLogoSelected() {
99  		return getArg(OPT_LOGO) != null;
100 	}
101 	
102 	public String getXML() {
103 		return getXMLHeader() + VelocityUtils.merge("executable", this, xmlTemplate);
104     }
105 	
106 	public String getCategory() {
107 		return category;
108 	}
109 	
110 	public void setCategory(String category) {
111 		this.category = category;
112 	}
113 	
114 	public String getTitle() {
115 		return title;
116 	}
117 	
118 	public void setTitle(String title) {
119 		this.title = title;
120 	}
121 	
122 	public String getDescription() {
123 		return description;
124 	}
125 	
126 	public void setDescription(String description) {
127 		this.description = description;
128 	}
129 	
130 	public String getVersion() {
131 		return version;
132 	}
133 	
134 	public void setVersion(String version) {
135 		this.version = version;
136 	}
137 
138 	public String getUrl() {
139 		return url;
140 	}
141 	
142 	public void setUrl(String documentationURL) {
143 		this.url = documentationURL;
144 	}
145 	
146 	public String getContributor() {
147 		return contributor;
148 	}
149 	
150 	public void setContributor(String contributor) {
151 		this.contributor = contributor;
152 	}
153 
154 	public String getLicense() {
155 		return license;
156 	}
157 
158 	public void setLicense(String license) {
159 		this.license = license;
160 	}
161 
162 	public String getAcknowledgements() {
163 		return acknowledgements;
164 	}
165 
166 	public void setAcknowledgements(String acknowledgements) {
167 		this.acknowledgements = acknowledgements;
168 	}
169 
170 	public List<GroupModel> getGroups() {
171 		return groups;
172 	}
173 	
174 	public GroupModel addGroup(String label, String description) {
175 		GroupModel group = new GroupModel(label, description);
176 		this.groups.add(group);
177 		return group;
178 	}
179 	
180 	public List<ParameterModel> getParameters() {
181 		List<ParameterModel> result = new ArrayList<ParameterModel>();
182 		for (GroupModel group : groups) result.addAll(group.getParameters());
183 		return result;
184 	}
185 }