blob: 32e4f200e888f1e2eb49b3930cbde177e428363f [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/* -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- //------100-columns-wide------>|*/
2// for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
3
4package org.xmlpull.v1;
5
Narayan Kamath03635562013-10-16 12:13:01 +01006import org.kxml2.io.KXmlParser;
7import org.kxml2.io.KXmlSerializer;
8
Narayan Kamathabd0b0a2013-10-18 10:06:55 +01009import java.util.ArrayList;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080010import java.util.HashMap;
Narayan Kamath03635562013-10-16 12:13:01 +010011import java.util.Map;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080012
13/**
14 * This class is used to create implementations of XML Pull Parser defined in XMPULL V1 API.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080015 *
16 * @see XmlPullParser
17 *
18 * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
19 * @author Stefan Haustein
20 */
21
22public class XmlPullParserFactory {
Narayan Kamathabd0b0a2013-10-18 10:06:55 +010023 // TODO: Deprecate or remove these fields. They're currently unused
24 // but are public APIs.
25
Narayan Kamath03635562013-10-16 12:13:01 +010026 /** Currently unused. */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080027 public static final String PROPERTY_NAME =
28 "org.xmlpull.v1.XmlPullParserFactory";
Narayan Kamathabd0b0a2013-10-18 10:06:55 +010029 /** Currently unused */
30 protected String classNamesLocation = null;
31 /** Currently unused */
32 protected ArrayList parserClasses = null;
33 /** Currently unused */
34 protected ArrayList serializerClasses = null;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080035
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080036 // features are kept there
Narayan Kamathabd0b0a2013-10-18 10:06:55 +010037 // TODO: This can't be made final because it's a public API.
38 protected HashMap<String, Boolean> features = new HashMap<String, Boolean>();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080039
40 /**
41 * Protected constructor to be called by factory implementations.
42 */
43
44 protected XmlPullParserFactory() {
45 }
46
47
48
49 /**
50 * Set the features to be set when XML Pull Parser is created by this factory.
51 * <p><b>NOTE:</b> factory features are not used for XML Serializer.
52 *
53 * @param name string with URI identifying feature
54 * @param state if true feature will be set; if false will be ignored
55 */
56
Elliott Hughes9c324212011-02-24 17:58:33 -080057 public void setFeature(String name, boolean state) throws XmlPullParserException {
58 features.put(name, state);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080059 }
60
61
62 /**
63 * Return the current value of the feature with given name.
64 * <p><b>NOTE:</b> factory features are not used for XML Serializer.
65 *
66 * @param name The name of feature to be retrieved.
67 * @return The value of named feature.
68 * Unknown features are <string>always</strong> returned as false
69 */
70
71 public boolean getFeature (String name) {
Narayan Kamath03635562013-10-16 12:13:01 +010072 Boolean value = features.get(name);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080073 return value != null ? value.booleanValue() : false;
74 }
75
76 /**
77 * Specifies that the parser produced by this factory will provide
78 * support for XML namespaces.
79 * By default the value of this is set to false.
80 *
81 * @param awareness true if the parser produced by this code
82 * will provide support for XML namespaces; false otherwise.
83 */
84
85 public void setNamespaceAware(boolean awareness) {
Elliott Hughes9c324212011-02-24 17:58:33 -080086 features.put (XmlPullParser.FEATURE_PROCESS_NAMESPACES, awareness);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080087 }
88
89 /**
90 * Indicates whether or not the factory is configured to produce
91 * parsers which are namespace aware
92 * (it simply set feature XmlPullParser.FEATURE_PROCESS_NAMESPACES to true or false).
93 *
94 * @return true if the factory is configured to produce parsers
95 * which are namespace aware; false otherwise.
96 */
97
98 public boolean isNamespaceAware() {
99 return getFeature (XmlPullParser.FEATURE_PROCESS_NAMESPACES);
100 }
101
102
103 /**
104 * Specifies that the parser produced by this factory will be validating
105 * (it simply set feature XmlPullParser.FEATURE_VALIDATION to true or false).
106 *
107 * By default the value of this is set to false.
108 *
109 * @param validating - if true the parsers created by this factory must be validating.
110 */
111
112 public void setValidating(boolean validating) {
Elliott Hughes9c324212011-02-24 17:58:33 -0800113 features.put (XmlPullParser.FEATURE_VALIDATION, validating);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800114 }
115
116 /**
117 * Indicates whether or not the factory is configured to produce parsers
118 * which validate the XML content during parse.
119 *
120 * @return true if the factory is configured to produce parsers
121 * which validate the XML content during parse; false otherwise.
122 */
123
124 public boolean isValidating() {
125 return getFeature (XmlPullParser.FEATURE_VALIDATION);
126 }
127
128 /**
129 * Creates a new instance of a XML Pull Parser
130 * using the currently configured factory features.
131 *
132 * @return A new instance of a XML Pull Parser.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800133 */
134
135 public XmlPullParser newPullParser() throws XmlPullParserException {
Narayan Kamath03635562013-10-16 12:13:01 +0100136 final XmlPullParser pp = new KXmlParser();
137 for (Map.Entry<String, Boolean> entry : features.entrySet()) {
138 pp.setFeature(entry.getKey(), entry.getValue());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800139 }
140
Narayan Kamath03635562013-10-16 12:13:01 +0100141 return pp;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800142 }
143
144
145 /**
146 * Creates a new instance of a XML Serializer.
147 *
148 * <p><b>NOTE:</b> factory features are not used for XML Serializer.
149 *
150 * @return A new instance of a XML Serializer.
151 * @throws XmlPullParserException if a parser cannot be created which satisfies the
152 * requested configuration.
153 */
154
155 public XmlSerializer newSerializer() throws XmlPullParserException {
Narayan Kamath03635562013-10-16 12:13:01 +0100156 return new KXmlSerializer();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800157 }
158
159 /**
Narayan Kamath03635562013-10-16 12:13:01 +0100160 * Creates a new instance of a PullParserFactory that can be used
161 * to create XML pull parsers. The factory will always return instances
162 * of {@link KXmlParser} and {@link KXmlSerializer}.
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800163 */
164 public static XmlPullParserFactory newInstance () throws XmlPullParserException {
Narayan Kamath03635562013-10-16 12:13:01 +0100165 return new XmlPullParserFactory();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800166 }
167
Narayan Kamath03635562013-10-16 12:13:01 +0100168 /**
169 * Creates a factory that always returns instances of of {@link KXmlParser} and
170 * {@link KXmlSerializer}. This <b>does not</b> support factories capable of
171 * creating arbitrary parser and serializer implementations. Both arguments to this
172 * method are unused.
173 */
174 public static XmlPullParserFactory newInstance (String unused, Class unused2)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800175 throws XmlPullParserException {
Narayan Kamath03635562013-10-16 12:13:01 +0100176 return newInstance();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800177 }
178}