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 java.util.Map;
041:
042: import org.xml.sax.Attributes;
043:
044: /**
045: * Element factory which creates elements by calling the constructor on a
046: * given {@link com.gargoylesoftware.htmlunit.html.HtmlElement} subclass.
047: * The constructor is expected to take 2 arguments of type
048: * {@link com.gargoylesoftware.htmlunit.html.HtmlPage} and {@link java.util.Map}
049: * where the first one is the owning page of the element, the second one is a map
050: * holding the initial attributes for the element.
051: *
052: * @version $Revision: 2132 $
053: * @author <a href="mailto:cse@dynabean.de">Christian Sell</a>
054: * @author Ahmed Ashour
055: * @author David K. Taylor
056: */
057: class DefaultElementFactory implements IElementFactory {
058:
059: /**
060: * @param page the owning page
061: * @param tagName the HTML tag name
062: * @param attributes initial attributes, possibly <code>null</code>
063: * @return the newly created element
064: */
065: public HtmlElement createElement(final HtmlPage page,
066: final String tagName, final Attributes attributes) {
067: return createElementNS(page, null, tagName, attributes);
068: }
069:
070: /**
071: * @param page the owning page
072: * @param namespaceURI the URI that identifies an XML namespace.
073: * @param qualifiedName The qualified name of the element type to instantiate
074: * @param attributes initial attributes, possibly <code>null</code>
075: * @return the newly created element
076: */
077: public HtmlElement createElementNS(final HtmlPage page,
078: final String namespaceURI, final String qualifiedName,
079: final Attributes attributes) {
080: final Map attributeMap = setAttributes(page, attributes);
081:
082: final HtmlElement element;
083: final String tagName;
084: final int colonIndex = qualifiedName.indexOf(':');
085: if (colonIndex == -1) {
086: tagName = qualifiedName;
087: } else {
088: tagName = qualifiedName.substring(colonIndex + 1)
089: .toLowerCase();
090: }
091: if (tagName.equals(HtmlAddress.TAG_NAME)) {
092: element = new HtmlAddress(namespaceURI, qualifiedName,
093: page, attributeMap);
094: } else if (tagName.equals(HtmlAnchor.TAG_NAME)) {
095: element = new HtmlAnchor(namespaceURI, qualifiedName, page,
096: attributeMap);
097: } else if (tagName.equals(HtmlApplet.TAG_NAME)) {
098: element = new HtmlApplet(namespaceURI, qualifiedName, page,
099: attributeMap);
100: } else if (tagName.equals(HtmlArea.TAG_NAME)) {
101: element = new HtmlArea(namespaceURI, qualifiedName, page,
102: attributeMap);
103: } else if (tagName.equals(HtmlBase.TAG_NAME)) {
104: element = new HtmlBase(namespaceURI, qualifiedName, page,
105: attributeMap);
106: } else if (tagName.equals(HtmlBaseFont.TAG_NAME)) {
107: element = new HtmlBaseFont(namespaceURI, qualifiedName,
108: page, attributeMap);
109: } else if (tagName.equals(HtmlBidirectionalOverride.TAG_NAME)) {
110: element = new HtmlBidirectionalOverride(namespaceURI,
111: qualifiedName, page, attributeMap);
112: } else if (tagName.equals(HtmlBlockQuote.TAG_NAME)) {
113: element = new HtmlBlockQuote(namespaceURI, qualifiedName,
114: page, attributeMap);
115: } else if (tagName.equals(HtmlBody.TAG_NAME)) {
116: element = new HtmlBody(namespaceURI, qualifiedName, page,
117: attributeMap);
118: } else if (tagName.equals(HtmlBreak.TAG_NAME)) {
119: element = new HtmlBreak(namespaceURI, qualifiedName, page,
120: attributeMap);
121: } else if (tagName.equals(HtmlButton.TAG_NAME)) {
122: element = new HtmlButton(namespaceURI, qualifiedName, page,
123: attributeMap);
124: } else if (tagName.equals(HtmlButtonInput.TAG_NAME)) {
125: element = new HtmlButtonInput(namespaceURI, qualifiedName,
126: page, attributeMap);
127: } else if (tagName.equals(HtmlCaption.TAG_NAME)) {
128: element = new HtmlCaption(namespaceURI, qualifiedName,
129: page, attributeMap);
130: } else if (tagName.equals(HtmlCenter.TAG_NAME)) {
131: element = new HtmlCenter(namespaceURI, qualifiedName, page,
132: attributeMap);
133: } else if (tagName.equals(HtmlCheckBoxInput.TAG_NAME)) {
134: element = new HtmlCheckBoxInput(namespaceURI,
135: qualifiedName, page, attributeMap);
136: } else if (tagName.equals(HtmlDefinitionDescription.TAG_NAME)) {
137: element = new HtmlDefinitionDescription(namespaceURI,
138: qualifiedName, page, attributeMap);
139: } else if (tagName.equals(HtmlDefinitionList.TAG_NAME)) {
140: element = new HtmlDefinitionList(namespaceURI,
141: qualifiedName, page, attributeMap);
142: } else if (tagName.equals(HtmlDefinitionTerm.TAG_NAME)) {
143: element = new HtmlDefinitionTerm(namespaceURI,
144: qualifiedName, page, attributeMap);
145: } else if (tagName.equals(HtmlDeletedText.TAG_NAME)) {
146: element = new HtmlDeletedText(namespaceURI, qualifiedName,
147: page, attributeMap);
148: } else if (tagName.equals(HtmlDivision.TAG_NAME)) {
149: element = new HtmlDivision(namespaceURI, qualifiedName,
150: page, attributeMap);
151: } else if (tagName.equals(HtmlFieldSet.TAG_NAME)) {
152: element = new HtmlFieldSet(namespaceURI, qualifiedName,
153: page, attributeMap);
154: } else if (tagName.equals(HtmlFileInput.TAG_NAME)) {
155: element = new HtmlFileInput(namespaceURI, qualifiedName,
156: page, attributeMap);
157: } else if (tagName.equals(HtmlFont.TAG_NAME)) {
158: element = new HtmlFont(namespaceURI, qualifiedName, page,
159: attributeMap);
160: } else if (tagName.equals(HtmlForm.TAG_NAME)) {
161: element = new HtmlForm(namespaceURI, qualifiedName, page,
162: attributeMap);
163: } else if (tagName.equals(HtmlFrame.TAG_NAME)) {
164: if (attributeMap != null) {
165: final HtmlAttr srcAttribute = (HtmlAttr) attributeMap
166: .get("src");
167: if (srcAttribute != null) {
168: srcAttribute.setHtmlValue(((String) srcAttribute
169: .getHtmlValue()).trim());
170: }
171: }
172: element = new HtmlFrame(namespaceURI, qualifiedName, page,
173: attributeMap);
174: } else if (tagName.equals(HtmlFrameSet.TAG_NAME)) {
175: element = new HtmlFrameSet(namespaceURI, qualifiedName,
176: page, attributeMap);
177: } else if (tagName.equals(HtmlHead.TAG_NAME)) {
178: element = new HtmlHead(namespaceURI, qualifiedName, page,
179: attributeMap);
180: } else if (tagName.equals(HtmlHeader1.TAG_NAME)) {
181: element = new HtmlHeader1(namespaceURI, qualifiedName,
182: page, attributeMap);
183: } else if (tagName.equals(HtmlHeader2.TAG_NAME)) {
184: element = new HtmlHeader2(namespaceURI, qualifiedName,
185: page, attributeMap);
186: } else if (tagName.equals(HtmlHeader3.TAG_NAME)) {
187: element = new HtmlHeader3(namespaceURI, qualifiedName,
188: page, attributeMap);
189: } else if (tagName.equals(HtmlHeader4.TAG_NAME)) {
190: element = new HtmlHeader4(namespaceURI, qualifiedName,
191: page, attributeMap);
192: } else if (tagName.equals(HtmlHeader5.TAG_NAME)) {
193: element = new HtmlHeader5(namespaceURI, qualifiedName,
194: page, attributeMap);
195: } else if (tagName.equals(HtmlHeader6.TAG_NAME)) {
196: element = new HtmlHeader6(namespaceURI, qualifiedName,
197: page, attributeMap);
198: } else if (tagName.equals(HtmlHiddenInput.TAG_NAME)) {
199: element = new HtmlHiddenInput(namespaceURI, qualifiedName,
200: page, attributeMap);
201: } else if (tagName.equals(HtmlHorizontalRule.TAG_NAME)) {
202: element = new HtmlHorizontalRule(namespaceURI,
203: qualifiedName, page, attributeMap);
204: } else if (tagName.equals(HtmlHtml.TAG_NAME)) {
205: element = new HtmlHtml(namespaceURI, qualifiedName, page,
206: attributeMap);
207: } else if (tagName.equals(HtmlImage.TAG_NAME)) {
208: element = new HtmlImage(namespaceURI, qualifiedName, page,
209: attributeMap);
210: } else if (tagName.equals(HtmlImageInput.TAG_NAME)) {
211: element = new HtmlImageInput(namespaceURI, qualifiedName,
212: page, attributeMap);
213: } else if (tagName.equals(HtmlInlineFrame.TAG_NAME)) {
214: if (attributeMap != null) {
215: final HtmlAttr srcAttribute = (HtmlAttr) attributeMap
216: .get("src");
217: if (srcAttribute != null) {
218: srcAttribute.setHtmlValue(((String) srcAttribute
219: .getHtmlValue()).trim());
220: }
221: }
222: element = new HtmlInlineFrame(namespaceURI, qualifiedName,
223: page, attributeMap);
224: } else if (tagName.equals(HtmlInlineQuotation.TAG_NAME)) {
225: element = new HtmlInlineQuotation(namespaceURI,
226: qualifiedName, page, attributeMap);
227: } else if (tagName.equals(HtmlInsertedText.TAG_NAME)) {
228: element = new HtmlInsertedText(namespaceURI, qualifiedName,
229: page, attributeMap);
230: } else if (tagName.equals(HtmlIsIndex.TAG_NAME)) {
231: element = new HtmlIsIndex(namespaceURI, qualifiedName,
232: page, attributeMap);
233: } else if (tagName.equals(HtmlLabel.TAG_NAME)) {
234: element = new HtmlLabel(namespaceURI, qualifiedName, page,
235: attributeMap);
236: } else if (tagName.equals(HtmlLegend.TAG_NAME)) {
237: element = new HtmlLegend(namespaceURI, qualifiedName, page,
238: attributeMap);
239: } else if (tagName.equals(HtmlLink.TAG_NAME)) {
240: element = new HtmlLink(namespaceURI, qualifiedName, page,
241: attributeMap);
242: } else if (tagName.equals(HtmlListItem.TAG_NAME)) {
243: element = new HtmlListItem(namespaceURI, qualifiedName,
244: page, attributeMap);
245: } else if (tagName.equals(HtmlMap.TAG_NAME)) {
246: element = new HtmlMap(namespaceURI, qualifiedName, page,
247: attributeMap);
248: } else if (tagName.equals(HtmlMenu.TAG_NAME)) {
249: element = new HtmlMenu(namespaceURI, qualifiedName, page,
250: attributeMap);
251: } else if (tagName.equals(HtmlMeta.TAG_NAME)) {
252: element = new HtmlMeta(namespaceURI, qualifiedName, page,
253: attributeMap);
254: } else if (tagName.equals(HtmlNoFrames.TAG_NAME)) {
255: element = new HtmlNoFrames(namespaceURI, qualifiedName,
256: page, attributeMap);
257: } else if (tagName.equals(HtmlNoScript.TAG_NAME)) {
258: element = new HtmlNoScript(namespaceURI, qualifiedName,
259: page, attributeMap);
260: } else if (tagName.equals(HtmlObject.TAG_NAME)) {
261: element = new HtmlObject(namespaceURI, qualifiedName, page,
262: attributeMap);
263: } else if (tagName.equals(HtmlOption.TAG_NAME)) {
264: element = new HtmlOption(namespaceURI, qualifiedName, page,
265: attributeMap);
266: } else if (tagName.equals(HtmlOptionGroup.TAG_NAME)) {
267: element = new HtmlOptionGroup(namespaceURI, qualifiedName,
268: page, attributeMap);
269: } else if (tagName.equals(HtmlOrderedList.TAG_NAME)) {
270: element = new HtmlOrderedList(namespaceURI, qualifiedName,
271: page, attributeMap);
272: } else if (tagName.equals(HtmlParagraph.TAG_NAME)) {
273: element = new HtmlParagraph(namespaceURI, qualifiedName,
274: page, attributeMap);
275: } else if (tagName.equals(HtmlParameter.TAG_NAME)) {
276: element = new HtmlParameter(namespaceURI, qualifiedName,
277: page, attributeMap);
278: } else if (tagName.equals(HtmlPasswordInput.TAG_NAME)) {
279: element = new HtmlPasswordInput(namespaceURI,
280: qualifiedName, page, attributeMap);
281: } else if (tagName.equals(HtmlPreformattedText.TAG_NAME)) {
282: element = new HtmlPreformattedText(namespaceURI,
283: qualifiedName, page, attributeMap);
284: } else if (tagName.equals(HtmlRadioButtonInput.TAG_NAME)) {
285: element = new HtmlRadioButtonInput(namespaceURI,
286: qualifiedName, page, attributeMap);
287: } else if (tagName.equals(HtmlResetInput.TAG_NAME)) {
288: element = new HtmlResetInput(namespaceURI, qualifiedName,
289: page, attributeMap);
290: } else if (tagName.equals(HtmlScript.TAG_NAME)) {
291: element = new HtmlScript(namespaceURI, qualifiedName, page,
292: attributeMap);
293: } else if (tagName.equals(HtmlSelect.TAG_NAME)) {
294: element = new HtmlSelect(namespaceURI, qualifiedName, page,
295: attributeMap);
296: } else if (tagName.equals(HtmlSpan.TAG_NAME)) {
297: element = new HtmlSpan(namespaceURI, qualifiedName, page,
298: attributeMap);
299: } else if (tagName.equals(HtmlStyle.TAG_NAME)) {
300: element = new HtmlStyle(namespaceURI, qualifiedName, page,
301: attributeMap);
302: } else if (tagName.equals(HtmlSubmitInput.TAG_NAME)) {
303: element = new HtmlSubmitInput(namespaceURI, qualifiedName,
304: page, attributeMap);
305: } else if (tagName.equals(HtmlTable.TAG_NAME)) {
306: element = new HtmlTable(namespaceURI, qualifiedName, page,
307: attributeMap);
308: } else if (tagName.equals(HtmlTableBody.TAG_NAME)) {
309: element = new HtmlTableBody(namespaceURI, qualifiedName,
310: page, attributeMap);
311: } else if (tagName.equals(HtmlTableColumn.TAG_NAME)) {
312: element = new HtmlTableColumn(namespaceURI, qualifiedName,
313: page, attributeMap);
314: } else if (tagName.equals(HtmlTableColumnGroup.TAG_NAME)) {
315: element = new HtmlTableColumnGroup(namespaceURI,
316: qualifiedName, page, attributeMap);
317: } else if (tagName.equals(HtmlTableDataCell.TAG_NAME)) {
318: element = new HtmlTableDataCell(namespaceURI,
319: qualifiedName, page, attributeMap);
320: } else if (tagName.equals(HtmlTableFooter.TAG_NAME)) {
321: element = new HtmlTableFooter(namespaceURI, qualifiedName,
322: page, attributeMap);
323: } else if (tagName.equals(HtmlTableHeader.TAG_NAME)) {
324: element = new HtmlTableHeader(namespaceURI, qualifiedName,
325: page, attributeMap);
326: } else if (tagName.equals(HtmlTableHeaderCell.TAG_NAME)) {
327: element = new HtmlTableHeaderCell(namespaceURI,
328: qualifiedName, page, attributeMap);
329: } else if (tagName.equals(HtmlTableRow.TAG_NAME)) {
330: element = new HtmlTableRow(namespaceURI, qualifiedName,
331: page, attributeMap);
332: } else if (tagName.equals(HtmlTextArea.TAG_NAME)) {
333: element = new HtmlTextArea(namespaceURI, qualifiedName,
334: page, attributeMap);
335: } else if (tagName.equals(HtmlTextDirection.TAG_NAME)) {
336: element = new HtmlTextDirection(namespaceURI,
337: qualifiedName, page, attributeMap);
338: } else if (tagName.equals(HtmlTextInput.TAG_NAME)) {
339: element = new HtmlTextInput(namespaceURI, qualifiedName,
340: page, attributeMap);
341: } else if (tagName.equals(HtmlTitle.TAG_NAME)) {
342: element = new HtmlTitle(namespaceURI, qualifiedName, page,
343: attributeMap);
344: } else if (tagName.equals(HtmlUnorderedList.TAG_NAME)) {
345: element = new HtmlUnorderedList(namespaceURI,
346: qualifiedName, page, attributeMap);
347: } else {
348: throw new IllegalStateException(
349: "Cannot find HtmlElement for " + qualifiedName);
350: }
351: return element;
352: }
353:
354: /**
355: * Convert Attributes into the Map needed by HtmlElement.
356: *
357: * @param attributes the SAX attributes.
358: * @return the Map of attribute values for HtmlElement.
359: */
360: static Map setAttributes(final HtmlPage page,
361: final Attributes attributes) {
362: Map attributeMap = null;
363: if (attributes != null) {
364: attributeMap = HtmlElement.createAttributeMap(attributes
365: .getLength());
366: for (int i = 0; i < attributes.getLength(); i++) {
367: HtmlElement.addAttributeToMap(page, attributeMap,
368: attributes.getURI(i), attributes.getQName(i),
369: attributes.getValue(i));
370: }
371: }
372: return attributeMap;
373: }
374: }
|