001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }