Class SnippetMacro
- java.lang.Object
-
- org.apache.maven.doxia.macro.AbstractMacro
-
- org.apache.maven.doxia.macro.snippet.SnippetMacro
-
- All Implemented Interfaces:
LogEnabled
,Macro
@Component(role=Macro.class, hint="snippet") public class SnippetMacro extends AbstractMacro
A macro that prints out the content of a file or a URL.- Version:
- $Id$
-
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<java.lang.String,java.lang.String>
cache
Holds the cache.private boolean
debug
Debug.private static int
HOUR
private boolean
ignoreDownloadError
in case of Exception during snippet download error will ignored and empty content returned.private static java.util.Map<java.lang.String,java.lang.Long>
timeCached
Holds the time cache.private long
timeout
One hour default cache.
-
Constructor Summary
Constructors Constructor Description SnippetMacro()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cacheSnippet(java.net.URL url, java.lang.String id, java.lang.String content)
Puts the given snippet into the cache.void
execute(Sink sink, MacroRequest request)
Execute the current macro using the given MacroRequest, and emit events into the given sink.private java.lang.String
getCachedSnippet(java.net.URL url, java.lang.String id)
Return a snippet from the cache.private java.lang.StringBuffer
getSnippet(java.net.URL url, java.lang.String encoding, java.lang.String id)
Return a snippet of the given url.(package private) long
getTimeCached(java.net.URL url, java.lang.String id)
Return the absolute value of when the snippet has been cached.private java.lang.String
globalSnippetId(java.net.URL url, java.lang.String id)
Return a global identifier for the snippet.(package private) boolean
isCacheTimedout(java.net.URL url, java.lang.String id)
Return true if the snippet has been cached longer than the current timeout.private void
removeFromCache(java.net.URL url, java.lang.String id)
Removes the snippet from the cache.void
setCacheTimeout(int time)
Set the cache timeout.(package private) long
timeInCache(java.net.URL url, java.lang.String id)
Return the time the snippet has been cached.-
Methods inherited from class org.apache.maven.doxia.macro.AbstractMacro
enableLogging, getAttributesFromMap, getLog, required
-
-
-
-
Field Detail
-
cache
private static java.util.Map<java.lang.String,java.lang.String> cache
Holds the cache.
-
HOUR
private static final int HOUR
- See Also:
- Constant Field Values
-
timeout
private long timeout
One hour default cache.
-
timeCached
private static java.util.Map<java.lang.String,java.lang.Long> timeCached
Holds the time cache.
-
debug
private boolean debug
Debug.
-
ignoreDownloadError
private boolean ignoreDownloadError
in case of Exception during snippet download error will ignored and empty content returned.
-
-
Method Detail
-
execute
public void execute(Sink sink, MacroRequest request) throws MacroExecutionException
Execute the current macro using the given MacroRequest, and emit events into the given sink.- Parameters:
sink
- The sink to receive the events.request
- The corresponding MacroRequest.- Throws:
MacroExecutionException
- if an error occurred during execution.
-
getSnippet
private java.lang.StringBuffer getSnippet(java.net.URL url, java.lang.String encoding, java.lang.String id) throws java.io.IOException
Return a snippet of the given url.- Parameters:
url
- The URL to parse.encoding
- The encoding of the URL to parse.id
- The id of the snippet.- Returns:
- The snippet.
- Throws:
java.io.IOException
- if something goes wrong.
-
getCachedSnippet
private java.lang.String getCachedSnippet(java.net.URL url, java.lang.String id)
Return a snippet from the cache.- Parameters:
url
- The URL to parse.id
- The id of the snippet.- Returns:
- The snippet.
-
isCacheTimedout
boolean isCacheTimedout(java.net.URL url, java.lang.String id)
Return true if the snippet has been cached longer than the current timeout.- Parameters:
url
- The URL to parse.id
- The id of the snippet.- Returns:
- True if timeout exceeded.
-
timeInCache
long timeInCache(java.net.URL url, java.lang.String id)
Return the time the snippet has been cached.- Parameters:
url
- The URL to parse.id
- The id of the snippet.- Returns:
- The cache time.
-
getTimeCached
long getTimeCached(java.net.URL url, java.lang.String id)
Return the absolute value of when the snippet has been cached.- Parameters:
url
- The URL to parse.id
- The id of the snippet.- Returns:
- The cache time.
-
removeFromCache
private void removeFromCache(java.net.URL url, java.lang.String id)
Removes the snippet from the cache.- Parameters:
url
- The URL to parse.id
- The id of the snippet.
-
globalSnippetId
private java.lang.String globalSnippetId(java.net.URL url, java.lang.String id)
Return a global identifier for the snippet.- Parameters:
url
- The URL to parse.id
- The id of the snippet.- Returns:
- An identifier, concatenated url and id, or just url.toString() if id is empty or null.
-
cacheSnippet
public void cacheSnippet(java.net.URL url, java.lang.String id, java.lang.String content)
Puts the given snippet into the cache.- Parameters:
url
- The URL to parse.id
- The id of the snippet.content
- The content of the snippet.
-
setCacheTimeout
public void setCacheTimeout(int time)
Set the cache timeout.- Parameters:
time
- The timeout to set.
-
-