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;
015:
016: import java.lang.ref.WeakReference;
017: import org.itsnat.core.ItsNatException;
018: import org.itsnat.core.ItsNatNode;
019: import org.itsnat.impl.core.ItsNatDocumentImpl;
020: import org.w3c.dom.DOMException;
021: import org.w3c.dom.Document;
022: import org.w3c.dom.Element;
023: import org.w3c.dom.NamedNodeMap;
024: import org.w3c.dom.Node;
025: import org.w3c.dom.NodeList;
026: import org.w3c.dom.UserDataHandler;
027: import org.w3c.dom.events.Event;
028: import org.w3c.dom.events.EventException;
029: import org.w3c.dom.events.EventListener;
030: import org.w3c.dom.events.EventTarget;
031:
032: /**
033: *
034: * @author jmarranz
035: */
036: public abstract class ItsNatNodeImpl implements ItsNatNode, Node,
037: EventTarget {
038: protected WeakReference weakNode;
039: protected ItsNatDocumentImpl itsNatDoc;
040:
041: /**
042: * Creates a new instance of ItsNatNodeImpl
043: */
044: public ItsNatNodeImpl(Node node, ItsNatDocumentImpl itsNatDoc) {
045: if (node == null)
046: throw new ItsNatException("INTERNAL ERROR");
047: this .weakNode = new WeakReference(node);
048: this .itsNatDoc = itsNatDoc;
049: }
050:
051: public static ItsNatNodeImpl newItsNatNode(Node node,
052: ItsNatDocumentImpl itsNatDoc) {
053: if (node.getNodeType() == Node.ELEMENT_NODE)
054: return ItsNatElementImpl.newItsNatElement((Element) node,
055: itsNatDoc);
056: else
057: return ItsNatNodeDefaultImpl.newItsNatNodeDefault(node,
058: itsNatDoc);
059: }
060:
061: public Node getNode() {
062: Node node = (Node) weakNode.get();
063: if (node == null)
064: throw new ItsNatException("NODE LOST"); // Esto significa que "alguien" sujeta este objeto cuando lo normal es que se pierda cuando se pierda el nodo asociado, gracias a usar un registro/colección Weak
065: return node;
066: }
067:
068: public int hashCode() {
069: return getNode().hashCode();
070: }
071:
072: public boolean equals(Object other) {
073: boolean res = super .equals(other);
074: if (res)
075: return true;
076: if (other instanceof ItsNatNode)
077: return getNode().equals(((ItsNatNode) other).getNode());
078: return false;
079: }
080:
081: public String getNodeName() {
082: return getNode().getNodeName();
083: }
084:
085: public String getNodeValue() throws DOMException {
086: return getNode().getNodeValue();
087: }
088:
089: public void setNodeValue(String nodeValue) throws DOMException {
090: getNode().setNodeValue(nodeValue);
091: }
092:
093: public short getNodeType() {
094: return getNode().getNodeType();
095: }
096:
097: public Node getParentNode() {
098: return getNode().getParentNode();
099: }
100:
101: public NodeList getChildNodes() {
102: return getNode().getChildNodes();
103: }
104:
105: public Node getFirstChild() {
106: return getNode().getFirstChild();
107: }
108:
109: public Node getLastChild() {
110: return getNode().getLastChild();
111: }
112:
113: public Node getPreviousSibling() {
114: return getNode().getPreviousSibling();
115: }
116:
117: public Node getNextSibling() {
118: return getNode().getNextSibling();
119: }
120:
121: public NamedNodeMap getAttributes() {
122: return getNode().getAttributes();
123: }
124:
125: public Document getOwnerDocument() {
126: return getNode().getOwnerDocument();
127: }
128:
129: public Node insertBefore(Node newChild, Node refChild)
130: throws DOMException {
131: return getNode().insertBefore(newChild, refChild);
132: }
133:
134: public Node replaceChild(Node newChild, Node oldChild)
135: throws DOMException {
136: return getNode().replaceChild(newChild, oldChild);
137: }
138:
139: public Node removeChild(Node oldChild) throws DOMException {
140: return getNode().removeChild(oldChild);
141: }
142:
143: public Node appendChild(Node newChild) throws DOMException {
144: return getNode().appendChild(newChild);
145: }
146:
147: public boolean hasChildNodes() {
148: return getNode().hasChildNodes();
149: }
150:
151: public Node cloneNode(boolean deep) {
152: return getNode().cloneNode(deep);
153: }
154:
155: public void normalize() {
156: getNode().normalize();
157: }
158:
159: public boolean isSupported(String feature, String version) {
160: return getNode().isSupported(feature, version);
161: }
162:
163: public String getNamespaceURI() {
164: return getNode().getNamespaceURI();
165: }
166:
167: public String getPrefix() {
168: return getNode().getPrefix();
169: }
170:
171: public void setPrefix(String prefix) throws DOMException {
172: getNode().setPrefix(prefix);
173: }
174:
175: public String getLocalName() {
176: return getNode().getLocalName();
177: }
178:
179: public boolean hasAttributes() {
180: return getNode().hasAttributes();
181: }
182:
183: public String getBaseURI() {
184: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
185: throw new ItsNatException("DOM 3 is not supported");
186: }
187:
188: public short compareDocumentPosition(Node other)
189: throws DOMException {
190: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
191: throw new ItsNatException("DOM 3 is not supported");
192: }
193:
194: public String getTextContent() throws DOMException {
195: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
196: throw new ItsNatException("DOM 3 is not supported");
197: }
198:
199: public void setTextContent(String textContent) throws DOMException {
200: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
201: throw new ItsNatException("DOM 3 is not supported");
202: }
203:
204: public boolean isSameNode(Node other) {
205: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
206: throw new ItsNatException("DOM 3 is not supported");
207: }
208:
209: public String lookupPrefix(String namespaceURI) {
210: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
211: throw new ItsNatException("DOM 3 is not supported");
212: }
213:
214: public boolean isDefaultNamespace(String namespaceURI) {
215: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
216: throw new ItsNatException("DOM 3 is not supported");
217: }
218:
219: public String lookupNamespaceURI(String prefix) {
220: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
221: throw new ItsNatException("DOM 3 is not supported");
222: }
223:
224: public boolean isEqualNode(Node arg) {
225: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
226: throw new ItsNatException("DOM 3 is not supported");
227: }
228:
229: public Object getFeature(String feature, String version) {
230: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
231: throw new ItsNatException("DOM 3 is not supported");
232: }
233:
234: public Object setUserData(String key, Object data,
235: UserDataHandler handler) {
236: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
237: throw new ItsNatException("DOM 3 is not supported");
238: }
239:
240: public Object getUserData(String key) {
241: // Para que compile con el JDK 1.5 (Node tiene métodos del DOM 3)
242: throw new ItsNatException("DOM 3 is not supported");
243: }
244:
245: public void removeEventListener(String type,
246: EventListener listener, boolean useCapture) {
247: itsNatDoc.removeEventListener((EventTarget) getNode(), type,
248: listener, useCapture);
249: }
250:
251: public void addEventListener(String type, EventListener listener,
252: boolean useCapture) {
253: itsNatDoc.addEventListener((EventTarget) getNode(), type,
254: listener, useCapture);
255: }
256:
257: public boolean dispatchEvent(Event evt) throws EventException {
258: return itsNatDoc.dispatchEvent((EventTarget) getNode(), evt);
259: }
260: }
|