View Javadoc
1 /* 2 * $Header: /cvsroot/ebxmlrr/ebxmlrr/src/share/com/sun/ebxml/registry/util/BindingUtility.java,v 1.17 2003/06/20 14:57:23 farrukh_najmi Exp $ 3 * 4 * ==================================================================== 5 * 6 * This code is subject to the freebxml License, Version 1.1 7 * 8 * Copyright (c) 2003 freebxml.org. All rights reserved. 9 * 10 * ==================================================================== 11 */ 12 package com.sun.ebxml.registry.util; 13 14 import com.sun.ebxml.registry.RegistryException; 15 import java.io.StringReader; 16 import java.util.ArrayList; 17 import java.util.Iterator; 18 import org.oasis.ebxml.registry.bindings.rim.ObjectRef; 19 import org.oasis.ebxml.registry.bindings.rim.ObjectRefList; 20 import org.oasis.ebxml.registry.bindings.rim.RegistryObjectType; 21 import org.oasis.ebxml.registry.bindings.rs.SubmitObjectsRequest; 22 23 /*** 24 * Utilities used with castor generated XML Data Binding objects. 25 * 26 * @author Farrukh S. Najmi 27 * @version 1.2, 05/02/00 28 */ 29 public class BindingUtility { 30 31 /*** 32 * @link 33 * @shapeType PatternLink 34 * @pattern Singleton 35 * @supplierRole Singleton factory 36 */ 37 38 /* # private BindingUtility _utility; */ 39 private static BindingUtility instance = null; 40 41 /*** 42 * Class Constructor. Protected and only used by getInstance() 43 * 44 */ 45 protected BindingUtility() {} 46 47 /*** 48 * Get ArrayList of RegistryObject except ObjectRef of LeafRegistryObjectList 49 */ 50 public ArrayList getRegistryObjectList(org.oasis.ebxml.registry.bindings.rim.LeafRegistryObjectListType objs) throws RegistryException { 51 ArrayList al = new ArrayList(); 52 53 if (objs != null) { 54 for (java.util.Enumeration e = objs.enumerateLeafRegistryObjectListTypeItem() ; e.hasMoreElements() ;) { 55 org.oasis.ebxml.registry.bindings.rim.LeafRegistryObjectListTypeItem obj = (org.oasis.ebxml.registry.bindings.rim.LeafRegistryObjectListTypeItem)e.nextElement(); 56 57 Object ro = null; 58 59 60 if (obj.getAssociation() != null) { 61 ro = obj.getAssociation(); 62 } else if (obj.getAuditableEvent() != null) { 63 ro = obj.getAuditableEvent(); 64 } else if (obj.getClassification() != null) { 65 ro = obj.getClassification(); 66 } else if (obj.getClassificationNode() != null) { 67 ro = obj.getClassificationNode(); 68 } else if (obj.getClassificationScheme() != null) { 69 ro = obj.getClassificationScheme(); 70 } else if (obj.getExternalIdentifier() != null) { 71 ro = obj.getExternalIdentifier(); 72 } else if (obj.getExternalLink() != null) { 73 ro = obj.getExternalLink(); 74 } else if (obj.getExtrinsicObject() != null) { 75 ro = obj.getExtrinsicObject(); 76 } else if (obj.getObjectRef() != null) { 77 ro = obj.getObjectRef(); 78 } else if (obj.getOrganization() != null) { 79 ro = obj.getOrganization(); 80 } else if (obj.getRegistryPackage() != null) { 81 ro = obj.getRegistryPackage(); 82 } else if (obj.getService() != null) { 83 ro = obj.getService(); 84 } else if (obj.getServiceBinding() != null) { 85 ro = obj.getServiceBinding(); 86 } else if (obj.getSpecificationLink() != null) { 87 ro = obj.getSpecificationLink(); 88 } else if (obj.getUser() != null) { 89 ro = obj.getUser(); 90 } else { 91 throw new RegistryException("Null LeafRegistryObjectListTypeItem"); 92 } 93 94 95 if (!((ro instanceof ObjectRef) || (ro instanceof org.oasis.ebxml.registry.bindings.rim.ObjectRefType))) { 96 al.add(ro); 97 } 98 } 99 } 100 101 return al; 102 } 103 104 /*** 105 * Get the ArrayList of id of ObjectRef of LeafRegistryObjectList 106 * Note: finally no class uses it at the moment 107 */ 108 public ArrayList getObjectRefIds(org.oasis.ebxml.registry.bindings.rim.LeafRegistryObjectListType objs) throws RegistryException { 109 ArrayList objectRefIds = new ArrayList(); 110 if (objs != null) { 111 for (java.util.Enumeration e = objs.enumerateLeafRegistryObjectListTypeItem() ; e.hasMoreElements() ;) { 112 org.oasis.ebxml.registry.bindings.rim.LeafRegistryObjectListTypeItem obj = (org.oasis.ebxml.registry.bindings.rim.LeafRegistryObjectListTypeItem)e.nextElement(); 113 114 if (obj.getObjectRef() != null) { 115 ObjectRef objectRef = obj.getObjectRef(); 116 objectRefIds.add(objectRef.getId()); 117 } 118 } 119 } 120 return objectRefIds; 121 } 122 123 /*** 124 * Get the id from an object that could either an ObjectRef or RegistryObject 125 */ 126 public String getObjectId(Object obj) throws RegistryException { 127 String id = null; 128 129 if (obj != null) { 130 if (obj instanceof org.oasis.ebxml.registry.bindings.rim.ObjectRefType) { 131 id = ((ObjectRef)obj).getId(); 132 } 133 else if (obj instanceof RegistryObjectType) { 134 id = ((RegistryObjectType)obj).getId(); 135 } 136 else if (obj instanceof String) { 137 id = (String)obj; 138 } 139 else { 140 throw new RegistryException("Unexpected object of type " + obj.getClass() + 141 ". Expected String or ObjectRef or RegistryObjectType"); 142 } 143 } 144 145 return id; 146 } 147 148 /*** 149 * Set the id for an object that could either an ObjectRef or RegistryObject 150 */ 151 public void setObjectId(Object obj, String id) throws RegistryException { 152 153 if (obj != null) { 154 if (obj instanceof org.oasis.ebxml.registry.bindings.rim.ObjectRefType) { 155 ((ObjectRef)obj).setId(id); 156 } 157 else if (obj instanceof RegistryObjectType) { 158 ((RegistryObjectType)obj).setId(id); 159 } 160 else { 161 throw new RegistryException("Unexpected object of type " + obj.getClass() + 162 ". Expected Object or ObjectRef or RegistryObjectType"); 163 } 164 } 165 } 166 167 168 /*** 169 * Gets trhe root element for a registry request 170 * @return the root element as a String 171 */ 172 public String getRequestRootElement(java.io.InputStream request) throws RegistryException { 173 String rootElementName = null; 174 175 try { 176 javax.xml.parsers.DocumentBuilderFactory dbf = 177 javax.xml.parsers.DocumentBuilderFactory.newInstance(); 178 179 dbf.setNamespaceAware(true); 180 javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder(); 181 org.w3c.dom.Document doc = db.parse(request); 182 183 org.w3c.dom.Element root = doc.getDocumentElement(); 184 rootElementName = root.getLocalName(); 185 } 186 catch (java.io.IOException e) { 187 throw new RegistryException(e); 188 } 189 catch (javax.xml.parsers.ParserConfigurationException e) { 190 throw new RegistryException(e); 191 } 192 catch (org.xml.sax.SAXException e) { 193 throw new RegistryException(e); 194 } 195 196 return rootElementName; 197 } 198 199 /*** 200 * Gets the binding object representing the request from specufied XML file. 201 */ 202 public Object getRequestObject(java.io.File file) throws RegistryException { 203 Object obj = null; 204 try { 205 java.io.FileInputStream fis = new java.io.FileInputStream(file); 206 String rootElement = getRequestRootElement(fis); 207 fis.close(); 208 fis = new java.io.FileInputStream(file); 209 String requestStr = new String(Utility.getInstance().getStringBufferFromInputStream(fis)); 210 211 obj = getRequestObject(rootElement, requestStr); 212 } 213 catch (Exception e) { 214 throw new RegistryException(e); 215 } 216 return obj; 217 } 218 219 public Object getRequestObject(String rootElement, String message) throws RegistryException { 220 Object req = null; 221 222 try { 223 if (rootElement.equals("AdhocQueryRequest")) { 224 req = org.oasis.ebxml.registry.bindings.query.AdhocQueryRequest.unmarshal(new StringReader(message)); 225 } 226 else if (rootElement.equals("GetContentRequest")) { 227 req = org.oasis.ebxml.registry.bindings.query.GetContentRequest.unmarshal(new StringReader(message)); 228 } 229 else if (rootElement.equals("ApproveObjectsRequest")) { 230 req = org.oasis.ebxml.registry.bindings.rs.ApproveObjectsRequest.unmarshal(new StringReader(message)); 231 } 232 else if (rootElement.equals("DeprecateObjectsRequest")) { 233 req = org.oasis.ebxml.registry.bindings.rs.DeprecateObjectsRequest.unmarshal(new StringReader(message)); 234 } 235 else if (rootElement.equals("AddSlotsRequest")) { 236 req = org.oasis.ebxml.registry.bindings.rs.AddSlotsRequest.unmarshal(new StringReader(message)); 237 } 238 else if (rootElement.equals("RemoveObjectsRequest")) { 239 req = org.oasis.ebxml.registry.bindings.rs.RemoveObjectsRequest.unmarshal(new StringReader(message)); 240 } 241 else if (rootElement.equals("RemoveSlotsRequest")) { 242 req = org.oasis.ebxml.registry.bindings.rs.RemoveSlotsRequest.unmarshal(new StringReader(message)); 243 } 244 else if (rootElement.equals("SubmitObjectsRequest")) { 245 req = SubmitObjectsRequest.unmarshal(new StringReader(message)); 246 } 247 else if (rootElement.equals("UpdateObjectsRequest")) { 248 req = org.oasis.ebxml.registry.bindings.rs.UpdateObjectsRequest.unmarshal(new StringReader(message)); 249 } 250 else { 251 throw new RegistryException("InvalidRequest: Unknown element " + rootElement); 252 } 253 } 254 catch (org.exolab.castor.xml.MarshalException e) { 255 throw new RegistryException(e); 256 } 257 catch (org.exolab.castor.xml.ValidationException e) { 258 throw new RegistryException(e); 259 } 260 261 return req; 262 } 263 264 /*** 265 * Gets a String represnetation of a list ids from a Collection of RegistryObjects. 266 */ 267 public StringBuffer getIdListFromRegistryObjects(ArrayList objs) { 268 StringBuffer idList = new StringBuffer(); 269 270 Iterator iter = objs.iterator(); 271 while (iter.hasNext()) { 272 RegistryObjectType obj = (RegistryObjectType)iter.next(); 273 String id = obj.getId(); 274 idList.append("'"+id+"'"); 275 276 if (iter.hasNext()) { 277 idList.append(", "); 278 } 279 } 280 281 return idList; 282 } 283 284 /*** 285 * Get ArrayList of id of RegistryObjects 286 */ 287 public ArrayList getIdsFromRegistryObjects(ArrayList objs) { 288 ArrayList ids = new ArrayList(); 289 if (objs.size() > 0) { 290 Iterator iter = objs.iterator(); 291 while(iter.hasNext()) { 292 RegistryObjectType ro = (RegistryObjectType)iter.next(); 293 ids.add(ro.getId()); 294 } 295 } 296 return ids; 297 } 298 299 /*** 300 * Filter out those RegistryObjects whose id are in the ArrayList ids 301 */ 302 public ArrayList getRegistryObjectsFromIds(ArrayList objs, ArrayList ids) { 303 ArrayList ros = new ArrayList(); 304 if (ids.size() > 0 && objs.size() > 0) { 305 Iterator iter = objs.iterator(); 306 while(iter.hasNext()) { 307 RegistryObjectType ro = (RegistryObjectType)iter.next(); 308 if (ids.contains(ro.getId())) { 309 ros.add(ro); 310 } 311 } 312 } 313 return ros; 314 } 315 316 /*** 317 * Gets a String represnetation of a list ids from an ObjectRefList. 318 */ 319 public StringBuffer getIdListFromObjectRefList(ObjectRefList refList) { 320 StringBuffer idList = new StringBuffer(); 321 322 org.oasis.ebxml.registry.bindings.rim.ObjectRefListItem[] items = refList.getObjectRefListItem(); 323 int cnt = items.length; 324 for (int i=0; i<cnt; i++) { 325 ObjectRef ref = items[i].getObjectRef(); 326 String id = ref.getId(); 327 idList.append("'"+id+"'"); 328 329 if (i < cnt-1) { 330 idList.append(", "); 331 } 332 } 333 334 return idList; 335 } 336 337 /*** 338 * Get comma delimited list of quoted id from ArrayList of ids. 339 */ 340 public StringBuffer getIdListFromIds(ArrayList ids) { 341 StringBuffer idList = new StringBuffer(); 342 Iterator iter = ids.iterator(); 343 while(iter.hasNext()) { 344 String id = (String)iter.next(); 345 idList.append("'" + id + "'"); 346 if (iter.hasNext()) { 347 idList.append(","); 348 } 349 } 350 return idList; 351 } 352 353 /*** 354 * Get ArrayList of id of ObjectRef under ObjectRefList. 355 */ 356 public ArrayList getIdsFromObjectRefList(ObjectRefList objectRefList) { 357 ArrayList ids = new ArrayList(); 358 java.util.Enumeration enum = objectRefList.enumerateObjectRefListItem(); 359 while(enum.hasMoreElements()) { 360 ObjectRef ref = ((org.oasis.ebxml.registry.bindings.rim.ObjectRefListItem)enum.nextElement()).getObjectRef(); 361 ids.add(ref.getId()); 362 } 363 return ids; 364 } 365 366 /*** 367 * Get the first-level RegistryObject by id from SubmitObjectsRequest. 368 */ 369 public Object getObjectFromRequest(SubmitObjectsRequest registryRequest, String id) throws RegistryException { 370 Object result=null; 371 org.oasis.ebxml.registry.bindings.rim.LeafRegistryObjectList objList = ((SubmitObjectsRequest)registryRequest).getLeafRegistryObjectList(); 372 ArrayList objs = getRegistryObjectList(objList); 373 374 Iterator iter = objs.iterator(); 375 while (iter.hasNext()) { 376 Object obj = iter.next(); 377 String objId = getObjectId(obj); 378 if (id.equalsIgnoreCase(objId)) { 379 result = obj; 380 break; 381 } 382 } 383 384 return result; 385 } 386 387 /*** 388 * Get ArrayList of Id of first-level RegistryObject or ObjectRef in a request. For 389 * those kinds of request having RegistryObject and ObjectRef (e.g. SubmitObjectsRequest), 390 * only the id of RegistryObject elements are returned. 391 */ 392 public ArrayList getIdsFromRequest(Object registryRequest) throws RegistryException { 393 394 ArrayList ids = new ArrayList(); 395 396 if (registryRequest instanceof org.oasis.ebxml.registry.bindings.query.AdhocQueryRequest) { 397 } 398 else if (registryRequest instanceof org.oasis.ebxml.registry.bindings.query.GetContentRequest) { 399 } 400 else if (registryRequest instanceof org.oasis.ebxml.registry.bindings.rs.ApproveObjectsRequest) { 401 ObjectRefList refList = ((org.oasis.ebxml.registry.bindings.rs.ApproveObjectsRequest)registryRequest).getObjectRefList(); 402 ids.addAll(getIdsFromObjectRefList(refList)); 403 } 404 else if (registryRequest instanceof org.oasis.ebxml.registry.bindings.rs.DeprecateObjectsRequest) { 405 ObjectRefList refList = ((org.oasis.ebxml.registry.bindings.rs.DeprecateObjectsRequest)registryRequest).getObjectRefList(); 406 ids.addAll(getIdsFromObjectRefList(refList)); 407 } 408 else if (registryRequest instanceof org.oasis.ebxml.registry.bindings.rs.RemoveObjectsRequest) { 409 ObjectRefList refList = ((org.oasis.ebxml.registry.bindings.rs.RemoveObjectsRequest)registryRequest).getObjectRefList(); 410 ids.addAll(getIdsFromObjectRefList(refList)); 411 } 412 else if (registryRequest instanceof SubmitObjectsRequest) { 413 org.oasis.ebxml.registry.bindings.rim.LeafRegistryObjectList objList = ((SubmitObjectsRequest)registryRequest).getLeafRegistryObjectList(); 414 ArrayList objs = getRegistryObjectList(objList); 415 ids.addAll(getIdsFromRegistryObjects(objs)); 416 } 417 else if (registryRequest instanceof org.oasis.ebxml.registry.bindings.rs.UpdateObjectsRequest) { 418 org.oasis.ebxml.registry.bindings.rim.LeafRegistryObjectList objList = ((org.oasis.ebxml.registry.bindings.rs.UpdateObjectsRequest)registryRequest).getLeafRegistryObjectList(); 419 ArrayList objs = getRegistryObjectList(objList); 420 ids.addAll(getIdsFromRegistryObjects(objs)); 421 } 422 else if (registryRequest instanceof org.oasis.ebxml.registry.bindings.rs.AddSlotsRequest) { 423 ObjectRef ref = ((org.oasis.ebxml.registry.bindings.rs.AddSlotsRequest)registryRequest).getObjectRef(); 424 ids.add(ref.getId()); 425 } 426 else if (registryRequest instanceof org.oasis.ebxml.registry.bindings.rs.RemoveSlotsRequest) { 427 ObjectRef ref = ((org.oasis.ebxml.registry.bindings.rs.RemoveSlotsRequest)registryRequest).getObjectRef(); 428 ids.add(ref.getId()); 429 } 430 else { 431 throw new RegistryException("InvalidRequest: Unknown request " + registryRequest.getClass().getName()); 432 } 433 434 return ids; 435 } 436 437 /*** 438 * Gets the singleton instance as defined by Singleton pattern. 439 * 440 * @return the singleton instance 441 * 442 */ 443 public static BindingUtility getInstance() { 444 if (instance == null) { 445 synchronized (BindingUtility.class) { 446 if (instance == null) { 447 instance = new BindingUtility(); 448 } 449 } 450 } 451 return instance; 452 } 453 454 455 }

This page was automatically generated by Maven