org.vectomatic.dom.svg.OMDocument Maven / Gradle / Ivy
Show all versions of lib-gwt-svg Show documentation
/**********************************************
* Copyright (C) 2010 Lukas Laag
* This file is part of lib-gwt-svg.
*
* libgwtsvg is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libgwtsvg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libgwtsvg. If not, see http://www.gnu.org/licenses/
**********************************************/
/*
* Copyright (c) 2004 World Wide Web Consortium,
*
* (Massachusetts Institute of Technology, European Research Consortium for
* Informatics and Mathematics, Keio University). All Rights Reserved. This
* work is distributed under the W3C(r) Software License [1] in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
*/
package org.vectomatic.dom.svg;
import org.vectomatic.dom.svg.utils.DOMHelper;
import org.w3c.dom.DOMException;
import com.google.gwt.core.client.JavaScriptException;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
/**
* Wrapper class for DOM Document
* @author laaglu
*/
public class OMDocument extends OMNode {
/**
* Constructor
* @param document The wrapped document
*/
protected OMDocument(Document document) {
super(document);
}
/**
* Returns the wrapped {@link com.google.gwt.dom.client.Document}
* @return the wrapped {@link com.google.gwt.dom.client.Document}
*/
public Document getDocument() {
return ot.cast();
}
// Implementation of the dom::Document W3C IDL interface
/**
* Creates an {@link OMElement} of the given qualified name and namespace URI.
*
Per [XML Namespaces]
* , applications must use the value null
as the
* namespaceURI parameter for methods if they wish to have no namespace.
* @param namespaceURI The namespace URI of the element to create.
* @param qualifiedName The qualified name of the element type to
* instantiate.
* @return A new {@link OMElement} object with the following
* attributes:
*
*
* Attribute
* Value
*
*
* Node.nodeName
*
* qualifiedName
*
*
* Node.namespaceURI
*
* namespaceURI
*
*
* Node.prefix
* prefix, extracted
* from qualifiedName
, or null
if there is
* no prefix
*
*
* Node.localName
* local name, extracted from
* qualifiedName
*
*
* Element.tagName
*
* qualifiedName
*
*
* @exception DOMException
* INVALID_CHARACTER_ERR: Raised if the specified
* qualifiedName
is not an XML name according to the XML
* version in use specified in the Document.xmlVersion
* attribute.
*
NAMESPACE_ERR: Raised if the qualifiedName
is a
* malformed qualified name, if the qualifiedName
has a
* prefix and the namespaceURI
is null
, or
* if the qualifiedName
has a prefix that is "xml" and
* the namespaceURI
is different from "
* http://www.w3.org/XML/1998/namespace" [XML Namespaces]
* , or if the qualifiedName
or its prefix is "xmlns" and
* the namespaceURI
is different from "http://www.w3.org/2000/xmlns/", or if the namespaceURI
is "http://www.w3.org/2000/xmlns/" and neither the qualifiedName
nor its prefix is "xmlns".
*
NOT_SUPPORTED_ERR: Always thrown if the current document does not
* support the "XML"
feature, since namespaces were
* defined by XML.
*/
public final OMElement createElementNS(String namespaceURI, String qualifiedName) throws JavaScriptException {
return OMNode.convert(DOMHelper.createElementNS(((Document)ot), namespaceURI, qualifiedName));
}
/**
* Creates a new {@link OMText} node and initializes it
* with the specified data. The node is not attached to the
* DOM tree.
* @param data The string to initialize the text node
* @return The newly created {@link OMText} node
*/
public final OMText createTextNode(String data) {
return OMNode.convert(((Document)ot).createTextNode(data));
}
/**
* Returns a OMNodeList
of all the OMElements
in
* document order with a given tag name and are contained in the
* document.
* @param tagname The name of the tag to match on. The special value "*"
* matches all tags. For XML, the tagname
parameter is
* case-sensitive, otherwise it depends on the case-sensitivity of the
* markup language in use.
* @return A new OMNodeList
object containing all the matched
* Elements
.
*/
public final OMNodeList getElementsByTagName(String tagname) {
return OMNode.convertList(((Document)ot).getElementsByTagName(tagname));
}
/**
* Returns a OMNodeList
of all the OMElements
with a
* given local name and namespace URI in document order.
* @param namespaceURI The namespace URI of the elements to match on. The
* special value "*"
matches all namespaces.
* @param localName The local name of the elements to match on. The
* special value "*" matches all local names.
* @return A new OMNodeList
object containing all the matched
* Elements
.
*/
public final OMNodeList getElementsByTagNameNS(String namespaceURI, String localName) {
return OMNode.convertList(DOMHelper.getElementsByTagNameNS(((Document)ot), namespaceURI, localName));
}
/**
* Returns the OMElement
that has an ID attribute with the
* given value. If no such element exists, this returns null
* . If more than one element has an ID attribute with that value, what
* is returned is undefined.
*
The DOM implementation is expected to use the attribute
* Attr.isId
to determine if an attribute is of type ID.
* Note: Attributes with the name "ID" or "id" are not of type
* ID unless so defined.
* @param elementId The unique id
value for an element.
* @return The matching element or null
if there is none.
*/
public final T getElementById(String elementId) {
Element elt = ((Document)ot).getElementById(elementId);
return elt != null ? OMNode.convert(elt) : null;
}
/**
* This is a convenience attribute that allows direct access to the child
* node that is the document element of the document.
*/
public final OMElement getDocumentElement() {
return OMNode.convert(((Document)ot).getDocumentElement());
}
/**
* Imports a node from another document to this document, without altering
* or removing the source node from the original document; this method
* creates a new copy of the source node. The returned node has no
* parent; (parentNode
is null
).
*
For all nodes, importing a node creates a node object owned by the
* importing document, with attribute values identical to the source
* node's nodeName
and nodeType
, plus the
* attributes related to namespaces (prefix
,
* localName
, and namespaceURI
). As in the
* cloneNode
operation, the source node is not altered.
* User data associated to the imported node is not carried over.
* However, if any UserDataHandlers
has been specified
* along with the associated data these handlers will be called with the
* appropriate parameters before this method returns.
*
Additional information is copied as appropriate to the
* nodeType
, attempting to mirror the behavior expected if
* a fragment of XML or HTML source was copied from one document to
* another, recognizing that the two documents may have different DTDs
* in the XML case. The following list describes the specifics for each
* type of node.
*
* - ATTRIBUTE_NODE
* - The
ownerElement
attribute
* is set to null
and the specified
flag is
* set to true
on the generated {@link OMAttr}. The
* descendants of the source {@link OMAttr} are recursively imported
* and the resulting nodes reassembled to form the corresponding subtree.
* Note that the deep
parameter has no effect on
* {@link OMAttr} nodes; they always carry their children with them
* when imported.
* - DOCUMENT_FRAGMENT_NODE
* - If the
deep
option
* was set to true
, the descendants of the source
* DocumentFragment
are recursively imported and the
* resulting nodes reassembled under the imported
* DocumentFragment
to form the corresponding subtree.
* Otherwise, this simply generates an empty
* DocumentFragment
.
* - DOCUMENT_NODE
* Document
* nodes cannot be imported.
* - DOCUMENT_TYPE_NODE
* DocumentType
* nodes cannot be imported.
* - ELEMENT_NODE
* - Specified attribute nodes of the source element are imported, and the generated
* {@link OMAttr} nodes are attached to the generated
* {@link OMElement}. Default attributes are not copied, though if the document being imported into defines default
* attributes for this element name, those are assigned. If the
*
importNode
deep
parameter was set to
* true
, the descendants of the source element are
* recursively imported and the resulting nodes reassembled to form the
* corresponding subtree.
* - ENTITY_NODE
* Entity
nodes can be
* imported, however in the current release of the DOM the
* DocumentType
is readonly. Ability to add these imported
* nodes to a DocumentType
will be considered for addition
* to a future release of the DOM.On import, the publicId
,
* systemId
, and notationName
attributes are
* copied. If a deep
import is requested, the descendants
* of the the source Entity
are recursively imported and
* the resulting nodes reassembled to form the corresponding subtree.
* -
* ENTITY_REFERENCE_NODE
* - Only the
EntityReference
itself is
* copied, even if a deep
import is requested, since the
* source and destination documents might have defined the entity
* differently. If the document being imported into provides a
* definition for this entity name, its value is assigned.
* - NOTATION_NODE
* -
*
Notation
nodes can be imported, however in the current
* release of the DOM the DocumentType
is readonly. Ability
* to add these imported nodes to a DocumentType
will be
* considered for addition to a future release of the DOM.On import, the
* publicId
and systemId
attributes are copied.
* Note that the deep
parameter has no effect on this type
* of nodes since they cannot have any children.
* -
* PROCESSING_INSTRUCTION_NODE
* - The imported node copies its
*
target
and data
values from those of the
* source node.Note that the deep
parameter has no effect
* on this type of nodes since they cannot have any children.
* - TEXT_NODE,
* CDATA_SECTION_NODE, COMMENT_NODE
* - These three types of nodes inheriting
* from
CharacterData
copy their data
and
* length
attributes from those of the source node.Note
* that the deep
parameter has no effect on these types of
* nodes since they cannot have any children.
*
* @param importedNode The node to import.
* @param deep If true
, recursively import the subtree under
* the specified node; if false
, import only the node
* itself, as explained above. This has no effect on nodes that cannot
* have any children, and on {@link OMAttr}, and
* EntityReference
nodes.
* @return The imported node that belongs to this Document
.
* @exception DOMException
* NOT_SUPPORTED_ERR: Raised if the type of node being imported is not
* supported.
*
INVALID_CHARACTER_ERR: Raised if one of the imported names is not
* an XML name according to the XML version in use specified in the
* Document.xmlVersion
attribute. This may happen when
* importing an XML 1.1 [XML 1.1] element
* into an XML 1.0 document, for instance.
*/
public final OMNode importNode(OMNode importedNode, boolean deep) {
return OMNode.convert(DOMHelper.importNode(((Document)ot), importedNode.getNode(), deep));
}
}