All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.enhydra.wireless.wml.dom.xerces.WMLDocumentImpl Maven / Gradle / Ivy

The newest version!
/*
 * Enhydra Java Application Server Project
 * 
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License on
 * the Enhydra web site ( http://www.enhydra.org/ ).
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
 * the License for the specific terms governing rights and limitations
 * under the License.
 * 
 * The Initial Developer of the Original Code is DigitalSesame
 * Portions created by DigitalSesame are Copyright (C) 1997-2000 DigitalSesame
 * All Rights Reserved.
 * 
 * Contributor(s):
 * 
 * $Id: WMLDocumentImpl.java,v 1.5 2005/04/05 06:17:30 jkjome Exp $
 */
package org.enhydra.wireless.wml.dom.xerces;

import java.lang.reflect.Constructor;
import java.util.Hashtable;

import org.enhydra.apache.xerces.dom.DocumentImpl;
import org.enhydra.wireless.wml.dom.WMLDocument;
import org.enhydra.xml.xmlc.XMLCError;
import org.enhydra.xml.xmlc.XMLObject;
import org.enhydra.xml.xmlc.XMLObjectLink;
import org.w3c.dom.DOMException;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class WMLDocumentImpl extends DocumentImpl implements WMLDocument, XMLObjectLink {
    // FIXME: would be faster to saved Constructor object.
    private static Hashtable        fElementTypes;
    private static final Class[]    fElemConstructorSig
        = new Class[] {WMLDocumentImpl.class, String.class, String.class};

    /**
     * overload in order to return the correct dom implementation for WML
     * @see org.enhydra.apache.xerces.dom.DocumentImpl#getImplementation
     */
    public DOMImplementation getImplementation() {
        return WMLDOMImplementationImpl.getDOMImplementation();
    }

    public Element createElementNS(String namespaceURI, 
                                   String qualifiedName) throws DOMException {
        // FIXME: should be a common method for doing this
        int index = qualifiedName.indexOf(':');
        String tagName;
        if (index < 0) {
            tagName = qualifiedName;
        } else {
            tagName = qualifiedName.substring(index+1);
        }
        Class elemClass = (Class)fElementTypes.get(tagName);
        if (elemClass != null) {
            try	{
                Constructor cnst = elemClass.getConstructor(fElemConstructorSig);
                return (Element)cnst.newInstance(new Object[] {this, namespaceURI, qualifiedName});
            } catch (Exception except) {
                // Need more specific error..
                throw new XMLCError("failed to construct element for \""
                                    + qualifiedName + "\"", except);
            }
        } else {
            return new WMLElementImpl(this, null, tagName);
        }
    }

    public Element createElement(String tagName) throws DOMException {
        return createElementNS(null, tagName);
    }

    static {
	fElementTypes = new Hashtable();
	fElementTypes.put("b", WMLBElementImpl.class);
	fElementTypes.put("noop", WMLNoopElementImpl.class);
	fElementTypes.put("a", WMLAElementImpl.class);
	fElementTypes.put("setvar", WMLSetvarElementImpl.class);
	fElementTypes.put("access", WMLAccessElementImpl.class);
	fElementTypes.put("strong", WMLStrongElementImpl.class);
	fElementTypes.put("postfield", WMLPostfieldElementImpl.class);
	fElementTypes.put("do", WMLDoElementImpl.class);
	fElementTypes.put("wml", WMLWmlElementImpl.class);
	fElementTypes.put("tr", WMLTrElementImpl.class);
	fElementTypes.put("go", WMLGoElementImpl.class);
	fElementTypes.put("big", WMLBigElementImpl.class);
	fElementTypes.put("anchor", WMLAnchorElementImpl.class);
	fElementTypes.put("timer", WMLTimerElementImpl.class);
	fElementTypes.put("small", WMLSmallElementImpl.class);
	fElementTypes.put("optgroup", WMLOptgroupElementImpl.class);
	fElementTypes.put("head", WMLHeadElementImpl.class);
	fElementTypes.put("td", WMLTdElementImpl.class);
	fElementTypes.put("fieldset", WMLFieldsetElementImpl.class);
	fElementTypes.put("img", WMLImgElementImpl.class);
	fElementTypes.put("refresh", WMLRefreshElementImpl.class);
	fElementTypes.put("onevent", WMLOneventElementImpl.class);
	fElementTypes.put("input", WMLInputElementImpl.class);
	fElementTypes.put("prev", WMLPrevElementImpl.class);
	fElementTypes.put("table", WMLTableElementImpl.class);
	fElementTypes.put("meta", WMLMetaElementImpl.class);
	fElementTypes.put("template", WMLTemplateElementImpl.class);
	fElementTypes.put("br", WMLBrElementImpl.class);
	fElementTypes.put("option", WMLOptionElementImpl.class);
	fElementTypes.put("u", WMLUElementImpl.class);
	fElementTypes.put("p", WMLPElementImpl.class);
	fElementTypes.put("select", WMLSelectElementImpl.class);
	fElementTypes.put("em", WMLEmElementImpl.class);
	fElementTypes.put("i", WMLIElementImpl.class);
	fElementTypes.put("card", WMLCardElementImpl.class);       
    }

    
    /* DOM level 2 */
    public WMLDocumentImpl(DocumentType doctype) {
        super(doctype, false);
    }

    public synchronized Element getElementById( String elementId )
    {
        return getElementById( elementId, this );
    }
    
    /**
     * Recursive method retreives an element by its id attribute.
     * Called by {@link #getElementById(String)}.
     *
     * @param elementId The id value to look for
     * @return The node in which to look for
     */
    private Element getElementById( String elementId, Node node )
    {
        Node    child;
        Element    result;

        child = node.getFirstChild();
        while ( child != null )
        {
            if ( child instanceof Element )
            {
                if ( elementId.equals( ( (Element) child ).getAttribute( "id" ) ) )
                    return (Element) child;
                result = getElementById( elementId, child );
                if ( result != null )
                    return result;
            }
            child = child.getNextSibling();
        }
        return null;
    }


    //-------------------------------------------------------------------------
    // XMLObjectLink implementation
    //-------------------------------------------------------------------------

    /**
     * Reference to XMLObject containing this Document.
     */
    private XMLObject fXmlObjectLink;

    /**
     * @see XMLObjectLink#setXMLObject
     */
    public void setXMLObject(XMLObject xmlObject) {
        fXmlObjectLink = xmlObject;
    }

    /**
     * @see XMLObjectLink#getXMLObject
     */
    public XMLObject getXMLObject() {
        return fXmlObjectLink;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy