/** * Binds data from reference model instance to XML binding classes * * @param obj * @return * @throws XMLBindingException */ public Object bindToXML(Object obj) throws XMLBindingException { if (obj == null) { return null; } String className = obj.getClass().getSimpleName(); Method[] methods = obj.getClass().getMethods(); try { Class xmlClass = Class.forName(XML_BINDING_PACKAGE + className.toUpperCase()); // debug code only if (xmlClass.getClasses().length != 1) { log.debug("XMLBinding: bindToXML(): xmlClass.getClass()=" + xmlClass.getClass()); log.debug("XMLBinding: bindToXML(): xmlClass.toString()=" + xmlClass.toString()); for (Class clazz : xmlClass.getClasses()) { log.debug("\t clazz.getClass()=" + clazz.getClass()); log.debug("\t clazz.toString()=" + clazz.toString()); } } Class factoryClass = xmlClass.getClasses()[0]; // ES modification: Add xmlOptions containing openehr (default) and xsi namespaces // Method factoryMethod = factoryClass.getMethod(NEW_INSTANCE, null); // Changed to pick the method with an XmlOptions parameter instead Method factoryMethod = factoryClass.getMethod(NEW_INSTANCE, XmlOptions.class); // First prameter null because it's a static method, see: // http://java.sun.com/docs/books/tutorial/reflect/member/methodInvocation.html // Second parameter should be the parameter of XmlObject.Factory.newInstance(XmlOptions // options) // see: // http://xmlbeans.apache.org/docs/2.2.0/reference/org/apache/xmlbeans/XmlObject.Factory.html // // Previous code was: Object xmlObj = factoryMethod.invoke(null, null); Object xmlObj = factoryMethod.invoke(null, xopt); Map<String, Class> attributes = builder.retrieveAttribute(className); Set<String> attributeNames = attributes.keySet(); Object attributeValue = null; Method setterMethod = null; for (Method method : methods) { String name = method.getName(); // cause dead-loop if ("getParent".equals(name)) { continue; // } if (isGetter(name, attributeNames)) { log.debug("getter: " + name); if (method.getParameterTypes().length > 0) { continue; } attributeValue = method.invoke(obj, null); if (attributeValue == null) { continue; } log.debug("value.class: " + attributeValue.getClass()); boolean isList = false; if (attributeValue.getClass().isArray()) { Object[] array = (Object[]) attributeValue; if (array.length == 0) { continue; } Object[] done = new Object[array.length]; for (int i = 0; i < array.length; i++) { done[i] = bindToXML(array[i]); } attributeValue = done; } else if (ProportionKind.class.equals(attributeValue.getClass())) { ProportionKind kind = (ProportionKind) attributeValue; attributeValue = BigInteger.valueOf(kind.getValue()); } else if (isOpenEHRRMClass(attributeValue)) { attributeValue = bindToXML(attributeValue); } else if (List.class.isAssignableFrom(attributeValue.getClass())) { isList = true; List list = (List) attributeValue; log.debug("list.size: " + list.size()); String attributeName = getAttributeNameFromGetter(name); setterMethod = findSetter(attributeName, xmlClass, isList); Method addNew = findAddNew(attributeName, xmlClass); log.debug("setter: " + setterMethod.getName() + ", xmlClass: " + xmlClass); for (int i = 0, j = list.size() - 1; i <= j; i++) { Object value = list.get(i); Object[] array = new Object[2]; addNew.invoke(xmlObj, null); array[0] = new Integer(i); array[1] = bindToXML(value); setterMethod.invoke(xmlObj, array); log.debug("list.member value set!!!!"); } } if (!isList) { String attributeName = getAttributeNameFromGetter(name); log.debug( "attribute: " + attributeName + ", value(" + attributeValue + "), " + "type: " + attributeValue.getClass()); // TODO fix for mismatched attribute name in XSD and RM if ("nullFlavor".equals(attributeName)) { attributeName = "nullFlavour"; } // skip function according to specs if ("isMerged".equals(attributeName)) { continue; } setterMethod = findSetter(attributeName, xmlClass, isList); if (setterMethod == null) { log.error( "failed to find setterMethod for attribute: " + attributeName + " with type: " + xmlClass); continue; } // special handling deals with 'real' typed // attributes in specs but typed 'float' in xsd String setter = setterMethod.getName(); if ("setAccuracy".equals(setter) || "setDenominator".equals(setter) || "setNumerator".equals(setter)) { Double d = (Double) attributeValue; attributeValue = d.floatValue(); } log.debug( "setter: " + setterMethod.getName() + ", xmlClass: " + xmlClass + ", attributeValue: " + attributeValue + ", attributeValue.class: " + attributeValue.getClass()); setterMethod.invoke(xmlObj, attributeValue); } } } return xmlObj; } catch (Exception e) { e.printStackTrace(); throw new XMLBindingException( "exception caught when bind obj to " + className + ", " + e.getMessage()); } }