summaryrefslogtreecommitdiffstats
path: root/xml
diff options
context:
space:
mode:
authorNarayan Kamath <narayan@google.com>2013-10-16 12:13:01 +0100
committerNarayan Kamath <narayan@google.com>2013-10-17 17:06:00 +0100
commit0363556bad8930a9d70683a0d02b297a561e63fa (patch)
tree327c0b0819f03d6751b7dd2fce0e163dcf4d88de /xml
parent3a74682b3c66f3a5fc377ebbaf14c94767220d33 (diff)
downloadlibcore-0363556bad8930a9d70683a0d02b297a561e63fa.zip
libcore-0363556bad8930a9d70683a0d02b297a561e63fa.tar.gz
libcore-0363556bad8930a9d70683a0d02b297a561e63fa.tar.bz2
Simplify code in XmlPullParserFactory.
Since we're hard coding KXmlParser and KXmlSerializer anyway, we might as well do it more directly. Also add a trivial unit test for this method. NOTE: This change preserves existing behaviour and doesn't throw from #newInstance if the parser class couldn't be found or instantiated. We instead throw from newPullParser or newSerializer as appropriate. Change-Id: Ic0c86e67691b8e662d95363bbe597d3abb7c3642
Diffstat (limited to 'xml')
-rw-r--r--xml/src/main/java/org/xmlpull/v1/XmlPullParserFactory.java227
1 files changed, 25 insertions, 202 deletions
diff --git a/xml/src/main/java/org/xmlpull/v1/XmlPullParserFactory.java b/xml/src/main/java/org/xmlpull/v1/XmlPullParserFactory.java
index 4375a41..d08c342 100644
--- a/xml/src/main/java/org/xmlpull/v1/XmlPullParserFactory.java
+++ b/xml/src/main/java/org/xmlpull/v1/XmlPullParserFactory.java
@@ -3,28 +3,14 @@
package org.xmlpull.v1;
-import java.io.InputStream;
-import java.util.ArrayList;
+import org.kxml2.io.KXmlParser;
+import org.kxml2.io.KXmlSerializer;
+
import java.util.HashMap;
-import java.util.Iterator;
+import java.util.Map;
/**
* This class is used to create implementations of XML Pull Parser defined in XMPULL V1 API.
- * The name of actual factory class will be determined based on several parameters.
- * It works similar to JAXP but tailored to work in J2ME environments
- * (no access to system properties or file system) so name of parser class factory to use
- * and its class used for loading (no class loader - on J2ME no access to context class loaders)
- * must be passed explicitly. If no name of parser factory was passed (or is null)
- * it will try to find name by searching in CLASSPATH for
- * META-INF/services/org.xmlpull.v1.XmlPullParserFactory resource that should contain
- * a comma separated list of class names of factories or parsers to try (in order from
- * left to the right). If none found, it will throw an exception.
- *
- * <br /><strong>NOTE:</strong>In J2SE or J2EE environments, you may want to use
- * <code>newInstance(property, classLoaderCtx)</code>
- * where first argument is
- * <code>System.getProperty(XmlPullParserFactory.PROPERTY_NAME)</code>
- * and second is <code>Thread.getContextClassLoader().getClass()</code> .
*
* @see XmlPullParser
*
@@ -33,40 +19,13 @@ import java.util.Iterator;
*/
public class XmlPullParserFactory {
- /** used as default class to server as context class in newInstance() */
- final static Class referenceContextClass;
-
- static {
- XmlPullParserFactory f = new XmlPullParserFactory();
- referenceContextClass = f.getClass();
- }
-
- /** Name of the system or midlet property that should be used for
- a system property containing a comma separated list of factory
- or parser class names (value:
- org.xmlpull.v1.XmlPullParserFactory). */
-
-
+ /** Currently unused. */
+ // TODO: Deprecate or remove this field.
public static final String PROPERTY_NAME =
"org.xmlpull.v1.XmlPullParserFactory";
- private static final String RESOURCE_NAME =
- "/META-INF/services/" + PROPERTY_NAME;
-
-
- // public static final String DEFAULT_PROPERTY =
- // "org.xmlpull.xpp3.XmlPullParser,org.kxml2.io.KXmlParser";
-
-
- protected ArrayList parserClasses;
- protected String classNamesLocation;
-
- protected ArrayList serializerClasses;
-
-
// features are kept there
- protected HashMap features = new HashMap();
-
+ protected final HashMap<String, Boolean> features = new HashMap<String, Boolean>();
/**
* Protected constructor to be called by factory implementations.
@@ -100,7 +59,7 @@ public class XmlPullParserFactory {
*/
public boolean getFeature (String name) {
- Boolean value = (Boolean) features.get(name);
+ Boolean value = features.get(name);
return value != null ? value.booleanValue() : false;
}
@@ -161,40 +120,15 @@ public class XmlPullParserFactory {
* using the currently configured factory features.
*
* @return A new instance of a XML Pull Parser.
- * @throws XmlPullParserException if a parser cannot be created which satisfies the
- * requested configuration.
*/
public XmlPullParser newPullParser() throws XmlPullParserException {
-
- if (parserClasses == null) throw new XmlPullParserException
- ("Factory initialization was incomplete - has not tried "+classNamesLocation);
-
- if (parserClasses.size() == 0) throw new XmlPullParserException
- ("No valid parser classes found in "+classNamesLocation);
-
- final StringBuilder issues = new StringBuilder();
-
- for (int i = 0; i < parserClasses.size(); i++) {
- final Class ppClass = (Class) parserClasses.get(i);
- try {
- final XmlPullParser pp = (XmlPullParser) ppClass.newInstance();
-
- for (Iterator iter = features.keySet().iterator(); iter.hasNext(); ) {
- final String key = (String) iter.next();
- final Boolean value = (Boolean) features.get(key);
- if(value != null && value.booleanValue()) {
- pp.setFeature(key, true);
- }
- }
- return pp;
-
- } catch(Exception ex) {
- issues.append (ppClass.getName () + ": "+ ex.toString ()+"; ");
- }
+ final XmlPullParser pp = new KXmlParser();
+ for (Map.Entry<String, Boolean> entry : features.entrySet()) {
+ pp.setFeature(entry.getKey(), entry.getValue());
}
- throw new XmlPullParserException ("could not create parser: "+issues);
+ return pp;
}
@@ -209,137 +143,26 @@ public class XmlPullParserFactory {
*/
public XmlSerializer newSerializer() throws XmlPullParserException {
-
- if (serializerClasses == null) {
- throw new XmlPullParserException
- ("Factory initialization incomplete - has not tried "+classNamesLocation);
- }
- if(serializerClasses.size() == 0) {
- throw new XmlPullParserException
- ("No valid serializer classes found in "+classNamesLocation);
- }
-
- final StringBuilder issues = new StringBuilder ();
-
- for (int i = 0; i < serializerClasses.size (); i++) {
- final Class ppClass = (Class) serializerClasses.get(i);
- try {
- final XmlSerializer ser = (XmlSerializer) ppClass.newInstance();
-
- return ser;
-
- } catch(Exception ex) {
- issues.append (ppClass.getName () + ": "+ ex.toString ()+"; ");
- }
- }
-
- throw new XmlPullParserException ("could not create serializer: "+issues);
+ return new KXmlSerializer();
}
/**
- * Create a new instance of a PullParserFactory that can be used
- * to create XML pull parsers (see class description for more
- * details).
- *
- * @return a new instance of a PullParserFactory, as returned by newInstance (null, null);
+ * Creates a new instance of a PullParserFactory that can be used
+ * to create XML pull parsers. The factory will always return instances
+ * of {@link KXmlParser} and {@link KXmlSerializer}.
*/
public static XmlPullParserFactory newInstance () throws XmlPullParserException {
- return newInstance(null, null);
+ return new XmlPullParserFactory();
}
- public static XmlPullParserFactory newInstance (String classNames, Class context)
+ /**
+ * Creates a factory that always returns instances of of {@link KXmlParser} and
+ * {@link KXmlSerializer}. This <b>does not</b> support factories capable of
+ * creating arbitrary parser and serializer implementations. Both arguments to this
+ * method are unused.
+ */
+ public static XmlPullParserFactory newInstance (String unused, Class unused2)
throws XmlPullParserException {
-
- /*
- if (context == null) {
- //NOTE: make sure context uses the same class loader as API classes
- // this is the best we can do without having access to context classloader in J2ME
- // if API is in the same classloader as implementation then this will work
- context = referenceContextClass;
- }
-
- String classNamesLocation = null;
-
- if (classNames == null || classNames.length() == 0 || "DEFAULT".equals(classNames)) {
- try {
- InputStream is = context.getResourceAsStream (RESOURCE_NAME);
-
- if (is == null) throw new XmlPullParserException
- ("resource not found: "+RESOURCE_NAME
- +" make sure that parser implementing XmlPull API is available");
- final StringBuilder sb = new StringBuilder();
-
- while (true) {
- final int ch = is.read();
- if (ch < 0) break;
- else if (ch > ' ')
- sb.append((char) ch);
- }
- is.close ();
-
- classNames = sb.toString ();
- }
- catch (Exception e) {
- throw new XmlPullParserException (null, null, e);
- }
- classNamesLocation = "resource "+RESOURCE_NAME+" that contained '"+classNames+"'";
- } else {
- classNamesLocation =
- "parameter classNames to newInstance() that contained '"+classNames+"'";
- }
- */
- classNames = "org.kxml2.io.KXmlParser,org.kxml2.io.KXmlSerializer";
-
- XmlPullParserFactory factory = null;
- final ArrayList parserClasses = new ArrayList();
- final ArrayList serializerClasses = new ArrayList();
- int pos = 0;
-
- while (pos < classNames.length ()) {
- int cut = classNames.indexOf (',', pos);
-
- if (cut == -1) cut = classNames.length ();
- final String name = classNames.substring (pos, cut);
-
- Class candidate = null;
- Object instance = null;
-
- try {
- candidate = Class.forName (name);
- // necessary because of J2ME .class issue
- instance = candidate.newInstance ();
- }
- catch (Exception e) {}
-
- if (candidate != null) {
- boolean recognized = false;
- if (instance instanceof XmlPullParser) {
- parserClasses.add(candidate);
- recognized = true;
- }
- if (instance instanceof XmlSerializer) {
- serializerClasses.add(candidate);
- recognized = true;
- }
- if (instance instanceof XmlPullParserFactory) {
- if (factory == null) {
- factory = (XmlPullParserFactory) instance;
- }
- recognized = true;
- }
- if (!recognized) {
- throw new XmlPullParserException ("incompatible class: "+name);
- }
- }
- pos = cut + 1;
- }
-
- if (factory == null) {
- factory = new XmlPullParserFactory ();
- }
- factory.parserClasses = parserClasses;
- factory.serializerClasses = serializerClasses;
- factory.classNamesLocation = "org.kxml2.io.kXmlParser,org.kxml2.io.KXmlSerializer";
- return factory;
+ return newInstance();
}
}