001/**
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on 2013-06-13
021 * Created by Douglas Myers-Turnbull
022 *
023 * @since 3.0.6
024 */
025package org.biojava.nbio.structure.rcsb;
026
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import org.w3c.dom.Document;
030import org.w3c.dom.Node;
031import org.w3c.dom.NodeList;
032import org.xml.sax.SAXException;
033
034import javax.xml.parsers.DocumentBuilder;
035import javax.xml.parsers.DocumentBuilderFactory;
036import javax.xml.parsers.ParserConfigurationException;
037import java.io.IOException;
038import java.io.InputStream;
039
040/**
041 * Package-level static utilities for parsing XML.
042 * @author dmyerstu
043 */
044public class ReadUtils {
045
046        private static final Logger logger = LoggerFactory.getLogger(ReadUtils.class);
047
048        // this IS needed
049        private static boolean documentBuilderFactorySet = false;
050
051        /**
052         * @param s
053         * @return {@code s}, or null if {@code s} is the empty string
054         */
055        static String toStr(String s) {
056                if (s.isEmpty()) return null;
057                return s;
058        }
059
060        /**
061         * @param stream
062         * @return A {@link NodeList} of top-level {@link Node Nodes} in {@code stream}.
063         * @throws IOException
064         */
065        static NodeList getNodes(InputStream stream) throws IOException {
066
067                if (!documentBuilderFactorySet) { // it's really stupid, but we have to do this
068                        System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
069                                        "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
070                        documentBuilderFactorySet = true;
071                }
072                DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
073                DocumentBuilder builder = null;
074                Document document = null;
075                try {
076                        builder = builderFactory.newDocumentBuilder();
077                } catch (ParserConfigurationException e) {
078                        logger.warn("Couldn't configure parser", e);
079                        stream.close();
080                        throw new IOException(e);
081                }
082                try {
083                        document = builder.parse(stream);
084                } catch (SAXException e) {
085                        stream.close();
086                        throw new IOException(e);
087                }
088                Node root = document.getDocumentElement();
089                return root.getChildNodes();
090        }
091
092        static Double toDouble(String s) {
093                if (s.isEmpty()) return null;
094                try {
095                        return Double.parseDouble(s);
096                } catch (NumberFormatException e) {
097                        logger.warn(s + " is not a floating-point number", e);
098                }
099                return null;
100        }
101
102        static Integer toInt(String s) {
103                if (s.isEmpty()) return null;
104                try {
105                        return Integer.parseInt(s);
106                } catch (NumberFormatException e) {
107                        logger.warn(s + " is not an integer", e);
108                }
109                return null;
110        }
111
112}