Using system macros, Arachnophilia can interface with external Java classes written by the user or provided by third parties.
This essentially means any imaginable function can be added to Arachnophilia.
Here is how to proceed:
- Create (or acquire) Java classes containing methods that
- Are public.
- Accept and return a String.
- Put these classes in your Arachnophilia user directory or a subdirectory of that directory.
- Invoke one of two macros:
- [RunCustomClassArg:arg] (arg) = (className,argument). Processes argument through user class (className)
- [RunCustomClassDoc:arg] (arg) = className. Processes current document through a user class named in (arg)
This is the general idea, and most programmers will have no problems at all. But, just for an example, we'll invoke a sample Java class that is included with Arachnophilia.
Here is a listing of the test class (Copy the block below to create your own content):
/*
* Created on Oct 26, 2009 12:06:55 PM
*/
/* TestClass is meant to demonstrate the use
* of Arachnophilia's custom class launcher feature.
* Just compile this class in place, create
* a macro that looks like this:
* "[RunCustomClassDoc:CustomClasses/TestClass]",
* load a suitable document, and activate the macro.
*/
package CustomClasses;
import java.util.*;
public class TestClass {
// this is a generic, minimal global search & replace method
private String searchReplace(String data,String find,String replace)
{
StringBuffer sb = new StringBuffer();
int a = 0,b;
int findLength = find.length();
while((b = data.indexOf(find,a)) != -1) {
sb.append(data.substring(a,b));
sb.append(replace);
a = b + findLength;
}
if(a < data.length()) {
sb.append(data.substring(a));
}
return sb.toString();
}
// this is the default method that custom classes
// must have to work with Arachnophilia's class
// launcher feature unless a specific method name
// is provided
public String processString(String s)
{
String result = "Hello! This is an example of Java Custom Class Interfacing!\n\n"
+ "You sent over this:\n"
+ "\"" + s + "\"\n"
+ "Here it is all uppercase:\n"
+ "\"" + s.toUpperCase() + "\"\n"
+ "Here it is all lowercase:\n"
+ "\"" + s.toLowerCase() + "\"\n"
+ "Here it is with a global change:\n"
+ "\"" + searchReplace(s,"a","(A)") + "\"\n";
return result;
}
// this is another method to show the ability to choose
// any method by name
public String reallyBoringMethod(String s)
{
return "On " + new Date() + ", you invoked this really boring Java method.";
}
}
This test class has already been created and compiled, and should be in your user directory under a subdirectory named "CustomClasses." Let's see if it is. :)
Open a new text document and type something into it. Here's our example text:
This is a test phrase.
Now find the menu item
Programming ... Custom Class Demo
and click it. The macro behind this menu item has this content by default:
[RunCustomClassDoc:CustomClasses/TestClass]
This macro means "Find a Java class in a subdirectory of the Arachnophilia user directory named "CustomClasses," find a class there named 'TestClass,' and run a method called 'ProcessString' with the current document's contents as its argument."
Here is the outcome for our example sentence:
Hello! This is an example of Java Custom Class Interfacing!
You sent over this:
"This is a test phrase."
Here it is all uppercase:
"THIS IS A TEST PHRASE."
Here it is all lowercase:
"this is a test phrase."
Here it is with a global change:
"This is (A) test phr(A)se."
If the macro is invoked as above, it will assume the existence of a public method called "ProcessString," and it will tell you if it cannot find it. If you want to use a different method name, simply provide the name, like this:
[RunCustomClassDoc:CustomClasses/TestClass.reallyBoringMethod]
It happens there is a method with this name in our test class, and if invoked it will return (something resembling):
On Fri Mar 08 21:58:27 PST 2002, you invoked this really boring Java method.
Custom classes created by the user can be located anywhere in the user's Arachnophilia directory or subdirectories, and there can be any number of methods with any names in the classes. The only requirements are that the package identifier be the same as the above listing ("CustomClasses") and that the methods be public and receive and return a String.