The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 4 | package org.xmlpull.v1; |
| 5 | |
Narayan Kamath | 0363556 | 2013-10-16 12:13:01 +0100 | [diff] [blame] | 6 | import org.kxml2.io.KXmlParser; |
| 7 | import org.kxml2.io.KXmlSerializer; |
| 8 | |
Narayan Kamath | abd0b0a | 2013-10-18 10:06:55 +0100 | [diff] [blame] | 9 | import java.util.ArrayList; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 10 | import java.util.HashMap; |
Narayan Kamath | 0363556 | 2013-10-16 12:13:01 +0100 | [diff] [blame] | 11 | import java.util.Map; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * This class is used to create implementations of XML Pull Parser defined in XMPULL V1 API. |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 15 | * |
| 16 | * @see XmlPullParser |
| 17 | * |
| 18 | * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a> |
| 19 | * @author Stefan Haustein |
| 20 | */ |
| 21 | |
| 22 | public class XmlPullParserFactory { |
Narayan Kamath | abd0b0a | 2013-10-18 10:06:55 +0100 | [diff] [blame] | 23 | // TODO: Deprecate or remove these fields. They're currently unused |
| 24 | // but are public APIs. |
| 25 | |
Narayan Kamath | 0363556 | 2013-10-16 12:13:01 +0100 | [diff] [blame] | 26 | /** Currently unused. */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 27 | public static final String PROPERTY_NAME = |
| 28 | "org.xmlpull.v1.XmlPullParserFactory"; |
Narayan Kamath | abd0b0a | 2013-10-18 10:06:55 +0100 | [diff] [blame] | 29 | /** 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 35 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 36 | // features are kept there |
Narayan Kamath | abd0b0a | 2013-10-18 10:06:55 +0100 | [diff] [blame] | 37 | // 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 39 | |
| 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 Hughes | 9c32421 | 2011-02-24 17:58:33 -0800 | [diff] [blame] | 57 | public void setFeature(String name, boolean state) throws XmlPullParserException { |
| 58 | features.put(name, state); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 59 | } |
| 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 Kamath | 0363556 | 2013-10-16 12:13:01 +0100 | [diff] [blame] | 72 | Boolean value = features.get(name); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 73 | 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 Hughes | 9c32421 | 2011-02-24 17:58:33 -0800 | [diff] [blame] | 86 | features.put (XmlPullParser.FEATURE_PROCESS_NAMESPACES, awareness); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 87 | } |
| 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 Hughes | 9c32421 | 2011-02-24 17:58:33 -0800 | [diff] [blame] | 113 | features.put (XmlPullParser.FEATURE_VALIDATION, validating); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 114 | } |
| 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 133 | */ |
| 134 | |
| 135 | public XmlPullParser newPullParser() throws XmlPullParserException { |
Narayan Kamath | 0363556 | 2013-10-16 12:13:01 +0100 | [diff] [blame] | 136 | 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Narayan Kamath | 0363556 | 2013-10-16 12:13:01 +0100 | [diff] [blame] | 141 | return pp; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 142 | } |
| 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 Kamath | 0363556 | 2013-10-16 12:13:01 +0100 | [diff] [blame] | 156 | return new KXmlSerializer(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /** |
Narayan Kamath | 0363556 | 2013-10-16 12:13:01 +0100 | [diff] [blame] | 160 | * 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 163 | */ |
| 164 | public static XmlPullParserFactory newInstance () throws XmlPullParserException { |
Narayan Kamath | 0363556 | 2013-10-16 12:13:01 +0100 | [diff] [blame] | 165 | return new XmlPullParserFactory(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Narayan Kamath | 0363556 | 2013-10-16 12:13:01 +0100 | [diff] [blame] | 168 | /** |
| 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 175 | throws XmlPullParserException { |
Narayan Kamath | 0363556 | 2013-10-16 12:13:01 +0100 | [diff] [blame] | 176 | return newInstance(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 177 | } |
| 178 | } |