FileDocCategorySizeDatePackage
ParserFactory.javaAPI DocAndroid 5.1 API4623Thu Mar 12 22:22:44 GMT 2015com.android.layoutlib.bridge.impl

ParserFactory

public class ParserFactory extends Object
A factory for {@link XmlPullParser}.

Fields Summary
private static final String
ENCODING
public static final boolean
LOG_PARSER
Constructors Summary
Methods Summary
public static org.xmlpull.v1.XmlPullParsercreate(java.io.File f)


        
               
        InputStream stream = new FileInputStream(f);
        return create(stream, f.getName(), f.length());
    
public static org.xmlpull.v1.XmlPullParsercreate(java.io.InputStream stream, java.lang.String name)

        return create(stream, name, -1);
    
private static org.xmlpull.v1.XmlPullParsercreate(java.io.InputStream stream, java.lang.String name, long size)

        KXmlParser parser = instantiateParser(name);

        stream = readAndClose(stream, name, size);

        parser.setInput(stream, ENCODING);
        return parser;
    
private static org.kxml2.io.KXmlParserinstantiateParser(java.lang.String name)

        KXmlParser parser;
        if (name != null) {
            parser = new CustomParser(name);
        } else {
            parser = new KXmlParser();
        }
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        return parser;
    
private static java.io.InputStreamreadAndClose(java.io.InputStream stream, java.lang.String name, long size)

        // just a sanity check. It's doubtful we'll have such big files!
        if (size > Integer.MAX_VALUE) {
            throw new XmlPullParserException("File " + name + " is too big to be parsed");
        }
        int intSize = (int) size;

        // create a buffered reader to facilitate reading.
        BufferedInputStream bufferedStream = new BufferedInputStream(stream);
        try {
            int avail;
            if (intSize != -1) {
                avail = intSize;
            } else {
                // get the size to read.
                avail = bufferedStream.available();
            }

            // create the initial buffer and read it.
            byte[] buffer = new byte[avail];
            int read = stream.read(buffer);

            // this is the easy case.
            if (read == intSize) {
                return new ByteArrayInputStream(buffer);
            }

            // check if there is more to read (read() does not necessarily read all that
            // available() returned!)
            while ((avail = bufferedStream.available()) > 0) {
                if (read + avail > buffer.length) {
                    // just allocate what is needed. We're mostly reading small files
                    // so it shouldn't be too problematic.
                    byte[] moreBuffer = new byte[read + avail];
                    System.arraycopy(buffer, 0, moreBuffer, 0, read);
                    buffer = moreBuffer;
                }

                read += stream.read(buffer, read, avail);
            }

            // return a new stream encapsulating this buffer.
            return new ByteArrayInputStream(buffer);

        } catch (IOException e) {
            throw new XmlPullParserException("Failed to read " + name, null, e);
        } finally {
            try {
                bufferedStream.close();
            } catch (IOException e) {
            }
        }