811a5a4a |
1 | /* \r |
2 | * Copyright (C) 2008 OpenIntents.org\r |
3 | *\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 |
7 | *\r |
8 | * http://www.apache.org/licenses/LICENSE-2.0\r |
9 | *\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 |
15 | */\r |
16 | \r |
17 | package org.openintents.filemanager.util;\r |
18 | \r |
19 | import java.io.IOException;\r |
20 | import java.io.InputStream;\r |
21 | import java.io.InputStreamReader;\r |
22 | \r |
23 | import org.xmlpull.v1.XmlPullParser;\r |
24 | import org.xmlpull.v1.XmlPullParserException;\r |
25 | import org.xmlpull.v1.XmlPullParserFactory;\r |
26 | \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 |
31 | \r |
32 | public class MimeTypeParser {\r |
33 | \r |
34 | private static final String LOG_TAG = "MimeTypeParser";\r |
35 | \r |
36 | public static final String TAG_MIMETYPES = "MimeTypes";\r |
37 | public static final String TAG_TYPE = "type";\r |
38 | \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 |
42 | \r |
43 | private XmlPullParser mXpp;\r |
44 | private MimeTypes mMimeTypes;\r |
45 | private Resources resources;\r |
46 | private String packagename;\r |
47 | \r |
48 | public MimeTypeParser(Context ctx, String packagename) throws NameNotFoundException{\r |
49 | this.packagename = packagename;\r |
50 | resources = ctx.getPackageManager().getResourcesForApplication(packagename);\r |
51 | }\r |
52 | \r |
53 | public MimeTypes fromXml(InputStream in)\r |
54 | throws XmlPullParserException, IOException {\r |
55 | XmlPullParserFactory factory = XmlPullParserFactory.newInstance();\r |
56 | \r |
57 | mXpp = factory.newPullParser();\r |
58 | mXpp.setInput(new InputStreamReader(in));\r |
59 | \r |
60 | return parse();\r |
61 | }\r |
62 | \r |
63 | public MimeTypes fromXmlResource(XmlResourceParser in)\r |
64 | throws XmlPullParserException, IOException {\r |
65 | mXpp = in;\r |
66 | \r |
67 | return parse();\r |
68 | }\r |
69 | \r |
70 | public MimeTypes parse()\r |
71 | throws XmlPullParserException, IOException {\r |
72 | \r |
73 | mMimeTypes = new MimeTypes();\r |
74 | \r |
75 | int eventType = mXpp.getEventType();\r |
76 | \r |
77 | while (eventType != XmlPullParser.END_DOCUMENT) {\r |
78 | String tag = mXpp.getName();\r |
79 | \r |
80 | if (eventType == XmlPullParser.START_TAG) {\r |
81 | if (tag.equals(TAG_MIMETYPES)) {\r |
82 | \r |
83 | } else if (tag.equals(TAG_TYPE)) {\r |
84 | addMimeTypeStart();\r |
85 | }\r |
86 | } else if (eventType == XmlPullParser.END_TAG) {\r |
87 | if (tag.equals(TAG_MIMETYPES)) {\r |
88 | \r |
89 | }\r |
90 | }\r |
91 | \r |
92 | eventType = mXpp.next();\r |
93 | }\r |
94 | \r |
95 | return mMimeTypes;\r |
96 | }\r |
97 | \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 |
102 | \r |
103 | if(icon != null){\r |
104 | int id = resources.getIdentifier(icon.substring(1) /* to cut the @ */, null, packagename);\r |
105 | if(id > 0){\r |
106 | mMimeTypes.put(extension, mimetype, id);\r |
107 | return;\r |
108 | }\r |
109 | }\r |
110 | \r |
111 | mMimeTypes.put(extension, mimetype);\r |
112 | }\r |
113 | \r |
114 | }\r |