001: /*
002: * $Id: ProcessWriter.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.org).
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.*;
017: import org.concern.model.Process;
018:
019: import java.io.*;
020: import java.util.*;
021: import java.nio.charset.Charset;
022:
023: /**
024: * <activity name="...">
025: * <interaction direction="out" message="start" target="Positions">
026: * </activity>
027: */
028: public class ProcessWriter {
029: private static final String[] CARDINALITIES = { "one to one",
030: "one to many", "many to one", "many to many" };
031: private static final String[] DIRECTIONS = { null, "in", "out",
032: "both" };
033:
034: public static void save(Process process, File file)
035: throws FileNotFoundException {
036:
037: OutputStream outputStream = new FileOutputStream(file);
038: PrintWriter writer = new PrintWriter(new OutputStreamWriter(
039: outputStream, Charset.forName("UTF-8")));
040:
041: writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
042: writer.print(" <process name=\"");
043: writer.print(process.getName());
044: writer.print("\" subject=\"");
045: writer.print(process.getSubject());
046: writer.println("\">");
047:
048: environment(process, writer);
049: attributes(process, writer);
050:
051: if (process.getLoader() != null) {
052: Loader loader = process.getLoader();
053: writer.print(" <loader name=\"");
054: writer.print(loader.getName());
055: writer.print("\" class=\"");
056: writer.print(loader.getImplementation());
057: writer.println("\">");
058:
059: environment(loader, writer);
060: writer.println(" </loader>");
061: }
062:
063: List actors = new ArrayList(process.getActors());
064: Collections.sort(actors);
065: for (Iterator iterator2 = actors.iterator(); iterator2
066: .hasNext();) {
067: Actor actor = (Actor) iterator2.next();
068: writer.print(" <actor name=\"");
069: writer.print(actor.getName());
070: writer.print("\" class=\"");
071: writer.print(actor.getImplementation());
072: writer.println("\">");
073:
074: if (actor.getDescription() != null) {
075: writer.print(" <description><![CDATA[");
076: writer.print(actor.getDescription());
077: writer.println("]]></description>");
078: }
079:
080: environment(actor, writer);
081: writer.println(" </actor>");
082: }
083: writer.println();
084:
085: List collaborations = new ArrayList(process.getCollaborators());
086: Collections.sort(collaborations);
087: for (Iterator iterator2 = collaborations.iterator(); iterator2
088: .hasNext();) {
089: Collaborator collaborator = (Collaborator) iterator2.next();
090: writer.print(" <collaboration name=\"");
091: writer.print(collaborator.getName());
092: writer.print("\" cardinality=\"");
093: writer.print(CARDINALITIES[collaborator.getCardinality()]);
094: writer.println("\">");
095:
096: if (collaborator.getDescription() != null) {
097: writer.print(" <description><![CDATA[");
098: writer.print(collaborator.getDescription());
099: writer.println("]]></description>");
100: }
101:
102: environment(collaborator, writer);
103: writer.println(" </collaboration>");
104: }
105: writer.println();
106:
107: List conditions = new ArrayList(process.getConditions());
108: Collections.sort(conditions);
109: for (Iterator iterator2 = conditions.iterator(); iterator2
110: .hasNext();) {
111: Condition condition = (Condition) iterator2.next();
112: writer.print(" <condition name=\"");
113: writer.print(condition.getName());
114: writer.print("\" class=\"");
115: writer.print(condition.getImplementation());
116: writer.print("\" temporal=\"");
117: writer.print(condition.isTemporal() ? "true" : "false");
118: writer.println("\">");
119:
120: if (condition.getDescription() != null) {
121: writer.print(" <description><![CDATA[");
122: writer.print(condition.getDescription());
123: writer.println("]]></description>");
124: }
125:
126: environment(condition, writer);
127: writer.println(" </condition>");
128: }
129: writer.println();
130:
131: List activities = new ArrayList(process.getActivities());
132: Collections.sort(activities);
133: for (Iterator iterator2 = activities.iterator(); iterator2
134: .hasNext();) {
135: Activity activity = (Activity) iterator2.next();
136: if (activity instanceof SynchronousActivity) {
137: writer.print(" <synchronous-activity name=\"");
138: } else {
139: writer.print(" <asynchronous-activity name=\"");
140: }
141: writer.print(activity.getName());
142: writer.print("\" class=\"");
143: writer.print(activity.getImplementation());
144: if (activity instanceof AsynchronousActivity) {
145: if (((AsynchronousActivity) activity).getActor() != null) {
146: writer.print("\" actor=\"");
147: writer.print(((AsynchronousActivity) activity)
148: .getActor());
149: }
150: writer.print("\" user=\"");
151: writer
152: .print(((AsynchronousActivity) activity)
153: .isUser() ? "true" : "false");
154: writer.print("\" optional=\"");
155: writer.print(((AsynchronousActivity) activity)
156: .isOptional() ? "true" : "false");
157: writer.print("\" timeout=\"");
158: writer.print(""
159: + ((AsynchronousActivity) activity)
160: .getTimeout());
161: } else {
162: writer.print("\" retry-delay=\"");
163: writer.print(""
164: + ((SynchronousActivity) activity)
165: .getRetryDelay());
166: writer.print("\" trials=\"");
167: writer.print(""
168: + ((SynchronousActivity) activity).getTrials());
169: }
170: writer.print("\" reentrant=\"");
171: writer.print(activity.isReentrant() ? "true" : "false");
172: writer.println("\">");
173:
174: writer.print(" <precondition><![CDATA[");
175: writer.print(activity.getPrecondition() != null ? activity
176: .getPrecondition() : "");
177: writer.println("]]></precondition>");
178: writer.print(" <postcondition><![CDATA[");
179: writer.print(activity.getPostcondition() != null ? activity
180: .getPostcondition() : "");
181: writer.println("]]></postcondition>");
182:
183: if (activity.getDescription() != null) {
184: writer.print(" <description><![CDATA[");
185: writer.print(activity.getDescription());
186: writer.println("]]></description>");
187: }
188:
189: interaction(activity.getInteraction(), writer);
190: environment(activity, writer);
191: attributes(activity, writer);
192:
193: if (activity instanceof SynchronousActivity) {
194: writer.println(" </synchronous-activity>");
195: } else {
196: writer.println(" </asynchronous-activity>");
197: }
198: }
199:
200: List events = new ArrayList(process.getEvents());
201: Collections.sort(events);
202: for (Iterator iterator2 = events.iterator(); iterator2
203: .hasNext();) {
204: Event event = (Event) iterator2.next();
205: writer.print(" <event name=\"");
206: writer.print(event.getName());
207: writer.print("\" class=\"");
208: writer.print(event.getImplementation());
209: writer.print("\" user=\"");
210: writer.print(event.isUser() ? "true" : "false");
211: writer.println("\">");
212:
213: writer.print(" <postcondition><![CDATA[");
214: writer.print(event.getPostcondition() != null ? event
215: .getPostcondition() : "");
216: writer.println("]]></postcondition>");
217:
218: if (event.getDescription() != null) {
219: writer.print(" <description><![CDATA[");
220: writer.print(event.getDescription());
221: writer.println("]]></description>");
222: }
223:
224: interaction(event.getInteraction(), writer);
225: environment(event, writer);
226: attributes(event, writer);
227:
228: writer.println(" </event>");
229: }
230:
231: List listeners = new ArrayList(process.getListeners());
232: Collections.sort(listeners);
233: for (Iterator iterator2 = listeners.iterator(); iterator2
234: .hasNext();) {
235: Listener listener = (Listener) iterator2.next();
236: writer.print(" <listener name=\"");
237: writer.print(listener.getName());
238: writer.print("\" class=\"");
239: writer.print(listener.getImplementation());
240: writer.println("\">");
241:
242: writer.print(" <precondition><![CDATA[");
243: writer.print(listener.getPrecondition() != null ? listener
244: .getPrecondition() : "");
245: writer.println("]]></precondition>");
246:
247: if (listener.getDescription() != null) {
248: writer.print(" <description><![CDATA[");
249: writer.print(listener.getDescription());
250: writer.println("]]></description>");
251: }
252:
253: interaction(listener.getInteraction(), writer);
254: environment(listener, writer);
255: attributes(listener, writer);
256:
257: writer.println(" </listener>");
258: }
259:
260: writer.println(" </process>");
261: writer.close();
262: }
263:
264: private static void interaction(Interaction interaction,
265: PrintWriter writer) {
266: if (interaction != null) {
267: writer.print(" <interaction direction=\"");
268: writer.print(DIRECTIONS[interaction.getDirection()]);
269: writer.print("\" message=\"");
270: writer.print(interaction.getMessage() != null ? interaction
271: .getMessage() : "");
272: writer.print("\" collaborator=\"");
273: writer.print(interaction.getCollaborator());
274: writer.println("\"/>");
275: }
276: }
277:
278: private static void environment(Element element, PrintWriter writer) {
279: if (element.getEnvironment() == null)
280: return;
281:
282: for (Iterator iterator = element.getEnvironment().iterator(); iterator
283: .hasNext();) {
284: EnvironmentEntry entry = (EnvironmentEntry) iterator.next();
285: writer.println(" <env-entry>");
286: if (entry.getDescription() != null) {
287: writer.print(" <description>");
288: writer.print(entry.getDescription());
289: writer.println("</description>");
290: }
291: writer.print(" <env-entry-name>");
292: writer.print(entry.getName());
293: writer.println("</env-entry-name>");
294: writer.print(" <env-entry-type>");
295: writer.print(entry.getType());
296: writer.println("</env-entry-type>");
297: writer.print(" <env-entry-value><![CDATA[");
298: writer.print(entry.getValue());
299: writer.println("]]></env-entry-value>");
300: writer.println(" </env-entry>");
301: }
302: }
303:
304: private static void attributes(DynaBean dynaBean, PrintWriter writer) {
305: for (Iterator iterator = dynaBean.getDynaPropertyNames()
306: .iterator(); iterator.hasNext();) {
307: String name = (String) iterator.next();
308: Object value = dynaBean.get(name);
309: if (value == null)
310: continue;
311:
312: writer.println(" <attribute>");
313: writer.print(" <attribute-name>");
314: writer.print(name);
315: writer.println("</attribute-name>");
316: writer.print(" <attribute-type>");
317: writer.print(value.getClass().getName());
318: writer.println("</attribute-type>");
319: writer.print(" <attribute-value><![CDATA[");
320: writer.print(value.toString());
321: writer.println("]]></attribute-value>");
322: writer.println(" </attribute>");
323: }
324: }
325: }
|