1 20 package org.objectweb.modfact.corba.xmiio.exporter; 21 22 import org.objectweb.modfact.corba.generator.TagGenerator; 23 import org.objectweb.modfact.corba.helper.CORBACommon; 24 import org.objectweb.modfact.corba.logging.Level; 25 import org.objectweb.modfact.corba.logging.ModFactLogger; 26 import org.objectweb.modfact.corba.provider.CORBAProvider; 27 import org.omg.mof.Model.ModelElement; 28 29 34 public abstract class XMIExport extends TagGenerator { 35 36 37 protected org.omg.CORBA.ORB _orb; 38 39 40 protected CORBACommon corbaHelper; 41 42 43 protected String _dtd; 44 45 46 protected String _namespace; 47 48 49 protected String _model; 50 51 52 protected String _metamodel; 53 54 55 protected java.util.Hashtable _elements = new java.util.Hashtable (); 56 57 58 protected int _element_index = 1; 59 60 61 protected java.util.Hashtable _type_codes = new java.util.Hashtable (); 62 63 64 protected int _type_code_index = 1; 65 66 67 protected ModFactLogger _logger; 68 69 76 protected XMIExport(String dtd, String namespace, String model, String metamodel) { 77 super(); 78 corbaHelper = new CORBAProvider(); 79 _dtd = dtd; 80 _namespace = namespace; 81 _model = model; 82 _metamodel = metamodel; 83 } 84 85 89 public void setORB(org.omg.CORBA.ORB orb) { 90 _orb = orb; 91 } 92 93 98 public void parseName(String name, boolean printIOR) 99 throws 100 org.omg.CORBA.ORBPackage.InvalidName , 101 org.omg.CosNaming.NamingContextPackage.InvalidName , 102 org.omg.CosNaming.NamingContextPackage.NotFound , 103 org.omg.CosNaming.NamingContextPackage.CannotProceed , 104 org.omg.mof.Reflective.MofError { 105 org.omg.CORBA.Object objRef = _orb.resolve_initial_references("NameService"); 107 org.omg.CosNaming.NamingContextExt ncRef = org.omg.CosNaming.NamingContextExtHelper.narrow(objRef); 109 org.omg.CORBA.Object object = ncRef.resolve_str(name); 111 parse(object); 112 if (printIOR) 113 corbaHelper.printIOR(_orb, object, System.out); 114 } 115 116 121 public void parseIOR(String filename, boolean printIOR) throws org.omg.mof.Reflective.MofError { 122 org.omg.CORBA.Object object = corbaHelper.readIOR(_orb, filename); 123 parse(object); 124 if (printIOR) 125 corbaHelper.printIOR(_orb, object, System.out); 126 } 127 128 132 public void parse(org.omg.CORBA.Object root_package) throws org.omg.mof.Reflective.MofError { 133 outputln("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 134 if (_dtd.length() > 0) { 135 try { 136 java.net.URL url = new java.net.URL (_dtd); 137 outputln("<!DOCTYPE XMI SYSTEM \"" + url.toString() + "\">"); 138 } catch (java.net.MalformedURLException murle) { 139 murle.printStackTrace(); 140 } 141 } 142 if (_namespace.length() > 0) 143 outputln("<XMI verified=\"false\" xmi.version=\"1.1\" xmlns:Model=\"" + _namespace + "\">"); 144 else 145 outputln("<XMI verifier=\"false\" xmi.version=\"1.1\">"); 146 xmiHeaderTemplate(); 147 xmiContentTemplate(root_package); 148 outputln("</XMI>"); 149 flushFile(); 150 } 151 152 155 public void xmiHeaderTemplate() { 156 _logger.log(Level.INFO, "XMI Header generation ..."); 157 outputln("<XMI.header>"); 158 outputln("<XMI.model xmi.name=\"" + _model + "\"/>"); 159 outputln("<XMI.metamodel xmi.name=\"" + _metamodel + "\" xmi.version=\"1.3\"/>"); 160 outputln("<XMI.documentation>"); 161 outputln("<XMI.exporter>Generated by the ModFact XMI exporter</XMI.exporter>"); 162 outputln("</XMI.documentation>"); 163 outputln("</XMI.header>"); 164 } 165 166 170 public void xmiContentTemplate(org.omg.CORBA.Object _root_package) throws org.omg.mof.Reflective.MofError { 171 _logger.log(Level.INFO, "XMI Content generation ..."); 172 outputln("<XMI.content>"); 173 rootPackageTemplate(_root_package); 174 outputln("</XMI.content>"); 175 } 176 177 181 public abstract void rootPackageTemplate(org.omg.CORBA.Object _root_package) throws org.omg.mof.Reflective.MofError; 182 183 187 191 protected String getXMIId(org.omg.mof.Reflective.RefBaseObjectOperations object) { 192 String mof_id = object.ref_mof_id(); 193 if (_elements.containsKey(mof_id)) 194 return (String ) _elements.get(mof_id); 195 String key = "a" + _element_index; 196 _element_index++; 197 _elements.put(mof_id, key); 198 return key; 199 } 200 201 205 protected String getTypeCodeId(org.omg.CORBA.TypeCode type_code) { 206 if (_type_codes.containsKey(type_code)) 207 return (String ) _type_codes.get(type_code); 208 String key = "tc" + _type_code_index; 209 _type_code_index++; 210 _type_codes.put(type_code, key); 211 return key; 212 } 213 214 220 public String typeCode2XML(org.omg.CORBA.TypeCode typeCode) throws org.omg.CORBA.BAD_TYPECODE { 221 int tc_value = typeCode.kind().value(); 222 switch (tc_value) { 223 case org.omg.CORBA.TCKind._tk_boolean : 224 return "XMI.CorbaTcBoolean"; 225 case org.omg.CORBA.TCKind._tk_octet : 226 return "XMI.CorbaTcOctet"; 227 case org.omg.CORBA.TCKind._tk_char : 228 return "XMI.CorbaTcChar"; 229 case org.omg.CORBA.TCKind._tk_wchar : 230 return "XMI.CorbaTcWChar"; 231 case org.omg.CORBA.TCKind._tk_short : 232 return "XMI.CorbaTcShort"; 233 case org.omg.CORBA.TCKind._tk_ushort : 234 return "XMI.CorbaTcUshort"; 235 case org.omg.CORBA.TCKind._tk_long : 236 return "XMI.CorbaTcLong"; 237 case org.omg.CORBA.TCKind._tk_ulong : 238 return "XMI.CorbaTcUlong"; 239 case org.omg.CORBA.TCKind._tk_longlong : 240 return "XMI.CorbaTcLongLong"; 241 case org.omg.CORBA.TCKind._tk_double : 242 return "XMI.CorbaTcDouble"; 243 case org.omg.CORBA.TCKind._tk_float : 244 return "XMI.CorbaTcFloat"; 245 case org.omg.CORBA.TCKind._tk_string : 246 return "XMI.CorbaTcString"; 247 case org.omg.CORBA.TCKind._tk_TypeCode : 248 return "XMI.CorbaTcTypeCode"; 249 case org.omg.CORBA.TCKind._tk_Principal : 250 return "XMI.CorbaTcPrincipal"; 251 case org.omg.CORBA.TCKind._tk_any : 252 default : 253 return "XMI.CorbaTcAny"; 254 } 255 } 256 257 260 public void generate() throws Exception { 261 263 } 264 265 268 public void setInput(ModelElement[] elt) throws Exception { 269 271 } 272 273 276 public void setLogger(ModFactLogger log) throws Exception { 277 _logger = log; 278 279 } 280 281 } 282 | Popular Tags |