KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > DefaultElementFactory


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.html;
39
40 import com.gargoylesoftware.htmlunit.ObjectInstantiationException;
41
42 import java.lang.reflect.Constructor JavaDoc;
43 import java.lang.reflect.InvocationTargetException JavaDoc;
44 import java.util.Map JavaDoc;
45 import java.util.HashMap JavaDoc;
46
47 import org.xml.sax.Attributes JavaDoc;
48
49 /**
50  * Element factory which createss elements by calling the constructor on a
51  * given {@link com.gargoylesoftware.htmlunit.html.HtmlElement} subclass.
52  * The constructor is expected to take 2 arguments of type
53  * {@link com.gargoylesoftware.htmlunit.html.HtmlPage} and {@link java.util.Map}
54  * where the first one is the owning page of the element, the second one is a map
55  * holding the initial attributes for the element.
56  *
57  * @version $Revision: 100 $
58  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
59  */

60 class DefaultElementFactory implements IElementFactory {
61
62     private static final Class JavaDoc[] CONSTRUCTOR_ARGS = new Class JavaDoc[]{HtmlPage.class, Map JavaDoc.class};
63
64     private final Constructor JavaDoc constructor_;
65
66     /**
67      * Create an instance
68      * @param elementClass The element class.
69      */

70     public DefaultElementFactory(final Class JavaDoc elementClass) {
71
72         if(!HtmlElement.class.isAssignableFrom(elementClass)) {
73             throw new ClassCastException JavaDoc(elementClass+" is not a subclass of "+HtmlElement.class);
74         }
75         try {
76             constructor_ = elementClass.getConstructor(CONSTRUCTOR_ARGS);
77         }
78         catch (final Exception JavaDoc e) {
79             throw new ObjectInstantiationException("required constructor not found in "+elementClass, e);
80         }
81     }
82
83     /**
84      * @param page the owning page
85      * @param tagName the HTML tag name
86      * @param attributes initial attributes, possibly <code>null</code>
87      * @return the newly created element
88      */

89     public HtmlElement createElement(
90             final HtmlPage page, final String JavaDoc tagName, final Attributes JavaDoc attributes) {
91
92         try {
93             Map JavaDoc attributeMap = null;
94             if(attributes != null) {
95                 attributeMap = new HashMap JavaDoc(attributes.getLength());
96                 for(int i=0; i < attributes.getLength(); i++) {
97                     attributeMap.put(attributes.getLocalName(i), attributes.getValue(i));
98                 }
99             }
100             final HtmlElement element = (HtmlElement)constructor_.newInstance(new Object JavaDoc[]{page, attributeMap});
101             return element;
102         }
103         catch( final IllegalAccessException JavaDoc e) {
104             throw new ObjectInstantiationException(
105                     "Exception when calling constructor ["+constructor_+"]", e);
106         }
107         catch( final InstantiationException JavaDoc e ) {
108             throw new ObjectInstantiationException(
109                     "Exception when calling constructor ["+constructor_+"]", e);
110         }
111         catch( final InvocationTargetException JavaDoc e ) {
112             throw new ObjectInstantiationException(
113                     "Exception when calling constructor ["+constructor_+"]", e.getTargetException());
114         }
115     }
116 }
117
Popular Tags