001: /*
002: * $Id: ActivityHandler.java 606 2005-12-12 12:49:05Z nebulosu $
003: * (c) Copyright 2004 con:cern development team.
004: *
005: * This file is part of con:cern (http://concern.sf.net).
006: *
007: * con:cern is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU Lesser General Public License
009: * as published by the Free Software Foundation; either version 2.1
010: * of the License, or (at your option) any later version.
011: *
012: * Please see COPYING for the complete licence.
013: */
014: package org.concern.model.cpd;
015:
016: import org.concern.model.Activity;
017: import org.concern.model.SynchronousActivity;
018: import org.concern.model.AsynchronousActivity;
019: import org.xml.sax.Attributes;
020: import org.xml.sax.SAXException;
021:
022: public class ActivityHandler extends ScopeHandler {
023: private Activity activity;
024: private TextHandler preconditionHandler = new TextHandler();
025: private TextHandler postconditionHandler = new TextHandler();
026: private TextHandler descriptionHandler = new TextHandler();
027: private InteractionHandler interactionHandler = new InteractionHandler();
028: private EnvironmentEntryHandler environmentEntryHandler = new EnvironmentEntryHandler();
029: private AttributeHandler attributeHandler = new AttributeHandler();
030:
031: public ActivityHandler() {
032: }
033:
034: public Activity getActivity() {
035: return activity;
036: }
037:
038: public void startElement(String uri, String localName,
039: String qName, Attributes attributes) throws SAXException {
040: if ("asynchronous-activity".equals(qName)
041: || ("activity".equals(qName) && "true"
042: .equals(attributes.getValue("asynchronous")))) {
043: activity = new AsynchronousActivity(attributes
044: .getValue("name"));
045: } else {
046: activity = new SynchronousActivity(attributes
047: .getValue("name"));
048: }
049: //common attributes
050: activity.setImplementation(attributes.getValue("class"));
051: if (attributes.getValue("once") != null)
052: activity.setReentrant("false".equals(attributes
053: .getValue("once")));
054: else
055: activity.setReentrant("true".equals(attributes
056: .getValue("reentrant")));
057:
058: if (activity instanceof SynchronousActivity) {
059: try {
060: int retryDelay = Integer.parseInt(attributes
061: .getValue("retry-delay"));
062: ((SynchronousActivity) activity)
063: .setRetryDelay(retryDelay);
064: } catch (Exception e) {
065: }
066: try {
067: int trials = Integer.parseInt(attributes
068: .getValue("trials"));
069: ((SynchronousActivity) activity).setTrials(trials);
070: } catch (Exception e) {
071: }
072: } else {
073: ((AsynchronousActivity) activity).setOptional("true"
074: .equals(attributes.getValue("optional")));
075: ((AsynchronousActivity) activity).setUser("true"
076: .equals(attributes.getValue("user")));
077: ((AsynchronousActivity) activity).setActor(attributes
078: .getValue("actor"));
079: try {
080: int timeout = Integer.parseInt(attributes
081: .getValue("timeout"));
082: ((AsynchronousActivity) activity).setTimeout(timeout);
083: } catch (Exception e) {
084: }
085: }
086: }
087:
088: public void characters(char ch[], int start, int length)
089: throws SAXException {
090: }
091:
092: public void endElement(String uri, String localName, String qName)
093: throws SAXException {
094: }
095:
096: public ScopeHandler getHandler(String localName, String qName) {
097: if ("precondition".equals(qName))
098: return preconditionHandler;
099: else if ("postcondition".equals(qName))
100: return postconditionHandler;
101: else if ("description".equals(qName))
102: return descriptionHandler;
103: else if ("interaction".equals(qName))
104: return interactionHandler;
105: else if ("env-entry".equals(qName))
106: return environmentEntryHandler;
107: else if ("attribute".equals(qName))
108: return attributeHandler;
109: else
110: throw new RuntimeException("unexpected tag " + localName
111: + " " + qName);
112: }
113:
114: public void fetchChild(ScopeHandler handler) {
115: if (handler == preconditionHandler)
116: activity.setPrecondition(preconditionHandler.getText());
117: else if (handler == postconditionHandler)
118: activity.setPostcondition(postconditionHandler.getText());
119: else if (handler == descriptionHandler)
120: activity.setDescription(descriptionHandler.getText());
121: else if (handler == interactionHandler)
122: activity
123: .setInteraction(interactionHandler.getInteraction());
124: else if (handler == environmentEntryHandler)
125: activity.getEnvironment().add(
126: environmentEntryHandler.getEnvironmentEntry());
127: else if (handler == attributeHandler)
128: activity.set(attributeHandler.getAttribute().getName(),
129: attributeHandler.getAttribute().getValue());
130: }
131: }
|