Ejemplo n.º 1
0
 private void writeCollection(
     final OutputStream outputStream,
     final Type collectionType,
     final Collection<?> collection,
     final QName outerTagName) {
   final Class<?> rawType;
   if (collectionType instanceof ParameterizedType) {
     final ParameterizedType returnType = (ParameterizedType) collectionType;
     rawType = (Class<?>) returnType.getRawType();
   } else if (collectionType instanceof Class<?>) {
     rawType = (Class<?>) collectionType;
   } else if (collectionType instanceof WildcardType) {
     final Type[] UpperBounds = ((WildcardType) collectionType).getUpperBounds();
     if (UpperBounds.length > 0) {
       rawType = (Class<?>) UpperBounds[0];
     } else {
       rawType = Object.class;
     }
   } else if (collectionType instanceof TypeVariable) {
     final Type[] UpperBounds = ((TypeVariable<?>) collectionType).getBounds();
     if (UpperBounds.length > 0) {
       rawType = (Class<?>) UpperBounds[0];
     } else {
       rawType = Object.class;
     }
   } else {
     throw new IllegalArgumentException("Unsupported type variable");
   }
   Class<?> elementType;
   if (Collection.class.isAssignableFrom(rawType)) {
     final Type[] paramTypes = Types.getTypeParametersFor(Collection.class, collectionType);
     elementType = Types.toRawType(paramTypes[0]);
     if (elementType.isInterface()) {
       // interfaces not supported by jaxb
       elementType = Types.commonAncestor(collection);
     }
   } else {
     elementType = Types.commonAncestor(collection);
   }
   try {
     // As long as JAXB is an option, we have to know that this is a StAXWriter as JAXB needs to
     // write to that.
     try (XmlWriter xmlWriter = XmlStreaming.newWriter(outputStream, "UTF-8")) {
       Marshaller marshaller = null;
       XmlWriterUtil.smartStartTag(xmlWriter, outerTagName);
       for (Object item : collection) {
         if (item != null) {
           if (item instanceof XmlSerializable) {
             ((XmlSerializable) item).serialize(xmlWriter);
           } else if (item instanceof Node) {
             XmlWriterUtil.serialize(xmlWriter, (Node) item);
           } else {
             if (marshaller == null) {
               JAXBContext jaxbcontext = null;
               if (elementType == null) {
                 jaxbcontext = newJAXBContext(JAXBCollectionWrapper.class);
               } else {
                 jaxbcontext = newJAXBContext(JAXBCollectionWrapper.class, elementType);
               }
               marshaller = jaxbcontext.createMarshaller();
             }
             marshaller.marshal(item, (XMLStreamWriter) getDelegateMethod().invoke(xmlWriter));
           }
         }
       }
       XmlWriterUtil.endTag(xmlWriter, outerTagName);
     }
   } catch (Throwable e) {
     throw new MessagingException(e);
   }
 }