WoW Macro explanation
The World of Warcraft plugin consists of two parts: The Macro, and The Web Resources.
The heart of any macro is the
execute method. This method is responsible for returning the result of the macro's function, either in HTML or in WikiMarkup.
This macro goes through a very predictable process:
- Validate and interpret the parameters
- Use the battlenet remote api to get the item details
- Use velocity to render the output
For the complete source take a look here.
Validate The Input
Although there is only one parameter for this macro, it may contain an item id (numerical) or the item name (string), so our macro needs to be able to handle either.
This code shows the process to check for the named and unnamed parameters (using the unnamed as preference). The string value is then validated by trying to convert to an integer.
Now that we have valid input, we use our
itemMapper component to call to battlenet and return an item for us. The internals on how this component connects to battlenet can be found in the source.
Render the Output
This macro uses velocity to render the output. This is helped using the
VelocityUtils class which provides easy to use methods for accessing the Velocity subsystem.
We first create a context map by calling
MacroUtils.defaultVelocityContext()). This creates a
Map of some useful components for our template rendering. Creating a context like this is important if you want to access macro's and other components supplied by Confluence. This example then places this map into a
VeloctyContext object to provide type safety on the put methods.
The template used by this macro is extremely simple.
The variable references to are resolved by Velocity as the template is processed. They are looked up in the context we supplied in the macro. The references to the mapping object resolve to the object we passed in when we rendered the template.
The #requireResource call tell Confluence to include the required resources in the page.
These resources are configured in the
atlassian-plugin.xml file inside the plugin.
This snippet of the configuration shows the definition of the resources this macro uses. Confluence will use the extension of the name attribute to work out how to link in the resource (ie: link or script tag).
You can download this plugin from here and install it through the Administration section of your Confluence instance.
The source is available here.
Compiling the Source
The general details of compiling a plugin applies to this plugin, so follow the instructions there.
For the impatient: