⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elementtype.java

📁 jbpm-bpel-1.1.Beta3 JBoss jBPM Starters Kit  是一个综合包
💻 JAVA
字号:
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the JBPM BPEL PUBLIC LICENSE AGREEMENT as
 * published by JBoss Inc.; either version 1.0 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
package org.jbpm.bpel.db.type;

import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import org.jbpm.bpel.xml.util.XmlUtil;

/**
 * @author Juan Cantu
 * @version $Revision: 1.3 $ $Date: 2006/09/27 03:53:02 $
 */
public class ElementType implements UserType {

  private static final int[] SQL_TYPES = { Types.LONGVARCHAR };
  private static final Log log = LogFactory.getLog(ElementType.class);

  public boolean equals(Object o1, Object o2) {
    return o1 == null ? o2 == null : o1.equals(o2);
  }

  public int hashCode(Object o) {
    return o.hashCode();
  }

  public Object deepCopy(Object o) {
    return o != null ? ((Element) o).cloneNode(true) : null;
  }

  public boolean isMutable() {
    return true;
  }

  public Serializable disassemble(Object o) throws HibernateException {
    return (Serializable) o;
  }

  public Object assemble(Serializable s, Object o) throws HibernateException {
    return s;
  }

  public Object replace(Object original, Object target, Object owner) {
    return target;
  }

  public int[] sqlTypes() {
    return SQL_TYPES;
  }

  public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
      throws HibernateException, SQLException {
    // retrieve stream from database
    String columnName = names[0];
    Reader xmlStream = rs.getCharacterStream(columnName);
    // if SQL value is NULL, element is null as well
    if (xmlStream == null)
      return null;
    // prepare input source with bare location info
    InputSource source = new InputSource(xmlStream);
    source.setPublicId(columnName);
    try {
      // parse XML text
      Element element = XmlUtil.getDocumentBuilder()
          .parse(source)
          .getDocumentElement();
      if (log.isTraceEnabled())
        log.trace("returning '" + element + "' as column: " + columnName);
      return element;
    }
    catch (SAXException e) {
      throw new HibernateException("could not parse column: " + columnName, e);
    }
    catch (IOException e) {
      throw new HibernateException("could not read column: " + columnName, e);
    }
  }

  public void nullSafeSet(PreparedStatement st, Object value, int index)
      throws HibernateException, SQLException {
    // easy way out: null value
    if (value == null) {
      st.setNull(index, SQL_TYPES[0]);
      if (log.isTraceEnabled())
        log.trace("binding null to parameter: " + index);
    }
    else {
      Element element = (Element) value;
      try {
        // create identity transformer
        Transformer idTransformer = XmlUtil.getTransformerFactory()
            .newTransformer();
        // allocate memory result stream
        StringWriter xmlStream = new StringWriter();
        // write DOM subtree to stream
        idTransformer.transform(new DOMSource(element), new StreamResult(
            xmlStream));
        // bind text from memory buffer
        StringBuffer xmlBuffer = xmlStream.getBuffer();
        st.setCharacterStream(index, new StringBufferReader(xmlBuffer),
            xmlBuffer.length());
        if (log.isTraceEnabled())
          log.trace("binding '" + xmlBuffer + "' to parameter: " + index);
      }
      catch (TransformerException e) {
        throw new HibernateException("could not transform to stream: "
            + element, e);
      }
    }
  }

  public Class returnedClass() {
    return Element.class;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -