public Object getJAXBObject( JAXBContextFinder finder, MediaType mediaType, Class<?> clazz, Element element) throws JAXBException { JAXBContext ctx = finder.findCachedContext(clazz, mediaType, null); return ctx.createUnmarshaller().unmarshal(element); }
public Object readFrom( Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { JAXBContextFinder finder = getFinder(mediaType); if (finder == null) { throw new JAXBUnmarshalException("Unable to find JAXBContext for media type: " + mediaType); } Class baseType = Types.getCollectionBaseType(type, genericType); JaxbCollection col = null; try { JAXBElement<JaxbCollection> ele = null; if (suppressExpandEntityExpansion()) { SAXSource source = new SAXSource(new InputSource(entityStream)); JAXBContext ctx = finder.findCachedContext(JaxbCollection.class, mediaType, annotations); Unmarshaller unmarshaller = ctx.createUnmarshaller(); unmarshaller = new ExternalEntityUnmarshaller(unmarshaller); ele = unmarshaller.unmarshal(source, JaxbCollection.class); } else { StreamSource source = new StreamSource(entityStream); JAXBContext ctx = finder.findCachedContext(JaxbCollection.class, mediaType, annotations); ele = ctx.createUnmarshaller().unmarshal(source, JaxbCollection.class); } Wrapped wrapped = FindAnnotation.findAnnotation(annotations, Wrapped.class); if (wrapped != null) { if (!wrapped.element().equals(ele.getName().getLocalPart())) { throw new JAXBUnmarshalException( "Collection wrapping failed, expected root element name of " + wrapped.element() + " got " + ele.getName().getLocalPart()); } if (!wrapped.namespace().equals(ele.getName().getNamespaceURI())) { throw new JAXBUnmarshalException( "Collection wrapping failed, expect namespace of " + wrapped.namespace() + " got " + ele.getName().getNamespaceURI()); } } col = ele.getValue(); } catch (JAXBException e) { throw new JAXBUnmarshalException(e); } try { JAXBContext ctx = finder.findCachedContext(baseType, mediaType, null); Unmarshaller unmarshaller = ctx.createUnmarshaller(); unmarshaller = AbstractJAXBProvider.decorateUnmarshaller(baseType, annotations, mediaType, unmarshaller); if (type.isArray()) { Object array = Array.newInstance(baseType, col.getValue().size()); for (int i = 0; i < col.getValue().size(); i++) { Element val = (Element) col.getValue().get(i); Array.set(array, i, unmarshaller.unmarshal(val)); } return array; } else { Collection outCol = null; if (type.isInterface()) { if (List.class.isAssignableFrom(type)) outCol = new ArrayList(); else if (SortedSet.class.isAssignableFrom(type)) outCol = new TreeSet(); else if (Set.class.isAssignableFrom(type)) outCol = new HashSet(); else outCol = new ArrayList(); } else { try { outCol = (Collection) type.newInstance(); } catch (Exception e) { throw new JAXBUnmarshalException(e); } } for (Object obj : col.getValue()) { Element val = (Element) obj; outCol.add(unmarshaller.unmarshal(val)); } return outCol; } } catch (JAXBException e) { throw new JAXBUnmarshalException(e); } }