All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.disy.commons.xml.bind.dwr.JAXBElementConverter Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/**
 * Legato is a configurable, lightweight web mapping client that can be
 * easily embedded into web pages and portals, CMS and individual web
 * applications. Legato is implemented in JavaScript and based on the
 * popular open source library OpenLayers.
 *
 * Copyright (C) 2010  disy Informationssysteme GmbH, http://www.disy.net
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package net.disy.commons.xml.bind.dwr;

import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;

import net.disy.commons.dwr.convert.AbstractConverter;

import org.apache.commons.lang.ObjectUtils;
import org.directwebremoting.dwrp.ObjectOutboundVariable;
import org.directwebremoting.extend.InboundContext;
import org.directwebremoting.extend.InboundVariable;
import org.directwebremoting.extend.MarshallException;
import org.directwebremoting.extend.OutboundContext;
import org.directwebremoting.extend.OutboundVariable;

import com.sun.xml.bind.api.JAXBRIContext;
import com.sun.xml.bind.v2.ContextFactory;
import com.sun.xml.bind.v2.model.runtime.RuntimeElementInfo;

public class JAXBElementConverter extends AbstractConverter {

  private JAXBRIContext context;

  public JAXBRIContext getJAXBRIContext() throws MarshallException {

    if (context == null) {
      try {
        this.context = (JAXBRIContext) ContextFactory.createContext(getContextPath(), Thread
            .currentThread()
            .getContextClassLoader(), Collections. emptyMap());

      }
      catch (JAXBException jaxbex) {
        throw new MarshallException(JAXBElement.class, jaxbex);

      }
    }
    return context;
  }

  @Override
  protected Object convertInbound(
      @SuppressWarnings("unchecked") Class paramType,
      InboundVariable data,
      Map tokens,
      InboundContext inctx,
      String value) throws MarshallException {

    QName name = getValue("name", QName.class, data, tokens, inctx); //$NON-NLS-1$

    for (RuntimeElementInfo elementInfo : getJAXBRIContext()
        .getRuntimeTypeInfoSet()
        .getAllElements()) {
      if (ObjectUtils.equals(name, elementInfo.getElementName())) {
        @SuppressWarnings("unchecked")
        final Class type = (Class) elementInfo.getContentInMemoryType();
        final Object content = getValue("value", type, data, tokens, inctx); //$NON-NLS-1$
        return new JAXBElement(name, type, content);
      }
    }
    throw new MarshallException(JAXBElement.class);
  }

  @Override
  public OutboundVariable convertOutbound(Object data, OutboundContext outctx)
      throws MarshallException {

    final JAXBElement element = (JAXBElement) data;

    //         // Where we collect out converted children
    final Map ovs = new TreeMap();

    // We need to do this before collecing the children to save recurrsion
    final ObjectOutboundVariable ov = new ObjectOutboundVariable(outctx);
    outctx.put(data, ov);

    ovs.put("name", getConverterManager().convertOutbound(element.getName(), outctx)); //$NON-NLS-1$
    ovs.put("value", getConverterManager().convertOutbound(element.getValue(), outctx)); //$NON-NLS-1$

    ov.init(ovs, null);

    return ov;
  }

  private String contextPath;

  public String getContextPath() {
    return contextPath;
  }

  public void setContextPath(String contextPath) {
    this.contextPath = contextPath;
  }

}