freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/W3CDocumentReader.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/W3CDocumentReader.java Sun Oct 25 14:54:58 2009 +0100
1.3 @@ -0,0 +1,46 @@
1.4 +/*
1.5 + * To change this template, choose Tools | Templates
1.6 + * and open the template in the editor.
1.7 + */
1.8 +
1.9 +package cz.xelfi.quoridor.freemarkerdor;
1.10 +
1.11 +import java.io.IOException;
1.12 +import java.io.InputStream;
1.13 +import java.lang.annotation.Annotation;
1.14 +import java.lang.reflect.Type;
1.15 +import javax.ws.rs.Consumes;
1.16 +import javax.ws.rs.WebApplicationException;
1.17 +import javax.ws.rs.core.MediaType;
1.18 +import javax.ws.rs.core.MultivaluedMap;
1.19 +import javax.ws.rs.ext.Provider;
1.20 +import javax.xml.parsers.DocumentBuilder;
1.21 +import javax.xml.parsers.DocumentBuilderFactory;
1.22 +import javax.xml.parsers.ParserConfigurationException;
1.23 +import org.w3c.dom.Document;
1.24 +import org.xml.sax.SAXException;
1.25 +
1.26 +/**
1.27 + *
1.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.29 + */
1.30 +@Provider
1.31 +@Consumes(MediaType.TEXT_XML)
1.32 +public class W3CDocumentReader implements javax.ws.rs.ext.MessageBodyReader<Document> {
1.33 + public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
1.34 + return type == Document.class;
1.35 + }
1.36 +
1.37 + public Document readFrom(Class<Document> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
1.38 + try {
1.39 + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
1.40 + DocumentBuilder builder = factory.newDocumentBuilder();
1.41 + return builder.parse(entityStream);
1.42 + } catch (ParserConfigurationException ex) {
1.43 + throw new WebApplicationException(ex);
1.44 + } catch (SAXException ex) {
1.45 + throw new WebApplicationException(ex);
1.46 + }
1.47 + }
1.48 +
1.49 +}