Search in sources :

Example 1 with Unmarshaller

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);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) IStatus(org.eclipse.core.runtime.IStatus) InputStreamReader(java.io.InputStreamReader) CharArrayReader(java.io.CharArrayReader) XMLReader(org.xml.sax.XMLReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) UnmarshalException(org.eclipse.epp.mpc.core.service.UnmarshalException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) BufferedInputStream(java.io.BufferedInputStream) UnmarshalException(org.eclipse.epp.mpc.core.service.UnmarshalException) Unmarshaller(org.eclipse.epp.internal.mpc.core.service.xml.Unmarshaller) IMarketplaceUnmarshaller(org.eclipse.epp.mpc.core.service.IMarketplaceUnmarshaller) XMLReader(org.xml.sax.XMLReader)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 CharArrayReader (java.io.CharArrayReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ByteBuffer (java.nio.ByteBuffer)1 IStatus (org.eclipse.core.runtime.IStatus)1 Unmarshaller (org.eclipse.epp.internal.mpc.core.service.xml.Unmarshaller)1 IMarketplaceUnmarshaller (org.eclipse.epp.mpc.core.service.IMarketplaceUnmarshaller)1 UnmarshalException (org.eclipse.epp.mpc.core.service.UnmarshalException)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 XMLReader (org.xml.sax.XMLReader)1