The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 android.util; |
| 18 | |
| 19 | import org.xml.sax.ContentHandler; |
| 20 | import org.xml.sax.InputSource; |
| 21 | import org.xml.sax.SAXException; |
| 22 | import org.xml.sax.XMLReader; |
| 23 | import org.xmlpull.v1.XmlPullParser; |
| 24 | import org.xmlpull.v1.XmlSerializer; |
| 25 | import org.xmlpull.v1.XmlPullParserException; |
| 26 | import org.xmlpull.v1.XmlPullParserFactory; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.io.InputStream; |
| 30 | import java.io.Reader; |
| 31 | import java.io.StringReader; |
| 32 | import java.io.UnsupportedEncodingException; |
| 33 | |
| 34 | import org.apache.harmony.xml.ExpatPullParser; |
| 35 | import org.apache.harmony.xml.ExpatReader; |
| 36 | |
| 37 | /** |
| 38 | * XML utility methods. |
| 39 | */ |
| 40 | public class Xml { |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 41 | /** @hide */ public Xml() {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * {@link org.xmlpull.v1.XmlPullParser} "relaxed" feature name. |
| 45 | * |
| 46 | * @see <a href="http://xmlpull.org/v1/doc/features.html#relaxed"> |
| 47 | * specification</a> |
| 48 | */ |
| 49 | public static String FEATURE_RELAXED = ExpatPullParser.FEATURE_RELAXED; |
| 50 | |
| 51 | /** |
| 52 | * Parses the given xml string and fires events on the given SAX handler. |
| 53 | */ |
| 54 | public static void parse(String xml, ContentHandler contentHandler) |
| 55 | throws SAXException { |
| 56 | try { |
| 57 | XMLReader reader = new ExpatReader(); |
| 58 | reader.setContentHandler(contentHandler); |
| 59 | reader.parse(new InputSource(new StringReader(xml))); |
| 60 | } |
| 61 | catch (IOException e) { |
| 62 | throw new AssertionError(e); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Parses xml from the given reader and fires events on the given SAX |
| 68 | * handler. |
| 69 | */ |
| 70 | public static void parse(Reader in, ContentHandler contentHandler) |
| 71 | throws IOException, SAXException { |
| 72 | XMLReader reader = new ExpatReader(); |
| 73 | reader.setContentHandler(contentHandler); |
| 74 | reader.parse(new InputSource(in)); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Parses xml from the given input stream and fires events on the given SAX |
| 79 | * handler. |
| 80 | */ |
| 81 | public static void parse(InputStream in, Encoding encoding, |
| 82 | ContentHandler contentHandler) throws IOException, SAXException { |
Tom O'Neill | 28e3f10 | 2010-11-05 10:14:24 -0700 | [diff] [blame] | 83 | XMLReader reader = new ExpatReader(); |
| 84 | reader.setContentHandler(contentHandler); |
| 85 | InputSource source = new InputSource(in); |
| 86 | source.setEncoding(encoding.expatName); |
| 87 | reader.parse(source); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Creates a new pull parser with namespace support. |
| 92 | * |
| 93 | * <p><b>Note:</b> This is actually slower than the SAX parser, and it's not |
| 94 | * fully implemented. If you need a fast, mostly implemented pull parser, |
| 95 | * use this. If you need a complete implementation, use KXML. |
| 96 | */ |
| 97 | public static XmlPullParser newPullParser() { |
| 98 | ExpatPullParser parser = new ExpatPullParser(); |
| 99 | parser.setNamespaceProcessingEnabled(true); |
| 100 | return parser; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Creates a new xml serializer. |
| 105 | */ |
| 106 | public static XmlSerializer newSerializer() { |
| 107 | try { |
| 108 | return XmlSerializerFactory.instance.newSerializer(); |
| 109 | } catch (XmlPullParserException e) { |
| 110 | throw new AssertionError(e); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** Factory for xml serializers. Initialized on demand. */ |
| 115 | static class XmlSerializerFactory { |
| 116 | static final String TYPE |
| 117 | = "org.kxml2.io.KXmlParser,org.kxml2.io.KXmlSerializer"; |
| 118 | static final XmlPullParserFactory instance; |
| 119 | static { |
| 120 | try { |
| 121 | instance = XmlPullParserFactory.newInstance(TYPE, null); |
| 122 | } catch (XmlPullParserException e) { |
| 123 | throw new AssertionError(e); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Supported character encodings. |
| 130 | */ |
| 131 | public enum Encoding { |
| 132 | |
| 133 | US_ASCII("US-ASCII"), |
| 134 | UTF_8("UTF-8"), |
| 135 | UTF_16("UTF-16"), |
| 136 | ISO_8859_1("ISO-8859-1"); |
| 137 | |
| 138 | final String expatName; |
| 139 | |
| 140 | Encoding(String expatName) { |
| 141 | this.expatName = expatName; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Finds an encoding by name. Returns UTF-8 if you pass {@code null}. |
| 147 | */ |
| 148 | public static Encoding findEncodingByName(String encodingName) |
| 149 | throws UnsupportedEncodingException { |
| 150 | if (encodingName == null) { |
| 151 | return Encoding.UTF_8; |
| 152 | } |
| 153 | |
| 154 | for (Encoding encoding : Encoding.values()) { |
| 155 | if (encoding.expatName.equalsIgnoreCase(encodingName)) |
| 156 | return encoding; |
| 157 | } |
| 158 | throw new UnsupportedEncodingException(encodingName); |
| 159 | } |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | /** |
| 162 | * Return an AttributeSet interface for use with the given XmlPullParser. |
| 163 | * If the given parser itself implements AttributeSet, that implementation |
| 164 | * is simply returned. Otherwise a wrapper class is |
| 165 | * instantiated on top of the XmlPullParser, as a proxy for retrieving its |
| 166 | * attributes, and returned to you. |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 167 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | * @param parser The existing parser for which you would like an |
| 169 | * AttributeSet. |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 170 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | * @return An AttributeSet you can use to retrieve the |
| 172 | * attribute values at each of the tags as the parser moves |
| 173 | * through its XML document. |
Jesse Wilson | a0f8bc5 | 2011-02-24 10:44:33 -0800 | [diff] [blame] | 174 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | * @see AttributeSet |
| 176 | */ |
| 177 | public static AttributeSet asAttributeSet(XmlPullParser parser) { |
| 178 | return (parser instanceof AttributeSet) |
| 179 | ? (AttributeSet) parser |
| 180 | : new XmlPullAttributes(parser); |
| 181 | } |
| 182 | } |