blob: b0c33e5fca61b25fbd87cd948f2a556534744cef [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
19import org.xml.sax.ContentHandler;
20import org.xml.sax.InputSource;
21import org.xml.sax.SAXException;
22import org.xml.sax.XMLReader;
23import org.xmlpull.v1.XmlPullParser;
24import org.xmlpull.v1.XmlSerializer;
25import org.xmlpull.v1.XmlPullParserException;
26import org.xmlpull.v1.XmlPullParserFactory;
27
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.Reader;
31import java.io.StringReader;
32import java.io.UnsupportedEncodingException;
33
34import org.apache.harmony.xml.ExpatPullParser;
35import org.apache.harmony.xml.ExpatReader;
36
37/**
38 * XML utility methods.
39 */
40public class Xml {
Jesse Wilsona0f8bc52011-02-24 10:44:33 -080041 /** @hide */ public Xml() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
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'Neill28e3f102010-11-05 10:14:24 -070083 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 Project9066cfe2009-03-03 19:31:44 -080088 }
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 Wilsona0f8bc52011-02-24 10:44:33 -0800160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 /**
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 Wilsona0f8bc52011-02-24 10:44:33 -0800167 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 * @param parser The existing parser for which you would like an
169 * AttributeSet.
Jesse Wilsona0f8bc52011-02-24 10:44:33 -0800170 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 * @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 Wilsona0f8bc52011-02-24 10:44:33 -0800174 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 * @see AttributeSet
176 */
177 public static AttributeSet asAttributeSet(XmlPullParser parser) {
178 return (parser instanceof AttributeSet)
179 ? (AttributeSet) parser
180 : new XmlPullAttributes(parser);
181 }
182}