📄 xmlpullreaderfactory.java
字号:
/*Copyright (c) 2005-2006, Dennis M. Sosnoski.All rights reserved.Redistribution and use in source and binary forms, with or without modification,are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of JiBX nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" ANDANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIEDWARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FORANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ONANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THISSOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.jibx.runtime.impl;import java.io.IOException;import java.io.InputStream;import java.io.Reader;import org.jibx.runtime.IXMLReader;import org.jibx.runtime.JiBXException;import org.xmlpull.v1.XmlPullParser;import org.xmlpull.v1.XmlPullParserException;import org.xmlpull.v1.XmlPullParserFactory;/** * Factory for creating XMLPull parser instances. * * @author Dennis M. Sosnoski * @version 1.0 */public class XMLPullReaderFactory implements IXMLReaderFactory{ /** Default parser factory name when nothing else found. */ private static final String DEFAULT_PARSER_NAME = "org.xmlpull.mxp1.MXParserFactory"; /** Singleton instance of class. */ private static final XMLPullReaderFactory s_instance; static { // first try getting factory defined by property value XmlPullParserFactory factory = null; ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader == null) { loader = XMLPullReaderFactory.class.getClassLoader(); } try { String name = System.getProperty(XmlPullParserFactory.PROPERTY_NAME); if (name != null && (name = name.trim()).length() > 0) { factory = XmlPullParserFactory.newInstance(name, loader.getClass()); } } catch (Exception ex) { /* deliberately empty */ } // if no luck that way, try getting it directly if (factory == null) { try { factory = XmlPullParserFactory.newInstance(); } catch (Exception ex) { /* deliberately empty */ } if (factory == null) { throw new RuntimeException("Unable to create XMLPull parser"); } } s_instance = new XMLPullReaderFactory(factory); } /** Factory used for constructing parser instances. */ private final XmlPullParserFactory m_factory; /** * Internal constructor. */ private XMLPullReaderFactory(XmlPullParserFactory factory) { m_factory = factory; } /** * Get instance of factory. * * @return factory instance */ public static XMLPullReaderFactory getInstance() { return s_instance; } /** * Create new parser instance. * * @param nsf enable namespace processing on parser flag * @return parser instance * @throws XmlPullParserException on error creating parser */ private XmlPullParser createParser(boolean nsf) throws XmlPullParserException { XmlPullParser parser = m_factory.newPullParser(); if (nsf) { parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); } return parser; } /* (non-Javadoc) * @see org.jibx.runtime.impl.IXMLReaderFactory#createReader(java.io.InputStream, java.lang.String, java.lang.String, boolean) */ public IXMLReader createReader(InputStream is, String name, String enc, boolean nsf) throws JiBXException { try { return recycleReader(new XMLPullReader(createParser(nsf)), is, name, enc); } catch (XmlPullParserException e) { throw new JiBXException("Error creating parser", e); } } /* (non-Javadoc) * @see org.jibx.runtime.impl.IXMLReaderFactory#createReader(java.io.Reader, java.lang.String, boolean) */ public IXMLReader createReader(Reader rdr, String name, boolean nsf) throws JiBXException { try { return recycleReader(new XMLPullReader(createParser(nsf)), rdr, name); } catch (XmlPullParserException e) { throw new JiBXException("Error creating parser", e); } } /* (non-Javadoc) * @see org.jibx.runtime.impl.IXMLReaderFactory#recycleReader(org.jibx.runtime.IXMLReader, java.io.InputStream, java.lang.String, java.lang.String) */ public IXMLReader recycleReader(IXMLReader old, InputStream is, String name, String enc) throws JiBXException { ((XMLPullReader)old).setDocument(is, name, enc); return old; } /* (non-Javadoc) * @see org.jibx.runtime.impl.IXMLReaderFactory#recycleReader(org.jibx.runtime.IXMLReader, java.io.Reader, java.lang.String) */ public IXMLReader recycleReader(IXMLReader old, Reader rdr, String name) throws JiBXException { ((XMLPullReader)old).setDocument(rdr, name); return old; } /** * Wrapper for an XMLPull parser implementation. Since the internal parser * API was originally based on XMLPull, this basically just delegates all * the calls with minimal processing. */ private static class XMLPullReader implements IXMLReader { /** Actual parser. */ private final XmlPullParser m_parser; /** Document name. */ private String m_docName; /** Wrapper for supplied input stream. */ private InputStreamWrapper m_streamWrapper; /** Input document character encoding (<code>null</code> if unknown) */ private String m_encoding; /** * Constructor used by factory. * * @param parser */ private XMLPullReader(XmlPullParser parser) { m_parser = parser; } /** * Set document to be parsed from input stream. * * @param is document input stream * @param name document name (<code>null</code> if unknown) * @param enc document character encoding (<code>null</code> if unknown) * @throws JiBXException on parser configuration error */ private void setDocument(InputStream is, String name, String enc) throws JiBXException { try { if (enc == null) { if (m_streamWrapper == null) { m_streamWrapper = new InputStreamWrapper(); } m_streamWrapper.setInput(is, enc); setDocument(m_streamWrapper.getReader(), name); m_encoding = m_streamWrapper.getEncoding(); } else { m_docName = name; m_encoding = enc; m_parser.setInput(is, enc); } } catch (XmlPullParserException e) { throw new JiBXException("Error initializing parser", e); } catch (IOException e) { throw new JiBXException("Error reading from stream", e); } } /** * Set document to be parsed from reader. * * @param rdr document reader * @param name document name (<code>null</code> if unknown) * @throws JiBXException on parser configuration error */ private void setDocument(Reader rdr, String name) throws JiBXException { try { m_docName = name; m_encoding = null; m_parser.setInput(rdr); } catch (XmlPullParserException e) { throw new JiBXException("Error initializing parser", e); } } /** * Format error message from exception. * * @param e root cause exception */ private String describeException(Exception e) { return "Error parsing document " + buildPositionString() + ": " + e.getMessage(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -