Dynamic Configuration File Techniques
When integrating EditLive into your application, it is often handy to generate your configuration files using a server-side web scripting language. However, as Adrian mentioned in a previous post, configuration files are cached by EditLive, which causes problems with a dynamic configuration. One of his solutions was to add a "cachebusting" parameter, the other was to use setConfigurationText. Today, I'll be expanding on the use of setConfigurationText with a dynamic configuration file.
The key issue is that you need a way to get the output from your configuration web script into a variable, so you can pass it to the setConfigurationText method. Below are several techniques for doing this. The final example provides an alternative to server-side scripting in generating dynamic configuration.
cfsavecontent
I was a ColdFusion programmer in a previous job, so the first solution that came to mind was to use the "cfsavecontent" tag. cfsavecontent is a very handy little tag, which stores the output of your script in a variable, rather than outputting it to the HTTP response. You then URL encode it, and pass it to the setConfigurationText call. e.g.
configuration.cfm
<cfsavecontent variable="myconfig">
<?xml version="1.0" encoding="utf-8"?>
<editlive>
…
</editlive>
</cfsavecontent>
mypage.cfm
<cfinclude template="configuration.cfm">
<cfoutput>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="./editlivejava/editlivejava.js"></script>
</head>
<body>
<script language="JavaScript">
var editlivejava1;
editlivejava1 = new EditLiveJava("ELJApplet1", "700", "400");
editlivejava1.setConfigurationText("#URLEncodedFormat(myconfig)#");
editlivejava1.setDownloadDirectory("./editlivejava");
editlivejava1.show();
</script>
</body>
</html>
</cfoutput>
ASP line-by-line
I found a very clunky way to do this in ASP, as a response to a support case. This uses a "write" method to build up the configuration string, one line at a time. This isn't very manageable, as each line needs a "write" method. It may also get in the way of the dynamically-generated sections. Adding the "write" calls to the beginning and end of each line can easily be done with a regular expression replace, or with an editor which has "column editing" e.g. in JEdit you can ctrl-drag to select the first character in the line, then everything you type will end up in each line. Still, this is nowhere near as elegant as the cfcontent version.
Note: this example uses EditLive's ASP instantiation API, which does the URL encoding for you.
<%
dim config
config = ""
sub write(text)
config = config & text
end sub
function generateConfig
write("<editLiveForJava> ")
write(" <mediaSettings> ")
write(" <images> ")
write(" <imageList /> ")
write(" </images> ")
write(" </mediaSettings> ")
write(" <hyperlinks> ")
write(" <hyperlinkList /> ")
write(" </hyperlinks> ")
write("</editLiveForJava> ")
generateConfig = config
end function
Dim elglobal
set elglobal= New EditLiveForJavaGlobal
elglobal.DownloadDirectory = "/editlivejava"
elglobal.Init()
Dim editlive1
set editlive1 = new EditLiveForJava
editlive1.name="ELApplet1"
editlive1.width=600
editlive1.height=800
editlive1.configurationtext = generateConfig
editlive1.Body = "<p>loading default content.</p>"
editlive1.show
%>
AJAX
The below code uses the Prototype Javascript library to get the configuration using an AJAX request. Note: this technique causes an extra HTTP request to get the config, which negates a key benefit of setConfigurationText.
However, if you're creating an AJAX application, it may be more cohesive for you to use AJAX to get the config.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script src="/editlivejava/editlivejava.js"></script>
<script src="prototype.js"></script>
<script language="JavaScript">
function loadELJ(transport) {
var editlivejava1;
editlivejava1 = new EditLiveJava("ELJApplet1", "700", "400");
editlivejava1.setConfigurationText(encodeURIComponent(transport.responseText));
editlivejava1.setDownloadDirectory("/editlivejava");
$("mydiv").update(editlivejava1.getAppletHTML());
}
function loadConfigFail() {
alert('Error retrieving EditLive config.');
}
new Ajax.Request('config.asp', {
method:'post',
onSuccess: loadELJ,
onFailure: loadConfigFail
});
</script>
</head>
<body>
<div id="mydiv">
loading…
</div>
</body>
</html>
Server-side HTTP Request
I won't go into this in detail, but the basic concept is to get the configuration by doing a HTTP request from your server-side scripts.
In ASP, this can be done with the Msxml2.ServerXMLHTTP or WinHttp.WinHttpRequest.5.1 COM objects. Examples of using these objects can be found at http://www.asp101.com/samples/http.asp and http://www.asp101.com/samples/winhttp5.asp
In ColdFusion, this can be done with the "cfhttp" tag.
String Replacement
Another way to dynamically generate your configuration file is to use string replacement, rather than a server-side script. With this technique, you have a static configuration file containing "tokens", e.g.
<images allowLocalImages="[%allowLocalImages%]" allowUserSpecified="[%allowUserSpecified%]">
You then read the file in (via HTTP, or straight off the filesystem), then use string replacement functions to replace the tokens with their actual values.
This technique is useful when using the EditLive Swing SDK, as you may not have a web scripting environment available. That said, the technique can also be used from server-side and client-side web scripts. It's particularly useful when you only have minor changes to the configuration since it's very simple to implement.

