2 * Copyright (C) 2008 OpenIntents.org
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.openintents.filemanager.util;
\r
19 import java.io.IOException;
\r
20 import java.io.InputStream;
\r
21 import java.io.InputStreamReader;
\r
23 import org.xmlpull.v1.XmlPullParser;
\r
24 import org.xmlpull.v1.XmlPullParserException;
\r
25 import org.xmlpull.v1.XmlPullParserFactory;
\r
27 import android.content.Context;
\r
28 import android.content.pm.PackageManager.NameNotFoundException;
\r
29 import android.content.res.Resources;
\r
30 import android.content.res.XmlResourceParser;
\r
32 public class MimeTypeParser {
\r
34 private static final String LOG_TAG = "MimeTypeParser";
\r
36 public static final String TAG_MIMETYPES = "MimeTypes";
\r
37 public static final String TAG_TYPE = "type";
\r
39 public static final String ATTR_EXTENSION = "extension";
\r
40 public static final String ATTR_MIMETYPE = "mimetype";
\r
41 public static final String ATTR_ICON = "icon";
\r
43 private XmlPullParser mXpp;
\r
44 private MimeTypes mMimeTypes;
\r
45 private Resources resources;
\r
46 private String packagename;
\r
48 public MimeTypeParser(Context ctx, String packagename) throws NameNotFoundException{
\r
49 this.packagename = packagename;
\r
50 resources = ctx.getPackageManager().getResourcesForApplication(packagename);
\r
53 public MimeTypes fromXml(InputStream in)
\r
54 throws XmlPullParserException, IOException {
\r
55 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
\r
57 mXpp = factory.newPullParser();
\r
58 mXpp.setInput(new InputStreamReader(in));
\r
63 public MimeTypes fromXmlResource(XmlResourceParser in)
\r
64 throws XmlPullParserException, IOException {
\r
70 public MimeTypes parse()
\r
71 throws XmlPullParserException, IOException {
\r
73 mMimeTypes = new MimeTypes();
\r
75 int eventType = mXpp.getEventType();
\r
77 while (eventType != XmlPullParser.END_DOCUMENT) {
\r
78 String tag = mXpp.getName();
\r
80 if (eventType == XmlPullParser.START_TAG) {
\r
81 if (tag.equals(TAG_MIMETYPES)) {
\r
83 } else if (tag.equals(TAG_TYPE)) {
\r
86 } else if (eventType == XmlPullParser.END_TAG) {
\r
87 if (tag.equals(TAG_MIMETYPES)) {
\r
92 eventType = mXpp.next();
\r
98 private void addMimeTypeStart() {
\r
99 String extension = mXpp.getAttributeValue(null, ATTR_EXTENSION);
\r
100 String mimetype = mXpp.getAttributeValue(null, ATTR_MIMETYPE);
\r
101 String icon = mXpp.getAttributeValue(null, ATTR_ICON);
\r
104 int id = resources.getIdentifier(icon.substring(1) /* to cut the @ */, null, packagename);
\r
106 mMimeTypes.put(extension, mimetype, id);
\r
111 mMimeTypes.put(extension, mimetype);
\r