private ClassLoaderStrategy getClassLoaderStrategy(String fullyQualifiedClassName) throws Exception { try { // Get just the package name StringBuffer sb = new StringBuffer(fullyQualifiedClassName); sb.delete(sb.lastIndexOf("."), sb.length()); currentPackage = sb.toString(); // Retrieve the Java classpath from the system properties String cp = System.getProperty("java.class.path"); String sepChar = System.getProperty("path.separator"); String[] paths = StringUtils.pieceList(cp, sepChar.charAt(0)); ClassLoaderStrategy cl = ClassLoaderUtil.getClassLoader(ClassLoaderUtil.FILE_SYSTEM_CLASS_LOADER, new String[] {}); // Iterate through paths until class with the specified name is found String classpath = StringUtils.replaceChar(currentPackage, '.', File.separatorChar); for (int i = 0; i < paths.length; i++) { Class[] classes = cl.getClasses(paths[i] + File.separatorChar + classpath, currentPackage); for (int j = 0; j < classes.length; j++) { if (classes[j].getName().equals(fullyQualifiedClassName)) { return ClassLoaderUtil.getClassLoader( ClassLoaderUtil.FILE_SYSTEM_CLASS_LOADER, new String[] {paths[i]}); } } } throw new Exception("Class could not be found."); } catch (Exception e) { System.err.println("Exception creating class loader strategy."); System.err.println(e.getMessage()); throw e; } }
// Serialize the bean using the specified namespace prefix & uri public String serialize(Object bean) throws IntrospectionException, IllegalAccessException { // Use the class name as the name of the root element String className = bean.getClass().getName(); String rootElementName = null; if (bean.getClass().isAnnotationPresent(ObjectXmlAlias.class)) { AnnotatedElement annotatedElement = bean.getClass(); ObjectXmlAlias aliasAnnotation = annotatedElement.getAnnotation(ObjectXmlAlias.class); rootElementName = aliasAnnotation.value(); } // Use the package name as the namespace URI Package pkg = bean.getClass().getPackage(); nsURI = pkg.getName(); // Remove a trailing semi-colon (;) if present (i.e. if the bean is an array) className = StringUtils.deleteTrailingChar(className, ';'); StringBuffer sb = new StringBuffer(className); String objectName = sb.delete(0, sb.lastIndexOf(".") + 1).toString(); domDocument = createDomDocument(objectName); document = domDocument.getDocument(); Element root = document.getDocumentElement(); // Parse the bean elements getBeanElements(root, rootElementName, className, bean); StringBuffer xml = new StringBuffer(); if (prettyPrint) xml.append(domDocument.serialize(lineSeperator, indentChars, includeXmlProlog)); else xml.append(domDocument.serialize(includeXmlProlog)); if (!includeTypeInfo) { int index = xml.indexOf(root.getNodeName()); xml.delete(index - 1, index + root.getNodeName().length() + 2); xml.delete(xml.length() - root.getNodeName().length() - 4, xml.length()); } return xml.toString(); }
// RPC API public AllergiesReactants getAllergiesReactants(String dfn) throws Exception { // setDefaultContext("OR CPRS GUI CHART"); setDefaultContext("VPS AVS INTERFACE"); // setDefaultRpcName("ORQQAL LIST"); setDefaultRpcName("VPS AL LIST"); List<String> list = lCall(dfn); /* Returns array of patient allergies. Returned data is delimited by "^" and includes: allergen/reactant, reactions/symptoms (multiple symptoms/ reactions are possible - delimited by ";"), severity, allergy id (record number from the Patient Allergies file (#120.8). */ AllergiesReactants allergiesReactants = new AllergiesReactants(); if (list.size() == 1) { String a = StringUtils.piece((String) list.get(0), 2); if (a.equalsIgnoreCase("no known allergies")) { allergiesReactants.setNoKnownAllergies(true); list.clear(); } else if (a.equalsIgnoreCase("no allergy assessment")) { allergiesReactants.setNoAllergyAssessment(true); list.clear(); } } List<AllergyReactant> allergiesReactantsList = new ArrayList<AllergyReactant>(); for (String s : list) { AllergyReactant allergyReactant = new AllergyReactant(); allergyReactant.setDfn(dfn); allergyReactant.setIen(StringUtils.piece(s, 1)); allergyReactant.setAllergenReactant(StringUtils.piece(s, 2)); allergyReactant.setSeverity(StringUtils.piece(s, 3)); String[] x = StringUtils.pieceList(StringUtils.piece(s, 4), ';'); allergyReactant.setReactionsSymptoms(x); allergiesReactantsList.add(allergyReactant); } allergiesReactants.setAllergiesReactants(allergiesReactantsList); return allergiesReactants; }
private Object createObject( Node node, String name, String classPackage, String type, String value, boolean setProperty) { Bean parentBean = null; if (beansStack.size() > 0) parentBean = (Bean) beansStack.peek(); Object object = null; // param is either an XSD type or a bean, // check if it can be converted to an XSD type XSDatatype dt = null; try { dt = DatatypeFactory.getTypeByName(type); } catch (DatatypeException dte) { // the type is not a valid XSD data type } // convert null value to default if ((dt != null) && (value == null)) { Class objType = dt.getJavaObjectType(); if (objType == String.class) value = ""; else if ((objType == java.math.BigInteger.class) || (objType == Long.class) || (objType == Integer.class) || (objType == Short.class) || (objType == Byte.class)) value = "0"; else if ((objType == java.math.BigDecimal.class) || (objType == Double.class) || (objType == Float.class)) value = "0.0"; else if (objType == Boolean.class) value = "false"; else if (objType == java.util.Date.class) value = DateUtils.getCurrentDate(); else if (objType == java.util.Calendar.class) value = DateUtils.getCurrentDateTime(); } // check whether the type was converted to an XSD datatype if ((dt != null) && dt.isValid(value, null)) { // create and return an XSD Java object (e.g. String, Integer, Boolean, etc) object = dt.createJavaObject(value, null); if (object instanceof java.util.Calendar) { // check that the object is truly a Calendar // because DatatypeFactory converts xsd:date // types to GregorianCalendar instead of Date. if (type.equals("date")) { java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd"); try { object = df.parse(value); } catch (java.text.ParseException pe) { object = new java.util.Date(); } } } } else { // Create a bean object if (topLevelBean == null) topLevelBean = parentBean; object = pushBeanOnStack(classPackage, type); // Check fields to see if this property is the 'value' for the object Field[] fields = object.getClass().getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (fields[i].isAnnotationPresent(ObjectXmlAsValue.class)) { try { StringBuffer fieldName = new StringBuffer(fields[i].getName()); char c = fieldName.charAt(0); if (c >= 'a' && c <= 'z') { c += 'A' - 'a'; } fieldName.setCharAt(0, c); fieldName.insert(0, "set"); Method method = object .getClass() .getMethod(fieldName.toString(), new Class[] {fields[i].getType()}); method.invoke(object, value); break; } catch (Exception e) { System.err.println(e.getMessage()); } } } NamedNodeMap nodeAttrs = node.getAttributes(); // iterate attributes and set field values for property attributes for (int i = 0; i < nodeAttrs.getLength(); i++) { String nodePrefix = nodeAttrs.item(i).getPrefix(); if (nodePrefix.equals(nsPrefix)) { String nodeName = nodeAttrs.item(i).getLocalName(); String nodeValue = nodeAttrs.item(i).getNodeValue(); try { Field field = object.getClass().getDeclaredField(nodeName); if (field.isAnnotationPresent(ObjectXmlAsAttribute.class)) { StringBuffer fieldName = new StringBuffer(field.getName()); char c = fieldName.charAt(0); if (c >= 'a' && c <= 'z') { c += 'A' - 'a'; } fieldName.setCharAt(0, c); fieldName.insert(0, "set"); Method method = object.getClass().getMethod(fieldName.toString(), new Class[] {field.getType()}); if (field.getType() == String.class) method.invoke(object, nodeValue); else if (field.getType() == Boolean.TYPE) method.invoke(object, StringUtils.strToBool(nodeValue, "true")); else if (field.getType() == Byte.TYPE) method.invoke(object, Byte.valueOf(nodeValue).byteValue()); else if (field.getType() == Character.TYPE) method.invoke(object, Character.valueOf(nodeValue.charAt(0))); else if (field.getType() == Double.TYPE) method.invoke(object, Double.valueOf(nodeValue).doubleValue()); else if (field.getType() == Float.TYPE) method.invoke(object, Float.valueOf(nodeValue).floatValue()); else if (field.getType() == Integer.TYPE) method.invoke(object, Integer.valueOf(nodeValue).intValue()); else if (field.getType() == Long.TYPE) method.invoke(object, Long.valueOf(nodeValue).longValue()); else if (field.getType() == Short.TYPE) method.invoke(object, Short.valueOf(nodeValue).shortValue()); } } catch (Exception e) { System.err.println(e.getMessage()); } } } } if ((parentBean != null) && (setProperty)) { parentBean.setProperty(name, object); } return object; }