Package aQute.lib.tag
Class Tag
- java.lang.Object
-
- aQute.lib.tag.Tag
-
public class Tag extends java.lang.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 Summary
Fields Modifier and Type Field Description static java.util.regex.Pattern
NAME_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 Tag
addAttribute(java.lang.String key, int value)
Add a new attribute.Tag
addAttribute(java.lang.String key, java.lang.Object value)
Add a new attribute.Tag
addAttribute(java.lang.String key, java.lang.String value)
Add a new attribute.Tag
addAttribute(java.lang.String key, java.util.Date value)
Add a new date attribute.Tag
addContent(Tag tag)
Add a new content tag.Tag
addContent(java.lang.String string)
Add a new content string.Tag
addContent(java.util.Map<java.lang.String,?> map)
Add a new content tags.java.lang.String
compact()
static java.lang.String
escape(java.lang.String s)
Escape a string, do entity conversion.java.lang.String
findRecursiveAttribute(java.lang.String name)
static Tag
fromDTO(java.lang.String rootName, java.lang.Object dto)
Convert the DTO object in arguments to a Tag object withrootName
as name.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 withrootName
as name.java.lang.String
getAttribute(java.lang.String key)
Return the attribute value.java.lang.String
getAttribute(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.String
getContentsAsString()
Return the whole contents as a String (no tag info and attributes).void
getContentsAsString(java.lang.StringBuilder sb)
convenient method to get the contents in a StringBuilder.java.lang.String
getLocalName()
java.lang.String
getName()
Return the name of the tag.java.lang.String
getNameSpace()
java.lang.String
getNameSpace(java.lang.String name)
java.lang.String
getString(java.lang.String path)
java.lang.String
getStringContent()
boolean
match(java.lang.String search, Tag child, Tag mapping)
Tag
print(int indent, java.io.PrintWriter pw)
Print the tag formatted to a PrintWriter.void
rename(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)
void
setCDATA()
java.lang.String
toString()
Return a string representation of this Tag and all its children recursively.java.lang.String
validate()
-
-
-
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 withrootName
as name.Keys in
Map
and public fields' name are used to name tags.
Objects inCollection
orarray
are converted toXML
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 benull
.arrayElementName
- a generic name for elements in lists, ifnull
or empty, the default value "element" will be used.dto
- the DTO to convert, ifnull
an 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 withrootName
as name.Keys in
Map
and public fields' name are used to name tags.
Objects inCollection
orarray
are converted toXML
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 benull
.dto
- the DTO to convert, ifnull
an 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:
toString
in 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()
-
-