001: /*
002: ItsNat Java Web Application Framework
003: Copyright (C) 2007 Innowhere Software Services S.L., Spanish Company
004: Author: Jose Maria Arranz Santamaria
005:
006: This program is free software: you can redistribute it and/or modify
007: it under the terms of the GNU Affero General Public License as published by
008: the Free Software Foundation, either version 3 of the License, or
009: (at your option) any later version. See the GNU Affero General Public
010: License for more details. See the copy of the GNU Affero General Public License
011: included in this program. If not, see <http://www.gnu.org/licenses/>.
012: */
013:
014: package org.itsnat.impl.core.dom.parse;
015:
016: import org.apache.html.dom.HTMLDocumentImpl;
017: import org.w3c.dom.Element;
018: import org.w3c.dom.Node;
019:
020: /**
021: *
022: * @author jmarranz
023: */
024: public class HTMLDocumentImplXercesPatch extends HTMLDocumentImpl {
025:
026: /** Creates a new instance of HTMLDocumentImplXercesPatch */
027: public HTMLDocumentImplXercesPatch() {
028: }
029:
030: public Element createElementNS(String namespaceURI,
031: String qualifiedName) {
032: // Redefinimos para soportar xhtml y a la vez creando objetos
033: // HTML DOM (HTMLElement etc) pues Xerces no soporta XHTML con objetos HTML DOM
034:
035: if (namespaceURI == null || namespaceURI.length() == 0)
036: return createElement(qualifiedName);
037: else {
038: // Caso de xhtml
039: if (namespaceURI.equals("http://www.w3.org/1999/xhtml"))
040: return createElement(qualifiedName); // Así se crean objetos HTMLElementImpl
041: else
042: return super .createElementNS(namespaceURI,
043: qualifiedName); // Se crea un ElementNSImpl que no es un HTMLElement
044: }
045: }
046:
047: /*
048: public synchronized Element getDocumentElement()
049: {
050: // Redefinimos el getDocumentElement() porque cuando se llama
051: // además de crear el objeto HTMLHtmlElement si no existe,
052: // lo cual está muy bien, recorre los hijos del Document y
053: // los inserta bajo el <html>, es el caso de nodos de texto
054: // y los comentarios por ejemplo.
055: // El problema es que cambia el árbol DOM de forma que puede
056: // desconcertar al programador pues añade nodos donde no se esperan, debajo del <html>,
057: // además los navegadores actuales
058: // tragan estos nodos extra sin problemas.
059:
060:
061: Node html = getFirstChild();
062: while ( html != null )
063: {
064: if ( html instanceof HTMLHtmlElement )
065: {
066: return (HTMLHtmlElement) html;
067: }
068: html = html.getNextSibling();
069: }
070:
071: // No tiene nodo <html>, lo cual es muy raro por cierto
072: html = new HTMLHtmlElementImpl( this, "HTML" );
073: appendChild( html );
074: return (HTMLHtmlElementImpl) html;
075: }
076:
077: public synchronized HTMLElement getHead()
078: {
079: // Leer comentarios de getDocumentElement()
080:
081: Node html = getDocumentElement();
082:
083: synchronized ( html )
084: {
085: Node head = html.getFirstChild();
086: while ( head != null && ! ( head instanceof HTMLHeadElement ) )
087: head = head.getNextSibling();
088:
089: if (head != null)
090: {
091: return (HTMLHeadElement) head;
092: }
093:
094: // Head does not exist, create a new one, place it at the top of the
095: // HTML element and return it.
096: // insertBefore() admite nodo referencia nulo.
097: head = new HTMLHeadElementImpl( this, "HEAD" );
098: html.insertBefore( head, html.getFirstChild() );
099: return (HTMLHeadElement) head;
100: }
101: }
102:
103: public synchronized HTMLElement getBody()
104: {
105: // Leer comentarios de getDocumentElement()
106:
107: Node html = getDocumentElement();
108: Node head = getHead();
109: synchronized ( html )
110: {
111: Node body = head.getNextSibling();
112: while ( body != null && ! ( body instanceof HTMLBodyElement )
113: && ! ( body instanceof HTMLFrameSetElement ) )
114: body = body.getNextSibling();
115:
116: if ( body != null )
117: {
118: // body es <body> o <frameset> no hacemos cast obviamente en este caso pues son dos posibles casos válidos
119: return (HTMLElement) body;
120: }
121:
122: body = new HTMLBodyElementImpl( this, "BODY" );
123: html.appendChild( body );
124: return (HTMLElement) body;
125: }
126: }
127: */
128:
129: public Node cloneNode(boolean deep) {
130: // Redefinimos para que el clonado sea de este tipo.
131: HTMLDocumentImpl newdoc = new HTMLDocumentImplXercesPatch();
132: // callUserDataHandlers(this, newdoc, UserDataHandler.NODE_CLONED);
133: // es del DOM3, volver a poner cuando se desprecien las versiones Xerces sin soporte alguno de DOM 3
134: cloneNode(newdoc, deep);
135: return newdoc;
136: }
137: }
|