View Javadoc

1   package com.explosion.datastream.exql.extensions;
2   
3   
4   /* =============================================================================
5    *       
6    *     Copyright 2004 Stephen Cowx
7    *
8    *     Licensed under the Apache License, Version 2.0 (the "License");
9    *     you may not use this file except in compliance with the License.
10   *     You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   *     Unless required by applicable law or agreed to in writing, software
15   *     distributed under the License is distributed on an "AS IS" BASIS,
16   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   *     See the License for the specific language governing permissions and
18   *     limitations under the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import com.explosion.expfmodules.wizard.standard.StandardWizard;
27  
28  
29  /***
30   * @author Steve.Cowx
31   * 
32   * This class loads a wizard definition from a file and 
33   * instantiates and initialises a wizard object
34   * from the definition it has loaded
35   */
36  public class ExtensionsLoader  {
37  	
38  	private static final String NAME_VALUE = "Value";
39  	private static final String NAME_VALUES = "Values";
40  	private static final String NAME_DATAITEM = "DataItem";
41  	private static final String NAME_DEFAULTVALUE = "DefaultValue";
42  	private static final String NAME_LABEL = "Label";
43  	private static final String NAME_SORTORDERNUMBER = "SortOrderNumber";
44  	private static final String NAME_TYPE = "Type";
45  	private static final String NAME_DATAITEMS = "DataItems";
46  	private static final String NAME_STEP = "Step";
47  	private static final String NAME_STEPS = "OutboundSteps";
48  	private static final String NAME_ICON = "Icon";
49  	private static final String NAME_NOTES = "Notes";
50  	private static final String NAME_ID = "Id";
51  	private static final String NAME_HELP = "Help";
52  	private static final String NAME_STEPDEFINITION = "StepDefinition";
53  	private static final String NAME_STEPDEFINITIONS = "StepDefinitions";
54  	private static final String NAME_VERSION = "Version";
55  	private static final String NAME_NAME = "Name";
56  	private static final String NAME_WIZARD = "Wizard";
57  
58  	private static org.apache.log4j.Logger log = org.apache.log4j.LogManager.getLogger(ExtensionsLoader.class);
59      private StandardWizard wizard;
60      private Map stepDefinitions = new HashMap();
61  
62  //    /***
63  //     * Loads and returns a Wizard object from the stream privided
64  //     * @param reader
65  //     * @return
66  //     * @throws Exception
67  //     */
68  //    public Wizard getWizard(Reader reader) throws Exception
69  //    {
70  //        Document document = getDocument(reader);
71  //        Wizard wizard = parseDocument(document);
72  //        return wizard;
73  //    }
74  //    
75  //    
76  //    /***
77  //     * Loads a document from a stream
78  //     *
79  //     * @throw a org.dom4j.DocumentException occurs whenever the buildprocess fails.
80  //     */
81  //    private Document getDocument(Reader reader) throws DocumentException {
82  //      SAXReader xmlReader = new SAXReader();
83  //      Document doc = xmlReader.read(reader);
84  //      return doc;
85  //    }
86  //    
87  //    /***
88  //     * Parses the document provided and instantiates a wizard from what it finds.
89  //     * @param document
90  //     * @return
91  //     * @throws Exception
92  //     */
93  //    private Wizard parseDocument(Document document) throws Exception
94  //    {
95  //        wizard = new StandardWizard();
96  //        List steps = new ArrayList();
97  //        Element root = document.getRootElement();
98  //        if (root.getName().equalsIgnoreCase(NAME_WIZARD))
99  //        {
100 //            processRoot(root);
101 //        }
102 //        
103 //        return wizard;
104 //    }
105 //    
106 //    /***
107 //     * This method processes the root element
108 //     * @param element
109 //     * @throws Exception
110 //     */
111 //    private void processRoot(Element element) throws Exception
112 //    {
113 //        wizard.setName(getValueOfFirstChildWithName(NAME_NAME, element, true));
114 //        wizard.setVersion(getValueOfFirstChildWithName(NAME_VERSION, element, true));
115 //        
116 //        loadStepDefinitions(element, wizard);
117 //        wizard.setStepDefinitions(stepDefinitions);
118 //        
119 //        wizard.setFirstStep(this.getFirstStep(element));
120 //    }
121 //    
122 //    /***
123 //     * This method parses the StepDefinitions seciton of the Wizard document
124 //     * and returns a List of StepDefinition objects
125 //     * @param rootElement
126 //     * @return
127 //     * @throws Exception
128 //     */
129 //    private void loadStepDefinitions(Element rootElement, Wizard wizard) throws Exception
130 //    {
131 //        Element steps = rootElement.element(NAME_STEPDEFINITIONS);
132 //        if (steps == null)
133 //            throw new ExpectedElementNotFoundException("Expected to find element with name 'StepDefinitions' as a child of '"+rootElement.getPath()+"' but did not.");
134 //            
135 //        for (Iterator it = steps.elementIterator(NAME_STEPDEFINITION); it.hasNext();)
136 //        {
137 //            Element xmlStep = (Element) it.next();
138 //            StandardStepDefinition step = new StandardStepDefinition();
139 //            
140 //            step.setName(getValueOfFirstChildWithName(NAME_NAME, xmlStep, true));
141 //            step.setHelp(getValueOfFirstChildWithName(NAME_HELP, xmlStep, false));
142 //            step.setId(getValueOfFirstChildWithName(NAME_ID,xmlStep,true));
143 //            step.setNotes(getValueOfFirstChildWithName(NAME_NOTES, xmlStep, false));
144 //            String icon = getValueOfFirstChildWithName(NAME_ICON, xmlStep, false);
145 //            if (icon != null && icon.trim().length() > 0)
146 //               step.setIcon(icon);
147 //            
148 //            String viewCLass = getValueOfFirstChildWithName("ViewClass", xmlStep, false);
149 //            if (viewCLass != null && viewCLass.trim().length() > 0)
150 //               step.setView((StepView) Class.forName(viewCLass).newInstance());
151 //            
152 //            String actionClass = getValueOfFirstChildWithName("ActionClass", xmlStep, false);
153 //            if (actionClass != null && actionClass.trim().length() > 0)
154 //               step.setAction((StepAction) Class.forName(actionClass).newInstance());
155 //            
156 //            Object[] dataItems = getDataItems( xmlStep);
157 //            if (dataItems != null)
158 //            {
159 //                step.setDataItems((Map)dataItems[0]);
160 //            }
161 //            
162 //            step.setWizard(wizard);
163 //            
164 //            if (stepDefinitions.get(step.getId()) != null)
165 //            {
166 //                throw new StepAlreadyExistsException("A step with the ID "+step.getId()+" has already been defined in this wizard.");
167 //            }
168 //            stepDefinitions.put(step.getId(),step);            
169 //        }
170 //    }
171 //    
172 //    /***
173 //     * @param rootElement
174 //     * @param steps
175 //     * @param xmlStep
176 //     * @throws ExpectedElementNotFoundException
177 //     * @throws InvalidDataForElementException
178 //     * @throws Exception
179 //     */
180 //    private Object[] getDataItems( Element xmlStep) throws ExpectedElementNotFoundException, InvalidDataForElementException, Exception
181 //    {
182 //        /* Get the data items */
183 //        Element dataItems = xmlStep.element(NAME_DATAITEMS);
184 //        if (dataItems == null)
185 //            return null;
186 //        
187 //        Map dataItemsMap = new HashMap();
188 //        Map preferenceStore = new HashMap();
189 //        List dataItemsList = new ArrayList();
190 //        for (Iterator ditems = dataItems.elementIterator(NAME_DATAITEM); ditems.hasNext();)
191 //        {
192 //            Element xmlDataItem = (Element) ditems.next();
193 //            
194 //            /* Extract various information about the Data Item */
195 //            String name = getValueOfFirstChildWithName(NAME_NAME, xmlDataItem, true);
196 //            String typeName = getValueOfFirstChildWithName(NAME_TYPE, xmlDataItem, true);
197 //            String collectionTypeName = null;
198 //            
199 //            /* Find out if it is a collection */
200 //            int indexOfDash = typeName.indexOf("-");
201 //            if (indexOfDash > 0)
202 //            {
203 //                collectionTypeName = typeName.substring(indexOfDash+1);
204 //                typeName = typeName.substring(0,indexOfDash);
205 //            }
206 //            
207 //            int collectionType = -1; 
208 //            int type = Preference.getTypeForName(typeName);
209 //            if (type < 0)
210 //                throw new InvalidDataItemTypeException("'"+typeName+"' is not a valid type for data items " +
211 //                		"in this wizard.  Please check value of element 'Type' in " + xmlDataItem.getPath() + 
212 //                		".  Valid types are string, file, directory, int, float, color, choice, int choice, boolean, font, encrypted, collection.");
213 //            
214 //            if (collectionTypeName != null)
215 //            {
216 //                collectionType = Preference.getTypeForName(collectionTypeName);
217 //                if (type < 0)
218 //                    throw new InvalidDataItemTypeException("'"+typeName+"' is not a valid type for data items " +
219 //                		"in this wizard.  Please check value of element 'Type' in " + xmlDataItem.getPath() + 
220 //                		".  Valid types are string, file, directory, int, float, color, choice, int choice, boolean, font, encrypted, collection.");
221 //            }
222 //            
223 //            String label = getValueOfFirstChildWithName(NAME_LABEL, xmlDataItem, true);
224 //            
225 //            String mandatory = getValueOfFirstChildWithName("Mandatory", xmlDataItem, false);
226 //            
227 //            int sortOrderNumber = getIntValue(xmlDataItem, NAME_SORTORDERNUMBER);
228 //            
229 //            /* Extract the default value */
230 //            String defaultValue = getValueOfFirstChildWithName(NAME_DEFAULTVALUE, xmlDataItem, false);
231 //            if (defaultValue == null)
232 //            {
233 //                defaultValue = "";
234 //            }
235 //            
236 //            Element valuesElements;
237 //            List values = getValues(xmlDataItem);
238 //            PreferenceDataItem item = null;
239 //            if (type == Preference.PROPERTY_TYPE_COLLECTION)
240 //            {
241 //                item = new PreferenceDataItem(name, type, collectionType, defaultValue, values, preferenceStore);
242 //            }
243 //            else
244 //            {
245 //                item = new PreferenceDataItem(name, type, defaultValue, values, preferenceStore);
246 //            }
247 //            item.getPreference().setLongName(label); 
248 //            item.setMandatory(GeneralUtils.getLenientBoolean(mandatory));
249 //            
250 //            Map attributes = getAttributes(xmlDataItem);
251 //            item.setAttributes(attributes);
252 //
253 //            dataItemsList.add(item);
254 //            dataItemsMap.put(item.getPreference().getUniqueIdentifier(), item);
255 //        }
256 //        
257 //        //Yes, this is a little bit clunky
258 //        Object[] lists = new Object[2];
259 //        lists[0] = dataItemsMap;
260 //        lists[1] = dataItemsList;
261 //                
262 //        return lists;
263 //    }
264 //
265 //    
266 //    /***
267 //     * Returns the first step in this wizard with refrences to all of the pther steps leading out from it
268 //     * @param rootElement
269 //     * @return
270 //     * @throws StepDefinitionNotFoundException
271 //     * @throws ExpectedElementNotFoundException
272 //     * @throws InvalidDataForElementException
273 //     * @throws InvalidConditionClassException
274 //     * @throws StepAlreadyExistsException
275 //     */
276 //    private Step getFirstStep(Element rootElement) throws StepDefinitionNotFoundException, ExpectedElementNotFoundException, InvalidDataForElementException, InvalidConditionClassException
277 //    {
278 //        Element firstStepElement = rootElement.element(NAME_STEP);
279 //        if (firstStepElement == null)
280 //            throw new ExpectedElementNotFoundException("Expected to find element with name 'Step' as a child of '"+rootElement.getPath()+"' but did not.");
281 //            
282 //        StandardStep step = createStep(firstStepElement);
283 //        return step;
284 //    }
285 //    
286 //    /***
287 //     * Recursive method which finds all of the outbound routes of a step
288 //     * @param rootElement
289 //     * @return
290 //     * @throws ExpectedElementNotFoundException
291 //     * @throws InvalidDataForElementException
292 //     * @throws InvalidConditionClassException
293 //     * @throws Exception
294 //     */
295 //    private List getSteps(Element stepElement) throws StepDefinitionNotFoundException, ExpectedElementNotFoundException, InvalidDataForElementException, InvalidConditionClassException
296 //    {
297 //        Element steps = stepElement.element(NAME_STEPS);
298 //        if (steps != null)
299 //        {
300 //            List list = new ArrayList();
301 //            for (Iterator it = steps.elementIterator(NAME_STEP); it.hasNext();)
302 //	        {
303 //	            list.add(createStep((Element)it.next()));
304 //	        }
305 //            return list;
306 //        }
307 //        return null;
308 //    }
309 //    
310 //    /***
311 //     * @param element
312 //     * @return
313 //     * @throws ExpectedElementNotFoundException
314 //     * @throws InvalidDataForElementException
315 //     * @throws StepDefinitionNotFoundException
316 //     * @throws InvalidConditionClassException
317 //     */
318 //    private StandardStep createStep(Element element) throws ExpectedElementNotFoundException, InvalidDataForElementException, StepDefinitionNotFoundException, InvalidConditionClassException
319 //    {
320 //        String stepId = getValueOfFirstChildWithName("StepDefinitionId",element,true);
321 //        
322 //        /* Check to see if this step exists */
323 //        StandardStepDefinition stepDefinition = (StandardStepDefinition) stepDefinitions.get(stepId);
324 //        if (stepDefinition == null)
325 //            throw new StepDefinitionNotFoundException("Invalid reference to a StepDefinition from a step.  StepDefinition with id "+stepId+" does not exist.");
326 //                
327 //        StandardStep step = new StandardStep();
328 //        step.setStepDefinition(stepDefinition);
329 //        step.setOutboundSteps(getSteps(element));
330 //        step.setCondition(getCondition(element));
331 //        return step;
332 //    }    
333 //    
334 //    /***
335 //     * This method parses out the condition for a particaular step.
336 //     * @param xmlStep
337 //     * @return
338 //     * @throws ExpectedElementNotFoundException
339 //     * @throws InvalidDataForElementException
340 //     * @throws Exception
341 //     */
342 //    private Condition getCondition( Element xmlStep) throws ExpectedElementNotFoundException, InvalidDataForElementException, InvalidConditionClassException
343 //    {
344 //        /* Get the data items */
345 //        Element conditionElement = xmlStep.element("Condition");
346 //        if (conditionElement == null)
347 //            return null;
348 //        
349 //        String conditionClassName = getValueOfFirstChildWithName("ClassName", conditionElement, true);
350 //        Condition condition;
351 //        try
352 //        {
353 //            condition = (Condition) Class.forName(conditionClassName).newInstance();
354 //        }
355 //        catch (Exception e)
356 //        {
357 //            throw new InvalidConditionClassException("Invalid condition class '"+conditionClassName+"'.");
358 //        }
359 //        
360 //        Element dataItemsElement = conditionElement.element("DataItems");
361 //        if (dataItemsElement != null)
362 //        {
363 //	        Map dataItemsMap = new HashMap();
364 //	        
365 //	        for (Iterator itemsIterator = dataItemsElement.elementIterator("DataItem"); itemsIterator.hasNext();)
366 //	        {
367 //	            Element dataItem = (Element) itemsIterator.next();
368 //	            
369 //	            /* Extract various information about the Data Item */
370 //	            String dataItemName = getValueOfFirstChildWithName("Name", dataItem, true);
371 //	            String dataItemValue = getValueOfFirstChildWithName("Value", dataItem, true);
372 //	            dataItemsMap.put(dataItemName, dataItemValue);	            
373 //	        }
374 //	        condition.setDataItems(dataItemsMap);
375 //        }
376 //        return condition;
377 //    }
378 //    
379 //    /***
380 //     * Returns the values nested inside this data item
381 //     * @param steps
382 //     * @param ditems
383 //     * @param xmlDataItem
384 //     */
385 //    private List getValues(Element xmlDataItem)
386 //    {
387 //        /* Extract the values for this data item */
388 //        Element valuesElements = xmlDataItem.element(NAME_VALUES);
389 //        List values = null;
390 //        if (valuesElements != null)
391 //        {
392 //            values = new ArrayList();
393 //            for (Iterator vals = valuesElements.elementIterator(NAME_VALUE); vals.hasNext();)
394 //            {
395 //                Element val = (Element) vals.next();
396 //                values.add(val.getTextTrim());
397 //            }
398 //        }
399 //        return values;
400 //    }
401 //    
402 //    /***
403 //     * Returns the values nested inside this data item
404 //     * @param steps
405 //     * @param ditems
406 //     * @param xmlDataItem
407 //     * @throws ExpectedElementNotFoundException
408 //     */
409 //    private Map getAttributes(Element xmlDataItem) throws ExpectedElementNotFoundException
410 //    {
411 //        /* Extract the values for this data item */
412 //        Element attributesElement = xmlDataItem.element("Attributes");
413 //        Map attributes = null;
414 //        if (attributesElement != null)
415 //        {
416 //            attributes = new HashMap();
417 //        	for (Iterator vals = attributesElement.elementIterator("Attribute"); vals.hasNext();)
418 //            {
419 //                Element attributeElement = (Element) vals.next();
420 //                String name = this.getValueOfFirstChildWithName("Name",attributeElement,true);
421 //                String value = this.getValueOfFirstChildWithName("Value",attributeElement,true);
422 //                attributes.put(name, value);
423 //            }
424 //        }
425 //        return attributes;
426 //    }
427 //    
428 //    /***
429 //     * @param rootElement
430 //     * @param xmlStep
431 //     * @param step
432 //     * @throws ExpectedElementNotFoundException
433 //     */
434 //    private int getIntValue(Element element, String name) throws ExpectedElementNotFoundException, InvalidDataForElementException
435 //    {
436 //        try
437 //        {
438 //            return new Integer(getValueOfFirstChildWithName(name, element, true)).intValue();
439 //        }
440 //        catch (NumberFormatException e)
441 //        {
442 //            throw new InvalidDataForElementException("Invalid number for '"+name+"' for element " + element.getPath(),e );
443 //        }
444 //    }
445 //
446 //    /***
447 //     * This method returns a string value representing the text for the element with the given name which is a child of the 
448 //     * element provided. 
449 //     * @param String the name of the child element sought
450 //     * @param The parent element
451 //     * @param If true and Element does not have a child with name name then an exceptio is thrown.  If false and the same is true, then null is returned.
452 //     * @return String
453 //     * @throws  ExpectedElementNotFoundException
454 //     */
455 //    private String getValueOfFirstChildWithName(String name, Element element, boolean expectedChild) throws ExpectedElementNotFoundException
456 //    {
457 //        String text = element.elementTextTrim(name);
458 //        if (expectedChild && text == null)
459 //            throw new ExpectedElementNotFoundException("Expected to find element with name '"+name+"' as a child of '"+element.getPath()+"' but did not.");
460 //        else 
461 //           return text;
462 //    }
463     
464 }