1 23 24 package org.objectweb.fractal.gui.repository.lib; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import java.util.Set ; 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 types = new HashMap (); 49 50 boolean forceInternal; 51 52 public String saveTemplate (final Component c, final GraphModel graph) 53 throws Exception 54 { 55 AdlNode n = new AdlNode("definition"); 56 saveComponent(c, n, null, forceInternal, new HashMap ()); 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 name, final boolean internalType, final Map sharing) 65 throws Exception 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 name, final Map sharing) 76 throws Exception 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 name, final Map sharing) 87 throws Exception 88 { 89 List 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 )sharing.get(master)); 111 } else { 112 sharing.put(master, getPath(sc)); 113 if (sc.getType().equals("") || forceInternal) { 114 saveComponent(sc, subComp, getComponentName(sc), true, sharing); 116 } else { 117 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 saveComponent(sc, subComp, getComponentName(sc), true, sharing); 129 } else { 130 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 List 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 name, final boolean internalType) 187 throws Exception 188 { 189 if (internalType) { 190 comp.astSetAttribute("name", name != null ? name : getComponentType(c)); 191 List itfs = c.getServerInterfaces(); 192 saveInterfaces(itfs, comp, true); 193 itfs = c.getClientInterfaces(); 194 saveInterfaces(itfs, comp, false); 195 } else { 196 String type = saveType(c); 197 comp.astSetAttribute("name", name != null ? name : getComponentType(c)); 198 comp.astSetAttribute("extends", type); 199 } 200 } 201 202 public String saveType (final Component c) throws Exception { 203 ComponentType ctype = new ComponentType(c); 204 String name = (String )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 itfs, AdlNode itfTypes, boolean server) 219 throws Exception 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 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 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 259 { 260 String attrController = c.getAttributeController(); 261 List 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 attrName = (String )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 280 { 281 List 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 getComponentName (final Component c) { 299 String 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 getComponentType (final Component c) { 310 String 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 getPath (final Component c) { 318 String 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 getInterfaceName (Interface i) { 326 if (i.isInternal()) { 327 i = i.getComplementaryInterface(); 328 } 329 if (i.getMasterInterface() != null) { 330 i = i.getMasterInterface(); 331 } 332 String 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 345 static class AdlNode extends AbstractNode { 346 347 public AdlNode (String type) { 348 super(type); 349 } 350 351 private Map attributes = new HashMap (); 352 353 private List nodes = new ArrayList (); 354 355 public Map astGetAttributes() { 356 return attributes; 357 } 358 359 public void astSetAttribute (String name, String value) { 360 attributes.put(name, value); 361 } 362 363 public void astSetAttributes (Map attributes) { 364 this.attributes = attributes; 365 } 366 367 public String [] astGetNodeTypes () { 368 List l = new ArrayList (); 369 for (int i = 0; i < nodes.size(); ++i) { 370 String n = ((Node)nodes.get(i)).astGetType(); 371 if (!l.contains(n)) { 372 l.add(n); 373 } 374 } 375 return (String [])l.toArray(new String [l.size()]); 376 } 377 378 public Node[] astGetNodes (String type) { 379 List l = new ArrayList (); 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 405 406 class ComponentType { 407 408 411 412 private Set provides; 413 414 417 418 private Set requires; 419 420 426 427 public ComponentType (final Component c) { 428 provides = new HashSet (); 429 requires = new HashSet (); 430 List 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 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 requires.add(new InterfaceType(itf)); 444 } 445 } 446 } 447 448 public boolean equals (final Object 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 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 475 476 class InterfaceType { 477 478 481 482 private Interface i; 483 484 490 491 public InterfaceType (final Interface i) { 492 this.i = i; 493 } 494 495 public boolean equals (final Object 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 |