exml/src/org/atriasoft/exml/XmlNode.java

178 lines
4.1 KiB
Java

/** @file
* @author Edouard DUPIN
* @copyright 2021, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
*/
package org.atriasoft.exml;
import org.atriasoft.exml.internal.FilePos;
/**
* Basic main object of all xml elements.
*/
public abstract class XmlNode {
protected FilePos pos; //!< position in the read file (null if the file is not parsed)
protected String value; //!< value of the node (for element this is the name, for text it is the inside text ...);
/**
* basic element of a xml structure
*/
public XmlNode() {
this.pos = null;
}
/**
* basic element of a xml structure
* @param[in] _value value of the node
*/
public XmlNode(final String _value) {
this.pos = null;
this.value = _value;
}
/**
* clear the Node
*/
public void clear() {
this.value = "";
this.pos = null;
}
@Override
protected XmlNode clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Can not clone an abstract class ...");
}
/**
* get the current position where the element is in the file
* @return The file position reference
*/
public FilePos getPos() {
return this.pos;
}
/**
* get the node type.
* @return the type of the Node.
*/
public abstract XmlNodeType getType();
/**
* get the current element Value.
* @return the reference of the string value.
*/
public String getValue() {
return this.value;
}
/**
* generate a string with the tree of the xml
* @param[in,out] _data string where to add the elements
* @param[in] _indent current indentation of the file
* @return false if an error occured.
*/
protected abstract boolean iGenerate(final StringBuilder _data, final int _indent);
/**
* parse the Current node [pure VIRUAL]
* @param[in] _data data string to parse.
* @param[in,out] _pos position in the string to start parse, return the position end of parsing.
* @param[in] _caseSensitive Request a parsion of element that is not case sensitive (all element is in low case)
* @param[in,out] _filePos file parsing position (line x col x)
* @param[in,out] _doc Base document reference
* @return false if an error occured.
*/
//protected abstract boolean iParse(String _data, PositionParsing _pos, boolean _caseSensitive, FilePos _filePos, Document _doc);
/**
* check if the node is a Comment
* @return true if the node is a Comment
*/
public final boolean isComment() {
return this instanceof XmlComment;
}
/**
* check if the node is a Declaration
* @return true if the node is a Declaration
*/
public final boolean isDeclaration() {
return this instanceof XmlDeclaration;
}
/**
* check if the node is a Document
* @return true if the node is a Document
*/
public final boolean isDocument() {
return this instanceof Document;
}
/**
* check if the node is a Element
* @return true if the node is a Element
*/
public final boolean isElement() {
return this instanceof XmlElement;
}
/**
* check if the node is a Text
* @return true if the node is a Text
*/
public final boolean isText() {
return this instanceof XmlText;
}
/**
* set the value of the node.
* @param[in] _value New value of the node.
*/
public final void setValue(final String _value) {
this.value = _value;
}
/**
* Cast the element in a Comment if it is possible.
* @return pointer on the class or null.
*/
public final XmlComment toComment() {
return (XmlComment) this;
}
/**
* Cast the element in a Declaration if it is possible.
* @return pointer on the class or null.
*/
public final XmlDeclaration toDeclaration() {
return (XmlDeclaration) this;
}
/**
* Cast the element in a Document if it is possible.
* @return pointer on the class or null.
*/
public final Document toDocument() {
return (Document) this;
}
/**
* Cast the element in a Element if it is possible.
* @return pointer on the class or null.
*/
public final XmlElement toElement() {
return (XmlElement) this;
}
/**
* Cast the element in a Text if it is possible.
* @return pointer on the class or null.
*/
public final XmlText toText() {
return (XmlText) this;
}
}