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 private static java.lang.StringARRAY_ELEMENT_NAME(package private) java.util.Map<java.lang.String,java.lang.String>attributes(package private) booleancdata(package private) java.util.List<java.lang.Object>content(package private) static java.time.format.DateTimeFormatterDATE_TIME_FORMATTER(package private) java.lang.Stringname(package private) static java.lang.StringNamestatic java.util.regex.PatternNAME_P(package private) static java.lang.StringNameChar(package private) static java.lang.StringNameStartChar(package private) Tagparent
-
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()private static java.lang.StringcomputeArrayElementName(java.lang.String name, java.lang.String arrayElementName)private static TagconvertDTO(java.lang.String rootName, java.lang.String arrayElementName, java.lang.Object dto, boolean suffix)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.private static java.util.stream.Stream<java.lang.reflect.Field>getFields(java.lang.Class<?> c)java.lang.StringgetLocalName()java.lang.StringgetName()Return the name of the tag.java.lang.StringgetNameSpace()java.lang.StringgetNameSpace(java.lang.String name)private java.lang.StringgetPath()java.lang.StringgetString(java.lang.String path)java.lang.StringgetStringContent()(package private) booleaninvalid(java.util.Formatter f)private static booleanisComplex(java.lang.Object a)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)(package private) voidselect(java.lang.String path, java.util.List<Tag> results, Tag mapping)voidsetCDATA()(package private) voidspaces(java.io.PrintWriter pw, int n)Make spaces.java.lang.StringtoString()Return a string representation of this Tag and all its children recursively.java.lang.Stringvalidate()
-
-
-
Field Detail
-
ARRAY_ELEMENT_NAME
private static final java.lang.String ARRAY_ELEMENT_NAME
- See Also:
- Constant Field Values
-
NameStartChar
static final java.lang.String NameStartChar
- See Also:
- Constant Field Values
-
NameChar
static final java.lang.String NameChar
- See Also:
- Constant Field Values
-
Name
static final java.lang.String Name
- See Also:
- Constant Field Values
-
NAME_P
public static final java.util.regex.Pattern NAME_P
-
parent
Tag parent
-
name
java.lang.String name
-
attributes
final java.util.Map<java.lang.String,java.lang.String> attributes
-
content
final java.util.List<java.lang.Object> content
-
DATE_TIME_FORMATTER
static final java.time.format.DateTimeFormatter DATE_TIME_FORMATTER
-
cdata
boolean cdata
-
-
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.
-
convertDTO
private static Tag convertDTO(java.lang.String rootName, java.lang.String arrayElementName, java.lang.Object dto, boolean suffix)
-
computeArrayElementName
private static java.lang.String computeArrayElementName(java.lang.String name, java.lang.String arrayElementName)
-
isComplex
private static boolean isComplex(java.lang.Object a)
-
getFields
private static java.util.stream.Stream<java.lang.reflect.Field> getFields(java.lang.Class<?> c)
-
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.
-
spaces
void spaces(java.io.PrintWriter pw, int n)Make spaces.
-
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()
-
invalid
boolean invalid(java.util.Formatter f)
-
getPath
private java.lang.String getPath()
-
-