jaxbcontextimpl.java

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

JAVA
927
字号
  {    if (_laxAnyTypeProperty == null)      _laxAnyTypeProperty = new LaxAnyTypeProperty(this);    return _laxAnyTypeProperty;  }  // XXX clean up all this argument tiering  public Property createProperty(Type type)    throws JAXBException  {    return createProperty(type, false);  }  public Property createProperty(Type type, boolean anyType)    throws JAXBException  {    return createProperty(type, anyType, null);  }  public Property createProperty(Type type, boolean anyType, String mimeType)    throws JAXBException  {    return createProperty(type, anyType, mimeType, false);  }  public Property createProperty(Type type, boolean anyType, String mimeType,                                  boolean xmlList)    throws JAXBException  {    return createProperty(type, anyType, mimeType, xmlList, false);  }  public Property createProperty(Type type, boolean anyType, String mimeType,                                  boolean xmlList, boolean xmlValue)    throws JAXBException  {    if (type instanceof Class) {      if (anyType && Object.class.equals(type))        return getLaxAnyTypeProperty();      Property simpleTypeProperty =         getSimpleTypeProperty((Class) type, mimeType);      if (simpleTypeProperty != null) {        // jaxb/12gb        if (simpleTypeProperty == ByteArrayProperty.PROPERTY &&             xmlList && ! xmlValue)          throw new JAXBException(L.l("@XmlList applied to byte[] valued fields or properties"));        return simpleTypeProperty;      }      Class cl = (Class) type;      if (cl.isArray()) {        Property componentProperty =           createProperty(cl.getComponentType(), anyType);        if (xmlList) {          if (! (componentProperty instanceof CDataProperty))            throw new JAXBException(L.l("Elements annotated with @XmlList or @XmlValue must be simple XML types"));          Class componentType = cl.getComponentType();          CDataProperty cdataProperty = (CDataProperty) componentProperty;          return XmlListArrayProperty.createXmlListArrayProperty(cdataProperty,                                                                 componentType);        }        else           return ArrayProperty.createArrayProperty(componentProperty,                                                   cl.getComponentType());      }      if (cl.isEnum()) {        EnumProperty enumProperty = new EnumProperty(cl, this);        _enums.add(enumProperty);        return enumProperty;      }      // XXX Map      if (List.class.isAssignableFrom(cl)) {        Property property = new SkeletonProperty(getSkeleton(Object.class));        if (xmlList) {          throw new JAXBException(L.l("Elements annotated with @XmlList or @XmlValue must be simple XML types"));        }        else          return new ListProperty(property);      }      if (Collection.class.isAssignableFrom(cl)) {        Property property = new SkeletonProperty(getSkeleton(Object.class));        if (xmlList) {          throw new JAXBException(L.l("Elements annotated with @XmlList or @XmlValue must be simple XML types"));        }        else          return new CollectionProperty(property);      }      ClassSkeleton skel = getSkeleton(cl);      // XXX: interfaces      if (skel != null)	return new SkeletonProperty(skel);      else	return null;    }    else if (type instanceof ParameterizedType) {      ParameterizedType ptype = (ParameterizedType) type;      Type rawType = ptype.getRawType();      if (rawType instanceof Class) {        Class rawClass = (Class) rawType;        if (Map.class.isAssignableFrom(rawClass)) {          Type[] args = ptype.getActualTypeArguments();          if (args.length != 2)            throw new JAXBException(L.l("unexpected number of generic arguments for Map<>: {0}", args.length));          Property keyProperty = createProperty(args[0], anyType);          Property valueProperty = createProperty(args[1], anyType);          return new MapProperty(rawClass, keyProperty, valueProperty);        }        if (Collection.class.isAssignableFrom(rawClass)) {          Type[] args = ptype.getActualTypeArguments();          if (args.length != 1)            throw new JAXBException(L.l("unexpected number of generic arguments for List<>: {0}", args.length));          Property componentProperty = createProperty(args[0], anyType);          if (xmlList) {            if (! (componentProperty instanceof CDataProperty))              throw new JAXBException(L.l("Elements annotated with @XmlList or @XmlValue must be simple XML types"));            CDataProperty cdataProperty = (CDataProperty) componentProperty;            return new XmlListCollectionProperty(cdataProperty, rawClass);          }          else if (List.class.isAssignableFrom(rawClass))            return new ListProperty(componentProperty);          else            return new CollectionProperty(componentProperty);        }      }    }    else if (type instanceof GenericArrayType) {      Type component = ((GenericArrayType) type).getGenericComponentType();      if (byte.class.equals(component))        return ByteArrayProperty.PROPERTY;      // XXX other component types?    }    throw new JAXBException(L.l("Unrecognized type: {0}", type.toString()));  }  public Property getSimpleTypeProperty(Class type)    throws JAXBException  {    return getSimpleTypeProperty(type, null);  }  public Property getSimpleTypeProperty(Class type, String mimeType)    throws JAXBException  {    if (String.class.equals(type))      return StringProperty.PROPERTY;    if (URI.class.equals(type))      return URIProperty.PROPERTY;    if (UUID.class.equals(type))      return UUIDProperty.PROPERTY;    if (Double.class.equals(type))      return DoubleProperty.OBJECT_PROPERTY;    if (Double.TYPE.equals(type))      return DoubleProperty.PRIMITIVE_PROPERTY;    if (Float.class.equals(type))      return FloatProperty.OBJECT_PROPERTY;    if (Float.TYPE.equals(type))      return FloatProperty.PRIMITIVE_PROPERTY;    if (Integer.class.equals(type))      return IntProperty.OBJECT_PROPERTY;    if (Integer.TYPE.equals(type))      return IntProperty.PRIMITIVE_PROPERTY;    if (Long.class.equals(type))      return LongProperty.OBJECT_PROPERTY;    if (Long.TYPE.equals(type))      return LongProperty.PRIMITIVE_PROPERTY;    if (Boolean.class.equals(type))      return BooleanProperty.OBJECT_PROPERTY;    if (Boolean.TYPE.equals(type))      return BooleanProperty.PRIMITIVE_PROPERTY;    if (Character.class.equals(type))      return CharacterProperty.OBJECT_PROPERTY;    if (Character.TYPE.equals(type))      return CharacterProperty.PRIMITIVE_PROPERTY;    if (Short.class.equals(type))      return ShortProperty.OBJECT_PROPERTY;    if (Short.TYPE.equals(type))      return ShortProperty.PRIMITIVE_PROPERTY;    if (Byte.class.equals(type))      return ByteProperty.OBJECT_PROPERTY;    if (Byte.TYPE.equals(type))      return ByteProperty.PRIMITIVE_PROPERTY;    if (BigDecimal.class.equals(type))      return BigDecimalProperty.PROPERTY;    if (BigInteger.class.equals(type))      return BigIntegerProperty.PROPERTY;    if (QName.class.equals(type))      return QNameProperty.PROPERTY;    if (Date.class.equals(type))      return DateTimeProperty.PROPERTY;    if (Calendar.class.equals(type))      return CalendarProperty.PROPERTY;    if (Duration.class.equals(type))      return DurationProperty.PROPERTY;    if (XMLGregorianCalendar.class.equals(type))      return XMLGregorianCalendarProperty.PROPERTY;    if (Image.class.equals(type))      return ImageProperty.getImageProperty(mimeType);    if (DataHandler.class.equals(type))      return DataHandlerProperty.PROPERTY;    if (Source.class.equals(type))      return SourceProperty.PROPERTY;    if (byte[].class.equals(type))      return ByteArrayProperty.PROPERTY;    return null;  }  public void addXmlType(QName typeName, ClassSkeleton skeleton)    throws JAXBException  {    if (_types.containsKey(typeName)) {      ClassSkeleton existing = _types.get(typeName);      if (! _pendingSkeletons.contains(existing.getType())) {        throw new JAXBException(L.l("Duplicate type name {0} for types {1} and {2}",                                    typeName,                                    skeleton.getType(),                                    existing.getType()));      }    }    _types.put(typeName, skeleton);  }  public void addRootElement(ClassSkeleton s)     throws JAXBException  {    ClassSkeleton old = _roots.get(s.getElementName());    // Use != here to check duplicate puts; equals() isn't necessary    if (old != null && old != s && ! _pendingSkeletons.contains(s.getType())) {      throw new JAXBException(L.l("Duplicate name {0} for classes {1} and {2}",                                  s.getElementName(),                                  s.getType(),                                  _roots.get(s.getElementName()).getType()));    }    _roots.put(s.getElementName(), s);  }  public boolean hasXmlType(QName typeName)  {    return _types.containsKey(typeName);  }  public boolean hasRootElement(QName elementName)  {    return _roots.containsKey(elementName);  }  public ClassSkeleton getRootElement(QName q)  {    return _roots.get(q);  }  private void loadPackage(String packageName, ClassLoader classLoader)     throws JAXBException  {    boolean success = false;    try {      Class cl = Class.forName(packageName + ".ObjectFactory");      introspectObjectFactory(cl);      success = true;    }    catch (ClassNotFoundException e) {      // we can still try for jaxb.index    }    String resourceName = packageName.replace('.', '/') + "/jaxb.index";    // For some reason, this approach works when running resin...    InputStream is = this.getClass().getResourceAsStream('/' + resourceName);    // ...and this approach works in QA    if (is == null)      is = classLoader.getResourceAsStream(resourceName);    if (is == null) {      if (success)        return;      throw new JAXBException(L.l("Unable to open jaxb.index for package {0}",                                  packageName));    }    try {      InputStreamReader isr = new InputStreamReader(is, "utf-8");      LineNumberReader in = new LineNumberReader(isr);      for (String line = in.readLine();           line != null;           line = in.readLine()) {        String[] parts = line.split("#", 2);        String className = parts[0].trim();        if (! "".equals(className)) {          Class cl = classLoader.loadClass(packageName + "." + className);          createSkeleton(cl);        }      }    }    catch (IOException e) {      throw new JAXBException(L.l("Error while reading jaxb.index for package {0}", packageName), e);    }    catch (ClassNotFoundException e) {      throw new JAXBException(e);    }  }  private void introspectObjectFactory(Class factoryClass)    throws JAXBException  {    Object objectFactory = null;    try {      objectFactory = factoryClass.newInstance();    }    catch (Exception e) {      throw new JAXBException(e);    }    String namespace = null;    Package pkg = factoryClass.getPackage();    if (pkg.isAnnotationPresent(XmlSchema.class)) {      XmlSchema schema = (XmlSchema) pkg.getAnnotation(XmlSchema.class);      if (! "".equals(schema.namespace()))        namespace = schema.namespace();    }    Method[] methods = factoryClass.getMethods();    for (Method method : methods) {      if (method.getName().startsWith("create")) {        XmlElementDecl decl = method.getAnnotation(XmlElementDecl.class);        Class cl = method.getReturnType();        ClassSkeleton skeleton = null;        QName root = null;        if (cl.equals(JAXBElement.class)) {          ParameterizedType type =             (ParameterizedType) method.getGenericReturnType();          cl = (Class) type.getActualTypeArguments()[0];          skeleton = new JAXBElementSkeleton(this, cl, method, objectFactory);          _jaxbElementSkeletons.put(cl, (JAXBElementSkeleton) skeleton);        }        else {          skeleton = getSkeleton(cl);          if (skeleton == null)             skeleton = createSkeleton(cl);          skeleton.setCreateMethod(method, objectFactory);          root = skeleton.getElementName();        }        if (decl != null) {          String localName = decl.name();          if (! "##default".equals(decl.namespace()))            namespace = decl.namespace();          if (namespace == null)            root = new QName(localName);          else            root = new QName(namespace, localName);        }        skeleton.setElementName(root);        addRootElement(skeleton);      }      else if (method.getName().equals("newInstance")) {        // XXX      }      else if (method.getName().equals("getProperty")) {        // XXX      }      else if (method.getName().equals("setProperty")) {        // XXX      }    }  }  static class DefaultValidationEventHandler implements ValidationEventHandler {    public boolean handleEvent(ValidationEvent event)    {      if (event == null)        throw new IllegalArgumentException("Event may not be null");      return event.getSeverity() != ValidationEvent.FATAL_ERROR;    }  }}

⌨️ 快捷键说明

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