Package aQute.lib.tag
Class Tag
- java.lang.Object
-
- aQute.lib.tag.Tag
-
public class Tag extends java.lang.ObjectThe 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 Summary
Fields Modifier and Type Field Description static java.util.regex.PatternNAME_P
-
Constructor Summary
Constructors Constructor Description Tag(Tag parent, java.lang.String name, java.lang.Object content)Tag(Tag parent, java.lang.String name, java.lang.Object... contents)Tag(java.lang.String name, java.lang.Object content)Construct a new Tag with a name.Tag(java.lang.String name, java.lang.Object... contents)Tag(java.lang.String name, java.lang.String[] attributes)Tag(java.lang.String name, java.lang.String[] attributes, java.lang.Object content)Construct a new Tag with a name and a set of attributes.Tag(java.lang.String name, java.lang.String[] attributes, java.lang.Object... contents)Tag(java.lang.String name, java.util.Map<java.lang.String,java.lang.String> attributes)Tag(java.lang.String name, java.util.Map<java.lang.String,java.lang.String> attributes, java.lang.Object content)Construct a new Tag with a name.Tag(java.lang.String name, java.util.Map<java.lang.String,java.lang.String> attributes, java.lang.Object... contents)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TagaddAttribute(java.lang.String key, int value)Add a new attribute.TagaddAttribute(java.lang.String key, java.lang.Object value)Add a new attribute.TagaddAttribute(java.lang.String key, java.lang.String value)Add a new attribute.TagaddAttribute(java.lang.String key, java.util.Date value)Add a new date attribute.TagaddContent(Tag tag)Add a new content tag.TagaddContent(java.lang.String string)Add a new content string.TagaddContent(java.util.Map<java.lang.String,?> map)Add a new content tags.java.lang.Stringcompact()static java.lang.Stringescape(java.lang.String s)Escape a string, do entity conversion.java.lang.StringfindRecursiveAttribute(java.lang.String name)static TagfromDTO(java.lang.String rootName, java.lang.Object dto)Convert the DTO object in arguments to a Tag object withrootNameas name.static TagfromDTO(java.lang.String rootName, java.lang.String arrayElementName, java.lang.Object dto)Convert the DTO object in arguments to a Tag object withrootNameas name.java.lang.StringgetAttribute(java.lang.String key)Return the attribute value.java.lang.StringgetAttribute(java.lang.String key, java.lang.String deflt)Return the attribute value or a default if not defined.java.util.Map<java.lang.String,java.lang.String>getAttributes()Answer the attributes as a Dictionary object.java.util.List<java.lang.Object>getContents()Return the contents.java.util.List<java.lang.Object>getContents(java.lang.String tag)Return only the tags of the first level of descendants that match the name.java.lang.StringgetContentsAsString()Return the whole contents as a String (no tag info and attributes).voidgetContentsAsString(java.lang.StringBuilder sb)convenient method to get the contents in a StringBuilder.java.lang.StringgetLocalName()java.lang.StringgetName()Return the name of the tag.java.lang.StringgetNameSpace()java.lang.StringgetNameSpace(java.lang.String name)java.lang.StringgetString(java.lang.String path)java.lang.StringgetStringContent()booleanmatch(java.lang.String search, Tag child, Tag mapping)Tagprint(int indent, java.io.PrintWriter pw)Print the tag formatted to a PrintWriter.voidrename(java.lang.String string)java.util.Collection<Tag>select(java.lang.String path)root/preferences/native/osjava.util.Collection<Tag>select(java.lang.String path, Tag mapping)voidsetCDATA()java.lang.StringtoString()Return a string representation of this Tag and all its children recursively.java.lang.Stringvalidate()
-
-
-
Constructor Detail
-
Tag
public Tag(java.lang.String name, java.lang.Object content)Construct a new Tag with a name.
-
Tag
public Tag(java.lang.String name, java.lang.Object... contents)
-
Tag
public Tag(Tag parent, java.lang.String name, java.lang.Object... contents)
-
Tag
public Tag(Tag parent, java.lang.String name, java.lang.Object content)
-
Tag
public Tag(java.lang.String name, java.util.Map<java.lang.String,java.lang.String> attributes, java.lang.Object content)Construct a new Tag with a name.
-
Tag
public Tag(java.lang.String name, java.util.Map<java.lang.String,java.lang.String> attributes, java.lang.Object... contents)
-
Tag
public Tag(java.lang.String name, java.util.Map<java.lang.String,java.lang.String> attributes)
-
Tag
public Tag(java.lang.String name, java.lang.String[] attributes, java.lang.Object content)Construct a new Tag with a name and a set of attributes. The attributes are given as ( name, value ) ...
-
Tag
public Tag(java.lang.String name, java.lang.String[] attributes, java.lang.Object... contents)
-
Tag
public Tag(java.lang.String name, java.lang.String[] attributes)
-
-
Method Detail
-
addAttribute
public Tag addAttribute(java.lang.String key, java.lang.String value)
Add a new attribute.
-
addAttribute
public Tag addAttribute(java.lang.String key, java.lang.Object value)
Add a new attribute.
-
addAttribute
public Tag addAttribute(java.lang.String key, int value)
Add a new attribute.
-
addAttribute
public Tag addAttribute(java.lang.String key, java.util.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(java.lang.String string)
Add a new content string.
-
addContent
public Tag addContent(java.util.Map<java.lang.String,?> map)
Add a new content tags.
-
fromDTO
public static Tag fromDTO(java.lang.String rootName, java.lang.String arrayElementName, java.lang.Object dto)
Convert the DTO object in arguments to a Tag object withrootNameas name.Keys in
Mapand public fields' name are used to name tags.
Objects inCollectionorarrayare converted toXMLelements and tags names are computed as follow:- If the parent element tag does not have a defined name,
arrayElementNamewill 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
arrayElementNameis 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,arrayElementNameis 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>nullvalues are ignored.- Parameters:
rootName- the name of the root tag, may benull.arrayElementName- a generic name for elements in lists, ifnullor empty, the default value "element" will be used.dto- the DTO to convert, ifnullan empty element is returned.- Returns:
- the corresponding Tag, never
null.
- If the parent element tag does not have a defined name,
-
fromDTO
public static Tag fromDTO(java.lang.String rootName, java.lang.Object dto)
Convert the DTO object in arguments to a Tag object withrootNameas name.Keys in
Mapand public fields' name are used to name tags.
Objects inCollectionorarrayare converted toXMLelements 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>nullvalues are ignored.- Parameters:
rootName- the name of the root tag, may benull.dto- the DTO to convert, ifnullan empty element is returned.- Returns:
- the corresponding Tag, never
null.
-
getName
public java.lang.String getName()
Return the name of the tag.
-
getAttribute
public java.lang.String getAttribute(java.lang.String key)
Return the attribute value.
-
getAttribute
public java.lang.String getAttribute(java.lang.String key, java.lang.String deflt)Return the attribute value or a default if not defined.
-
getAttributes
public java.util.Map<java.lang.String,java.lang.String> getAttributes()
Answer the attributes as a Dictionary object.
-
getContents
public java.util.List<java.lang.Object> getContents()
Return the contents.
-
toString
public java.lang.String toString()
Return a string representation of this Tag and all its children recursively.- Overrides:
toStringin classjava.lang.Object
-
getContents
public java.util.List<java.lang.Object> getContents(java.lang.String tag)
Return only the tags of the first level of descendants that match the name.
-
getContentsAsString
public java.lang.String getContentsAsString()
Return the whole contents as a String (no tag info and attributes).
-
getContentsAsString
public void getContentsAsString(java.lang.StringBuilder sb)
convenient method to get the contents in a StringBuilder.
-
print
public Tag print(int indent, java.io.PrintWriter pw)
Print the tag formatted to a PrintWriter.
-
escape
public static java.lang.String escape(java.lang.String s)
Escape a string, do entity conversion.
-
select
public java.util.Collection<Tag> select(java.lang.String path)
root/preferences/native/os
-
getString
public java.lang.String getString(java.lang.String path)
-
getStringContent
public java.lang.String getStringContent()
-
getNameSpace
public java.lang.String getNameSpace()
-
getNameSpace
public java.lang.String getNameSpace(java.lang.String name)
-
findRecursiveAttribute
public java.lang.String findRecursiveAttribute(java.lang.String name)
-
getLocalName
public java.lang.String getLocalName()
-
rename
public void rename(java.lang.String string)
-
setCDATA
public void setCDATA()
-
compact
public java.lang.String compact()
-
validate
public java.lang.String validate()
-
-