3. Implement the findServerBehaviors() function
Key Concepts
|
Find all locked nodes and search them with regular expressions |
|
Use the SSRecord class for each occurrence found |
|
Set title and participant properties |
The findServerBehaviors() function is called whenever the document has changed and the search string (from step 2) is found in the document. This function finds all occurrences of the run-time code and returns an array of objects describing them. Each object will be an instance of the SSRecord class (documented in Configuration/Shared/UltraDev/scripts/ssClasses.js ).
In the user's document, all ASP tags are translated and locked. Internally, each ASP tag <% ... %> is stored in a special lock tag that you can find quickly. This tag has an ORIG attribute containing the escaped original code. Here's how to find it:
function findServerBehaviors() {
var dom = dw.getDocumentDOM();
var lockedNodes = dom.getElementsByTagName("MM:BEGINLOCK");
for (var i=0; i<lockedNodes.length; i++) {
node = lockedNodes[i];
nodeStr = unescape(node.orig);
alert("Found an ASP tag: "+nodeStr);
}
return Array();
}
So far, this code finds all ASP tags and displays them. Test the code by saving your server behavior and restarting Dreamweaver. Open your testRedirectIfEmpty.asp document. You should see some alerts displaying each ASP tag found.
Next, add some search code to find your specific run-time ASP. Start by defining a global pattern to search for. At the top of your JavaScript, add the following regular expression as a global constant:
var PATT1 = /if\s*\((\w+)\.EOF\)\s*Response\.Redirect\("([^"]*)"/;
Note that there are no spaces (\s* indicates optional spaces) Also note that some literal punctuation characters, such as periods and parentheses, have special meanings and must be "escaped" by preceding them with a backslash. You'll definitely need to find a good regular expression reference, such as O'Reilly's JavaScript guide.
To use this pattern, replace your alert line with the following regular expression search:
tokens = nodeStr.match(PATT1);
if (tokens && tokens.length > 2) {
alert("Found "+nodeStr+",\nRecordset:"+tokens[1]+", URL:"+tokens[2]);
}
The match() method remembers each token parameter grouped in parentheses. Notice that for the PATT1 attribute line, the recordset and URL are wrapped in unescaped parentheses. The values found are saved in tokens[1] and tokens[2] . Test this code by saving your server behavior file, restarting Dreamweaver, and opening the test file. It should display an alert for each run-time code chunk and display the extracted data.
Dreamweaver expects findServerBehaviors() to return an array. At the top of the function, define an array called ssRecList :
var ssRecList = new Array();
and at the bottom of the function, return ssRecList .
Each time you find an occurrence of the ASP run-time code, add required information to the array. Dreamweaver is expecting an array of JavaScript objects with certain properties. To make this simpler, use the predefined SSRecord class created for this purpose. As you find each run-time occurrence, create a new instance of the class with the statement:
The ssRec object has several properties that are required by Dreamweaver. For a full explanation of these properties, see ssClasses.js. First, set the title property to a string that you would like displayed in the server behavior window. The participants property is an array of participant nodes. In this example, you only have one participant, so set participant[0] equal to node. Here's the finished code:
var PATT1 = /if\s*\((\w+)\.EOF\)\s*Response\.Redirect\("([^"]*)"/;
function findServerBehaviors() {
var ssRecList = new Array();
var dom = dw.getDocumentDOM();
var lockedNodes = dom.getElementsByTagName("MM:BEGINLOCK");
for (var i=0; i<lockedNodes.length; i++) {
node = lockedNodes[i];
nodeStr = unescape(node.orig);
tokens = nodeStr.match(PATT1);
if (tokens && tokens.length>2) {
ssRec = new SSRecord();
ssRec.title = "Redirect If Empty ("+tokens[1]+")";
ssRec.participants[0] = node;
ssRecList.push(ssRec);
}
}
return ssRecList;
}
Your server behavior file should now look like the sample file Step03_RedirectIfEmpty.htm.
Restart Dreamweaver and open the test file. Open the server behavior window. It should list "Redirect If Empty (Recordset1)" and "Redirect If Empty (Recordset2)."
|