org.tinyjee.maven.dim.spi
Class Globals

java.lang.Object
  extended by org.tinyjee.maven.dim.spi.Globals

public class Globals
extends Object

Is a global, thread-local, recursive configuration singleton that may be used to exchange global settings between classes.

Author:
Juergen_Kellerer, 2011-10-13

Method Summary
 String attachBinaryContent(String url)
          Overloads attachBinaryContent(URL).
 String attachBinaryContent(String fileName, byte[] binaryContent)
          Overloads attachBinaryContent(String, java.io.InputStream).
 String attachBinaryContent(String fileName, InputStream binaryContent)
          Attempts to attach the given content as a file and returns the relative link to the content on success.
 String attachBinaryContent(String fileName, String url)
          Overloads attachBinaryContent(String, URL).
 String attachBinaryContent(String fileName, URL sourceUrl)
          Overloads attachBinaryContent(String, java.io.InputStream).
 String attachBinaryContent(URL sourceUrl)
          Overloads attachBinaryContent(String, URL).
 String attachContent(String fileName, String content)
          Attempts to attach the given content as a file and returns the relative link to the content on success.
 boolean attachCss(String fileName, String cssContent)
          Attempts to attach the given CSS (cascading style sheet).
 boolean attachJs(String fileName, String scriptContent)
          Attempts to attach the given client side java script.
 boolean attachScript(String fileName, String scriptContent, String scriptLanguage, String contentType)
          Attempts to attach the given client side script.
static void cleanupGlobals()
           
 byte[] fetch(String path)
          Fetches the given url and converts the content to a string.
 String fetchText(String path)
          Fetches the given path and converts the content to a string.
 byte[] fetchUrl(URL url)
          Fetches the given url and converts the content to a string.
 String fetchUrlText(URL url)
          Fetches the given url and converts the content to a string.
static String getAttachedIncludesDirectory()
          Returns the relative path (from web root) to the directory hosting the attached includes.
 File getAttachedIncludesPath()
          Returns the file path to use for storing attached includes.
 Set<String> getAttachedResources()
          Returns a set of all resources that were attached to the current sink.
 File getBasedir()
          Returns the current base directory the Macro is operating against.
 Charset getCharset()
          Returns the charset to use when reading text.
 boolean getCharsetIsUserSupplied()
          Returns true if the user supplied a charset with the macro call.
static Globals getInstance()
          Returns a thread local, recursion safe instance.
static org.apache.maven.doxia.logging.Log getLog()
          Returns a safe logger.
 org.apache.maven.doxia.logging.Log getLogger()
          Returns a macro logger to use for logging to the build log.
 MacroRequest getRequest()
          The current macro request, may be 'null'.
 Sink getSink()
          The current sink, may be 'null'.
static Globals initializeGlobals(String charset, Sink sink, MacroRequest request, org.apache.maven.doxia.logging.Log logger, File attachedIncludesPath)
           
 boolean isHTMLCapableSink()
          Returns true if the sink produces some sort of HTML.
 int nextId()
          Returns the next value of the monotonically increasing integer ID (IDs are consistent only for the lifetime of a single module's site build).
 int nextLocalId()
          Returns the next page local value of the increasing integer ID (local IDs are consistent only within a single page).
 Globals operateOnSink(Sink sink)
          Creates a globals instance for the given sink.
static Globals reInitializeGlobals(String charset, Sink sink, MacroRequest request, org.apache.maven.doxia.logging.Log logger, File attachedIncludesPath)
           
 File resolveLocalPath(String localPath)
          Resolves the given local path and returns the corresponding file.
 URL resolvePath(String path)
          Resolves the given path to an URL that can be fetched via UrlFetcher.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

initializeGlobals

public static Globals initializeGlobals(String charset,
                                        Sink sink,
                                        MacroRequest request,
                                        org.apache.maven.doxia.logging.Log logger,
                                        File attachedIncludesPath)

reInitializeGlobals

public static Globals reInitializeGlobals(String charset,
                                          Sink sink,
                                          MacroRequest request,
                                          org.apache.maven.doxia.logging.Log logger,
                                          File attachedIncludesPath)

cleanupGlobals

public static void cleanupGlobals()

getInstance

public static Globals getInstance()
Returns a thread local, recursion safe instance.

Returns:
a thread local, recursion safe instance.

getLog

public static org.apache.maven.doxia.logging.Log getLog()
Returns a safe logger.

Returns:
a safe logger.

getAttachedIncludesDirectory

public static String getAttachedIncludesDirectory()
Returns the relative path (from web root) to the directory hosting the attached includes.

Returns:
the relative path (from web root) to the directory hosting the attached includes.

getCharset

public Charset getCharset()
Returns the charset to use when reading text.

Returns:
the charset to use when reading text.

getCharsetIsUserSupplied

public boolean getCharsetIsUserSupplied()
Returns true if the user supplied a charset with the macro call.

Returns:
true if the user supplied a charset with the macro call.

getLogger

public org.apache.maven.doxia.logging.Log getLogger()
Returns a macro logger to use for logging to the build log.

Returns:
a macro logger to use for logging to the build log.

nextId

public int nextId()
Returns the next value of the monotonically increasing integer ID (IDs are consistent only for the lifetime of a single module's site build).

This method is primarily meant to assign unique ids to page elements (e.g. for javascript, svg, etc).

Returns:
an increasing integer ID.

nextLocalId

public int nextLocalId()
Returns the next page local value of the increasing integer ID (local IDs are consistent only within a single page).

This method is primarily meant to assign unique ids to page elements (e.g. for javascript, svg, etc).

Returns:
an increasing integer ID.

getSink

public Sink getSink()
The current sink, may be 'null'.

Returns:
the current sink, may be 'null'.

operateOnSink

public Globals operateOnSink(Sink sink)
Creates a globals instance for the given sink.

If sink is 'null' or the same as the current, the method returns 'this'.

Parameters:
sink - the sink to return a globals instance for.
Returns:
a globals instance that operates on the given sink.

isHTMLCapableSink

public boolean isHTMLCapableSink()
Returns true if the sink produces some sort of HTML.

Returns:
true if the sink produces some sort of HTML.

getRequest

public MacroRequest getRequest()
The current macro request, may be 'null'.

Returns:
the current macro request, may be 'null'.

getBasedir

public File getBasedir()
Returns the current base directory the Macro is operating against.

Returns:
the current base directory the Macro is operating against.

getAttachedIncludesPath

public File getAttachedIncludesPath()
Returns the file path to use for storing attached includes.

Returns:
the file path to use for storing attached includes, or 'null' if attaching includes as file is not supported.

resolvePath

public URL resolvePath(String path)
Resolves the given path to an URL that can be fetched via UrlFetcher.

Note: Under the hood this method delegates to ResourceResolver.findSource(java.io.File, String).

Parameters:
path - the path to resolve.
Returns:
an URL that can be fetched.

resolveLocalPath

public File resolveLocalPath(String localPath)
Resolves the given local path and returns the corresponding file.

Parameters:
localPath - the local path to resolve.
Returns:
the resolved file or 'null' if the path couldn't be resolved to a local file or directory.

fetchUrl

public byte[] fetchUrl(URL url)
                throws IOException
Fetches the given url and converts the content to a string.

Parameters:
url - the url to fetch.
Returns:
url content as string.
Throws:
IOException - In case of IO failed.

fetch

public byte[] fetch(String path)
             throws IOException
Fetches the given url and converts the content to a string.

Parameters:
path - the path to fetch.
Returns:
the content as byte array.
Throws:
IOException - In case of IO failed.

fetchUrlText

public String fetchUrlText(URL url)
                    throws IOException
Fetches the given url and converts the content to a string.

Parameters:
url - the url to fetch.
Returns:
url content as string.
Throws:
IOException - In case of IO failed.

fetchText

public String fetchText(String path)
                 throws IOException
Fetches the given path and converts the content to a string.

Parameters:
path - the path to fetch.
Returns:
the content as string.
Throws:
IOException - In case of IO failed.

getAttachedResources

public Set<String> getAttachedResources()
Returns a set of all resources that were attached to the current sink.

Returns:
a set of all resources that were attached to the current sink.

attachCss

public boolean attachCss(String fileName,
                         String cssContent)
                  throws IOException
Attempts to attach the given CSS (cascading style sheet).

Parameters:
fileName - a unique name to reference the content. If the name is empty the css will be inlined.
cssContent - the content to attach, either in-lined or using the filename reference. If the css content starts with "classpath:", it is interpreted as a classpath URL to include instead of the real content.
Returns:
a status whether attaching the cascading style sheets is supported by the underlying sink.
Throws:
IOException - In case of disk IO failed.

attachJs

public boolean attachJs(String fileName,
                        String scriptContent)
                 throws IOException
Attempts to attach the given client side java script.

Parameters:
fileName - a unique name to reference the script.
scriptContent - the content to attach, either in-lined or using the filename reference. If the script content starts with "classpath:", it is interpreted as a classpath URL to include instead of the real content.
Returns:
a status whether attaching the script is supported by the underlying sink.
Throws:
IOException - In case of disk IO failed.

attachScript

public boolean attachScript(String fileName,
                            String scriptContent,
                            String scriptLanguage,
                            String contentType)
                     throws IOException
Attempts to attach the given client side script.

Parameters:
fileName - a unique name to reference the script. If the name is empty the script will be inlined.
scriptContent - the content to attach, either in-lined or using the filename reference. If the script content starts with "classpath:", it is interpreted as a classpath URL to include instead of the real content.
scriptLanguage - the script language, defaults to "javascript".
contentType - the content type, defaults to "text/javascript".
Returns:
a status whether attaching the script is supported by the underlying sink.
Throws:
IOException - In case of disk IO failed.

attachContent

public String attachContent(String fileName,
                            String content)
                     throws IOException
Attempts to attach the given content as a file and returns the relative link to the content on success.

Content can only be attached when building sites, as there has to be a resolvable resource directory that is used to put the attached content.

Files of the same name are overwritten if they already exist in the output directory.

When content is stored, it is saved in 2 versions, uncompressed and gzipped. The latter is provided to reduce transfer sizes on supporting web-servers and is created if the compressed size is smaller than 95% of the uncompressed size.

Parameters:
fileName - a unique name to reference the content.
content - the content to attach. If the content starts with "classpath:", it is interpreted as a classpath URL to include instead of the real content.
Returns:
A relative link to the content or 'null' if attaching content is not supported by the underlying environment.
Throws:
IOException - In case of disk IO failed.

attachBinaryContent

public String attachBinaryContent(String url)
                           throws IOException
Overloads attachBinaryContent(URL).

Parameters:
url - the url or path to fetch the binary content from.
Returns:
A relative link to the content or 'null' if attaching content is not supported by the underlying environment.
Throws:
IOException - In case of disk IO failed.

attachBinaryContent

public String attachBinaryContent(String fileName,
                                  String url)
                           throws IOException
Overloads attachBinaryContent(String, URL).

Parameters:
fileName - a unique name to reference the content.
url - the url or path to fetch the binary content from.
Returns:
A relative link to the content or 'null' if attaching content is not supported by the underlying environment.
Throws:
IOException - In case of disk IO failed.

attachBinaryContent

public String attachBinaryContent(URL sourceUrl)
                           throws IOException
Overloads attachBinaryContent(String, URL).

Parameters:
sourceUrl - an URL to fetch the binary content that should be attached.
Returns:
A relative link to the content or 'null' if attaching content is not supported by the underlying environment.
Throws:
IOException - In case of disk IO failed.

attachBinaryContent

public String attachBinaryContent(String fileName,
                                  URL sourceUrl)
                           throws IOException
Overloads attachBinaryContent(String, java.io.InputStream).

Parameters:
fileName - a unique name to reference the content.
sourceUrl - an URL to fetch the binary content that should be attached.
Returns:
A relative link to the content or 'null' if attaching content is not supported by the underlying environment.
Throws:
IOException - In case of disk IO failed.

attachBinaryContent

public String attachBinaryContent(String fileName,
                                  byte[] binaryContent)
                           throws IOException
Overloads attachBinaryContent(String, java.io.InputStream).

Parameters:
fileName - a unique name to reference the content.
binaryContent - the content to attach.
Returns:
A relative link to the content or 'null' if attaching content is not supported by the underlying environment.
Throws:
IOException - In case of disk IO failed.

attachBinaryContent

public String attachBinaryContent(String fileName,
                                  InputStream binaryContent)
                           throws IOException
Attempts to attach the given content as a file and returns the relative link to the content on success.

Content can only be attached when building sites, as there has to be a resolvable resource directory that is used to put the attached content.

Files of the same name are overwritten if they already exist in the output directory.

When content is stored, it is saved in 2 versions, uncompressed and gzipped. The latter is provided to reduce transfer sizes on supporting web-servers and is created if the compressed size is smaller than 95% of the uncompressed size.

Parameters:
fileName - a unique name to reference the content.
binaryContent - the content to attach.
Returns:
A relative link to the content or 'null' if attaching content is not supported by the underlying environment.
Throws:
IOException - In case of disk IO failed.

toString

public String toString()
Overrides:
toString in class Object