use of org.eclipse.epp.internal.mpc.core.service.xml.Unmarshaller in project epp.mpc by eclipse.
the class MarketplaceUnmarshaller method unmarshal.
public <T> T unmarshal(InputStream in, Class<T> type, IProgressMonitor monitor) throws IOException, UnmarshalException {
if (in == null) {
throw new IOException(Messages.MarketplaceUnmarshaller_errorNullStream);
}
final Unmarshaller unmarshaller = new Unmarshaller();
final XMLReader xmlReader = Unmarshaller.createXMLReader(unmarshaller);
BufferedInputStream bufferedInput = in instanceof BufferedInputStream ? (BufferedInputStream) in : new BufferedInputStream(in);
ByteBuffer peekBuffer = peekResponseContent(bufferedInput);
// FIXME how can the charset be determined?
Reader reader = new InputStreamReader(bufferedInput, RemoteMarketplaceService.UTF_8);
try {
xmlReader.parse(new InputSource(reader));
} catch (final SAXException e) {
IStatus error = createContentError(peekBuffer, NLS.bind(Messages.MarketplaceUnmarshaller_invalidResponseContent, e.getMessage()), e);
throw new UnmarshalException(error);
}
Object model = unmarshaller.getModel();
if (model == null) {
// if we reach here this should never happen
IStatus error = createContentError(peekBuffer, Messages.MarketplaceUnmarshaller_unexpectedResponseContentNullResult, null);
throw new UnmarshalException(error);
} else {
try {
return type.cast(model);
} catch (Exception e) {
String message = NLS.bind(Messages.DefaultMarketplaceService_unexpectedResponseContent, model.getClass().getSimpleName());
IStatus error = createContentError(peekBuffer, message, e);
throw new UnmarshalException(error);
}
}
}
Aggregations