001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: BeanDocumentFactory.java,v 1.8 2001/08/09 13:35:14 jstrachan Exp $ 008 */ 009 010package org.dom4j.bean; 011 012import java.util.Map; 013 014import org.dom4j.DocumentFactory; 015import org.dom4j.Attribute; 016import org.dom4j.Element; 017import org.dom4j.QName; 018import org.dom4j.tree.DefaultAttribute; 019import org.dom4j.tree.DefaultElement; 020 021import org.xml.sax.Attributes; 022 023/** <p><code>BeanDocumentFactory</code> is a factory of DOM4J objects 024 * which may be BeanElements which are backed by JavaBeans 025 * and their properties. </p> 026 * 027 * <p>The tree built allows full XPath expressions from anywhere on the 028 * tree.</p> 029 * 030 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 031 * @version $Revision: 1.8 $ 032 */ 033public class BeanDocumentFactory extends DocumentFactory { 034 035 /** The Singleton instance */ 036 private static BeanDocumentFactory singleton = new BeanDocumentFactory(); 037 038 /** <p>Access to the singleton instance of this factory.</p> 039 * 040 * @return the default singleon instance 041 */ 042 public static DocumentFactory getInstance() { 043 return singleton; 044 } 045 046 047 // Factory methods 048 049 public Element createElement(QName qname) { 050 Object bean = createBean( qname ); 051 if ( bean == null ) { 052 return new BeanElement(qname); 053 } 054 else { 055 return new BeanElement(qname, bean); 056 } 057 } 058 059 public Element createElement(QName qname, Attributes attributes) { 060 Object bean = createBean( qname, attributes ); 061 if ( bean == null ) { 062 return new BeanElement(qname); 063 } 064 else { 065 return new BeanElement(qname, bean); 066 } 067 } 068 069 public Attribute createAttribute(Element owner, QName qname, String value) { 070 return new DefaultAttribute(qname, value); 071 } 072 073 074 // Implementation methods 075 076 protected Object createBean( QName qname ) { 077 return null; 078 } 079 080 protected Object createBean( QName qname, Attributes attributes ) { 081 String value = attributes.getValue( "class" ); 082 if ( value != null ) { 083 try { 084 Class beanClass = Class.forName( 085 value, 086 true, 087 BeanDocumentFactory.class.getClassLoader() 088 ); 089 return beanClass.newInstance(); 090 } 091 catch (Exception e) { 092 handleException(e); 093 } 094 } 095 return null; 096 } 097 098 protected void handleException(Exception e) { 099 // ignore introspection exceptions 100 System.out.println( "#### Warning: couldn't create bean: " + e ); 101 } 102} 103 104 105 106 107/* 108 * Redistribution and use of this software and associated documentation 109 * ("Software"), with or without modification, are permitted provided 110 * that the following conditions are met: 111 * 112 * 1. Redistributions of source code must retain copyright 113 * statements and notices. Redistributions must also contain a 114 * copy of this document. 115 * 116 * 2. Redistributions in binary form must reproduce the 117 * above copyright notice, this list of conditions and the 118 * following disclaimer in the documentation and/or other 119 * materials provided with the distribution. 120 * 121 * 3. The name "DOM4J" must not be used to endorse or promote 122 * products derived from this Software without prior written 123 * permission of MetaStuff, Ltd. For written permission, 124 * please contact dom4j-info@metastuff.com. 125 * 126 * 4. Products derived from this Software may not be called "DOM4J" 127 * nor may "DOM4J" appear in their names without prior written 128 * permission of MetaStuff, Ltd. DOM4J is a registered 129 * trademark of MetaStuff, Ltd. 130 * 131 * 5. Due credit should be given to the DOM4J Project 132 * (http://dom4j.org/). 133 * 134 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 135 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 136 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 137 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 138 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 139 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 140 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 141 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 142 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 143 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 144 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 145 * OF THE POSSIBILITY OF SUCH DAMAGE. 146 * 147 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 148 * 149 * $Id: BeanDocumentFactory.java,v 1.8 2001/08/09 13:35:14 jstrachan Exp $ 150 */