Available Transformations
Version 9.1 by Vincent Massol on 2011/12/09 13:17
Here's the list of available Transformations you can use:
Macro Transformation
See Architecture.
Icon Transformation
See the Icon Transformation documentation to understand what this transformation does.
Example of using it:
// Initialize Rendering components and allow getting instances
final EmbeddableComponentManager cm = new EmbeddableComponentManager();
cm.initialize(this.getClass().getClassLoader());
XDOM xdom = new XDOM(Arrays.<Block>asList(new ParagraphBlock(Arrays.asList((Block) new SpecialSymbolBlock(':'),
new SpecialSymbolBlock(')')))));
Transformation transformation = cm.lookup(Transformation.class, "icon");
TransformationContext txContext = new TransformationContext();
transformation.transform(xdom, txContext);
WikiPrinter printer = new DefaultWikiPrinter();
BlockRenderer renderer = cm.lookup(BlockRenderer.class, Syntax.XWIKI_2_0.toIdString());
renderer.render(xdom, printer);
String expected = "image:emoticon_smile";
Assert.assertEquals(expected, printer.toString());It's also possible to configure new emoticons or override existing ones by getting the IconTransformationConfiguration component as shown below (works on XWiki Rendering 3.3M1+):
// Initialize Rendering components and allow getting instances
final EmbeddableComponentManager cm = new EmbeddableComponentManager();
cm.initialize(this.getClass().getClassLoader());
XDOM xdom = new XDOM(Arrays.<Block>asList(new ParagraphBlock(Arrays.asList((Block) new SpecialSymbolBlock(':'),
new SpecialSymbolBlock(':')))));
// Test adding a new Icon Mapping.
cm.lookup(IconTransformationConfiguration.class).addMapping("::", "something");
Transformation transformation = cm.lookup(Transformation.class, "icon");
TransformationContext txContext = new TransformationContext();
transformation.transform(xdom, txContext);
WikiPrinter printer = new DefaultWikiPrinter();
BlockRenderer renderer = cm.lookup(BlockRenderer.class, Syntax.XWIKI_2_0.toIdString());
renderer.render(xdom, printer);
String expected = "image:box";
Assert.assertEquals(expected, printer.toString());WikiWord Transformation
See the WikiWord Transformation documentation to understand what this transformation does.
Example of using it:
// Initialize Rendering components and allow getting instances
final EmbeddableComponentManager cm = new EmbeddableComponentManager();
cm.initialize(this.getClass().getClassLoader());
XDOM xdom = new XDOM(
Arrays.<Block>asList(new ParagraphBlock(Arrays.asList((Block) new WordBlock("WikiWord")))));
Transformation transformation = cm.lookup(Transformation.class, "wikiword");
TransformationContext txContext = new TransformationContext();
transformation.transform(xdom, txContext);
WikiPrinter printer = new DefaultWikiPrinter();
BlockRenderer renderer = cm.lookup(BlockRenderer.class, Syntax.XWIKI_2_0.toIdString());
renderer.render(xdom, printer);
String expected = "[[WikiWord]]";
Assert.assertEquals(expected, printer.toString());LinkChecker Transformation
Checks validity of external links (URLs) and store the result in memory which can then be accessed through a Component or through a Script Service.
Here's how it works:
- When the LinkChecker Transformation Component is first looked up, it spawns a LinkChecker Thread
- All external links (URLs) found are put on a Queue in order to not impact the rendering process
- The LinkChecker Thread is a low priority Thread that checks if there are links to verify on the Queue, take them one by one and verify the links by calling them and storing the response code of the HTTP request in memory
- The LinkChecker Thread only rechecks a given link after a certain amount of time has elapsed (1 hour by default)
- When an external link is checked and a response code < 200 or > 299 is returned then an event of type org.xwiki.rendering.transformation.linkchecker.InvalidURLEvent is sent and the passed Event data is a Map containing the following key/values:
- url: the link reference
- source: the reference to the source where the link was found or "default" if not source reference was found
- state: a org.xwiki.rendering.transformation.linkchecker.LinkState containing the response code and the last checked time
Accessing Link states from a Component
Example:
...
<dependency>
<groupId>org.xwiki.rendering</groupId>
<artifactId>xwiki-rendering-transformation-linkchecker</artifactId>
<version>3.3-milestone-1</version>
</dependency>
......
@Inject
private LinkStateManager linkStateManager;
...
public void someMethod()
{
Map<String, Map<String, LinkState>> linkStates = this.linkStateManager.getLinkStates();
}Listening to links with problems
Here's an example in Groovy to listen to InvalidURLEvent events.
{{groovy}}
import groovy.util.logging.*
import org.xwiki.observation.*
import org.xwiki.observation.event.*
import org.xwiki.rendering.transformation.linkchecker.*
import com.xpn.xwiki.web.*
import com.xpn.xwiki.*
@Log
class MyLinkListener implements EventListener
{
def xwiki
def context
MyLinkListener(xwiki, context)
{
this.xwiki = xwiki
this.context = context
}
String getName()
{
return "myLinkListener"
}
List<Event> getEvents()
{
return Arrays.asList(new InvalidURLEvent())
}
void onEvent(Event event, Object eventSource, Object data)
{
def url = eventSource.get("url")
def source = eventSource.get("source")
def state = eventSource.get("state")
log.info("Error for {url} in ${source} - Response code: ${state.getResponseCode()} - Checked: ${String.format('%tF %<tT', state.getLastCheckedTime())}")
}
}
// Register against the Observation Manager
def observation = Utils.getComponent(ObservationManager.class)
observation.removeListener("myLinkListener")
def listener = new MyLinkListener(xwiki, xcontext)
observation.addListener(listener)
{{/groovy}}