/ProjetoPA

Primary LanguageKotlin

Kotlin XML Handling Tutorial

Introduction

This tutorial will guide you through the usage of a Kotlin library for handling XML elements and documents. The library includes classes and functions to create and manipulate XML structures.

Constants

The library utilizes color codes for printing colored text:

  • red: Represents red color.
  • green: Represents green color.
  • brightred: Represents bright red color.
  • reset: Resets text color to default.

XMLElement Class

XMLElement represents an XML element with a name, optional text content, and an optional parent element.

Constructor

  • XMLElement(name: String, text: String = "", parent: XMLElement? = null): Creates an XML element with the given name, text content, and parent element.

Methods

  • getElementName(): Returns the name of the XML element.
  • getChildren(): Returns a list of children elements.
  • getParents(): Returns the parent element.
  • addElement(element: XMLElement): Adds a child element.
  • removeElement(name: String, attributes: Map<String, String> = mapOf()): Removes a child element by name and attributes.
  • getAttributes(): Returns the attributes of the element.
  • addAttribute(key: String, value: String): Adds an attribute.
  • updateAttribute(key: String, value: String): Updates an attribute.
  • renameAttribute(key: String, newkey: String): Renames an attribute.
  • removeAttribute(key: String): Removes an attribute.
  • toText(): Converts the element and its children to a string representation with colors.
  • textToFile(): Converts the element and its children to a string representation without colors and writes in a file.
  • accept(visitor: (XMLElement) -> Boolean): Accepts a visitor function to traverse the element tree.

Properties

  • path: Calculates the path of the element in the tree.

XMLDocument Class

XMLDocument represents an XML document containing a root XML element.

Properties

  • element: The root XML element.

Methods

  • addRoot(element: XMLElement): Adds a root element to the document.
  • removeElement(name: String): Removes an element by name.
  • generateXML(): Generates XML content as a string.
  • generateXMLFile(name: String): Generates a file with the specified name.
  • accept(visitor: (XMLElement) -> Boolean): Accepts a visitor function to traverse the document.
  • addAttribute(name: String, attributename: String, attributevalue: String): Adds an attribute to an element.
  • renameXMLElements(oldname: String, newname: String): Renames an XML element.
  • renameAttributes(elementname: String, oldname: String, newname: String): Renames an attribute on an element.
  • removeAttributes(entityname: String, attributename: String): Removes an attribute from an element.
  • xPath(path: String): Finds elements matching the specified path.

Kotlin XML Annotations

This class provides annotations used for XML handling.

Annotations

ElementXML Annotation

  • Target: AnnotationTarget.CLASS, AnnotationTarget.PROPERTY
  • Attributes:
    • name: The XML element name.
    • text: The text content of the XML element.

AttributeXML Annotation

  • Target: AnnotationTarget.PROPERTY
  • Attributes:
    • name: The name of the XML attribute.

XmlDelist Annotation

  • Target: AnnotationTarget.CLASS, AnnotationTarget.PROPERTY

XmlString Annotation

  • Target: AnnotationTarget.PROPERTY
  • Attributes:
    • attribute: The class responsible for changing attribute values.

XMLadapter Annotation

  • Target: AnnotationTarget.CLASS
  • Attributes:
    • adapter: The class responsible for adapting XML elements.

ExcludeXML Annotation

  • Target: AnnotationTarget.PROPERTY

Function

translate Function

The translate function converts the objects into XML elements using the specified annotations.

  • Parameters:
    • obj: Any object to be translated into an XML element.
  • Returns:
    • An XMLElement that will represent the translated XML element.

The translate function iterates through the properties of the object and checks for annotations to generate the XML elements and attributes accordingly.

Domain-Specific language

This class provides functions that can be used to create a directory structure.

Functions

directory Function

  • Description: Creates a new directory element with the specified name and builds its contents.
  • Parameters:
    • name: The name of the directory.
    • build: Lambda function to build the contents of the directory.
  • Returns:
    • An XMLElement representing the directory.

directory Extension Function

  • Description: Creates a new directory element within the current element and builds its contents.
  • Parameters:
    • name: The name of the directory.
    • build: Lambda function to build the contents of the directory.
  • Returns:
    • An XMLElement representing the directory inside another directory.

div Operator Overload

  • Description: Retrieves a directory with the specified name.
  • Parameters:
    • name: The name of the directory.
  • Returns:
    • An XMLElement representing the elements inside the directory.