View Javadoc

1   package org.catacomb.serial.om;
2   
3   import org.catacomb.interlish.structure.Element;
4   import org.catacomb.interlish.structure.ElementFactory;
5   import org.catacomb.interlish.structure.Specified;
6   import org.catacomb.interlish.structure.Specifier;
7   
8   
9   public class OmElementFactory implements ElementFactory {
10  
11  
12  
13      public OmElementFactory() {
14  
15      }
16  
17      // for elements that should ocntain all their context as attibutes (ie, the package)
18      public Element makeStandaloneElementFor(Object obj) {
19          Element elt = null;
20  
21          if (obj instanceof Specified) {
22              elt = makeSpecifiedElement((Specified)obj);
23  
24          } else {
25              elt = makeClassElement(obj);
26          }
27  
28          return elt;
29      }
30  
31  
32  
33      private Element makeSpecifiedElement(Specified spd) {
34          Specifier sp = spd.getSpecifier();
35  
36          String sch = sp.getSpecifiedTypeName(spd);
37  
38          OmElement ome = new OmElement();
39          ome.setName(sch);
40          return ome;
41      }
42  
43  
44      private Element makeClassElement(Object obj) {
45          String cnm = obj.getClass().getName();
46          int ild = cnm.lastIndexOf(".");
47          String pkg = "";
48          String enm = cnm;
49          if (ild >= 0) {
50              enm = cnm.substring(ild+1, cnm.length());
51              pkg = cnm.substring(0, ild);
52          }
53          OmElement elt = new OmElement();
54          elt.setName(enm);
55          elt.addAttribute("package", pkg);
56          return elt;
57      }
58  
59  
60  
61      // for elements within a known context - just get the class name and use it for the name
62      public Element makeElementFor(Object obj) {
63          String  cnm = obj.getClass().getName();
64          int ild = cnm.lastIndexOf(".");
65          if (ild >= 0) {
66              cnm = cnm.substring(ild+1, cnm.length());
67          }
68          OmElement elt = new OmElement();
69          elt.setName(cnm);
70          return elt;
71      }
72  
73  
74  
75      public Element makeElement(String name) {
76          OmElement elt = new OmElement();
77          elt.setName(name);
78          return elt;
79      }
80  
81      public Element makeElement(String name, String body) {
82          OmElement elt = new OmElement();
83          elt.setName(name);
84          elt.setBody(body);
85          return elt;
86      }
87  
88  
89  
90  
91  
92      // in following the objects are whatever class is returned by the above two;
93      public void addAttribute(Element elt, String name, String value) {
94          ((OmElement)elt).addAttribute(name, value);
95      }
96  
97  
98      public void addAttribute(Element elt, String name, double value) {
99          ((OmElement)elt).addAttribute(name, "" + value);
100     }
101 
102 
103     public void addElement(Element parent, Object child) {
104         ((OmElement)parent).addElement((OmElement)child);
105     }
106 
107 
108 }
109