freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/W3CDocumentReader.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Jan 2010 22:34:17 +0100
branchstatistics-and-elo
changeset 178 4b78d4f028b3
parent 134 6544ad908b05
child 264 d60370059c3c
permissions -rw-r--r--
Initial version of statistics and ELO rating. Donated by Martin Rexa
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.freemarkerdor;
    28 
    29 import java.io.IOException;
    30 import java.io.InputStream;
    31 import java.lang.annotation.Annotation;
    32 import java.lang.reflect.Type;
    33 import javax.ws.rs.Consumes;
    34 import javax.ws.rs.WebApplicationException;
    35 import javax.ws.rs.core.MediaType;
    36 import javax.ws.rs.core.MultivaluedMap;
    37 import javax.ws.rs.ext.Provider;
    38 import javax.xml.parsers.DocumentBuilder;
    39 import javax.xml.parsers.DocumentBuilderFactory;
    40 import javax.xml.parsers.ParserConfigurationException;
    41 import org.w3c.dom.Document;
    42 import org.xml.sax.SAXException;
    43 
    44 /**
    45  *
    46  * @author Jaroslav Tulach <jtulach@netbeans.org>
    47  */
    48 @Provider
    49 @Consumes(MediaType.TEXT_XML)
    50 public class W3CDocumentReader implements javax.ws.rs.ext.MessageBodyReader<Document> {
    51     public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
    52         return type == Document.class;
    53     }
    54 
    55     public Document readFrom(Class<Document> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    56         try {
    57             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    58             DocumentBuilder builder = factory.newDocumentBuilder();
    59             return builder.parse(entityStream);
    60         } catch (ParserConfigurationException ex) {
    61             throw new WebApplicationException(ex);
    62         } catch (SAXException ex) {
    63             throw new WebApplicationException(ex);
    64         }
    65     }
    66 
    67 }