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    import com.liferay.portal.security.xml.SecureXMLFactoryProviderUtil;
019    
020    import javax.xml.stream.XMLEventReader;
021    import javax.xml.stream.XMLInputFactory;
022    import javax.xml.stream.XMLStreamException;
023    import javax.xml.stream.events.XMLEvent;
024    
025    /**
026     * @author Shuyang Zhou
027     * @author Brian Wing Shun Chan
028     */
029    public class StAXReaderUtil {
030    
031            public static XMLInputFactory getXMLInputFactory() {
032                    return _xmlInputFactory;
033            }
034    
035            public static String read(XMLEventReader xmlEventReader)
036                    throws XMLStreamException {
037    
038                    XMLEvent xmlEvent = xmlEventReader.peek();
039    
040                    if (xmlEvent.isCharacters()) {
041                            xmlEvent = xmlEventReader.nextEvent();
042    
043                            return xmlEvent.asCharacters().getData();
044                    }
045                    else {
046                            return StringPool.BLANK;
047                    }
048            }
049    
050            private static XMLInputFactory _createXMLInputFactory() {
051                    XMLInputFactory xmlInputFactory =
052                            SecureXMLFactoryProviderUtil.newXMLInputFactory();
053    
054                    xmlInputFactory.setProperty(
055                            XMLInputFactory.IS_COALESCING, Boolean.TRUE);
056    
057                    return xmlInputFactory;
058            }
059    
060            private static XMLInputFactory _xmlInputFactory = _createXMLInputFactory();
061    
062    }