001: /*
002: * Copyright (c) 2002-2008 Gargoyle Software Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: * 2. Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: * 3. The end-user documentation included with the redistribution, if any, must
013: * include the following acknowledgment:
014: *
015: * "This product includes software developed by Gargoyle Software Inc.
016: * (http://www.GargoyleSoftware.com/)."
017: *
018: * Alternately, this acknowledgment may appear in the software itself, if
019: * and wherever such third-party acknowledgments normally appear.
020: * 4. The name "Gargoyle Software" must not be used to endorse or promote
021: * products derived from this software without prior written permission.
022: * For written permission, please contact info@GargoyleSoftware.com.
023: * 5. Products derived from this software may not be called "HtmlUnit", nor may
024: * "HtmlUnit" appear in their name, without prior written permission of
025: * Gargoyle Software Inc.
026: *
027: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
028: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
029: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
030: * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
031: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
032: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
033: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
036: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: */
038: package com.gargoylesoftware.htmlunit.html;
039:
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042: import org.xml.sax.Attributes;
043:
044: import java.util.Map;
045: import java.util.HashMap;
046:
047: /**
048: * A specialized creator that knows how to create input objects
049: *
050: * @version $Revision: 2132 $
051: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
052: * @author Marc Guillemot
053: * @author Ahmed Ashour
054: * @author David K. Taylor
055: */
056: public final class InputElementFactory implements IElementFactory {
057:
058: /** The singleton instance */
059: public static final InputElementFactory instance = new InputElementFactory();
060:
061: /* private singleton constructor */
062: private InputElementFactory() {
063: }
064:
065: /**
066: * Create an HtmlElement for the specified xmlElement, contained in the specified page.
067: *
068: * @param page The page that this element will belong to.
069: * @param tagName the HTML tag name
070: * @param attributes the SAX attributes
071: *
072: * @return a new HtmlInput element.
073: */
074: public HtmlElement createElement(final HtmlPage page,
075: final String tagName, final Attributes attributes) {
076: return createElementNS(page, null, tagName, attributes);
077: }
078:
079: /**
080: * {@inheritDoc}
081: */
082: public HtmlElement createElementNS(final HtmlPage page,
083: final String namespaceURI, final String qualifiedName,
084: final Attributes attributes) {
085:
086: Map attributeMap = DefaultElementFactory.setAttributes(page,
087: attributes);
088: if (attributeMap == null) {
089: attributeMap = new HashMap();
090: }
091:
092: String type = null;
093: if (attributes != null) {
094: type = attributes.getValue("type");
095: }
096: if (type == null) {
097: type = "";
098: } else {
099: type = type.toLowerCase();
100: }
101:
102: final HtmlInput result;
103: if (type.length() == 0) {
104: // This not an illegal value, as it defaults to "text"
105: // cf http://www.w3.org/TR/REC-html40/interact/forms.html#adef-type-INPUT
106: // and the common browsers seem to treat it as a "text" input so we will as well.
107: HtmlElement.addAttributeToMap(page, attributeMap, null,
108: "type", "text");
109: result = new HtmlTextInput(namespaceURI, qualifiedName,
110: page, attributeMap);
111: } else if (type.equals("submit")) {
112: result = new HtmlSubmitInput(namespaceURI, qualifiedName,
113: page, attributeMap);
114: } else if (type.equals("checkbox")) {
115: result = new HtmlCheckBoxInput(namespaceURI, qualifiedName,
116: page, attributeMap);
117: } else if (type.equals("radio")) {
118: result = new HtmlRadioButtonInput(namespaceURI,
119: qualifiedName, page, attributeMap);
120: } else if (type.equals("text")) {
121: result = new HtmlTextInput(namespaceURI, qualifiedName,
122: page, attributeMap);
123: } else if (type.equals("hidden")) {
124: result = new HtmlHiddenInput(namespaceURI, qualifiedName,
125: page, attributeMap);
126: } else if (type.equals("password")) {
127: result = new HtmlPasswordInput(namespaceURI, qualifiedName,
128: page, attributeMap);
129: } else if (type.equals("image")) {
130: result = new HtmlImageInput(namespaceURI, qualifiedName,
131: page, attributeMap);
132: } else if (type.equals("reset")) {
133: result = new HtmlResetInput(namespaceURI, qualifiedName,
134: page, attributeMap);
135: } else if (type.equals("button")) {
136: result = new HtmlButtonInput(namespaceURI, qualifiedName,
137: page, attributeMap);
138: } else if (type.equals("file")) {
139: result = new HtmlFileInput(namespaceURI, qualifiedName,
140: page, attributeMap);
141: } else {
142: getLog().info(
143: "Bad input type: \"" + type
144: + "\", creating a text input");
145: result = new HtmlTextInput(namespaceURI, qualifiedName,
146: page, attributeMap);
147: }
148: return result;
149: }
150:
151: /**
152: * Return the log that is being used for all scripting objects
153: * @return The log.
154: */
155: protected Log getLog() {
156: return LogFactory.getLog(getClass());
157: }
158:
159: }
|