Package org.apache.maven.doxia.util
Class DoxiaUtils
java.lang.Object
org.apache.maven.doxia.util.DoxiaUtils
General Doxia utility methods. The methods in this class should not assume
any specific Doxia module or document format.
- Since:
- 1.1
- Author:
- ltheussl
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringConstruct a valid Doxia id.static MutableAttributeSetgetImageAttributes(String logo) Determine width and height of an image.static booleanisExternalLink(String link) Checks if the given string corresponds to an external URI, ie is not a link within the same document nor a relative link to another document (a local link) of the same site.static booleanisInternalLink(String link) Checks if the given string corresponds to an internal link, ie it is a link to an anchor within the same document.static booleanisLocalLink(String link) static booleanDetermines if the specified text is a valid id according to the rules laid out inencodeId(String).static DateParses a string representing a date by trying different date patterns.
-
Method Details
-
isInternalLink
Checks if the given string corresponds to an internal link, ie it is a link to an anchor within the same document. If link is not null, then exactly one of the three methodsisInternalLink(java.lang.String),isExternalLink(java.lang.String)andisLocalLink(java.lang.String)will return true.- Parameters:
link- The link to check. Not null.- Returns:
- True if the link starts with "#".
- Throws:
NullPointerException- if link is null.- See Also:
-
isExternalLink
Checks if the given string corresponds to an external URI, ie is not a link within the same document nor a relative link to another document (a local link) of the same site. If link is not null, then exactly one of the three methodsisInternalLink(java.lang.String),isExternalLink(java.lang.String)andisLocalLink(java.lang.String)will return true.- Parameters:
link- The link to check. Not null.- Returns:
- True if the link (ignoring case) starts with either "http:/", "https:/", "ftp:/", "mailto:", "file:/", or contains the string "://". Note that Windows style separators "\" are not allowed for URIs, see http://www.ietf.org/rfc/rfc2396.txt , section 2.4.3.
- Throws:
NullPointerException- if link is null.- See Also:
-
isLocalLink
Checks if the given string corresponds to a relative link to another document within the same site, ie it is neither aninternalnor anexternallink. If link is not null, then exactly one of the three methodsisInternalLink(java.lang.String),isExternalLink(java.lang.String)andisLocalLink(java.lang.String)will return true.- Parameters:
link- The link to check. Not null.- Returns:
- True if the link is neither an external nor an internal link.
- Throws:
NullPointerException- if link is null.- See Also:
-
encodeId
Construct a valid Doxia id.A valid Doxia id corresponds to an XML id which is a {code NCName} which is in turn identical to a
Name, but without a colon and without any character above0x7F.To achieve this we need to convert the id String. Two conversions are necessary and one is done to get prettier ids:
- Trim with
String.trim()before starting to process, - if the first character is not a
NameStartCharprepend the letter 'a', - any space character (
0x20) is replaced with an underscore, - any character not matching the above pattern is either dropped, or replaced with its UTF-8 encoding where each byte is prepended with a dot.
Here are some examples:
DoxiaUtils.encodeId(null) = null DoxiaUtils.encodeId("") = null DoxiaUtils.encodeId(" ") = null DoxiaUtils.encodeId(" _ ") = "_" DoxiaUtils.encodeId("1") = "a1" DoxiaUtils.encodeId("1anchor") = "a1anchor" DoxiaUtils.encodeId("_anchor") = "_anchor" DoxiaUtils.encodeId("a b-c123 ") = "a_b-c123" DoxiaUtils.encodeId(" anchor") = "anchor" DoxiaUtils.encodeId("myAnchor") = "myAnchor" DoxiaUtils.encodeId("€") = "a.E2.82.AC"- Parameters:
text- The text to be encoded. May be null, empty or blank in which case null is returned.- Returns:
- The trimmed and encoded id, or null if id is null. If id is not null, the return value is guaranteed to be a valid Doxia id.
- Since:
- 1.1.1
- See Also:
- Trim with
-
isValidId
Determines if the specified text is a valid id according to the rules laid out inencodeId(String).- Parameters:
text- The id to be tested. May be null or empty in which case false is returned.- Returns:
trueif the text is a valid id, otherwisefalse.- See Also:
-
parseDate
Parses a string representing a date by trying different date patterns.
The following date patterns are tried (in the given order):
"yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd", "yyyy", "dd.MM.yyyy", "dd MMM yyyy", "dd MMM. yyyy", "MMMM yyyy", "MMM. dd, yyyy", "MMM. yyyy", "MMMM dd, yyyy", "MMM d, ''yy", "MMM. ''yy", "MMMM ''yy"
A parse is only sucessful if it parses the whole of the input string. If no parse patterns match, a ParseException is thrown.
As a special case, the strings
"today"and"now"(ignoring case) return the current date.- Parameters:
str- the date to parse, not null.- Returns:
- the parsed date, or the current date if the input String (ignoring case) was
"today"or"now". - Throws:
ParseException- if no pattern matches.NullPointerException- if str is null.- Since:
- 1.1.1.
-
getImageAttributes
Determine width and height of an image. If successful, the returned SinkEventAttributes contain width and height attribute keys whose values are the width and height of the image (as a String).- Parameters:
logo- a String containing either a URL or a path to an image file. Not null.- Returns:
- a set of SinkEventAttributes, or null if no ImageReader was found to read the image.
- Throws:
IOException- if an error occurs during reading.NullPointerException- if logo is null.- Since:
- 1.1.1
-