Class XmlFileArtifact
- java.lang.Object
-
- net.ssehub.easy.instantiation.core.model.artifactModel.CompositeArtifact
-
- net.ssehub.easy.instantiation.core.model.artifactModel.FileArtifact
-
- net.ssehub.easy.instantiation.core.model.artifactModel.xml.XmlFileArtifact
-
- All Implemented Interfaces:
IArtifact,IArtifactChangedListener,IFileSystemArtifact,IXmlContainer,IActualValueProvider,IStringValueProvider,IVilType
public class XmlFileArtifact extends FileArtifact implements IXmlContainer
Represents a parsed XML file artifact. Due to Java internal intelligent processing, it might be that the original encoding name is replaced by a normalized one. If this is not intended, the official encoding table of the JDK needs to be redefined (system property "com.sun.org.apache.xalan.internal.serialize.encodings"), e.g., to exclude the specific encoding... with possible side effects. Please note, that there is a comment end-of-line workaround in this class which is required for proper operation on some JDKs (OpenJDK 1.7). In future, this may be circumvented by using a distinct version of XALAN instead of the builtin one....- Author:
- Holger Eichelberger, Patrik Pastuschek
-
-
Nested Class Summary
-
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 org.w3c.dom.Documentdocprivate Dtddtdprivate java.lang.Stringencodingprivate java.io.Filefileprivate intindentationprivate longlastModificationprivate longlastPersistedprivate booleanomitXmlDeclarationprivate booleanomitXmlStandaloneprivate XmlElementrootElementprivate booleansynchronizeAttributeSequence-
Fields inherited from class net.ssehub.easy.instantiation.core.model.artifactModel.FileArtifact
DEFAULT
-
-
Constructor Summary
Constructors Constructor Description XmlFileArtifact(java.io.File file, ArtifactModel model)Creates a new XML file artifact.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidartifactChanged(java.lang.Object cause)Is called when the artifact was changed, e.g., to trigger a reanalysis of substructures.private XmlNodebuild(org.w3c.dom.Node node, XmlElement parent)Building up the representing object structure.private voidcleanTree(org.w3c.dom.Node parent)Cleans the dom tree from empty text nodes.protected voidconfigureTransformer(javax.xml.transform.Transformer transformer)Configures the transformer.static FileArtifactconvert(java.lang.String val)Conversion operation.static XmlFileArtifactconvert(IFileSystemArtifact val)Conversion operation.static FileArtifactcreate()Creates a temporary file artifact.private XmlAttribute[]createAttributes(org.w3c.dom.Node node, XmlElement element)Creates and returns all Attributes for given node and XmlElement.XmlElementcreateRootElement(java.lang.String name)Creates a root element if it does not exist.voiddelete()Deletes this artifact.private voiddeleteNode(org.w3c.dom.Node node)Deletes a node from its parent.private voidensureLineEndedComments(java.util.List<java.lang.String> lineEndedComments)Ensures line the line ends infileaccording to the previously collected line ends bygetLineEndedComments().private XmlCommentfill(org.w3c.dom.Node node, XmlComment element)Fills an XML comment with data.private XmlElementfill(org.w3c.dom.Node node, XmlElement element, java.util.List<java.lang.Integer> childIndexes)Fills an XML element with data.voidformat()Enforces re-loading, formatting and writing of this artifact.protected javax.xml.parsers.DocumentBuilderFactorygetDocumentBuilderFactory()Returns the document builder factory.java.lang.StringgetEncoding()Returns the encoding.private java.util.List<java.lang.String>getLineEndedComments()Return all line ended comments in sequence of their occurrence infile.XmlElementgetRootElement()Returns the Root Element.protected javax.xml.transform.TransformerFactorygetTransformerFactory()Returns the transformer builder factory.booleangetXmlStandalone()Returns the XML standalone flag.private org.w3c.dom.DocumentinitFromDoc(org.w3c.dom.Document doc)Initializes this instance fromdoc.private voidinitialize()(Re)initializes this artifact.private static booleanisLineEndChar(char ch)Returns whetherchis a line end character.private voidload(java.io.File file)Loads and builds the jdom-tree.private voidpruneWhitespaces(org.w3c.dom.NodeList nodes)Removes start and end childs within nodes, whyever xerces suddenly introduced this "feature".private voidreadDtd()Checks the base file for embedded DTDs.Set<? extends FragmentArtifact>selectAll()Returns all artifacts this composite artifact is composed of.Set<XmlElement>selectByName(java.lang.String name)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 that start with prefix and end with suffix.Set<XmlElement>selectByXPath(java.lang.String path)Selects XML elements according to a given XPath.Set<? extends XmlElement>selectChilds()Returns a set of all childs of the root element.voidsetEncoding(java.lang.String encoding)Defines the (input) encoding.voidsetIndentation(int indentation)Sets the number of whitespaces to use for auto-indenting this artifact while writing/storing.voidsetOmitXmlDeclaration(boolean omitXmlDeclaration)Defines whether the usual XML header declaration shall be omitted or printed while writing/storing this artifact.voidsetOmitXmlStandalone(boolean omitXmlStandalone)Defines whether the usual XML header standalone attribute shall be omitted or printed while writing/storing this artifact.voidsetSynchronizeAttributeSequence(boolean synchronizeAttributeSequence)Changes whether the attribute sequence of XML elements shall be synchronized between document and artifact fragments, i.e., the fragment sequence defines the sequence of the attributes.voidsetXmlStandalone(boolean standalone)Defines whether the XML artifact is standalone.voidstore()Stores this artifact in case of changes.private voidsynchronizeAttributes(java.io.File file)Synchronizes the XML attributes (depends onsynchronizeAttributeSequence).private voidsynchronizeAttributes(java.io.InputStream stream)Synchronizes the XML attributes (depends onsynchronizeAttributeSequence).booleansynchronizeAttributeSequence()Returns whether the attribute sequence of XML elements shall be synchronized between document and artifact fragments, i.e., the fragment sequence defines the sequence of the attributes.private org.w3c.dom.NodetoEmptyTextNode(org.w3c.dom.Node node)Turns a node into an empty text node if it is a text node and its content is empty (after trimming).private java.lang.StringtoYesNo(boolean yes)Turns a boolean into a transformer "yes" or "no" string.voidupdate()Called if the underlying real artifact may have been updated, e.g., by overriding it by another file.private voidwriteToFile()Writes the Dom tree back to the file.-
Methods inherited from class net.ssehub.easy.instantiation.core.model.artifactModel.FileArtifact
accept, convert, convert, copy, createBinary, createText, determineActualValue, exists, getArtifactModel, getName, getPath, getPathSegments, getPlainName, getStringValue, isTemporary, lastModification, move, rename, setExecutable, substituteExtension
-
Methods inherited from class net.ssehub.easy.instantiation.core.model.artifactModel.CompositeArtifact
getBinary, getBinaryInstance, getRepresentationChanged, getText, getTextInstance
-
-
-
-
Field Detail
-
rootElement
private XmlElement rootElement
-
doc
private org.w3c.dom.Document doc
-
file
private java.io.File file
-
dtd
private Dtd dtd
-
omitXmlDeclaration
private boolean omitXmlDeclaration
-
omitXmlStandalone
private boolean omitXmlStandalone
-
synchronizeAttributeSequence
private boolean synchronizeAttributeSequence
-
indentation
private int indentation
-
lastModification
private long lastModification
-
lastPersisted
private long lastPersisted
-
encoding
private java.lang.String encoding
-
-
Constructor Detail
-
XmlFileArtifact
XmlFileArtifact(java.io.File file, ArtifactModel model) throws VilExceptionCreates a new XML file artifact.- Parameters:
file- the file to read the artifact contents frommodel- the artifact model to create this folder artifact within- Throws:
VilException- if artifact could not be created.
-
-
Method Detail
-
initialize
private void initialize() throws VilException(Re)initializes this artifact.- Throws:
VilException- if artifact could not be created.
-
create
public static FileArtifact create() throws VilException
Creates a temporary file artifact.- Returns:
- the created file artifact
- Throws:
VilException- in case that the creation fails
-
delete
public void delete() throws VilExceptionDescription copied from interface:IArtifactDeletes this artifact.- Specified by:
deletein interfaceIArtifact- Overrides:
deletein classFileArtifact- Throws:
VilException- in case of serious problems so that this operation cannot continue
-
artifactChanged
public void artifactChanged(java.lang.Object cause) throws VilExceptionDescription copied from interface:IArtifactChangedListenerIs called when the artifact was changed, e.g., to trigger a reanalysis of substructures.- Specified by:
artifactChangedin interfaceIArtifactChangedListener- Overrides:
artifactChangedin classCompositeArtifact- Parameters:
cause- the cause for the change, e.g., an artifact representation- Throws:
VilException- in case that the required operations fail for some reason
-
store
public void store() throws VilExceptionDescription copied from interface:IArtifactStores this artifact in case of changes.- Specified by:
storein interfaceIArtifact- Overrides:
storein classFileArtifact- Throws:
VilException- in case of problems storing this artifact
-
selectAll
public Set<? extends FragmentArtifact> selectAll()
Description copied from class:CompositeArtifactReturns all artifacts this composite artifact is composed of.- Overrides:
selectAllin classFileArtifact- Returns:
- all fragments
-
getRootElement
public XmlElement getRootElement()
Returns the Root Element.- Returns:
- the representation of the root element
-
createRootElement
public XmlElement createRootElement(java.lang.String name) throws VilException
Creates a root element if it does not exist.- Parameters:
name- the name of the root element- Returns:
- the actual root element
- Throws:
VilException- in case that creating the root element fails
-
selectChilds
public Set<? extends XmlElement> selectChilds() throws VilException
Returns a set of all childs of the root element.- Returns:
- Set of all childs of the root element.
- Throws:
VilException
-
load
private void load(java.io.File file) throws VilExceptionLoads and builds the jdom-tree.- Parameters:
file- the file that is to be parsed.- Throws:
VilException- in case domTree could not be built.
-
initFromDoc
private org.w3c.dom.Document initFromDoc(org.w3c.dom.Document doc)
Initializes this instance fromdoc.- Parameters:
doc- the document to initialize from (may be null, is ignored then)- Returns:
doc
-
pruneWhitespaces
private void pruneWhitespaces(org.w3c.dom.NodeList nodes)
Removes start and end childs within nodes, whyever xerces suddenly introduced this "feature".- Parameters:
nodes- the nodes to prune
-
deleteNode
private void deleteNode(org.w3c.dom.Node node)
Deletes a node from its parent.- Parameters:
node- the node to be deleted (may be null)
-
toEmptyTextNode
private org.w3c.dom.Node toEmptyTextNode(org.w3c.dom.Node node)
Turns a node into an empty text node if it is a text node and its content is empty (after trimming).- Parameters:
node- the node (may be null)- Returns:
nodeor null
-
build
private XmlNode build(org.w3c.dom.Node node, XmlElement parent)
Building up the representing object structure.- Parameters:
node- the node to parse. (Will parse all childs aswell).parent- the representation of the parent node. (Null if root element).- Returns:
- an array of XmlElements including all child elements.
-
fill
private XmlElement fill(org.w3c.dom.Node node, XmlElement element, java.util.List<java.lang.Integer> childIndexes)
Fills an XML element with data.- Parameters:
node- the XML node to take the data fromelement- the XML elementchildIndexes- the child indexes indicating the children to take over- Returns:
element
-
fill
private XmlComment fill(org.w3c.dom.Node node, XmlComment element)
Fills an XML comment with data.- Parameters:
node- the node to take data fromelement- the XML comment to fill- Returns:
comment
-
getLineEndedComments
private java.util.List<java.lang.String> getLineEndedComments()
Return all line ended comments in sequence of their occurrence infile. This is part of a workaround as in some JDKs the line ends of comments seem to be omitted.- Returns:
- the line ends in sequence
-
ensureLineEndedComments
private void ensureLineEndedComments(java.util.List<java.lang.String> lineEndedComments)
Ensures line the line ends infileaccording to the previously collected line ends bygetLineEndedComments(). This is part of a workaround as in some JDKs the line ends of comments seem to be omitted.- Parameters:
lineEndedComments- the line ended comments in sequence of their occurrence
-
isLineEndChar
private static boolean isLineEndChar(char ch)
Returns whetherchis a line end character.- Parameters:
ch- the character to be analyzed- Returns:
trueif it is a line end character,falseelse
-
writeToFile
private void writeToFile() throws VilExceptionWrites the Dom tree back to the file.- Throws:
VilException- If a new file (which nodocshould be created out ofCompositeArtifact.getText()andCompositeArtifact.getText()is not parseable.
-
configureTransformer
protected void configureTransformer(javax.xml.transform.Transformer transformer)
Configures the transformer.- Parameters:
transformer- the transformer to be configured
-
toYesNo
private java.lang.String toYesNo(boolean yes)
Turns a boolean into a transformer "yes" or "no" string.- Parameters:
yes- whether the output shall represent "yes"- Returns:
- "yes" or "no"
-
createAttributes
private XmlAttribute[] createAttributes(org.w3c.dom.Node node, XmlElement element)
Creates and returns all Attributes for given node and XmlElement.- Parameters:
node- The node for which to return all attributeselement- The element that represents the node.- Returns:
- Array with XmlAttributes
-
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.
-
selectByRegEx
public Set<XmlElement> selectByRegEx(java.lang.String regEx) throws VilException
Returns a set of all XmlElements that start with prefix and end with suffix. To get a set of all XmlElements having a certain pre- and suffix use: "prefix.*suffix" (replace prefix and suffix).- 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.
-
update
public void update() throws VilExceptionDescription copied from interface:IArtifactCalled if the underlying real artifact may have been updated, e.g., by overriding it by another file.- Specified by:
updatein interfaceIArtifact- Overrides:
updatein classCompositeArtifact- Throws:
VilException- in case that updating fails
-
cleanTree
private void cleanTree(org.w3c.dom.Node parent)
Cleans the dom tree from empty text nodes. These nodes are used for internal indent representation. These empty text nodes cause bugs in indentation if not removed.- Parameters:
parent- The parentNode that is to clean.
-
readDtd
private void readDtd() throws VilExceptionChecks the base file for embedded DTDs.- Throws:
VilException- If File not found.
-
convert
public static FileArtifact convert(java.lang.String val) throws VilException
Conversion operation.- Parameters:
val- the value to be converted- Returns:
- the converted value
- Throws:
VilException- in case that creating the artifact fails
-
convert
public static XmlFileArtifact convert(IFileSystemArtifact val)
Conversion operation.- Parameters:
val- the value to be converted- Returns:
- the converted value
-
setIndentation
public void setIndentation(int indentation)
Sets the number of whitespaces to use for auto-indenting this artifact while writing/storing.- Parameters:
indentation- the number of whitespaces (default 4), a negative value disables indentation
-
setEncoding
public void setEncoding(java.lang.String encoding)
Defines the (input) encoding.- Parameters:
encoding- a valid encoding name, may be null for none
-
getEncoding
public java.lang.String getEncoding()
Returns the encoding.- Returns:
- the encoding
-
setXmlStandalone
public void setXmlStandalone(boolean standalone)
Defines whether the XML artifact is standalone.- Parameters:
standalone- whether it is standalone or not- See Also:
setOmitXmlStandalone(boolean)
-
getXmlStandalone
public boolean getXmlStandalone()
Returns the XML standalone flag.- Returns:
- the standalone flag, always false if no document is present
-
setOmitXmlDeclaration
public void setOmitXmlDeclaration(boolean omitXmlDeclaration)
Defines whether the usual XML header declaration shall be omitted or printed while writing/storing this artifact.- Parameters:
omitXmlDeclaration- omit iftrue, emit iffalse(default)
-
setOmitXmlStandalone
public void setOmitXmlStandalone(boolean omitXmlStandalone)
Defines whether the usual XML header standalone attribute shall be omitted or printed while writing/storing this artifact.- Parameters:
omitXmlStandalone- omit iftrue(default), emit iffalse
-
synchronizeAttributeSequence
public boolean synchronizeAttributeSequence()
Returns whether the attribute sequence of XML elements shall be synchronized between document and artifact fragments, i.e., the fragment sequence defines the sequence of the attributes. Else attributes are sorted by name (default XALAN behavior).- Returns:
true(default),falsefor sorted
-
setSynchronizeAttributeSequence
public void setSynchronizeAttributeSequence(boolean synchronizeAttributeSequence)
Changes whether the attribute sequence of XML elements shall be synchronized between document and artifact fragments, i.e., the fragment sequence defines the sequence of the attributes. Else attributes are sorted by name (default XALAN behavior).- Parameters:
synchronizeAttributeSequence-true(default),falsefor sorted
-
getTransformerFactory
protected javax.xml.transform.TransformerFactory getTransformerFactory()
Returns the transformer builder factory.- Returns:
- the factory
-
getDocumentBuilderFactory
protected javax.xml.parsers.DocumentBuilderFactory getDocumentBuilderFactory()
Returns the document builder factory.- Returns:
- the factory
-
format
public void format() throws VilExceptionEnforces re-loading, formatting and writing of this artifact.- Throws:
VilException- in case that loading, formatting or writing fails
-
synchronizeAttributes
private void synchronizeAttributes(java.io.InputStream stream)
Synchronizes the XML attributes (depends onsynchronizeAttributeSequence).- Parameters:
stream- the stream to synchronize
-
synchronizeAttributes
private void synchronizeAttributes(java.io.File file)
Synchronizes the XML attributes (depends onsynchronizeAttributeSequence).- Parameters:
file- the file to synchronize
-
-