Skip to content

Class JSX

hxs edited this page Nov 28, 2019 · 19 revisions

JSX

Package jsx
File JSX.java

JSX is the principal class to manage the XML file.

Constructs


JSX()

This is a simple construct.

Example:

JSX jsx = new JSX();

JSX(String filePath)

throws JSXLockException

This construct set the file path of XML document to be load.

Parameters:

  • filePath, path to XML file

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock failed

Example:

JSX jsx = new JSX("path/to/file.xml");

JSX(Document document)

This construct set the XML document to work on.

Parameters:

  • document, XML document. Class org.w3c.dom.Document

Example:

Document doc = JSX.createDocument("path/to/file.xml");
JSX jsx = new JSX(doc);

Methods



Static

Document createDocument(String pathFile)

throws XMLException

This method write a log.

Return:
Return an object org.w3c.dom.Document that rapresents the XML file.

Parameters:

  • pathFile, path to XML file

Exceptions:

  • XMLException, exception thrown the document creation return a error.

Example:

Document doc = JSX.createDocument("path/to/file.xml");

Document createDocument(String pathFile, boolean validating, boolean namespaceAware, boolean featValidation, boolean featNamespaces, boolean featLoadDTDGramm, boolean featLoadExtDTD)

throws XMLException

This method write a log.

Return:
Return an object org.w3c.dom.Document that rapresents the XML file.

Parameters:

  • pathFile, path to XML file
  • validating, activate validating
  • namespaceAware, activate namespace aware
  • featValidation, activate feature validation
  • featNamespaces, activate feature namespaces
  • featLoadDTDGramm, activate feature load DTD gramm
  • featLoadExtDTD, activate feature load ext DTD

Exceptions:

  • XMLException, exception thrown the document creation return a error.

Example:

Document doc = JSX.createDocument("path/to/file.xml", false, false, false, false, false, false);

int getGreatId(Set<String> idSet)

throws XMLException

This method get the great Integer id.

Return:
Return the great Integer id

Parameters:

  • idSet, set of id

Example:

JSX jsx = new JSX("path/to/file.xml");
HashMap<String, Node> mapElement = jsx.getMapIdElement("my-element");
Set<String> idSet = mapElement.keySet();
int greatId = Jogger.getGreatId(idSet);

void removeBlankLines(Document document)

throws JSXException

This method remove the blank lines on XML document.

Parameters:

  • document, XML document. Class org.w3c.dom.Document

Exceptions:

  • JSXException, exception thrown if the operation return a error.

Example:

Document doc = JSX.createDocument("path/to/file.xml");
JSX.removeBlankLines(doc);

Get and Set

String getFilePath()

This method get the path of XML file.

Default null

Return:
Return the path of XML file.

Example:

JSX jsx = new JSX("path/to/file.xml");
String path = jsx.getFilePath();

void setFilePath(String filePath)

This method set the path of XML file.

Parameters:

  • filePath, path of XML file

Example:

JSX jsx = new JSX();
jsx.setFilePath("path/to/file.xml");

Document getDocument()

This method get the XML document.

Default null

Return:
Return the XML document. Class org.w3c.dom.Document

Example:

JSX jsx = new JSX("path/to/file.xml");
String path = jsx.getFilePath();

void setDocument(Document document)

This method set the the XML document.

Parameters:

  • document, XML document. Class org.w3c.dom.Document

Example:

JSX jsx = new JSX();
Document doc = JSX.getDocument("path/to/file.xml");
jsx.setDocument(doc);

boolean isLock()

This method check if the JSX-Lock is active. Read JSX-Lock.

Default false

Return:
Return true if JSX-Lock is active, false otherwise.

Example:

JSX jsx = new JSX("path/to/file.xml");
if (jsx.isLock()) {
  //your code
}

void setLock(boolean lock)

This method can enable or disable JSX-Lock.
Read JSX-Lock.

Parameters:

  • lock, true for active it, false otherwise

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.setLock(true);

int getIndentAmount()

This method get the number of sapces for the indent.

Default 4

Return:
Return the number of sapces for the indent.

Example:

JSX jogger = new JSX("path/to/file.xml");
int indAmnt = jsx.getIndentAmount();

void setIndentAmount(int indentAmount)

This method set the number of sapces for the indent.

Parameters:

  • indentAmount, number of sapces for the indent

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.setIndentAmount(8);

boolean isAutoFlush()

This method check if the autoflush is active.

Default false

Return:
Return true if autoflush is active, false otherwise.

Example:

JSX jsx = new JSX("path/to/file.xml");
if (jsx.isAutoFlush()) {
  //your code
}

void setAutoFlush(boolean autoFlush)

This method can enable or disable autoflush.

Parameters:

  • autoFlush, true for active it, false otherwise

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.setAutoFlush(true);

boolean isValidating()

This method check if the validating is active. For javax.xml.parsers.DocumentBuilderFactory object.

Default false

Return:
Return true if the validating is active, false otherwise.

Example:

JSX jogger = new JSX("path/to/file.xml");
if (jsx.isValidating()) {
  //your code
}

void setValidating(boolean validating)

This method can enable or disable the validation of XML file.
It passed at setValidating(validating) method of javax.xml.parsers.DocumentBuilderFactory object.

Parameters:

  • validating, true for active it, false otherwise

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.setValidating(true);

boolean isNamespaceAware()

This method check if the namespace aware is active. For javax.xml.parsers.DocumentBuilderFactory object.

Default false

Return:
Return true if namespace aware is active, false otherwise.

Example:

JSX jogger = new JSX("path/to/file.xml");
if (jsx.isNamespaceAware()) {
  //your code
}

void setNamespaceAware(boolean namespaceAware)

This method can enable or disable the namespace aware.
It passed at setNamespaceAware(namespaceAware) method of javax.xml.parsers.DocumentBuilderFactory object.

Parameters:

  • namespaceAware, true for active it, false otherwise

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.setNamespaceAware(true);

boolean isFeatValidation()

This method check if the feature validation is active. For javax.xml.parsers.DocumentBuilderFactory object.

Default false

Return:
Return true if the feature validation is active, false otherwise.

Example:

JSX jsx = new JSX("path/to/file.xml");
if (jsx.isFeatValidation()) {
  //your code
}

void setFeatValidation(boolean featValidation)

This method can enable or disable the feature validation.
Feature http://xml.org/sax/features/validation used for javax.xml.parsers.DocumentBuilderFactory object.

Parameters:

  • featValidation, true for active it, false otherwise

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.setFeatValidation(true);

boolean isFeatNamespaces()

This method check if the feature namespaces is active.

Default false

Return:
Return true if feature namespaces is active, false otherwise.

Example:

JSX jogger = new JSX("path/to/file.xml");
if (jsx.isFeatNamespaces()) {
  //your code
}

void setFeatNamespaces(boolean featNamespaces)

This method can enable or disable the feature namespaces.
Feature http://xml.org/sax/features/namespaces used for javax.xml.parsers.DocumentBuilderFactory object.

Parameters:

  • featNamespaces, true for active it, false otherwise

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.setFeatNamespaces(true);

boolean isFeatLoadDTDGramm()

This method check if the feature load DTD gramm is active.

Default false

Return:
Return true if the feature load DTD gramm is active, false otherwise.

Example:

JSX jsx = new JSX("path/to/file.xml");
if (jsx.isFeatLoadDTDGramm()) {
  //your code
}

void setFeatLoadDTDGramm(boolean featLoadDTDGramm)

This method can enable or disable the feature load DTD gramm.
Feature http://apache.org/xml/features/nonvalidating/load-dtd-grammar used for javax.xml.parsers.DocumentBuilderFactory object.

Parameters:

  • featLoadDTDGramm, true for active it, false otherwise

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.setFeatLoadDTDGramm(true);

boolean isFeatLoadExtDTD()

This method check if the feature load ext DTD is active.

Default false

Return:
Return true if the feature load ext DTD is active, false otherwise.

Example:

JSX jsx = new JSX("path/to/file.xml");
if (jsx.isFeatLoadExtDTD()) {
  //your code
}

void setFeatLoadExtDTD(boolean featLoadExtDTD)

This method can enable or disable the feature load ext DTD.
Feature http://apache.org/xml/features/nonvalidating/load-external-dtd used for javax.xml.parsers.DocumentBuilderFactory object.

Parameters:

  • validating, true for active it, false otherwise

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.setFeatLoadExtDTD(true);

ReentrantLock getReentrantLock()

This method get the reentrant lock used for XML file.

Return:
Return the java.util.concurrent.locks.ReentrantLock object used for the lock.

Example:

JSX jsx = new JSX("path/to/file.xml");
if (jsx.getReentrantLock().tryLock()) {
  // your code
}

Writer

void addElement(String nameElement)

throws JSXLockException

This method add a element on document.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • nameElement, name of element to be added

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.addElement("new-element");

void addElement(String nameElement, String textContent)

throws JSXLockException

This method add a element with text on document.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • nameElement, name of element to be added
  • textContent, text body of the the new element

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.addElement("jsx", "Awesome!!!");

void addElement(String nameElement, Map<String, String> attributesMap)

throws JSXLockException

This method add a element with attributes on document.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • nameElement, name of element to be added
  • attributesMap, map of attributes

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
HashMap<String, String> attrMap = new HashMap<String, String>();
attrMap.put("attribute", "value");
jsx.addElement("new-element", attrMap);

void addElementWithId(String nameElement, String idElement)

throws JSXLockException

This method add a element with the id attribute on document.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • nameElement, name of element to be added
  • idElement, id for element

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
jsx.addElementWithId("new-element", "jsx-element");

void addElementWithChild(String nameElement, String idElement, Map<String, String> mapChildNode)

throws JSXLockException

This method add a element with the id attribute and child elements on document.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • nameElement, name of element to be added
  • idElement, id for element
  • mapChildNode, map of child node for new element. The key is the name of child element, the value is the text body

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
HashMap<String, String> childs = new HashMap<String, String>();
childs.put("jsx", "cool");
jsx.addElementWithChild("new-element", "jsx-element", childs);

void addElementWithChild(String nameElement, Map<String, String> attributesMap, Map<String, String> mapChildNode)

throws JSXLockException

This method add a element with attributes and child elements on document.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • nameElement, name of element to be added
  • attributesMap, map of attributes
  • mapChildNode, map of child node for new element. The key is the name of child element, the value is the text body

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
HashMap<String, String> attrMap = new HashMap<String, String>();
HashMap<String, String> childs = new HashMap<String, String>();
attrMap.put("attr", "val");
childs.put("jsx", "cool");
jsx.addElementWithChild("new-element", attrMap, childs);

void addElementWithChild(String nameElement, String idElement, Map<String, String> mapChildNode, Map<String, String> mapIdChild)

throws JSXLockException

This method add a element with the id attribute and child elements with id on document.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • nameElement, name of element to be added
  • idElement, id for element
  • mapChildNode, map of child node for new element. The key is name of child element, the value is the text body
  • mapIdChild, map of id for child. The key is the name of child element, the value is the id

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
HashMap<String, String> childs = new HashMap<String, String>();
HashMap<String, String> idChilds = new HashMap<String, String>();
childs.put("jsx", "cool");
idChilds.put("jsx", "jsx-id");
jsx.addElementWithChild("new-element", "jsx-element", childs, idChilds);

void addElementWithChild(String nameElement, Map<String, String> attributesMap, Map<String, String> mapChildNode, Map<String, Map<String, String>> attributesMapChild)

throws JSXLockException

This method add a element and child elements, with attributes, on document.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • nameElement, name of element to be added
  • attributesMap, map of attributes
  • mapChildNode, map of child node for new element. The key is name of child element, the value is the text body
  • attributesMapChild, bidimensional map for attributes of childs. The key is name of child element, the value is the map with the attributes

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
HashMap<String, String> attrMap = new HashMap<String, String>();
HashMap<String, String> childs = new HashMap<String, String>();
HashMap<String, HashMap<String, String>> attrChildsMap = new HashMap<String, HashMap<String, String>>();
HashMap<String, String> attr = new HashMap<String, String>();
attrMap.put("attr-element", "val");
childs.put("jsx", "cool");
attr.put("attr-child", "val");
attrChildsMap.put("jsx", attr);
jsx.addElementWithChild("new-element", attrMap, childs, attrChildsMap);

void appendElement(Node parentNode, String nameElement, String textContext)

throws JSXLockException

This method append new element on parent node.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • parentNode, parent to node to be append the new element. Class org.w3c.dom.Node
  • nameElement, name of element to be added
  • textContent, text body of the the new element

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
Node node = jsx.getElementById("element", "jsx");
jsx.appendElement(node, "time", "420");

void appendElement(Node parentNode, String nameElement, String idElement, String textContext)

throws JSXLockException

This method append new element on parent node.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • parentNode, parent to node to be append the new element. Class org.w3c.dom.Node
  • nameElement, name of element to be added
  • idElement, id for element
  • textContent, text body of the the new element

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
Node node = jsx.getElementById("element", "jsx");
jsx.appendElement(node, "time", "time-element", "420");

void appendElement(Node parentNode, String nameElement, Map<String, String> attributesMap, String textContext)

throws JSXLockException

This method append new element on parent node.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • parentNode, parent to node to be append the new element. Class org.w3c.dom.Node
  • nameElement, name of element to be added
  • attributesMap, map of attributes
  • textContent, text body of the the new element

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
HashMap<String, String> attrMap = new HashMap<String, String>();
Node node = jsx.getElementById("element", "jsx");
attrMap.put("need", "beer");
jsx.appendElement(node, "new-element", attrMap, "YEEEEEAAAAAHHHH!!!");

void appendElementWithChild(Node parentNode, String nameElement, Map<String, String> mapChildNode)

throws JSXLockException

This method append new element on parent node.
Implement JSX-Lock. For more info JSX-Lock.

Parameters:

  • parentNode, parent to node to be append the new element. Class org.w3c.dom.Node
  • nameElement, name of element to be added
  • mapChildNode, map of child node for new element. The key is name of child element, the value is the text body

Exceptions:

  • JSXLockException, exception thrown if JSX-Lock_ failed

Example:

JSX jsx = new JSX("path/to/file.xml");
HashMap<String, String> childs = new HashMap<String, String>();
Node node = jsx.getElementById("element", "jsx");
childs.put("time", "hacking");
jsx.appendElement(node, "new-element", childs);

Reader

String getLogFilePath()

throws LogFileException

This method get the path of log file to work on.

Return:
Return the string with the path of log file to work on.

Exceptions:

  • LogFileException, exception thrown if an error is return while working on the log file

Example:

Jogger jogger = new Jogger();
String logFilePath = jogger.getLogFilePath();

File getFile()

throws LogFileException

This method get the log file to work on.

Return:
Return the log file to work on.

Exceptions:

  • LogFileException, exception thrown if an error is return while working on the log file

Example:

Jogger jogger = new Jogger();
File logFile = jogger.getFile();

File getFileIfExists()

throws LogFileException

This method get the log file to work on, only if it exists.

Return:
If the log file exists return it, otherwise return null.

Exceptions:

  • LogFileException, exception thrown if an error is return while working on the log file

Example:

Jogger jogger = new Jogger();
File logFile = jogger.getFileIfExists();

Protected

boolean tryLock()

throws LockLogException

This method is used to implement Jogger-Lock.
It check if Jogger-Lock is enable, then in this case try to lock the file using the class java.util.concurrent.locks.ReentrantLock. For more info Jogger-Lock.

Return:
Return true if Jogger-Lock is disable or the lock is successful, false otherwise.

Exceptions:

  • LockLogException, exception thrown if the timeout for the lock expires

void tryUnlock()

This method is used to implement Jogger-Lock.
It check if Jogger-Lock is enable, then in this case unlock the file using the class java.util.concurrent.locks.ReentrantLock. For more info Jogger-Lock.


Clone this wiki locally