1 17 package org.alfresco.repo.dictionary; 18 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.Date ; 24 import java.util.List ; 25 26 import org.alfresco.service.cmr.dictionary.DictionaryException; 27 import org.jibx.runtime.BindingDirectory; 28 import org.jibx.runtime.IBindingFactory; 29 import org.jibx.runtime.IMarshallingContext; 30 import org.jibx.runtime.IUnmarshallingContext; 31 import org.jibx.runtime.JiBXException; 32 33 34 40 public class M2Model 41 { 42 private String name = null; 43 private String description = null; 44 private String author = null; 45 private Date published = null; 46 private String version; 47 48 private List <M2Namespace> namespaces = new ArrayList <M2Namespace>(); 49 private List <M2Namespace> imports = new ArrayList <M2Namespace>(); 50 private List <M2DataType> dataTypes = new ArrayList <M2DataType>(); 51 private List <M2Type> types = new ArrayList <M2Type>(); 52 private List <M2Aspect> aspects = new ArrayList <M2Aspect>(); 53 54 55 private M2Model() 56 { 57 } 58 59 60 66 public static M2Model createModel(String name) 67 { 68 M2Model model = new M2Model(); 69 model.name = name; 70 return model; 71 } 72 73 74 80 public static M2Model createModel(InputStream xml) 81 { 82 try 83 { 84 IBindingFactory factory = BindingDirectory.getFactory(M2Model.class); 85 IUnmarshallingContext context = factory.createUnmarshallingContext(); 86 Object obj = context.unmarshalDocument(xml, null); 87 return (M2Model)obj; 88 } 89 catch(JiBXException e) 90 { 91 throw new DictionaryException("Failed to parse model", e); 92 } 93 } 94 95 96 101 public void toXML(OutputStream xml) 102 { 103 try 104 { 105 IBindingFactory factory = BindingDirectory.getFactory(M2Model.class); 106 IMarshallingContext context = factory.createMarshallingContext(); 107 context.setIndent(4); 108 context.marshalDocument(this, "UTF-8", null, xml); 109 } 110 catch(JiBXException e) 111 { 112 throw new DictionaryException("Failed to create M2 Model", e); 113 } 114 } 115 116 117 124 CompiledModel compile(DictionaryDAO dictionaryDAO, NamespaceDAO namespaceDAO) 125 { 126 CompiledModel compiledModel = new CompiledModel(this, dictionaryDAO, namespaceDAO); 127 return compiledModel; 128 } 129 130 131 public String getName() 132 { 133 return name; 134 } 135 136 137 public void setName(String name) 138 { 139 this.name = name; 140 } 141 142 143 public String getDescription() 144 { 145 return description; 146 } 147 148 149 public void setDescription(String description) 150 { 151 this.description = description; 152 } 153 154 155 public String getAuthor() 156 { 157 return author; 158 } 159 160 161 public void setAuthor(String author) 162 { 163 this.author = author; 164 } 165 166 167 public Date getPublishedDate() 168 { 169 return published; 170 } 171 172 173 public void setPublishedDate(Date published) 174 { 175 this.published = published; 176 } 177 178 179 public String getVersion() 180 { 181 return version; 182 } 183 184 185 public void setVersion(String version) 186 { 187 this.version = version; 188 } 189 190 191 public M2Type createType(String name) 192 { 193 M2Type type = new M2Type(); 194 type.setName(name); 195 types.add(type); 196 return type; 197 } 198 199 200 public void removeType(String name) 201 { 202 M2Type type = getType(name); 203 if (type != null) 204 { 205 types.remove(types); 206 } 207 } 208 209 210 public List <M2Type> getTypes() 211 { 212 return Collections.unmodifiableList(types); 213 } 214 215 216 public M2Type getType(String name) 217 { 218 for (M2Type candidate : types) 219 { 220 if (candidate.getName().equals(name)) 221 { 222 return candidate; 223 } 224 } 225 return null; 226 } 227 228 229 public M2Aspect createAspect(String name) 230 { 231 M2Aspect aspect = new M2Aspect(); 232 aspect.setName(name); 233 aspects.add(aspect); 234 return aspect; 235 } 236 237 238 public void removeAspect(String name) 239 { 240 M2Aspect aspect = getAspect(name); 241 if (aspect != null) 242 { 243 aspects.remove(name); 244 } 245 } 246 247 248 public List <M2Aspect> getAspects() 249 { 250 return Collections.unmodifiableList(aspects); 251 } 252 253 254 public M2Aspect getAspect(String name) 255 { 256 for (M2Aspect candidate : aspects) 257 { 258 if (candidate.getName().equals(name)) 259 { 260 return candidate; 261 } 262 } 263 return null; 264 } 265 266 267 public M2DataType createPropertyType(String name) 268 { 269 M2DataType type = new M2DataType(); 270 type .setName(name); 271 dataTypes.add(type); 272 return type; 273 } 274 275 276 public void removePropertyType(String name) 277 { 278 M2DataType type = getPropertyType(name); 279 if (type != null) 280 { 281 dataTypes.remove(name); 282 } 283 } 284 285 286 public List <M2DataType> getPropertyTypes() 287 { 288 return Collections.unmodifiableList(dataTypes); 289 } 290 291 292 public M2DataType getPropertyType(String name) 293 { 294 for (M2DataType candidate : dataTypes) 295 { 296 if (candidate.getName().equals(name)) 297 { 298 return candidate; 299 } 300 } 301 return null; 302 } 303 304 305 public M2Namespace createNamespace(String uri, String prefix) 306 { 307 M2Namespace namespace = new M2Namespace(); 308 namespace.setUri(uri); 309 namespace.setPrefix(prefix); 310 namespaces.add(namespace); 311 return namespace; 312 } 313 314 315 public void removeNamespace(String uri) 316 { 317 M2Namespace namespace = getNamespace(uri); 318 if (namespace != null) 319 { 320 namespaces.remove(namespace); 321 } 322 } 323 324 325 public List <M2Namespace> getNamespaces() 326 { 327 return Collections.unmodifiableList(namespaces); 328 } 329 330 331 public M2Namespace getNamespace(String uri) 332 { 333 for (M2Namespace candidate : namespaces) 334 { 335 if (candidate.getUri().equals(uri)) 336 { 337 return candidate; 338 } 339 } 340 return null; 341 } 342 343 344 public M2Namespace createImport(String uri, String prefix) 345 { 346 M2Namespace namespace = new M2Namespace(); 347 namespace.setUri(uri); 348 namespace.setPrefix(prefix); 349 imports.add(namespace); 350 return namespace; 351 } 352 353 354 public void removeImport(String uri) 355 { 356 M2Namespace namespace = getImport(uri); 357 if (namespace != null) 358 { 359 imports.remove(namespace); 360 } 361 } 362 363 364 public List <M2Namespace> getImports() 365 { 366 return Collections.unmodifiableList(imports); 367 } 368 369 370 public M2Namespace getImport(String uri) 371 { 372 for (M2Namespace candidate : imports) 373 { 374 if (candidate.getUri().equals(uri)) 375 { 376 return candidate; 377 } 378 } 379 return null; 380 } 381 382 383 private static List createList() 385 { 386 return new ArrayList (); 387 } 388 389 } 390 | Popular Tags |