Class XmlElement
- java.lang.Object
-
- net.ssehub.easy.instantiation.core.model.artifactModel.FragmentArtifact
-
- net.ssehub.easy.instantiation.core.model.artifactModel.CompositeFragmentArtifact
-
- net.ssehub.easy.instantiation.core.model.artifactModel.xml.XmlNode
-
- net.ssehub.easy.instantiation.core.model.artifactModel.xml.XmlElement
-
- All Implemented Interfaces:
IArtifact,IArtifactChangedListener,IXmlContainer,IStringValueProvider,IVilType
- Direct Known Subclasses:
XmlRootElement
public class XmlElement extends XmlNode implements IXmlContainer
Represents a parsed XML element with contained attributes.- Author:
- Holger Eichelberger, Patrik Pastuschek
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classXmlElement.NodeComparatorA (attribute) node comparator based on XML attributes.-
Nested classes/interfaces inherited from interface net.ssehub.easy.instantiation.core.model.vilTypes.IStringValueProvider
IStringValueProvider.StringComparator
-
-
Field Summary
Fields Modifier and Type Field Description private XmlAttribute[]attributesprivate java.lang.Stringnameprivate XmlNode[]nodes
-
Constructor Summary
Constructors Constructor Description XmlElement(XmlElement parent, java.lang.String name, XmlAttribute[] attributes, org.w3c.dom.Node node)Creates a new XML element fragment artifact.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) XmlAttributeaddAttribute(java.lang.String name, java.lang.String value)Adds an attribute to this XmlElement.(package private) XmlAttributeaddAttribute(java.lang.String name, java.lang.String value, boolean forceOverwrite)Adds an attribute to this XmlElement.(package private) voidaddChild(XmlNode child)Adds a child XmlElement to this XmlElement.(package private) XmlElementappendChild(XmlElement child, org.w3c.dom.Node childNode)Appends a new child.Sequence<XmlAttribute>attributes()Returns all attributes.static XmlElementbuildAttribute(XmlElement parent, java.lang.String name, java.lang.String value)Creates a new Attribute for given XmlElement, with given name and value, but returns the parent XML element.static XmlElementbuildAttribute(XmlElement parent, java.lang.String name, java.lang.String value, boolean forceOverwrite)Creates a new Attribute for given XmlElement, with given name and value, but returns the parent.static XmlElementbuildElement(XmlElement parent, java.lang.String name, java.lang.String contents)Creates a new XmlElement as child of given parent, with given name, but returns the parent element.Sequence<XmlComment>comments()Returns all nested comments in sequence.static XmlElementcreate(XmlElement parent, java.lang.String name, java.lang.String contents)Creates a new XmlElement as child of given parent, with given name.static XmlElementcreate(XmlFileArtifact parent, java.lang.String name, java.lang.String contents)Creates a new XmlElement as child of the root element of the given file artifact, with given name.(package private) voiddeleteAttribute(XmlAttribute attribute)Deletes an attribute (internal function, called from attribute).(package private) voiddeleteChild(XmlNode child)Removes a child from this Element.Sequence<XmlElement>elements()Returns all nested elements in sequence.booleanexists()Returns whether this artifact is exists.private java.lang.reflect.FieldfindField(java.lang.Class<?> cls, java.lang.String name)Findes a field callednameinclsor one of its super classes except for Object (not needed here).XmlAttributegetAttribute(java.lang.String name)Returns a specific attribute.(package private) XmlNodegetChild(int index)Returns the child at the givenindex.(package private) intgetChildCount()Returns the number of children.private EASyLoggerFactory.EASyLoggergetLogger()Returns the EASy logger.java.lang.StringgetName()Returns the name of this artifact.(package private) java.lang.StringgetNameSafe()Returns the name of this attribute without potential exception.java.lang.StringgetStringValue(IStringValueProvider.StringComparator comparator)Returns the string value of this object.(package private) booleanhasChild(XmlElement child)Checks whether this element has child as a child.private voidinsertElement(XmlNode elem, XmlNode previous)Inserts an XmlElement at a certain position.private voidlogSequenceException(java.lang.String text, java.lang.Exception ex)Logs a sequence synchronization exception.Set<XmlNode>nodes()Returns all nested nodes in sequence.private <T> Sequence<T>projectNodes(java.lang.Class<? extends T> type)Projects nodes to a node type.voidrename(java.lang.String name)Renames this artifact.(package private) voidrenameAttribute(XmlAttribute attribute, java.lang.String name)Renames an Attribute (internal called from XmlAttribute).Set<? extends FragmentArtifact>selectAll()Returns all artifacts this composite artifact is composed of.Set<XmlAttribute>selectAttributeByRegex(java.lang.String regEx)Returns a specific attribute.Set<XmlElement>selectByName(java.lang.String name)Returns a Set with all XmlElements matching name.Set<XmlElement>selectByName(java.lang.String name, boolean caseSensitive)Returns a Set with all XmlElements matching name.Set<XmlElement>selectByPath(java.lang.String path)Selects XML elements according to a given path.Set<XmlElement>selectByRegEx(java.lang.String regEx)Returns a set of all XmlElements specified by regEx.Set<XmlElement>selectByXPath(java.lang.String path)Selects XML elements according to a given XPath.voidsetAttribute(java.lang.String name, java.lang.String value)Directly modifies an attribute.(package private) voidsetName(java.lang.String name)Changes the name of this element.(package private) voidsetNodes(XmlNode[] nodes)Replaces the child nodes of this element.(package private) voidsortAttributes(org.xml.sax.Attributes att)Sorts the attribute sequence of this element according to the given SAX attribute set.(package private) voidsynchronizeAttributeSequence()Synchronizes the attribute sequence.-
Methods inherited from class net.ssehub.easy.instantiation.core.model.artifactModel.xml.XmlNode
accept, artifactChanged, checkRoot, checkValidity, delete, getBinary, getCdata, getCdataDirect, getFile, getNode, getParent, getText, getTextDirect, notifyChange, notifyChange, setCdata, setNode, update
-
Methods inherited from class net.ssehub.easy.instantiation.core.model.artifactModel.FragmentArtifact
lastModification, store
-
-
-
-
Field Detail
-
nodes
private XmlNode[] nodes
-
attributes
private XmlAttribute[] attributes
-
name
private java.lang.String name
-
-
Constructor Detail
-
XmlElement
XmlElement(XmlElement parent, java.lang.String name, XmlAttribute[] attributes, org.w3c.dom.Node node)
Creates a new XML element fragment artifact.- Parameters:
parent- the parent artifactname- the name of the elementattributes- the attributesnode- the node that is represented by this XmlElement
-
-
Method Detail
-
create
public static XmlElement create(XmlElement parent, java.lang.String name, java.lang.String contents) throws VilException
Creates a new XmlElement as child of given parent, with given name.- Parameters:
parent- The parent of the new XmlElement.name- The name of the new XmlElement.contents- optional initial contents, ignored if empty- Returns:
- The created XmlElement.
- Throws:
VilException- if element could not be created.
-
create
public static XmlElement create(XmlFileArtifact parent, java.lang.String name, java.lang.String contents) throws VilException
Creates a new XmlElement as child of the root element of the given file artifact, with given name.- Parameters:
parent- The parent of the new XmlElement.name- The name of the new XmlElement.contents- optional initial contents, ignored if empty- Returns:
- The created XmlElement.
- Throws:
VilException- if element could not be created.
-
buildElement
public static XmlElement buildElement(XmlElement parent, java.lang.String name, java.lang.String contents) throws VilException
Creates a new XmlElement as child of given parent, with given name, but returns the parent element.- Parameters:
parent- The parent of the new XmlElement.name- The name of the new XmlElement.contents- optional initial contents, ignored if empty- Returns:
parent(element builder style)- Throws:
VilException- if element could not be created.
-
buildAttribute
public static XmlElement buildAttribute(XmlElement parent, java.lang.String name, java.lang.String value) throws VilException
Creates a new Attribute for given XmlElement, with given name and value, but returns the parent XML element.- Parameters:
parent- The XmlElement the attribute is for.name- The name of the new attribute.value- The value of the new attribute.- Returns:
parent(element builder style)- Throws:
VilException- if attribute could not be created.
-
buildAttribute
public static XmlElement buildAttribute(XmlElement parent, java.lang.String name, java.lang.String value, boolean forceOverwrite) throws VilException
Creates a new Attribute for given XmlElement, with given name and value, but returns the parent. Overwrites by default.- Parameters:
parent- The XmlElement the attribute is for.name- The name of the attribute.value- The value of the new attribute.forceOverwrite- True if existing attributes with same name shall be overwritten. Default = true.- Returns:
parent(element builder style)- Throws:
VilException- if attribute could not be created for different reason then pre-existance.
-
findField
private java.lang.reflect.Field findField(java.lang.Class<?> cls, java.lang.String name)Findes a field callednameinclsor one of its super classes except for Object (not needed here).- Parameters:
cls- the class to searchname- the name of the field- Returns:
- the field or null if not found
-
sortAttributes
void sortAttributes(org.xml.sax.Attributes att)
Sorts the attribute sequence of this element according to the given SAX attribute set.- Overrides:
sortAttributesin classXmlNode- Parameters:
att- the SAX attributes
-
synchronizeAttributeSequence
void synchronizeAttributeSequence()
Synchronizes the attribute sequence. This is a horrible hack. Reason: Extending JDK-Xalan is not really possible (restricted access, implementation requires certain class hierarchy preventing delegation) and we do not want to extend the EASy deployment by Eclipse-Xalan and its dependency. So access the attribute implementation and sort the nodes in there accordingly.- Overrides:
synchronizeAttributeSequencein classXmlNode
-
logSequenceException
private void logSequenceException(java.lang.String text, java.lang.Exception ex)Logs a sequence synchronization exception.- Parameters:
text- the text indicating the problem stated by the exceptionex- the exception detailing the problem (may be null)
-
deleteChild
void deleteChild(XmlNode child)
Description copied from class:XmlNodeRemoves a child from this Element.- Specified by:
deleteChildin classXmlNode- Parameters:
child- the child that is to be removed.
-
getName
public java.lang.String getName() throws VilExceptionDescription copied from interface:IArtifactReturns the name of this artifact.- Specified by:
getNamein interfaceIArtifact- Returns:
- the name of this artifact
- Throws:
VilException- in case of serious problems so that this operation cannot continue
-
getNameSafe
java.lang.String getNameSafe()
Description copied from class:XmlNodeReturns the name of this attribute without potential exception.- Overrides:
getNameSafein classXmlNode- Returns:
- the name
-
rename
public void rename(java.lang.String name) throws VilExceptionDescription copied from interface:IArtifactRenames this artifact.- Specified by:
renamein interfaceIArtifact- Parameters:
name- the new name of this artifact- Throws:
VilException- in case of serious problems so that this operation cannot continue
-
selectAll
public Set<? extends FragmentArtifact> selectAll() throws VilException
Description copied from class:CompositeFragmentArtifactReturns all artifacts this composite artifact is composed of.- Specified by:
selectAllin classCompositeFragmentArtifact- Returns:
- all fragments
- Throws:
VilException- in case that this element or query is invalid
-
exists
public boolean exists()
Description copied from interface:IArtifactReturns whether this artifact is exists.- Specified by:
existsin interfaceIArtifact- Overrides:
existsin classFragmentArtifact- Returns:
trueif this artifact exists,falseelse
-
attributes
public Sequence<XmlAttribute> attributes() throws VilException
Returns all attributes.- Returns:
- the attributes
- Throws:
VilException- in case that access is not possible
-
nodes
public Set<XmlNode> nodes() throws VilException
Returns all nested nodes in sequence.- Returns:
- the nodes
- Throws:
VilException- in case that access is not possible
-
comments
public Sequence<XmlComment> comments()
Returns all nested comments in sequence.- Returns:
- the elements
- Throws:
VilException- in case that access is not possible
-
elements
public Sequence<XmlElement> elements() throws VilException
Returns all nested elements in sequence.- Returns:
- the elements
- Throws:
VilException- in case that access is not possible
-
projectNodes
private <T> Sequence<T> projectNodes(java.lang.Class<? extends T> type)
Projects nodes to a node type.- Type Parameters:
T- the target element type- Parameters:
type- the type to project to- Returns:
- the projected elements (may be empty)
-
getAttribute
public XmlAttribute getAttribute(java.lang.String name) throws VilException
Returns a specific attribute.- Parameters:
name- the name of the attribute (may be a regular expression)- Returns:
- the attribute or null if there is none
- Throws:
VilException- in case of already deleted attributes
-
setAttribute
public void setAttribute(java.lang.String name, java.lang.String value) throws VilExceptionDirectly modifies an attribute. [convenience]- Parameters:
name- the name of the attributevalue- the new value of the attribute- Throws:
VilException- in case that the attribute cannot be found
-
selectAttributeByRegex
public Set<XmlAttribute> selectAttributeByRegex(java.lang.String regEx) throws VilException
Returns a specific attribute.- Parameters:
regEx- a regular expression specifying the attributes to be selected- Returns:
- the set of attribute, being empty if there is none
- Throws:
VilException- in case thatregExis invalid
-
selectByName
public Set<XmlElement> selectByName(java.lang.String name) throws VilException
Description copied from interface:IXmlContainerReturns a Set with all XmlElements matching name. Childs are processed aswell.
Uses case sensitivity by default.- Specified by:
selectByNamein interfaceIXmlContainer- Parameters:
name- The required name.- Returns:
- set with matching XmlElements.
- Throws:
VilException- in case that invalid Elements are used.
-
selectByPath
public Set<XmlElement> selectByPath(java.lang.String path) throws VilException
Description copied from interface:IXmlContainerSelects XML elements according to a given path. We may go for XPath expressions in future.- Specified by:
selectByPathin interfaceIXmlContainer- Parameters:
path- the path to the elements- Returns:
- the matching elements (may be empty)
- Throws:
VilException- in case invalid XmlElements are used.
-
selectByXPath
public Set<XmlElement> selectByXPath(java.lang.String path) throws VilException
Description copied from interface:IXmlContainerSelects XML elements according to a given XPath.- Specified by:
selectByXPathin interfaceIXmlContainer- Parameters:
path- the XPath to the elements- Returns:
- the matching elements (may be empty)
- Throws:
VilException- in case invalid XmlElements are used.
-
selectByName
public Set<XmlElement> selectByName(java.lang.String name, boolean caseSensitive) throws VilException
Returns a Set with all XmlElements matching name. Childs are processed aswell.
Case sensitivity can be set.- Parameters:
name- The required name.caseSensitive- True if case-sensitive, false otherwise.- Returns:
- set with matching XmlElements.
- Throws:
VilException- in case that invalid Elements are used.
-
selectByRegEx
public Set<XmlElement> selectByRegEx(java.lang.String regEx) throws VilException
Returns a set of all XmlElements specified by regEx. Childs are processed aswell.
To get a set of all XmlElements having a certain pre- and suffix use:
prefix.*suffix (replace prefix and suffix).
To enable case insensitivity use (?i) :
(?i) cASe iS noT imPORtaNt- Parameters:
regEx- a regular expression specifying the elements to be selected- Returns:
- Set of XmlElements matching the regEx.
- Throws:
VilException- in case that invalid Elements are used.
-
deleteAttribute
void deleteAttribute(XmlAttribute attribute)
Deletes an attribute (internal function, called from attribute).- Parameters:
attribute- the attribute to be deleted.
-
renameAttribute
void renameAttribute(XmlAttribute attribute, java.lang.String name) throws VilException
Renames an Attribute (internal called from XmlAttribute).- Parameters:
attribute- the attribute to be renamed.name- the new name of this attribute- Throws:
VilException- in case that invalid attributes are used
-
setName
void setName(java.lang.String name)
Changes the name of this element.- Parameters:
name- the new name of this element.
-
setNodes
void setNodes(XmlNode[] nodes)
Replaces the child nodes of this element.- Parameters:
nodes- An array with all child nodes.
-
hasChild
boolean hasChild(XmlElement child)
Checks whether this element has child as a child.- Parameters:
child- the child element to check for.- Returns:
- true if child is a child. false otherwise.
-
addChild
void addChild(XmlNode child) throws VilException
Adds a child XmlElement to this XmlElement.- Parameters:
child- The new child.- Throws:
VilException- If child could not be added.
-
appendChild
XmlElement appendChild(XmlElement child, org.w3c.dom.Node childNode) throws VilException
Appends a new child. Default position is after last child of the parent.- Parameters:
child- The new child XmlElement.childNode- The new child Node.- Returns:
- The created XmlElement.
- Throws:
VilException- If child could not be appended.
-
addAttribute
XmlAttribute addAttribute(java.lang.String name, java.lang.String value) throws VilException
Adds an attribute to this XmlElement.- Parameters:
name- The name of the new attribute.value- The value of the new attribute.- Returns:
- The created attribute.
- Throws:
VilException- If attribute could not be added.
-
addAttribute
XmlAttribute addAttribute(java.lang.String name, java.lang.String value, boolean forceOverwrite) throws VilException
Adds an attribute to this XmlElement. Overwrites by default.- Parameters:
name- The name of the new attribute.value- The value of the new attribute.forceOverwrite- True if existing attributes with same name shall be overwritten. Default = true.- Returns:
- The created attribute.
- Throws:
VilException- If attribute could not be added.
-
insertElement
private void insertElement(XmlNode elem, XmlNode previous) throws VilException
Inserts an XmlElement at a certain position.- Parameters:
elem- The XmlElement that is to be inserted.previous- The XmlElement previous to the new XmlElement. If null, XmlElement will be inserted as first.- Throws:
VilException- if XmlElement could not be inserted for whatever reason.
-
getStringValue
public java.lang.String getStringValue(IStringValueProvider.StringComparator comparator)
Description copied from interface:IStringValueProviderReturns the string value of this object.- Specified by:
getStringValuein interfaceIStringValueProvider- Parameters:
comparator- if the sequence of elements, e.g., in case of collections, shall be ordered according to the comparator, null if the original sequence shall be returned- Returns:
- the string value
-
getChildCount
int getChildCount()
Description copied from class:XmlNodeReturns the number of children.- Overrides:
getChildCountin classXmlNode- Returns:
- the number of children
-
getChild
XmlNode getChild(int index)
Description copied from class:XmlNodeReturns the child at the givenindex.
-
getLogger
private EASyLoggerFactory.EASyLogger getLogger()
Returns the EASy logger.- Returns:
- the logger
-
-