001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.xml;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    
019    import javax.xml.stream.XMLEventReader;
020    import javax.xml.stream.XMLInputFactory;
021    import javax.xml.stream.XMLStreamException;
022    import javax.xml.stream.events.XMLEvent;
023    
024    /**
025     * @author Shuyang Zhou
026     * @author Brian Wing Shun Chan
027     */
028    public class StAXReaderUtil {
029    
030            public static XMLInputFactory getXMLInputFactory() {
031                    return _xmlInputFactory;
032            }
033    
034            public static String read(XMLEventReader xmlEventReader)
035                    throws XMLStreamException {
036    
037                    XMLEvent xmlEvent = xmlEventReader.peek();
038    
039                    if (xmlEvent.isCharacters()) {
040                            xmlEvent = xmlEventReader.nextEvent();
041    
042                            return xmlEvent.asCharacters().getData();
043                    }
044                    else {
045                            return StringPool.BLANK;
046                    }
047            }
048    
049            private static XMLInputFactory _createXMLInputFactory() {
050                    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
051    
052                    xmlInputFactory.setProperty(
053                            XMLInputFactory.IS_COALESCING, Boolean.TRUE);
054    
055                    return xmlInputFactory;
056            }
057    
058            private static XMLInputFactory _xmlInputFactory = _createXMLInputFactory();
059    
060    }