• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.gallery3d.exif;
18 
19 import org.xmlpull.v1.XmlPullParser;
20 import org.xmlpull.v1.XmlPullParserException;
21 
22 import java.io.IOException;
23 import java.util.HashMap;
24 
25 public class ExifXmlReader {
26 
27     private static final String XML_EXIF_TAG = "exif";
28     private static final String XML_IFD_TAG = "ifd";
29     private static final String XML_IFD_NAME = "name";
30     private static final String XML_TAG = "tag";
31     private static final String XML_IFD0 = "ifd0";
32     private static final String XML_IFD1 = "ifd1";
33     private static final String XML_EXIF_IFD = "exif-ifd";
34     private static final String XML_INTEROPERABILITY_IFD = "interoperability-ifd";
35     private static final String XML_TAG_ID = "id";
36 
readXml(XmlPullParser parser, HashMap<Short, String> ifd0, HashMap<Short, String> ifd1, HashMap<Short, String> exifIfd, HashMap<Short, String> interoperabilityIfd)37     public static void readXml(XmlPullParser parser, HashMap<Short, String> ifd0,
38             HashMap<Short, String> ifd1, HashMap<Short, String> exifIfd,
39             HashMap<Short, String> interoperabilityIfd) throws XmlPullParserException,
40             IOException {
41 
42         while (parser.next() != XmlPullParser.END_DOCUMENT) {
43             if (parser.getEventType() == XmlPullParser.START_TAG) {
44                 break;
45             }
46         }
47 
48         assert(parser.getName().equals(XML_EXIF_TAG));
49 
50         parser.require(XmlPullParser.START_TAG, null, XML_EXIF_TAG);
51         while (parser.next() != XmlPullParser.END_TAG) {
52             if (parser.getEventType() == XmlPullParser.START_TAG) {
53                 readXmlIfd(parser, ifd0, ifd1, exifIfd, interoperabilityIfd);
54             }
55         }
56         parser.require(XmlPullParser.END_TAG, null, XML_EXIF_TAG);
57     }
58 
readXmlIfd(XmlPullParser parser, HashMap<Short, String> ifd0, HashMap<Short, String> ifd1, HashMap<Short, String> exifIfd, HashMap<Short, String> interoperabilityIfd)59     private static void readXmlIfd(XmlPullParser parser, HashMap<Short, String> ifd0,
60             HashMap<Short, String> ifd1, HashMap<Short, String> exifIfd,
61             HashMap<Short, String> interoperabilityIfd) throws XmlPullParserException,
62             IOException {
63         parser.require(XmlPullParser.START_TAG, null, XML_IFD_TAG);
64         String name = parser.getAttributeValue(null, XML_IFD_NAME);
65         HashMap<Short, String> ifdData = null;
66         if (XML_IFD0.equals(name)) {
67             ifdData = ifd0;
68         } else if (XML_IFD1.equals(name)) {
69             ifdData = ifd1;
70         } else if (XML_EXIF_IFD.equals(name)) {
71             ifdData = exifIfd;
72         } else if (XML_INTEROPERABILITY_IFD.equals(name)) {
73             ifdData = interoperabilityIfd;
74         } else {
75             throw new RuntimeException("Unknown IFD name in xml file: " + name);
76         }
77         while (parser.next() != XmlPullParser.END_TAG) {
78             if (parser.getEventType() == XmlPullParser.START_TAG) {
79                 readXmlTag(parser, ifdData);
80             }
81         }
82         parser.require(XmlPullParser.END_TAG, null, XML_IFD_TAG);
83     }
84 
readXmlTag(XmlPullParser parser, HashMap<Short, String> data)85     private static void readXmlTag(XmlPullParser parser, HashMap<Short, String> data)
86         throws XmlPullParserException, IOException {
87         parser.require(XmlPullParser.START_TAG, null, XML_TAG);
88         short id = Integer.decode(parser.getAttributeValue(null, XML_TAG_ID)).shortValue();
89         String value = "";
90         if (parser.next() == XmlPullParser.TEXT) {
91             value = parser.getText();
92             parser.next();
93         }
94         data.put(id, value);
95         parser.require(XmlPullParser.END_TAG, null, XML_TAG);
96     }
97 }