|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object sk.baka.ikslibs.modify.DOMMutils
public final class DOMMutils
Utility class for the document mutation. Does not check if the mutation operation breaks validity of the document.
Method Summary | |
---|---|
static void |
clearAttributes(Node node)
Removes all attribute nodes (including xmlns: attributes)
from given node. |
static boolean |
clearChildren(Node node,
boolean checkUndeletable)
Deletes all child nodes in the node (all nodes from the document, for example). |
static Attr |
createAttribute(Element e,
QName qname,
String value)
Creates an attribute. |
static Attr |
createAttribute(Element e,
String prefix,
String localName,
String namespace,
String value)
Creates an attribute. |
static Attr |
createXmlnsAttribute(Element e,
String namespaceUri,
String prefix)
Creates an xmlns attribute, that maps given namespace to a
prefix. |
static void |
createXmlnsAttributes(Element e,
Map<String,String> namespaces)
Adds and/or replaces xmlns mapping for all given namespaces in given element. |
static DocumentFragment |
cut(DOMInterval interval)
Tries to remove all nodes in the specified range. |
static NodeListID |
delete(NodeListID list,
int fromPos,
int toPos)
Tries to delete specified range of text. |
static NodeListID |
insertEntity(NodeListID list,
int pos,
String entityName,
EntityManager em)
Inserts an entity into specified position. |
static void |
insertFragment(DocumentFragment frag,
DomPointer ptr)
Inserts all nodes from specified document fragment into specified position. |
static void |
insertNode(Node node,
DomPointer ptr,
boolean merge)
Inserts the node at the specified pointer. |
static CharacterData |
insertText(DOMPoint p,
Element contextElement,
String value,
short type)
Inserts text into element, depending on given parameters. |
static CharacterData |
insertText(DomPointer point,
String value,
short type)
Inserts text into element, depending on given parameters. |
static NodeListID |
insertText(NodeListID list,
int pos,
String text,
short type)
Tries to insert given text into specified position. |
boolean |
isRemovable(DomPointer from,
DomPointer to)
Checks whether all nodes from specified range can be deleted - i.e. |
static void |
mergeTextNodes(Node node,
MergeTypeEnum mergeType)
Merges the adjacent text nodes into one. |
static void |
mergeTextNodesOnly(Node node,
MergeTypeEnum mergeType)
Merges the adjacent text nodes into one. |
static DocumentFragment |
moveContentToFragment(Document doc)
Moves all content nodes from the document into newly created document fragment. |
static void |
normalizeWhitespaces(Node node)
If the node is a text node then all starting and trailing whitespaces are removed, and all consecutive whitespaces are replaced by a single space. |
static String |
normalizeWhitespaces(String string)
All starting and trailing whitespaces are removed, and all consecutive whitespaces are replaced by a single space character. |
static void |
normalizeWhitespacesTree(Node node)
Whitespace-normalizes all nodes that are descendants of given node. |
static void |
remove(DOMInterval interval)
Tries to remove all nodes in the specified range. |
static void |
remove(Node node)
Removes given node from the document. |
static void |
remove(NodeListID nodeList)
Removes node(s) from the document. |
static void |
removeIf(Node node,
boolean onlyIfEmpty,
boolean onlyCharacterContainer)
Removes node from its parent (or owner element in case of an attribute). |
static void |
removeXmlns(Element e)
Removes all xmlns attributes from given element. |
void |
replaceNode(Node node,
Node newNode)
Replaces the node with another node. |
static boolean |
setData(Node node,
String data)
Modifies data, associated with given node. |
static void |
setText(NodeListID nodeList,
String value)
Sets text, denoted by given id, to given value. |
static void |
setText(Node node,
String value)
Sets text of given node to given value. |
static Text |
splitText(DomPointer ptr)
Splits the text/cdata node at the specified position. |
static Text |
splitText(NodeListID list,
int pos)
Splits the text/cdata node at the specified position. |
static Text |
splitText(Text text,
int pos)
Splits the text/cdata node at the specified position. |
static DocumentFragment |
toFragment(Node node)
Converts the node into a document fragment. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
---|
public static void normalizeWhitespaces(Node node)
node
- the node to modifypublic static String normalizeWhitespaces(String string)
string
- the string to normalize
public static void normalizeWhitespacesTree(Node node)
node
- the root of the tree to normalize.public static void setText(NodeListID nodeList, String value)
nodeList
- the node list that will receive new value.value
- new text value. null
value is equivalent to
an empty string.public static void setText(Node node, String value)
node
- the text node, that receives new value.value
- new text value. null
value is equivalent to
an empty string.public static Text splitText(NodeListID list, int pos)
list
- the nodelist.pos
- zero-based position where the text shall be splitted.
null
when no splitting occured.public static Text splitText(Text text, int pos)
text
- the text/cdata nodepos
- zero-based position where the text shall be splitted.
null
when no splitting occured.public static Text splitText(DomPointer ptr)
ptr
- the pointer pointing into the text/cdata node.
null
when no splitting occured.public static CharacterData insertText(DomPointer point, String value, short type)
point
- insert point, where to insert new text. It must not point
into an entity.value
- new text value. if null
or empty string ("")
is given, then nothing is created.type
- type of created node, one of Node.TEXT_NODE
or
Node.CDATA_SECTION_NODE
constants. When modifying text,
this parameter is ignored.
null
is returned.public static CharacterData insertText(DOMPoint p, Element contextElement, String value, short type)
p
- insert point, where to insert new text.contextElement
- the context element, where new text must be
inserted.value
- new text value. if null
or empty string ("")
is given, then nothing is created.type
- type of created node, one of Node.TEXT_NODE
or
Node.CDATA_SECTION_NODE
constants. When modifying text,
this parameter is ignored.
null
is returned.public static void remove(NodeListID nodeList)
nodeList
- nodes that shall be removed.public static void remove(Node node)
node
- the node, that will be removed. Its id is removed aswell, but
the id of descendants is not removed.public static void removeIf(Node node, boolean onlyIfEmpty, boolean onlyCharacterContainer)
node
- the node to removeonlyIfEmpty
- if true
then the node is removed only
if it has zero children (or empty data in case of character container).onlyCharacterContainer
- if true
then only character
containers are removed (empty elements are not removed)public static Attr createAttribute(Element e, String prefix, String localName, String namespace, String value)
e
- element, that contains the attribute.prefix
- prefix of qname of attribute. See
NamespaceManager
for details.localName
- local name of attribute.namespace
- namespace of attribute.value
- new value for attribute.
public static Attr createAttribute(Element e, QName qname, String value)
e
- element, that contains the attribute.qname
- qualified name of the attributevalue
- new value for attribute.
public static void insertNode(Node node, DomPointer ptr, boolean merge)
node
- the node to insert, it must be element, comment, pi, text or
cdata only.ptr
- where to insert the node.merge
- if true
then if new node is a text node then
it will be merged with adjacent nodes if possible. If false
then no merging is performed.public static void insertFragment(DocumentFragment frag, DomPointer ptr)
emp:id
attributes are ignored. All nodes are removed from the fragment in the
process and become part of the document.
frag
- the fragmentptr
- where to insert nodes from the fragment.public void replaceNode(Node node, Node newNode)
newNode
must have
null
parent or it must be from another document.
node
- the node to be replaced.newNode
- the node will be replaced with this node. If
null
then given node is removed. If not from our document
then it is automatically imported.public boolean isRemovable(DomPointer from, DomPointer to)
delete
with these parameters shall succeed.
from
- start of the removal interval. The node where this pointer
points shall be removed (an exception is if some contents of the node are
left after the operation).to
- end of the removal interval.
true
if the specified interval is removable,
false
otherwise.public static void remove(DOMInterval interval)
Tries to remove all nodes in the specified range. The function shall not remove elements that shall not have all its children removed. A quick normalization is performed after the node removal.
For description of removed contents please see
Range.deleteContents()
and DOM
ranges.
interval
- the interval to be removed. It must not be attribute
interval.public static DocumentFragment cut(DOMInterval interval)
Tries to remove all nodes in the specified range. Removed nodes are
returned in a DocumentFragment
instance. A quick
normalization is performed after the node removal.
For description of removed contents please see
Range.extractContents()
and DOM
ranges.
interval
- the interval to be removed. It must not be attribute
interval.
DocumentFragment
instance containing nodes that
have been cut from the document. Never null
.public static NodeListID insertText(NodeListID list, int pos, String text, short type)
list
- current list of nodes, where to insert the text.pos
- the position in the textValue
string.type
- type of created node, one of Node.TEXT_NODE
or
Node.CDATA_SECTION_NODE
constants. When modifying text,
this parameter is ignored.text
- the text to be inserted
IllegalArgumentException
- if no text can be inserted at specified
position.public static NodeListID insertEntity(NodeListID list, int pos, String entityName, EntityManager em)
list
- current list of nodes, where to insert the entity.pos
- the position in the textValue
string.entityName
- the name of the entity.em
- the entity manager for the document.
public static NodeListID delete(NodeListID list, int fromPos, int toPos)
list
- list of text/cdata nodes. If function finishes succesfully
and the document gets modified, this list is invalid and must not be
used.fromPos
- the starting positiontoPos
- the ending position - character at this position shall not
be deleted.
public static void mergeTextNodes(Node node, MergeTypeEnum mergeType)
node
- denotes the placement of the nodes to merge. If node is a
container node then all its children are mergedmergeType
- one of the merge constants.public static void mergeTextNodesOnly(Node node, MergeTypeEnum mergeType)
node
- denotes the placement of the nodes to merge.mergeType
- one of the merge constants.public static void removeXmlns(Element e)
e
- element to modify.
UnremovableNodeException
- if an unremovable xmlns attribute
occured.public static void clearAttributes(Node node)
xmlns:
attributes)
from given node.
node
- remove attributes from this node. Only element can have
attributes hence function does nothing for other types of nodes.
UnremovableNodeException
- if some attributes are not removable.public static DocumentFragment toFragment(Node node)
moveContentToFragment
method. If node
is content node then it shall be the sole child of the returned fragment.
node
- node to convert.
IllegalArgumentException
- if node is not fragment, document nor
content node.public static DocumentFragment moveContentToFragment(Document doc)
doc
- the document node.
public static boolean clearChildren(Node node, boolean checkUndeletable)
node
- the node.checkUndeletable
- if true
then function will fail if
undeletable element occurs.
true
if all children were succesfully deleted,
false
otherwise.
UnremovableNodeException
- if one of its children cannot be
removed.public static boolean setData(Node node, String data)
node
- the node.data
- character data, that the node will contain. null
is threated as an empty string.
true
, if data was changed, or false
when the original data were the same as the new data.public static void createXmlnsAttributes(Element e, Map<String,String> namespaces)
e
- the element.namespaces
- collection of namespaces. All these namespaces'
mappings are added to element. Maps namespace to a prefix.public static Attr createXmlnsAttribute(Element e, String namespaceUri, String prefix)
xmlns
attribute, that maps given namespace to a
prefix. When an XML namespace is given then null
is
returned. Attribute is attached to the element.
e
- element, where to create element.namespaceUri
- creates xmlns mapping for this namespace.prefix
- the prefix.
null
if trying to map a
xmlns prefix and/or namespace.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |