%%commentbox
See also ImageGenNestedPluginExample for examples that use JSPlugin to generate ImageGen charts.
%%
Here are a few [JSPlugin] example scripts.

! Show the current time

{{{
[{JSPlugin script='new java.util.Date()'}]
}}}

! Show a page's version history

{{{
[{JSPlugin

result = "|| Author || Version || Modified \n";
for ( i = wikiPage.versions.iterator(); i.hasNext(); ) {
   v = i.next();
   result += "| " + v.author;
   result += "| " + v.versionNumber;
   result += "| " + v.modified;
   result += "\n";
}
result;
}]
}}}

! Show pages matching a pattern

{{{
[{JSPlugin

result = "|| Page || Author \n";
re = new RegExp( "Script" );
for ( i = wikiInfo.pages.iterator(); i.hasNext(); ) {
  page = i.next();
  if ( page.name.match( re ) ) {
     result += "| " + page.name + "| " + page.author + "\n";
  }
}
result;

}]
}}}

! Output some HTML

{{{
[{JSPlugin script='"<div style=\\"margin: 1in; background-color: yellow\\">" + pluginBody + "</span>";' scriptResult='xml' 

The possible values that may be passed in are as defined for get. 
A class that implements this method may choose to ignore calls to 
set certain properties, in which case those properties are effectively 
read-only.

}]
}}}

! Use HTTPClient to get some content from the web

This uses the HTTPClient package from [http://jakarta.apache.org/commons/httpclient/].

{{{
[{JSPlugin result='text'

function GET( url ) {
  client = new Packages.org.apache.commons.httpclient.HttpClient();
  method = new Packages.org.apache.commons.httpclient.methods.GetMethod(url)
  client.executeMethod(method);
  responseBody = method.getResponseBodyAsString();
  return responseBody;
}

GET( "http://joelonsoftware.com/rss.xml" );
}]
}}}

! Cache the content from the web into another page

{{{
[{JSPlugin result='text'

function GET( url ) {
  client = new Packages.org.apache.commons.httpclient.HttpClient();
  method = new Packages.org.apache.commons.httpclient.methods.GetMethod(url)
  client.executeMethod(method);
  responseBody = method.getResponseBodyAsString();
  return responseBody;
}

function CONTENT() {
  return GET( "http://joelonsoftware.com/rss.xml" );
}

cacheName = wikiPahge.name + "cache";

if ( ! wikiInfo.hasPage( cacheName ) ) {
   cachePage = wikiInfo.createPage( cacheName, CONTENT() );
}
else {
   cachePage = wikiInfo.getPage( cacheName );
   if ( wikiPage.modified > cachePage.modified ) {
      cachePage.setContent( CONTENT() );
   }
}

cachePage.content;
}]
}}}

! Use StringTemplate to format a result

This uses the StringTemplate package from [http://www.stringtemplate.org/].

{{{
[{JSPlugin

query = new Packages.org.antlr.stringtemplate.StringTemplate( "SELECT $column; separator=\",\"$ FROM $table$;" );
query.setAttribute( "column", "name" );
query.setAttribute( "column", "email" );
query.setAttribute( "table", "User" );
query.toString();
}]
}}}


! Simple Estimate Script

Use this script to convert task data

{{{
Feature A
   Task a1  4
   Task a2  4
   Task a3  12

Feature B
   Task b1  4
   Task b2  8
   Task b3  1

Feature C
   Task c1  12
   Task c2  4
   Task c3  4
}}}

into this table

| ||Feature A| 
|1.|Task a1 |4 
|2.|Task a2 |4 
|3.|Task a3 |12 
| | | 20 hours 
| | | 5 days 
| ||Feature B| 
|1.|Task b1 |4 
|2.|Task b2 |8 
|3.|Task b3 |1 
| | | 13 hours 
| | | 3 days 
| ||Feature C| 
|1.|Task c1 |12 
|2.|Task c2 |4 
|3.|Task c3 |4 
| | | 20 hours 
| | | 5 days 
| || Project overall | 
| | || 53 hours 
| | || 11 days

{{{
result = "";

eolPattern = /\r?\n/;
commentPattern = /^\s*(#.*)?$/;
taskPattern = /^[ \t]+(.*)[ \t]+([0-9]+)[ \t]*$/;
featurePattern = /^[ \t]*(.*)[ \t]*$/;

hoursPerDay = +pluginArgs.hoursPerDay;
hasFeature = 0;
taskIndex = 0;
totalFeatureHours = 0;
totalHours = 0;
hasFeature = false;

lines = pluginBody.split( eolPattern );
for ( i = 0; i < lines.length; i++ ) {
    line = lines[i];
    if ( line.match( commentPattern ) ) {
       continue;
    }
    else if ( m = taskPattern.exec( line ) ) {
        totalFeatureHours += +m[2];
        result += "|" + ( ++taskIndex ) + ".|" + m[1] + "|" + m[2] + "\n";
    }
    else if ( m = featurePattern.exec( line ) ) {
        if ( hasFeature ) {
           result += "| | | " + totalFeatureHours + " hours\n";
           result += "| | | " + Math.round( ( totalFeatureHours + hoursPerDay - 1 ) / hoursPerDay ) + " days\n";
           totalHours += totalFeatureHours;
        }
        hasFeature = true;
        totalFeatureHours = 0;
        taskIndex = 0;
        result += "|  ||" + m[1] + "|\n";
    }
    else {
        throw ( "can't understand line " + i + " " + line );
    }
}

if ( hasFeature ) {
    result += "| | | " + totalFeatureHours + " hours\n";
    result += "| | | " + Math.round( ( totalFeatureHours + hoursPerDay - 1 ) / hoursPerDay ) + " days\n";
    totalHours += totalFeatureHours;
}

result += "| || Project overall |\n";
result += "| | || " + totalHours + " hours \n";
result += "| | || " + Math.round( ( totalHours + hoursPerDay - 1 ) / hoursPerDay ) + " days\n";

result;
}}}

! A small E4X example using an RSS feed.

{{{
[{JSPlugin scriptResult='xml'

function GET( url ) {
   ...
}

rss = XML( GET( "http://joelonsoftware.com/rss.xml" ) );

result = "";
for each ( item in rss..item ) {
   result += "<b>" + item.title + "</b><br />\n";
   result += "<a href=\"" + item.link + "\">" + item.link + "</a><br />\n"
   result += "<div>" + item.description + "</div>\n";
   result += "<hr />";
}

result;
}]
}}}


! An Alternative to the ~RecentChanges plugin
See [JSPluginRecentChangesExample].