Package aQute.lib.tag

Class Tag

java.lang.Object
aQute.lib.tag.Tag

public class Tag extends Object
The Tag class represents a minimal XML tree. It consist of a named element with a hashtable of named attributes. Methods are provided to walk the tree and get its constituents. The content of a Tag is a list that contains String objects or other Tag objects.
  • Field Details

  • Constructor Details

  • Method Details

    • addAttribute

      public Tag addAttribute(String key, String value)
      Add a new attribute.
    • addAttribute

      public Tag addAttribute(String key, Object value)
      Add a new attribute.
    • addAttribute

      public Tag addAttribute(String key, int value)
      Add a new attribute.
    • addAttribute

      public Tag addAttribute(String key, Date value)
      Add a new date attribute. The date is formatted by DATE_TIME_FORMATTER described at the top of this class.
    • addContent

      public Tag addContent(String string)
      Add a new content string.
    • addContent

      public Tag addContent(Tag tag)
      Add a new content tag.
    • addContent

      public Tag addContent(Map<String,?> map)
      Add a new content tags.
    • fromDTO

      public static Tag fromDTO(String rootName, String arrayElementName, Object dto)
      Convert the DTO object in arguments to a Tag object with rootName as name.

      Keys in Map and public fields' name are used to name tags.
      Objects in Collection or array are converted to XML elements and tags names are computed as follow:

      • If the parent element tag does not have a defined name, arrayElementName will be used.
      • If the parent element tag name ends with a 's' or 'S', the depluralized version will be used.
      • Otherwise, the first letter of arrayElementName is capitalized and appended to the parent element tag name to name the tag (If the parent element tag name does not end with a lowercase letter, arrayElementName is entirely capitalized and an '_' is first appended to to it)

      Example:

       fromDTO("things", "element", `[{"FRIEND": ["Amy"]},{"children": ["Emily"]},["Bob", "Bill"]]`)
       
      gives
       
       <things>
          <thing>
             <FRIEND>
                <FRIEND_ELEMENT>Amy</FRIEND_ELEMENT>
             </FRIEND>
             <children>
                <childrenElement>Emily</childrenElement>
             </children>
          </thing>
          <thing>
             <element>Bob</element>
             <element>Bill</element>
          </thing>
       </things>
       
       

      null values are ignored.

      Parameters:
      rootName - the name of the root tag, may be null.
      arrayElementName - a generic name for elements in lists, if null or empty, the default value "element" will be used.
      dto - the DTO to convert, if null an empty element is returned.
      Returns:
      the corresponding Tag, never null.
    • fromDTO

      public static Tag fromDTO(String rootName, Object dto)
      Convert the DTO object in arguments to a Tag object with rootName as name.

      Keys in Map and public fields' name are used to name tags.
      Objects in Collection or array are converted to XML elements and tags names are computed as follow:

      • If the parent element tag does not have a defined name, "element" will be used.
      • If the parent element tag name ends with a 's' or 'S', the depluralized version will be used.
      • Otherwise, the first letter of "element" is capitalized and appended to the parent element tag name to name the tag (If the parent element tag name does not end with a lowercase letter, "element" is entirely capitalized and an '_' is first appended to to it)

      Example:

       fromDTO("things", "element", `[{"FRIEND": ["Amy"]},{"children": ["Emily"]},["Bob", "Bill"]]`)
       
      gives
       
       <things>
          <thing>
             <FRIEND>
                <FRIEND_ELEMENT>Amy</FRIEND_ELEMENT>
             </FRIEND>
             <children>
                <childrenElement>Emily</childrenElement>
             </children>
          </thing>
          <thing>
             <element>Bob</element>
             <element>Bill</element>
          </thing>
       </things>
       
       

      null values are ignored.

      Parameters:
      rootName - the name of the root tag, may be null.
      dto - the DTO to convert, if null an empty element is returned.
      Returns:
      the corresponding Tag, never null.
    • convertDTO

      private static Tag convertDTO(String rootName, String arrayElementName, Object dto, boolean suffix)
    • computeArrayElementName

      private static String computeArrayElementName(String name, String arrayElementName)
    • isComplex

      private static boolean isComplex(Object a)
    • getFields

      private static Stream<Field> getFields(Class<?> c)
    • getName

      public String getName()
      Return the name of the tag.
    • getAttribute

      public String getAttribute(String key)
      Return the attribute value.
    • getAttribute

      public String getAttribute(String key, String deflt)
      Return the attribute value or a default if not defined.
    • getAttributes

      public Map<String,String> getAttributes()
      Answer the attributes as a Dictionary object.
    • getContents

      public List<Object> getContents()
      Return the contents.
    • toString

      public String toString()
      Return a string representation of this Tag and all its children recursively.
      Overrides:
      toString in class Object
    • getContents

      public List<Object> getContents(String tag)
      Return only the tags of the first level of descendants that match the name.
    • getContentsAsString

      public String getContentsAsString()
      Return the whole contents as a String (no tag info and attributes).
    • getContentsAsString

      public void getContentsAsString(StringBuilder sb)
      convenient method to get the contents in a StringBuilder.
    • print

      public Tag print(int indent, PrintWriter pw)
      Print the tag formatted to a PrintWriter.
    • escape

      public static String escape(String s)
      Escape a string, do entity conversion.
    • spaces

      void spaces(PrintWriter pw, int n)
      Make spaces.
    • select

      public Collection<Tag> select(String path)
      root/preferences/native/os
    • select

      public Collection<Tag> select(String path, Tag mapping)
    • select

      void select(String path, List<Tag> results, Tag mapping)
    • match

      public boolean match(String search, Tag child, Tag mapping)
    • getString

      public String getString(String path)
    • getStringContent

      public String getStringContent()
    • getNameSpace

      public String getNameSpace()
    • getNameSpace

      public String getNameSpace(String name)
    • findRecursiveAttribute

      public String findRecursiveAttribute(String name)
    • getLocalName

      public String getLocalName()
    • rename

      public void rename(String string)
    • setCDATA

      public void setCDATA()
    • compact

      public String compact()
    • validate

      public String validate()
    • invalid

      boolean invalid(Formatter f)
    • getPath

      private String getPath()