jaxbutil.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 534 行 · 第 1/2 页

JAVA
534
字号
/* * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source 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 2 of the License, or * (at your option) any later version. * * Resin Open Source 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, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the *   Free SoftwareFoundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Emil Ong */package com.caucho.jaxb;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import javax.xml.bind.JAXBException;import javax.xml.bind.annotation.XmlAnyAttribute;import javax.xml.bind.annotation.XmlAnyElement;import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlElements;import javax.xml.bind.annotation.XmlElementRef;import javax.xml.bind.annotation.XmlID;import javax.xml.bind.annotation.XmlSchema;import javax.xml.bind.annotation.XmlType;import javax.xml.bind.annotation.XmlValue;import javax.xml.datatype.DatatypeFactory;import javax.xml.datatype.DatatypeConfigurationException;import javax.xml.datatype.XMLGregorianCalendar;import javax.xml.namespace.QName;import javax.xml.stream.XMLEventFactory;import javax.xml.ws.Holder;import static java.lang.Character.*;import java.lang.reflect.AnnotatedElement;import java.lang.reflect.GenericArrayType;import java.lang.reflect.Method;import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;import java.math.BigDecimal;import java.math.BigInteger;import java.util.ArrayList;import java.util.Calendar;import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.logging.Logger;import com.caucho.util.L10N;/** * JAXB utilities. */public class JAXBUtil {  private static final L10N L = new L10N(JAXBUtil.class);  private static final Logger log = Logger.getLogger(JAXBUtil.class.getName());  private static final Map<Class,QName> _datatypeMap    = new HashMap<Class,QName>();  private static final Map<QName,Class> _classMap    = new HashMap<QName,Class>();  private static DatatypeFactory _datatypeFactory;  public static final XMLEventFactory EVENT_FACTORY     = XMLEventFactory.newInstance();  public static final String XML_SCHEMA_NS = "http://www.w3.org/2001/XMLSchema";  public static final String XML_SCHEMA_PREFIX = "xsd";  public static DatatypeFactory getDatatypeFactory()    throws JAXBException  {    if (_datatypeFactory == null) {      try {        _datatypeFactory = DatatypeFactory.newInstance();      }      catch (DatatypeConfigurationException e) {        throw new JAXBException(e);      }    }    return _datatypeFactory;  }  public static QName qnameFromNode(Node node)  {    if (node == null)      return null;    int colon = node.getNodeName().indexOf(':');    if (colon > 0) {       String prefix = node.getNodeName().substring(0, colon);      String localName = node.getNodeName().substring(colon + 1);      return new QName(node.getNamespaceURI(), localName, prefix);    }     else if (node.getNamespaceURI() != null)      return new QName(node.getNamespaceURI(), node.getNodeName());    else      return new QName(node.getNodeName());  }  public static Element elementFromQName(QName name, Node node)  {    Document doc = node.getOwnerDocument();    if (name.getPrefix() != null)      return doc.createElementNS(name.getNamespaceURI(),                                 name.getPrefix() + ':' + name.getLocalPart());    else if (name.getNamespaceURI() != null)      return doc.createElementNS(name.getNamespaceURI(), name.getLocalPart());    else      return doc.createElement(name.getLocalPart());  }  // skip all the whitespace and comments  public static Node skipIgnorableNodes(Node node)   {    while (node != null) {      if (node.getNodeType() == Node.TEXT_NODE) {        String text = node.getTextContent();        boolean whitespace = true;        for (int i = 0; i < text.length(); i++) {          if (! Character.isWhitespace(text.charAt(i))) {            whitespace = false;            break;          }        }        if (! whitespace)          break;      }      else if (node.getNodeType() != Node.COMMENT_NODE)         break;      node = node.getNextSibling();    }    return node;  }  /**   * Gets the type of a parameter.  If the type is something like Holder<T>,   * it return T, otherwise, it returns the passed type.   *   **/  public static Type getActualParameterType(Type type)    throws JAXBException  {    if (type instanceof ParameterizedType) {      ParameterizedType ptype = (ParameterizedType) type;      if (ptype.getRawType().equals(Holder.class)) {        Type[] arguments = ptype.getActualTypeArguments();        if (arguments.length != 1)          throw new JAXBException("Holder has incorrect number of arguments");        return arguments[0];      }    }    return type;  }  public static void introspectClass(Class cl, Collection<Class> jaxbClasses)    throws JAXBException  {    log.finest("Introspecting class " + cl.getName());    Method[] methods = cl.getMethods();    for (Method method : methods)      introspectMethod(method, jaxbClasses);  }  /**   * Finds all the classes mentioned in a method signature (return type and   * parameters) and adds them to the passed in classList.  Pass in a set if   * you expect multiple references.   */  public static void introspectMethod(Method method,                                       Collection<Class> jaxbClasses)    throws JAXBException  {    log.finest("Introspecting method " + method.getName());    introspectType(method.getReturnType(), jaxbClasses);    Type[] params = method.getGenericParameterTypes();    for (Type param : params) {      if (param.equals(Holder.class))        continue;      introspectType(getActualParameterType(param), jaxbClasses);    }    // XXX: Check for @WebFault annotation    /*    Type[] exceptions = method.getGenericExceptionTypes();    for (Type exception : exceptions) {      if (! exception.toString().endsWith("_Exception"))        introspectType(exception, jaxbClasses);    }*/  }  /**   * Add all classes referenced by type to jaxbClasses.   */  private static void introspectType(Type type, Collection<Class> jaxbClasses)  {    log.finest("  Introspecting type " + type);    if (type instanceof Class) {      Class cl = (Class) type;      if (! cl.isInterface())        jaxbClasses.add((Class) type);    }    else if (type instanceof ParameterizedType) {      ParameterizedType pType = (ParameterizedType) type;      introspectType(pType.getRawType(), jaxbClasses);      introspectType(pType.getOwnerType(), jaxbClasses);      Type[] arguments = pType.getActualTypeArguments();      for (Type argument : arguments)        introspectType(argument, jaxbClasses);    }    else if (type instanceof GenericArrayType) {      Type component = ((GenericArrayType) type).getGenericComponentType();      introspectType(component, jaxbClasses);    }    else if (type != null) {      // Type variables must be instantiated      throw new UnsupportedOperationException(L.l("Method arguments cannot have uninstantiated type variables or wildcards ({0} of type {1})", type, type.getClass().getName()));    }  }

⌨️ 快捷键说明

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