104ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet/*
204ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet * Copyright (C) 2011 The Android Open Source Project
304ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet *
404ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet * Licensed under the Apache License, Version 2.0 (the "License");
504ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet * you may not use this file except in compliance with the License.
604ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet * You may obtain a copy of the License at
704ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet *
804ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet *      http://www.apache.org/licenses/LICENSE-2.0
904ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet *
1004ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet * Unless required by applicable law or agreed to in writing, software
1104ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet * distributed under the License is distributed on an "AS IS" BASIS,
1204ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1304ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet * See the License for the specific language governing permissions and
1404ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet * limitations under the License.
1504ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet */
1604ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
1704ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohetpackage com.android.layoutlib.bridge.impl;
1804ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
1904ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
2004ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohetimport org.kxml2.io.KXmlParser;
2104ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohetimport org.xmlpull.v1.XmlPullParser;
2204ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohetimport org.xmlpull.v1.XmlPullParserException;
2304ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
2404ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohetimport java.io.File;
2504ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohetimport java.io.FileInputStream;
2604ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohetimport java.io.FileNotFoundException;
2704ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohetimport java.io.InputStream;
2804ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
2904ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet/**
3004ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet * A factory for {@link XmlPullParser}.
3104ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet *
3204ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet */
3304ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohetpublic class ParserFactory {
3404ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
3504ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet    private final static String ENCODING = "UTF-8"; //$NON-NLS-1$
3604ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
3704ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet    public final static boolean LOG_PARSER = false;
3804ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
3904ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet    public static XmlPullParser create(File f)
4004ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet            throws XmlPullParserException, FileNotFoundException {
4104ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        KXmlParser parser = instantiateParser(f.getName());
4204ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        parser.setInput(new FileInputStream(f), ENCODING);
4304ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        return parser;
4404ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet    }
4504ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
4604ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet    public static XmlPullParser create(InputStream stream, String name)
4704ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet            throws XmlPullParserException {
4804ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        KXmlParser parser = instantiateParser(name);
4904ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        parser.setInput(stream, ENCODING);
5004ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        return parser;
5104ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet    }
5204ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
5304ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet    private static KXmlParser instantiateParser(String name) throws XmlPullParserException {
5404ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        KXmlParser parser;
5504ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        if (name != null) {
5604ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet            parser = new CustomParser(name);
5704ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        } else {
5804ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet            parser = new KXmlParser();
5904ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        }
6004ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
6104ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        return parser;
6204ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet    }
6304ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
6404ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet    private static class CustomParser extends KXmlParser {
6504ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        private final String mName;
6604ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
6704ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        CustomParser(String name) {
6804ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet            super();
6904ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet            mName = name;
7004ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        }
7104ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet
7204ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        @Override
7304ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        public String toString() {
7404ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet            return mName;
7504ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet        }
7604ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet    }
7704ce81113107d2bfa0b8248b13145b4cf24cb943Xavier Ducrohet}
78