Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Log content with hyperlinks



The ScriptLogger is an interface which logs informations and errors during the script execution. Some of these methods allow to show hyperlinked content.

Overview

It is possible to log hyperlinks in the ScriptLogger. This is based on several methods in the logger: The logger also has the following methods methods which allow to show hyperlinks using Strings:
If you want to use these methods, the ScriptLoggerHyperLinkListener.visitHyperlink(String) method will be called in the ScriptLoggerHyperLinkListener with the linkID as argument. You will need to decode the link ID yourself.
For a single link using a String, you must set the text shown for the link and the ID of the link.

For example:
      int i = 0;
      public void process(TreeElement element) {
         i++;
         String linkID = >the link ID generated from the element>
         logger.appendLink("the element " + i, linkID);
      }      

hyperlinksStrings For a single link using a String, you must set the text and the list of links. The text will contain a {[0-9 _A-Za-z]+} pattern for each link. The content inside the accolades will be used for the text of the link.

For example:
      int i = 0;
      public void process(TreeElement element) {
         i++;      
         String linkID = >the link ID generated from the element> 
         String text = "This is the {element " + i + "} link";    
         logger.appendLinks(text, linkID);
      }      

hyperlinksStrings2
Main Article: LinkIndexConverter

The logger also has the following methods methods which allow to show hyperlinks using HyperlinkElements:
If you want to use these methods, you must have registered a LinkIndexConverter with the ScriptLogger.registerLinkIndexConverter(LinkIndexConverter) method.


appendObjectlink
If the LinkIndexConverter has not been able to convert the Object to a link ID (the returned result is null), then by default an error message will be shown. It is possible to append a regular text without an hyperlink instead by setting the ScriptLogger.acceptNullObjectLinks(boolean) method.
The HyperlinkElement contains both the link associated text and the element associated with the link. The element will be converted to a link ID with the associated LinkIndexConverter.

For example:
      public void process(TreeElement element) {
         HyperlinkElement linkElt = new HyperlinkElement("the text shown for the link", element);
         logger.appendObjectLink(linkElt);
      }      

hyperlinksStrings For a single link using a String, you must set the text and the list of links. The text will contain a %\d+ pattern for each link, and will refer to the i-th HyperlinkElement in the arguments.

For example:
      public void process(TreeElement element) {
         HyperlinkElement linkElt = new HyperlinkElement(element.name, element);
         logger.appendObjectLinks("The element is %1", linkElt);
      }     

hyperlinksObjects

HyperlinkElement

The HyperlinkElement contains:
  • The object which is used for the hyperlink
  • The text which will be shown for the hyperlink
For example:
      public void process(TreeElement element) {
         HyperlinkElement linkElt = new HyperlinkElement("the text shown for the link", element);
         logger.appendObjectLink(linkElt);
      }      

To be notified when an hyperlink is visited, you must register a ScriptLoggerHyperLinkListener with the ScriptLogger.registerHyperLinkListener(ScriptLoggerHyperLinkListener).

For example:
      GroovyScriptWrapper<Script> wrapper = new GroovyScriptWrapper<Script>() {
      };
      ScriptLoggerHyperLinkListener linkListener = new MyLoggerHyperLinkListener();
      ScriptLogger logger = new DefaultSwingScriptLogger();
      logger.registerHyperLinkListener(linkListener);
      wrapper.setScriptLogger(logger);
The method called for a visited link notification depends on if a LinkIndexConverter has been registered or not:
You don't need to implement both of the methods of the ScriptLoggerHyperLinkListener interface. Both of the methods have a default empty implementation.

No LinkIndexConverter

visithyperlink
The ScriptLoggerHyperLinkListener.visitHyperlink(String) method will be called.

Note that in this case it is the responsability of the ScriptLoggerHyperLinkListener to decode the link ID provided as an argument to detect which element the link represents.


For example:
      public class MyLoggerHyperLinkListener implements ScriptLoggerHyperLinkListener {
         @Override
         public void visitHyperLink(String linkID) {
           // get the associated elemet from the linkID and do something with it
         }      
      }

With a LinkIndexConverter

Main Article: LinkIndexConverter


visitobjecthyperlink
The ScriptLoggerHyperLinkListener.visitHyperlink(Object) method will be called.

If the LinkIndexConverter has not been able to convert the link ID (the returned result is null), then an error message will be shown.


For example:
      public class MyLoggerHyperLinkListener implements ScriptLoggerHyperLinkListener<TreeElement> {
         @Override
         public void visitHyperLink(TreeElement element) {
           // do something with the tree element
         }      
      }
Note that you are not required to specify the type of the Object used for the ScriptLoggerHyperLinkListener, If you don't do it, you will have this method to implement:
      public class MyLoggerHyperLinkListener implements ScriptLoggerHyperLinkListener {
         @Override
         public void visitHyperLink(Object o) {
           // do something with the Object
         }      
      }

See also


Categories: api

Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence