1 2 4 package org.xmlpull.v1; 5 6 import java.io.InputStream ; 7 import java.io.IOException ; 8 import java.io.Reader ; 9 import java.io.Writer ; 10 import java.util.Enumeration ; 11 import java.util.Hashtable ; 12 import java.util.Vector ; 13 14 37 38 public class XmlPullParserFactory { 39 40 44 45 46 public static final String PROPERTY_NAME = 47 "org.xmlpull.v1.XmlPullParserFactory"; 48 49 private static final String RESOURCE_NAME = 50 "/META-INF/services/" + PROPERTY_NAME; 51 52 53 56 57 protected Vector parserClasses; 58 protected String classNamesLocation; 59 60 protected Vector serializerClasses; 61 62 63 protected Hashtable features = new Hashtable (); 65 66 67 70 71 protected XmlPullParserFactory() { 72 } 73 74 75 76 83 84 public void setFeature(String name, 85 boolean state) throws XmlPullParserException { 86 87 features.put(name, new Boolean (state)); 88 } 89 90 91 99 100 public boolean getFeature (String name) { 101 Boolean value = (Boolean ) features.get(name); 102 return value != null ? value.booleanValue() : false; 103 } 104 105 113 114 public void setNamespaceAware(boolean awareness) { 115 features.put (XmlPullParser.FEATURE_PROCESS_NAMESPACES, new Boolean (awareness)); 116 } 117 118 126 127 public boolean isNamespaceAware() { 128 return getFeature (XmlPullParser.FEATURE_PROCESS_NAMESPACES); 129 } 130 131 132 140 141 public void setValidating(boolean validating) { 142 features.put (XmlPullParser.FEATURE_VALIDATION, new Boolean (validating)); 143 } 144 145 152 153 public boolean isValidating() { 154 return getFeature (XmlPullParser.FEATURE_VALIDATION); 155 } 156 157 165 166 public XmlPullParser newPullParser() throws XmlPullParserException { 167 168 if (parserClasses == null) throw new XmlPullParserException 169 ("Factory initialization was incomplete - has not tried "+classNamesLocation); 170 171 if (parserClasses.size() == 0) throw new XmlPullParserException 172 ("No valid parser classes found in "+classNamesLocation); 173 174 StringBuffer issues = new StringBuffer (); 175 176 for (int i = 0; i < parserClasses.size (); i++) { 177 Class ppClass = (Class ) parserClasses.elementAt (i); 178 try { 179 XmlPullParser pp = (XmlPullParser) ppClass.newInstance(); 180 184 for (Enumeration e = features.keys (); e.hasMoreElements ();) { 185 String key = (String ) e.nextElement(); 186 Boolean value = (Boolean ) features.get(key); 187 if(value != null && value.booleanValue()) { 188 pp.setFeature(key, true); 189 } 190 } 191 return pp; 192 193 } catch(Exception ex) { 194 issues.append (ppClass.getName () + ": "+ ex.toString ()+"; "); 195 } 196 } 197 198 throw new XmlPullParserException ("could not create parser: "+issues); 199 } 200 201 202 211 212 public XmlSerializer newSerializer() throws XmlPullParserException { 213 214 if (serializerClasses == null) { 215 throw new XmlPullParserException 216 ("Factory initialization incomplete - has not tried "+classNamesLocation); 217 } 218 if(serializerClasses.size() == 0) { 219 throw new XmlPullParserException 220 ("No valid serializer classes found in "+classNamesLocation); 221 } 222 223 StringBuffer issues = new StringBuffer (); 224 225 for (int i = 0; i < serializerClasses.size (); i++) { 226 Class ppClass = (Class ) serializerClasses.elementAt (i); 227 try { 228 XmlSerializer ser = (XmlSerializer) ppClass.newInstance(); 229 230 return ser; 238 239 } catch(Exception ex) { 240 issues.append (ppClass.getName () + ": "+ ex.toString ()+"; "); 241 } 242 } 243 244 throw new XmlPullParserException ("could not create serializer: "+issues); 245 } 246 247 253 254 public static XmlPullParserFactory newInstance () throws XmlPullParserException { 255 return newInstance(null, null); 256 } 257 258 259 260 public static XmlPullParserFactory newInstance (String classNames, Class context) 261 throws XmlPullParserException { 262 263 if (context == null) context = "".getClass (); 264 265 String classNamesLocation = null; 266 267 268 if (classNames == null || classNames.length() == 0 || "DEFAULT".equals(classNames)) { 269 try { 270 InputStream is = context.getResourceAsStream (RESOURCE_NAME); 271 272 if (is == null) { 273 classNames = "org.xmlpull.mxp1.MXParserFactory"; 274 } else { 275 276 StringBuffer sb = new StringBuffer (); 277 278 while (true) { 279 int ch = is.read(); 280 if (ch < 0) break; 281 else if (ch > ' ') 282 sb.append((char) ch); 283 } 284 is.close (); 285 286 classNames = sb.toString (); 287 } 288 } 289 catch (Exception e) { 290 throw new XmlPullParserException (null, null, e); 291 } 292 classNamesLocation = "resource "+RESOURCE_NAME+" that contained '"+classNames+"'"; 293 } else { 294 classNamesLocation = 295 "parameter classNames to newInstance() that contained '"+classNames+"'"; 296 } 297 298 XmlPullParserFactory factory = null; 299 Vector parserClasses = new Vector (); 300 Vector serializerClasses = new Vector (); 301 int pos = 0; 302 303 while (pos < classNames.length ()) { 304 int cut = classNames.indexOf (',', pos); 305 306 if (cut == -1) cut = classNames.length (); 307 String name = classNames.substring (pos, cut); 308 309 Class candidate = null; 310 Object instance = null; 311 312 try { 313 candidate = Class.forName (name); 314 instance = candidate.newInstance (); 316 } 317 catch (Exception e) {} 318 319 if (candidate != null) { 320 boolean recognized = false; 321 if (instance instanceof XmlPullParser) { 322 parserClasses.addElement (candidate); 323 recognized = true; 324 } 325 if (instance instanceof XmlSerializer) { 326 serializerClasses.addElement (candidate); 327 recognized = true; 328 } 329 if (instance instanceof XmlPullParserFactory) { 330 if (factory == null) { 331 factory = (XmlPullParserFactory) instance; 332 } 333 recognized = true; 334 } 335 if (!recognized) { 336 throw new XmlPullParserException ("incompatible class: "+name); 337 } 338 } 339 pos = cut + 1; 340 } 341 342 if (factory == null) { 343 factory = new XmlPullParserFactory (); 344 } 345 factory.parserClasses = parserClasses; 346 factory.serializerClasses = serializerClasses; 347 factory.classNamesLocation = classNamesLocation; 348 return factory; 349 } 350 } 351 352 353 | Popular Tags |