map a row from the ResultSet to an XmlObject instance

@return An XmlObject instance.
553914
public Object mapRowToReturnType() {

        Object resultObject = null;
        if (_columnCount == 1) {

            final int typeId = _tmf.getTypeId(_returnTypeClass);

            try {
                if (typeId != TypeMappingsFactory.TYPE_UNKNOWN) {
                    return extractColumnValue(1, typeId);
                } else {
                    // we still might want a single value (i.e. java.util.Date)
                    Object val = extractColumnValue(1, typeId);
                    if (_returnTypeClass.isAssignableFrom(val.getClass())) {
                        return val;
                    }
                }
            } catch (SQLException e) {
                throw new ControlException(e.getMessage(), e);
            }
        }

        if (_setterMethods == null) {
            try {
                getResultSetMappings();
            } catch (SQLException e) {
                throw new ControlException(e.getMessage(), e);
            }
        }

        resultObject = XmlObject.Factory.newInstance(new XmlOptions().setDocumentType(_schemaType));

        for (int i = 1; i < _setterMethods.length; i++) {
            Method setterMethod = _setterMethods[i].getSetter();
            Object resultValue = null;

            try {
                resultValue = extractColumnValue(i, _setterMethods[i].getParameterType());

                // if the setter is for an xmlbean enum type, convert the extracted resultset column
                // value to the proper xmlbean enum type. All xmlbean enums are derived from the class
                // StringEnumAbstractBase
                if (_setterMethods[i].getParameterType() == TypeMappingsFactory.TYPE_XMLBEAN_ENUM) {
                    Class parameterClass = _setterMethods[i].getParameterClass();
                    Method m = parameterClass.getMethod("forString", new Class[]{String.class});
                    resultValue = m.invoke(null, new Object[]{resultValue});
                }

                _args[0] = resultValue;
                setterMethod.invoke(resultObject, _args);

                if (_setterMethods[i].getNilable() != null) {
                    if (_resultSet.wasNull()) {
                        _setterMethods[i].getNilable().invoke(resultObject, (Object[]) null);
                    }
                }
            } catch (SQLException se) {
                throw new ControlException(se.getMessage(), se);
            } catch (IllegalArgumentException iae) {
                try {
                    ResultSetMetaData md = _resultSet.getMetaData();
                    throw new ControlException("The declared Java type for method " + setterMethod.getName()
                                               + setterMethod.getParameterTypes()[0].toString()
                                               + " is incompatible with the SQL format of column " + md.getColumnName(i).toString()
                                               + md.getColumnTypeName(i).toString()
                                               + " which returns objects of type " + resultValue.getClass().getName());
                } catch (SQLException e) {
                    throw new ControlException(e.getMessage(), e);
                }
            } catch (IllegalAccessException e) {
                throw new ControlException("IllegalAccessException when trying to access method " + setterMethod.getName(), e);
            } catch (NoSuchMethodException e) {
                throw new ControlException("NoSuchMethodException when trying to map schema enum value using Enum.forString().", e);
            } catch (InvocationTargetException e) {
                throw new ControlException("IllegalInvocationException when trying to access method " + setterMethod.getName(), e);
            }
        }
        return resultObject;
    }
22417
public static void appendXMLToTable(final TableRow row, final XMLUtil xmlUtil,
                                        final Appendable appendable) throws IOException {
        if (row == null) appendable.append("<row />");
        else row.appendXMLToTable(xmlUtil, appendable);
    }
16703
private <T> T xmlToObject(String xml, Class<T> type) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(type);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        T obj = (T) jaxbUnmarshaller.unmarshal(new StringReader(xml));
        return obj;
    }
875913
public Object next() {
        try {
            if (!_primed) {
                _primed = _rs.next();
                if (!_primed) {
                    throw new NoSuchElementException();
                }
            }
            // reset upon consumption
            _primed = false;
            return _rowMapper.mapRowToReturnType();
        } catch (SQLException e) {
            // Since Iterator interface is locked, all we can do
            // is put the real exception inside an expected one.
            NoSuchElementException xNoSuch = new NoSuchElementException("ResultSet exception: " + e);
            xNoSuch.initCause(e);
            throw xNoSuch;
        }
    }
844749
public static XmlUtil getInstance() {

    if (instance == null) {
      synchronized (XmlUtilImpl.class) {
        if (instance == null) {
          XmlUtilImpl util = new XmlUtilImpl();
          util.initialize();
          instance = util;
        }
      }
    }
    return instance;
  }
959083
private boolean getRow() {
    try {
      rowCache.clear();
      while(rowCache.size() < rowCacheSize && parser.hasNext()) {
        handleEvent(parser.nextEvent());
      }
      rowCacheIterator = rowCache.iterator();
      return rowCacheIterator.hasNext();
    } catch(XMLStreamException e) {
      throw new ParseException("Error reading XML stream", e);
    }
  }
1176431
public Object sqlToJava(Object source)
    {
        if(source == null)
            return null;
        try
        {
            return SerializationUtils.deserialize((byte[]) source);
        }
        catch(Throwable t)
        {
            throw new ConversionException(t);
        }
    }
110549
private void getResultSetMappings() throws SQLException {

        //
        // special case for XmlObject, find factory class
        //
        if (_schemaType.isDocumentType()) {
            return;
        }

        final String[] keys = getKeysFromResultSet();

        //
        // find setters for return class
        //
        HashMap<String, Method> mapFields = new HashMap<String, Method>(_columnCount * 2);
        for (int i = 1; i <= _columnCount; i++) {
            mapFields.put(keys[i], null);
        }

        // public methods
        Method[] classMethods = _returnTypeClass.getMethods();
        for (Method method : classMethods) {

            if (isSetterMethod(method)) {
                final String fieldName = method.getName().substring(3).toUpperCase();
                if (mapFields.containsKey(fieldName)) {
                    mapFields.put(fieldName, method);
                }
            }
        }

        // finally actually init the fields array
        _setterMethods = new SetterMethod[_columnCount + 1];

        for (int i = 1; i < _setterMethods.length; i++) {
            Method setterMethod = mapFields.get(keys[i]);
            if (setterMethod == null) {
                throw new ControlException("Unable to map the SQL column " + keys[i]
                                           + " to a field on the " + _returnTypeClass.getName() +
                                           " class. Mapping is done using a case insensitive comparision of SQL ResultSet "
                                           + "columns to public setter methods on the return class.");
            }

            _setterMethods[i] = new SetterMethod(setterMethod);
        }
    }
79034
public static Row copy(Row row) {
		final Row newRow = new Row(row.fields.length);
		System.arraycopy(row.fields, 0, newRow.fields, 0, row.fields.length);
		return newRow;
	}
288321
private Object _deserializeQuery(Element recordset) throws ConverterException {
	try {
	    // create Query Object
	    Query query = new QueryImpl(lucee.runtime.type.util.ListUtil.listToArray(recordset.getAttribute("fieldNames"), ','),
		    Caster.toIntValue(recordset.getAttribute("rowCount")), "query");

	    NodeList list = recordset.getChildNodes();
	    int len = list.getLength();
	    for (int i = 0; i < len; i++) {
		Node node = list.item(i);
		if (node instanceof Element) {
		    _deserializeQueryField(query, (Element) node);
		}
	    }
	    return query;
	}
	catch (PageException e) {
	    throw toConverterException(e);
	}

    }
1274703
public static Object convertSqlXml(Object sqlXml, String value) throws SQLException {
        return convertSqlXml(sqlXml, value.getBytes());
    }
1127365
public static Document getResultSetAsXML(ResultSet rs, String tableName) throws Exception {
        return getResultSetAsXML(rs, tableName, "");
    }
776070
public Object javaToSql(Object source)
    {
        if (source == null)
            return null;
        try
        {
            return SerializationUtils.serialize((Serializable) source);
        }
        catch(Throwable t)
        {
            throw new ConversionException(t);
        }
    }
483778
public Object object()
  {
    if (m_DTMXRTreeFrag.getXPathContext() != null)
      return new org.apache.xml.dtm.ref.DTMNodeIterator((DTMIterator)(new org.apache.xpath.NodeSetDTM(m_dtmRoot, m_DTMXRTreeFrag.getXPathContext().getDTMManager())));
    else
      return super.object();
  }
1028781
function XML2Object(node, mapping){  // require for refactoring
    var nodeType = node.nodeType;
    var attributes = node.attributes;
    var firstChild = node.firstChild;

    if (!firstChild)
    {
      var firstAttr = attributes && attributes[0];
      if (nodeType == ELEMENT_NODE)
      {
        if (!firstAttr)
          return '';

        // test for <node xsi:nil="true"/>
        if (attributes.length == 1 &&
            (firstAttr.baseName || firstAttr.localName) == XSI_NIL_LOCALPART &&
            firstAttr.namespaceURI == XSI_NAMESPACE)
          return null;
      }
      else
      {
        if (!firstAttr)
          return null;
      }
    }
    else
    {
      // single child node and not an element -> return child nodeValue
      if (firstChild.nodeType != ELEMENT_NODE && firstChild === node.lastChild)
        return firstChild.nodeValue;
      else
        if (firstChild !== node.lastChild && firstChild.nodeType == TEXT_NODE)
        {
          var isSeparatedTextNode = true;
          var result = '';
          var cursor = firstChild;
          do
          {
            if (cursor.nodeType !== TEXT_NODE)
            {
              isSeparatedTextNode = false;
              break;
            }
            result += cursor.nodeValue;
          }
          while (cursor = cursor.nextSibling);

          if (isSeparatedTextNode)
            return result;
        }
    }

    var result = {};
    var nodes = [];
    var childNodesCount = 0;
    var value;
    var cursor;
    var object;
    var name;
    var isElement;
    var map;

    if (cursor = firstChild)
    {
      do
      {
        childNodesCount = nodes.push(cursor);
      }
      while (cursor = cursor.nextSibling);
    }

    if (attributes)
      for (var i = 0, attr; attr = attributes[i]; i++)
        nodes.push(attr);

    if (!mapping)
      mapping = {};

    for (var i = 0, child; child = nodes[i]; i++)
    {
      name = child.nodeName;
      isElement = i < childNodesCount;
      map = mapping[name];

      // fetch value
      if (isElement)
      {
        value = XML2Object(child, mapping);
      }
      else
      {
        if (name == 'xmlns')
          continue;

        value = child.nodeValue;
      }

      // mapping keys
      while (map)
      {
        if (map.storeName)
          value[map.storeName] = name;

        if (map.rename)
        {
          name = map.rename;
          map = mapping[name];
        }
        else
        {
          if (map.format)
            value = map.format(value);

          if (!result[name] && map.forceArray)
            value = [value];

          break;
        }
      }

      // store result
      if (name in result)
      {
        if ((object = result[name]) && object.push)
          object.push(value);
        else
          result[name] = [object, value];
      }
      else
        result[name] = value;
    }

    return result;
  }
1296319
public String toXML()
    {
        String eol = SystemUtils.LINE_SEPARATOR;
        StringBuffer buf = new StringBuffer();

        // write all ClassDescriptors
        Iterator i = this.iterator();
        while (i.hasNext())
        {
            buf.append(((XmlCapable) i.next()).toXML() + eol);
        }
        return buf.toString();
    }
1077712
public static Object convertSqlXml(Connection conn, byte[] value) throws SQLException {
        Object result = createSQLXML(conn);

        result = convertSqlXml(result, value);

        return result;
    }