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