1 7 8 package org.dom4j.bean; 9 10 import org.dom4j.Attribute; 11 import org.dom4j.DocumentFactory; 12 import org.dom4j.Element; 13 import org.dom4j.QName; 14 import org.dom4j.tree.DefaultAttribute; 15 16 import org.xml.sax.Attributes ; 17 18 31 public class BeanDocumentFactory extends DocumentFactory { 32 33 private static BeanDocumentFactory singleton = new BeanDocumentFactory(); 34 35 42 public static DocumentFactory getInstance() { 43 return singleton; 44 } 45 46 public Element createElement(QName qname) { 48 Object bean = createBean(qname); 49 50 if (bean == null) { 51 return new BeanElement(qname); 52 } else { 53 return new BeanElement(qname, bean); 54 } 55 } 56 57 public Element createElement(QName qname, Attributes attributes) { 58 Object bean = createBean(qname, attributes); 59 60 if (bean == null) { 61 return new BeanElement(qname); 62 } else { 63 return new BeanElement(qname, bean); 64 } 65 } 66 67 public Attribute createAttribute(Element owner, QName qname, String value) { 68 return new DefaultAttribute(qname, value); 69 } 70 71 protected Object createBean(QName qname) { 73 return null; 74 } 75 76 protected Object createBean(QName qname, Attributes attributes) { 77 String value = attributes.getValue("class"); 78 79 if (value != null) { 80 try { 81 Class beanClass = Class.forName(value, true, 82 BeanDocumentFactory.class.getClassLoader()); 83 84 return beanClass.newInstance(); 85 } catch (Exception e) { 86 handleException(e); 87 } 88 } 89 90 return null; 91 } 92 93 protected void handleException(Exception e) { 94 System.out.println("#### Warning: couldn't create bean: " + e); 96 } 97 } 98 99 135 | Popular Tags |