--- /dev/null
+/* \r
+ * Copyright (C) 2008 OpenIntents.org\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.openintents.filemanager.util;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.InputStreamReader;\r
+\r
+import org.xmlpull.v1.XmlPullParser;\r
+import org.xmlpull.v1.XmlPullParserException;\r
+import org.xmlpull.v1.XmlPullParserFactory;\r
+\r
+import android.content.Context;\r
+import android.content.pm.PackageManager.NameNotFoundException;\r
+import android.content.res.Resources;\r
+import android.content.res.XmlResourceParser;\r
+\r
+public class MimeTypeParser {\r
+\r
+ private static final String LOG_TAG = "MimeTypeParser";\r
+\r
+ public static final String TAG_MIMETYPES = "MimeTypes";\r
+ public static final String TAG_TYPE = "type";\r
+ \r
+ public static final String ATTR_EXTENSION = "extension";\r
+ public static final String ATTR_MIMETYPE = "mimetype";\r
+ public static final String ATTR_ICON = "icon";\r
+ \r
+ private XmlPullParser mXpp;\r
+ private MimeTypes mMimeTypes;\r
+ private Resources resources;\r
+ private String packagename;\r
+ \r
+ public MimeTypeParser(Context ctx, String packagename) throws NameNotFoundException{\r
+ this.packagename = packagename;\r
+ resources = ctx.getPackageManager().getResourcesForApplication(packagename);\r
+ }\r
+ \r
+ public MimeTypes fromXml(InputStream in)\r
+ throws XmlPullParserException, IOException {\r
+ XmlPullParserFactory factory = XmlPullParserFactory.newInstance();\r
+\r
+ mXpp = factory.newPullParser();\r
+ mXpp.setInput(new InputStreamReader(in));\r
+\r
+ return parse();\r
+ }\r
+ \r
+ public MimeTypes fromXmlResource(XmlResourceParser in)\r
+ throws XmlPullParserException, IOException {\r
+ mXpp = in;\r
+ \r
+ return parse();\r
+ }\r
+\r
+ public MimeTypes parse()\r
+ throws XmlPullParserException, IOException {\r
+ \r
+ mMimeTypes = new MimeTypes();\r
+ \r
+ int eventType = mXpp.getEventType();\r
+\r
+ while (eventType != XmlPullParser.END_DOCUMENT) {\r
+ String tag = mXpp.getName();\r
+\r
+ if (eventType == XmlPullParser.START_TAG) {\r
+ if (tag.equals(TAG_MIMETYPES)) {\r
+ \r
+ } else if (tag.equals(TAG_TYPE)) {\r
+ addMimeTypeStart();\r
+ }\r
+ } else if (eventType == XmlPullParser.END_TAG) {\r
+ if (tag.equals(TAG_MIMETYPES)) {\r
+ \r
+ }\r
+ }\r
+\r
+ eventType = mXpp.next();\r
+ }\r
+\r
+ return mMimeTypes;\r
+ }\r
+ \r
+ private void addMimeTypeStart() {\r
+ String extension = mXpp.getAttributeValue(null, ATTR_EXTENSION);\r
+ String mimetype = mXpp.getAttributeValue(null, ATTR_MIMETYPE);\r
+ String icon = mXpp.getAttributeValue(null, ATTR_ICON);\r
+ \r
+ if(icon != null){\r
+ int id = resources.getIdentifier(icon.substring(1) /* to cut the @ */, null, packagename);\r
+ if(id > 0){\r
+ mMimeTypes.put(extension, mimetype, id);\r
+ return;\r
+ }\r
+ }\r
+ \r
+ mMimeTypes.put(extension, mimetype);\r
+ }\r
+ \r
+}\r