001    /*
002     Copyright (c) 2012, Regents of the University of Colorado
003     All rights reserved.
004    
005     Redistribution and use in source and binary forms, with or without modification, 
006     are permitted provided that the following conditions are met:
007    
008     * Redistributions of source code must retain the above copyright notice, this 
009        list of conditions and the following disclaimer.
010       
011     * Redistributions in binary form must reproduce the above copyright notice, 
012        this list of conditions and the following disclaimer in the documentation 
013        and/or other materials provided with the distribution.
014       
015     * Neither the name of the University of Colorado nor the names of its 
016        contributors may be used to endorse or promote products derived from this 
017        software without specific prior written permission.
018    
019     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
022     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
023     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
024     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
025     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
026     ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
027     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
028     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029     */
030    package edu.ucdenver.ccp.medline.parser;
031    
032    import java.io.EOFException;
033    import java.io.FileNotFoundException;
034    import java.io.IOException;
035    import java.io.InputStream;
036    import java.io.ObjectInputStream;
037    import java.util.Iterator;
038    import java.util.NoSuchElementException;
039    
040    import com.thoughtworks.xstream.XStream;
041    import com.thoughtworks.xstream.io.xml.StaxDriver;
042    
043    import edu.ucdenver.ccp.common.file.CharacterEncoding;
044    import edu.ucdenver.ccp.common.file.FileReaderUtil;
045    
046    /**
047     * @author Center for Computational Pharmacology, UC Denver; ccpsupport@ucdenver.edu
048     * 
049     */
050    public class MedlineXmlDeserializer implements Iterator<Object> {
051    
052            private Object nextCitation = null;
053            private final ObjectInputStream in;
054    
055            public MedlineXmlDeserializer(InputStream medlineXmlFileStream) throws FileNotFoundException, IOException {
056                    XStream xstream = new XStream(new StaxDriver());
057                    xstream.processAnnotations(MedlineCitationSet.class);
058                    xstream.processAnnotations(MedlineCitation.class);
059    
060                    in = xstream.createObjectInputStream(FileReaderUtil.initBufferedReader(medlineXmlFileStream,
061                                    CharacterEncoding.UTF_8));
062            }
063    
064            /*
065             * (non-Javadoc)
066             * 
067             * @see java.util.Iterator#hasNext()
068             */
069            public boolean hasNext() {
070                    if (nextCitation == null) {
071                            try {
072                                    Object citation = in.readObject();
073                                    nextCitation = citation;
074                            } catch (EOFException e) {
075                                    return false;
076                            } catch (IOException e) {
077                                    throw new RuntimeException(e);
078                            } catch (ClassNotFoundException e) {
079                                    throw new RuntimeException(e);
080                            }
081                    }
082                    return true;
083            }
084    
085            /*
086             * (non-Javadoc)
087             * 
088             * @see java.util.Iterator#next()
089             */
090            public Object next() {
091                    if (!hasNext())
092                            throw new NoSuchElementException();
093    
094                    Object citationToReturn = nextCitation;
095                    nextCitation = null;
096                    return citationToReturn;
097            }
098    
099            /*
100             * (non-Javadoc)
101             * 
102             * @see java.util.Iterator#remove()
103             */
104            public void remove() {
105                    throw new UnsupportedOperationException("The remove() method is not supported for this iterator.");
106            }
107    
108    }