KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > gui > repository > lib > FractalAdlWriter


1 /***
2  * FractalGUI: a graphical tool to edit Fractal component configurations.
3  * Copyright (C) 2003 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: fractal@objectweb.org
20  *
21  * Authors: Eric Bruneton, Patrice Fauvel
22  */

23
24 package org.objectweb.fractal.gui.repository.lib;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33
34 import org.objectweb.fractal.adl.AbstractNode;
35 import org.objectweb.fractal.adl.Node;
36 import org.objectweb.fractal.gui.graph.model.GraphModel;
37 import org.objectweb.fractal.gui.graph.model.Rect;
38 import org.objectweb.fractal.gui.model.ClientInterface;
39 import org.objectweb.fractal.gui.model.Component;
40 import org.objectweb.fractal.gui.model.Interface;
41 import org.objectweb.fractal.gui.model.ServerInterface;
42 import org.objectweb.fractal.gui.repository.api.Storage;
43
44 public class FractalAdlWriter {
45   
46   Storage storage;
47     
48   Map JavaDoc types = new HashMap JavaDoc();
49   
50   boolean forceInternal;
51   
52   public String JavaDoc saveTemplate (final Component c, final GraphModel graph)
53     throws Exception JavaDoc
54   {
55     AdlNode n = new AdlNode("definition");
56     saveComponent(c, n, null, forceInternal, new HashMap JavaDoc());
57     if (graph != null) {
58       saveCoordinates(graph, c, n);
59     }
60     storage.store(getComponentType(c), n);
61     return getComponentType(c);
62   }
63   
64   public void saveComponent (final Component c, final AdlNode comp, final String JavaDoc name, final boolean internalType, final Map JavaDoc sharing)
65     throws Exception JavaDoc
66   {
67     saveComponentHeader(c, comp, name, internalType);
68     if (c.isComposite()) {
69       saveCompositeComponent(c, comp, name, sharing);
70     } else {
71       savePrimitiveComponent(c, comp, name, sharing);
72     }
73   }
74   
75   public void savePrimitiveComponent (final Component c, final AdlNode comp, final String JavaDoc name, final Map JavaDoc sharing)
76     throws Exception JavaDoc
77   {
78     if (c.getImplementation().length() > 0) {
79       AdlNode impl = new AdlNode("content");
80       impl.astSetAttribute("class", c.getImplementation());
81       comp.astAddNode(impl);
82     }
83     saveController(c, comp);
84   }
85
86   public void saveCompositeComponent (final Component c, final AdlNode comp, final String JavaDoc name, final Map JavaDoc sharing)
87     throws Exception JavaDoc
88   {
89     // sub components
90
List JavaDoc subComps = c.getSubComponents();
91     for (int i = 0; i < subComps.size(); ++i) {
92       Component sc = (Component)subComps.get(i);
93       boolean shared;
94       Component master;
95       if (sc.getMasterComponent() != null) {
96         shared = true;
97         master = sc.getMasterComponent();
98       } else if (sc.getSlaveComponents().size() > 0) {
99         shared = true;
100         master = sc;
101       } else {
102         shared = false;
103         master = null;
104       }
105
106       AdlNode subComp = new AdlNode("component");
107       if (shared) {
108         if (sharing.get(master) != null) {
109           subComp.astSetAttribute("name", getComponentName(sc));
110           subComp.astSetAttribute("definition", (String JavaDoc)sharing.get(master));
111         } else {
112           sharing.put(master, getPath(sc));
113           if (sc.getType().equals("") || forceInternal) {
114             // inline definition
115
saveComponent(sc, subComp, getComponentName(sc), true, sharing);
116           } else {
117             // external definition
118
subComp.astSetAttribute("name", getComponentName(sc));
119             subComp.astSetAttribute("definition", getComponentType(sc));
120             AdlNode def = new AdlNode("definition");
121             saveComponent(sc, def, null, false, sharing);
122             storage.store(getComponentType(sc), def);
123           }
124         }
125       } else {
126         if (sc.getType().equals("") || forceInternal) {
127           // inline definition
128
saveComponent(sc, subComp, getComponentName(sc), true, sharing);
129         } else {
130           // external definition
131
subComp.astSetAttribute("name", getComponentName(sc));
132           subComp.astSetAttribute("definition", getComponentType(sc));
133           AdlNode def = new AdlNode("definition");
134           saveComponent(sc, def, null, false, sharing);
135           storage.store(getComponentType(sc), def);
136         }
137       }
138       comp.astAddNode(subComp);
139     }
140
141     // bindings
142
List JavaDoc itfs = c.getServerInterfaces();
143     for (int i = 0; i < itfs.size(); ++i) {
144       ServerInterface sitf = (ServerInterface)itfs.get(i);
145       ClientInterface citf = (ClientInterface)sitf.getComplementaryInterface();
146       if (citf.getBinding() != null) {
147         AdlNode binding = new AdlNode("binding");
148         binding.astSetAttribute("client", "this." + getInterfaceName(citf));
149         sitf = citf.getBinding().getServerInterface();
150         if (sitf.getOwner() == c) {
151           binding.astSetAttribute("server", "this." + getInterfaceName(sitf));
152         } else {
153           binding.astSetAttribute("server", getComponentName(sitf.getOwner()) + "." + getInterfaceName(sitf));
154         }
155         comp.astAddNode(binding);
156       }
157     }
158     for (int i = 0; i < subComps.size(); ++i) {
159       Component sc = (Component)subComps.get(i);
160       if (sc.getMasterComponent() != null) {
161         itfs = sc.getMasterComponent().getClientInterfaces();
162       } else {
163         itfs = sc.getClientInterfaces();
164       }
165       for (int j = 0; j < itfs.size(); ++j) {
166         ClientInterface citf = (ClientInterface)itfs.get(j);
167         if (citf.getBinding() != null) {
168           AdlNode binding = new AdlNode("binding");
169           binding.astSetAttribute("client", getComponentName(sc) + "." + getInterfaceName(citf));
170           Interface sitf = citf.getBinding().getServerInterface();
171           if (sitf.getOwner() == c) {
172             binding.astSetAttribute("server", "this." + getInterfaceName(sitf));
173           } else if (sitf.getOwner().getParent() != c) {
174             continue;
175           } else {
176             binding.astSetAttribute("server", getComponentName(sitf.getOwner()) + "." + getInterfaceName(sitf));
177           }
178           comp.astAddNode(binding);
179         }
180       }
181     }
182   
183     saveController(c, comp);
184   }
185
186   public void saveComponentHeader (final Component c, final AdlNode comp, final String JavaDoc name, final boolean internalType)
187     throws Exception JavaDoc
188   {
189     if (internalType) {
190       comp.astSetAttribute("name", name != null ? name : getComponentType(c));
191       List JavaDoc itfs = c.getServerInterfaces();
192       saveInterfaces(itfs, comp, true);
193       itfs = c.getClientInterfaces();
194       saveInterfaces(itfs, comp, false);
195     } else {
196       String JavaDoc type = saveType(c);
197       comp.astSetAttribute("name", name != null ? name : getComponentType(c));
198       comp.astSetAttribute("extends", type);
199     }
200   }
201
202   public String JavaDoc saveType (final Component c) throws Exception JavaDoc {
203     ComponentType ctype = new ComponentType(c);
204     String JavaDoc name = (String JavaDoc)types.get(ctype);
205     if (name != null) {
206       return name;
207     }
208     name = getComponentType(c) + "Type";
209     AdlNode type = new AdlNode("definition");
210     type.astSetAttribute("name", name);
211     saveInterfaces(c.getServerInterfaces(), type, true);
212     saveInterfaces(c.getClientInterfaces(), type, false);
213     storage.store(name, type);
214     types.put(ctype, name);
215     return name;
216   }
217   
218   public void saveInterfaces (final List JavaDoc itfs, AdlNode itfTypes, boolean server)
219     throws Exception JavaDoc
220   {
221     for (int i = 0; i < itfs.size(); ++i) {
222       Interface itf = (Interface)itfs.get(i);
223       if (itf.isCollection() && itf.getMasterCollectionInterface() != null) {
224         // do not take into account slave collection interfaces
225
continue;
226       }
227       AdlNode itfType = new AdlNode("interface");
228       itfType.astSetAttribute("name", getInterfaceName(itf));
229       itfType.astSetAttribute("role", server ? "server" : "client");
230       itfType.astSetAttribute("signature", itf.getSignature());
231       if (itf.isOptional()) {
232         itfType.astSetAttribute("contingency", "optional");
233       }
234       if (itf.isCollection()) {
235         itfType.astSetAttribute("cardinality", "collection");
236       }
237       itfTypes.astAddNode(itfType);
238     }
239   }
240
241   public void saveController (final Component c, final AdlNode comp)
242     throws Exception JavaDoc
243   {
244     saveAttributes(c, comp);
245     if (c.getTemplateControllerDescriptor().length() > 0) {
246       AdlNode compDesc = new AdlNode("template-controller");
247       compDesc.astSetAttribute("desc", c.getComponentControllerDescriptor());
248       comp.astAddNode(compDesc);
249     }
250     if (c.getComponentControllerDescriptor().length() > 0) {
251       AdlNode compDesc = new AdlNode("controller");
252       compDesc.astSetAttribute("desc", c.getComponentControllerDescriptor());
253       comp.astAddNode(compDesc);
254     }
255   }
256
257   public void saveAttributes (final Component c, final AdlNode comp)
258     throws Exception JavaDoc
259   {
260     String JavaDoc attrController = c.getAttributeController();
261     List JavaDoc attrNames = c.getAttributeNames();
262     if (attrController.length() > 0 || attrNames.size() > 0) {
263       AdlNode attrs = new AdlNode("attributes");
264       if (attrController.length() > 0) {
265         attrs.astSetAttribute("signature", attrController);
266       }
267       for (int i = 0; i < attrNames.size(); ++i) {
268         AdlNode attr = new AdlNode("attribute");
269         String JavaDoc attrName = (String JavaDoc)attrNames.get(i);
270         attr.astSetAttribute("name", attrName);
271         attr.astSetAttribute("value", c.getAttribute(attrName));
272         attrs.astAddNode(attr);
273       }
274       comp.astAddNode(attrs);
275     }
276   }
277   
278   public void saveCoordinates (final GraphModel g, final Component c, final AdlNode comp)
279     throws Exception JavaDoc
280   {
281     List JavaDoc comps = c.getSubComponents();
282     for (int i = 0; i < comps.size(); ++i) {
283       Component sc = (Component)comps.get(i);
284       Rect rect = g.getComponentPosition(sc);
285       int color = g.getComponentColor(sc).getRGB();
286       AdlNode n = new AdlNode("coordinates");
287       n.astSetAttribute("name", sc.getName());
288       n.astSetAttribute("x0", Double.toString(rect.x0));
289       n.astSetAttribute("y0", Double.toString(rect.y0));
290       n.astSetAttribute("x1", Double.toString(rect.x1));
291       n.astSetAttribute("y1", Double.toString(rect.y1));
292       n.astSetAttribute("color", Integer.toString(color));
293       comp.astAddNode(n);
294       saveCoordinates(g, sc, n);
295     }
296   }
297    
298   String JavaDoc getComponentName (final Component c) {
299     String JavaDoc s = c.getName();
300     if (c.getMasterComponent() != null) {
301       s = c.getMasterComponent().getName();
302     }
303     if (s.length() == 0) {
304       s = "GENERATED-" + Integer.toHexString(c.hashCode());
305     }
306     return s;
307   }
308
309   String JavaDoc getComponentType (final Component c) {
310     String JavaDoc s = c.getType();
311     if (s.length() == 0) {
312       s = "org.objectweb.fractal.gui.tmp.C" + Integer.toHexString(c.hashCode());
313     }
314     return s;
315   }
316
317   String JavaDoc getPath (final Component c) {
318     String JavaDoc p = getComponentName(c);
319     if (c.getParent() != null && c.getParent().getParent() != null) {
320       p = getPath(c.getParent()) + "/" + p;
321     }
322     return p;
323   }
324   
325   String JavaDoc getInterfaceName (Interface i) {
326     if (i.isInternal()) {
327       i = i.getComplementaryInterface();
328     }
329     if (i.getMasterInterface() != null) {
330       i = i.getMasterInterface();
331     }
332     String JavaDoc s = i.getName();
333     if (i.getMasterCollectionInterface() != null) {
334       if (i.getMasterCollectionInterface().getName().length() == 0) {
335         s = getInterfaceName(i.getMasterCollectionInterface()) + s;
336       }
337     } else if (s.length() == 0) {
338       s = "GENERATED-" + Integer.toHexString(i.hashCode());
339     }
340     return s;
341   }
342
343   // -------------------------------------------------------------------------
344

345   static class AdlNode extends AbstractNode {
346
347     public AdlNode (String JavaDoc type) {
348       super(type);
349     }
350
351     private Map JavaDoc attributes = new HashMap JavaDoc();
352
353     private List JavaDoc nodes = new ArrayList JavaDoc();
354     
355     public Map JavaDoc astGetAttributes() {
356       return attributes;
357     }
358
359     public void astSetAttribute (String JavaDoc name, String JavaDoc value) {
360       attributes.put(name, value);
361     }
362     
363     public void astSetAttributes (Map JavaDoc attributes) {
364       this.attributes = attributes;
365     }
366
367     public String JavaDoc[] astGetNodeTypes () {
368       List JavaDoc l = new ArrayList JavaDoc();
369       for (int i = 0; i < nodes.size(); ++i) {
370         String JavaDoc n = ((Node)nodes.get(i)).astGetType();
371         if (!l.contains(n)) {
372           l.add(n);
373         }
374       }
375       return (String JavaDoc[])l.toArray(new String JavaDoc[l.size()]);
376     }
377
378     public Node[] astGetNodes (String JavaDoc type) {
379       List JavaDoc l = new ArrayList JavaDoc();
380       for (int i = 0; i < nodes.size(); ++i) {
381         Node n = (Node)nodes.get(i);
382         if (n.astGetType().equals(type)) {
383           l.add(n);
384         }
385       }
386       return (Node[])l.toArray(new Node[l.size()]);
387     }
388
389     public void astAddNode (Node n) {
390       nodes.add(n);
391     }
392
393     public void astRemoveNode(Node n) {
394       nodes.remove(n);
395     }
396
397     public Node astNewInstance() {
398       return new AdlNode(astGetType());
399     }
400   }
401
402   /**
403    * A component type.
404    */

405
406   class ComponentType {
407
408     /**
409      * The types of the interfaces provided by components of this type.
410      */

411
412     private Set JavaDoc provides;
413
414     /**
415      * The types of the interfaces required by components of this type.
416      */

417
418     private Set JavaDoc requires;
419
420     /**
421      * Constructs the {@link FractalAdlWriter.ComponentType} of the given
422      * component.
423      *
424      * @param c a component.
425      */

426
427     public ComponentType (final Component c) {
428       provides = new HashSet JavaDoc();
429       requires = new HashSet JavaDoc();
430       List JavaDoc l = c.getServerInterfaces();
431       for (int i = 0; i < l.size(); ++i) {
432         Interface itf = (Interface)l.get(i);
433         if (itf.getMasterCollectionInterface() == null) {
434           // do not take into account slave collection interfaces
435
provides.add(new InterfaceType(itf));
436         }
437       }
438       l = c.getClientInterfaces();
439       for (int i = 0; i < l.size(); ++i) {
440         Interface itf = (Interface)l.get(i);
441         if (itf.getMasterCollectionInterface() == null) {
442           // do not take into account slave collection interfaces
443
requires.add(new InterfaceType(itf));
444         }
445       }
446     }
447
448     public boolean equals (final Object JavaDoc o) {
449       if (o instanceof ComponentType) {
450         ComponentType other = (ComponentType)o;
451         if (other.provides.equals(provides)) {
452           return other.requires.equals(requires);
453         }
454       }
455       return false;
456     }
457
458     public int hashCode () {
459       int hashCode = 1;
460       Iterator JavaDoc i = provides.iterator();
461       while (i.hasNext()) {
462         hashCode *= i.next().hashCode();
463       }
464       i = requires.iterator();
465       while (i.hasNext()) {
466         hashCode *= i.next().hashCode();
467       }
468       return hashCode;
469     }
470   }
471
472   /**
473    * An interface type.
474    */

475
476   class InterfaceType {
477
478     /**
479      * The interface from which this interface type is derived.
480      */

481
482     private Interface i;
483
484     /**
485      * Constructs the {@link FractalAdlWriter.InterfaceType} of the given
486      * interface.
487      *
488      * @param i an interface.
489      */

490
491     public InterfaceType (final Interface i) {
492       this.i = i;
493     }
494
495     public boolean equals (final Object JavaDoc o) {
496       if (o instanceof InterfaceType) {
497         Interface j = ((InterfaceType)o).i;
498         if (getInterfaceName(i).equals(getInterfaceName(j))) {
499           if (i.getSignature().equals(j.getSignature())) {
500             if (i.isOptional() == j.isOptional()) {
501               return i.isCollection() == j.isCollection();
502             }
503           }
504         }
505       }
506       return false;
507     }
508
509     public int hashCode () {
510       return getInterfaceName(i).hashCode();
511     }
512   }
513 }
514
Popular Tags