blob: e3b8fec3559eba8384c066e778b3b7d82ae88ba2 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.util;
18
Neil Fuller2f5a6892018-07-10 18:41:19 +010019import libcore.util.XmlObjectFactory;
20
Brian Carlstrom08065b92011-04-01 15:49:41 -070021import org.xml.sax.ContentHandler;
22import org.xml.sax.InputSource;
23import org.xml.sax.SAXException;
24import org.xml.sax.XMLReader;
25import org.xmlpull.v1.XmlPullParser;
26import org.xmlpull.v1.XmlPullParserException;
Brian Carlstrom08065b92011-04-01 15:49:41 -070027import org.xmlpull.v1.XmlSerializer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Neil Fuller2f5a6892018-07-10 18:41:19 +010029import java.io.IOException;
30import java.io.InputStream;
31import java.io.Reader;
32import java.io.StringReader;
33import java.io.UnsupportedEncodingException;
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035/**
36 * XML utility methods.
37 */
38public class Xml {
Neil Fuller2f5a6892018-07-10 18:41:19 +010039 private Xml() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
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 Carlstrom08065b92011-04-01 15:49:41 -070047 public static String FEATURE_RELAXED = "http://xmlpull.org/v1/doc/features.html#relaxed";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
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 Fuller2f5a6892018-07-10 18:41:19 +010055 XMLReader reader = XmlObjectFactory.newXMLReader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 reader.setContentHandler(contentHandler);
57 reader.parse(new InputSource(new StringReader(xml)));
Brian Carlstrom08065b92011-04-01 15:49:41 -070058 } catch (IOException e) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 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 Fuller2f5a6892018-07-10 18:41:19 +010069 XMLReader reader = XmlObjectFactory.newXMLReader();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 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 Fuller2f5a6892018-07-10 18:41:19 +010080 XMLReader reader = XmlObjectFactory.newXMLReader();
Tom O'Neill28e3f102010-11-05 10:14:24 -070081 reader.setContentHandler(contentHandler);
82 InputSource source = new InputSource(in);
83 source.setEncoding(encoding.expatName);
84 reader.parse(source);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 }
86
87 /**
Brian Carlstrom08065b92011-04-01 15:49:41 -070088 * Returns a new pull parser with namespace support.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 */
90 public static XmlPullParser newPullParser() {
Brian Carlstrom08065b92011-04-01 15:49:41 -070091 try {
Neil Fuller2f5a6892018-07-10 18:41:19 +010092 XmlPullParser parser = XmlObjectFactory.newXmlPullParser();
Brian Carlstrom08065b92011-04-01 15:49:41 -070093 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 Project9066cfe2009-03-03 19:31:44 -080099 }
100
101 /**
102 * Creates a new xml serializer.
103 */
104 public static XmlSerializer newSerializer() {
Neil Fuller2f5a6892018-07-10 18:41:19 +0100105 return XmlObjectFactory.newXmlSerializer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 }
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 Wilsona0f8bc52011-02-24 10:44:33 -0800140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 /**
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 Wilsona0f8bc52011-02-24 10:44:33 -0800147 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 * @param parser The existing parser for which you would like an
149 * AttributeSet.
Jesse Wilsona0f8bc52011-02-24 10:44:33 -0800150 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 * @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 Wilsona0f8bc52011-02-24 10:44:33 -0800154 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 * @see AttributeSet
156 */
157 public static AttributeSet asAttributeSet(XmlPullParser parser) {
158 return (parser instanceof AttributeSet)
159 ? (AttributeSet) parser
160 : new XmlPullAttributes(parser);
161 }
162}