Jun 04

Using jrunx classes in ColdFusion

Posted by James Netherton | Wednesday 04 June 2008 8:34 PM | In ColdFusion

I've been screwing around with classes within the jrunx Java package that comes bundled with ColdFusion (not sure if it exists if you aren't running CF on JRun). There are some useful and interesting utilities and I've put together a very brief set of demos for a few of them.

jrunx.util.NetUtil

Lookup an IP address from a DNS string:

<cfset variables.netUtil = createObject("java", "jrunx.util.NetUtil")/>

<cfset variables.ip = variables.netUtil.resolveIp("www.google.com")/>

<cfoutput>#variables.ip#</cfoutput>

Validate an IP address:

<cfset variables.netUtil = createObject("java", "jrunx.util.NetUtil")/>

<cfset variables.hostValid = variables.netUtil.validateHostIp("127.0.0.1")/>

<cfoutput>#variables.hostValid#</cfoutput>

jrunx.util.ZipTool

Zip a directory:

<cfset variables.zipUtil = createObject("java", "jrunx.util.ZipTool")/>
<cfset variables.zipUtil.zipDir("C:/Test", "C:/Test.zip", true)/>

Zip a single file:

<cfset variables.zipUtil = createObject("java", "jrunx.util.ZipTool")/>
<cfset variables.zipUtil.unZipSingleFile("C:/Test.zip", "C:/TestUnzip", "myHTMLFile.html")/>

Unzip an entire archive:

<cfset variables.zipUtil = createObject("java", "jrunx.util.ZipTool")/>
<cfset variables.zipUtil.unzipToPath("C:/Test.zip", "C:/TestUnzip")/>

jrunx.xml.XMLScript

Change an XML entity value within an XML file: XML Input File

<root>
   <person id="1">
      <name>James</name>
      <age>00</age>
      <occupation>Developer</occupation>
   </person>
   <person id="2">
      <name>Bob</name>
      <age>00</age>
      <occupation>Developer</occupation>
   </person>
</root>

<cfset variables.xmlScript = createObject("java", "jrunx.xml.XMLScript").init()/>

<cfset variables.inputFile = expandPath("./input.xml")/>
<cfset variables.outputFile = expandPath("./output.xml")/>

<cfset variables.args = arrayNew(1)/>

<cfset variables.args.add("-i")/>
<cfset variables.args.add(variables.inputFile)/>
<cfset variables.args.add("-o")/>
<cfset variables.args.add(variables.outputFile)/>
<cfset variables.args.add("-s")/>
<cfset variables.args.add("/root/person[@id='1']/name/text()")/>

<cfset variables.args.add("Joe")/>

<!--- Hackery to create a java string array --->

<cfset variables.dummyString = repeatString(" : ", arrayLen(variables.args) - 1)/>
<cfset variables.stringArray = variables.dummyString.split(":")/>

<cfloop from="1" to="#arrayLen(variables.args)#" index="i">
   <cfset variables.stringArray[i] = variables.args[i]/>
</cfloop>

<cfset variables.xmlScript.command(variables.stringArray)/>

XML Output File

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <person id="1">
      <name>Joe</name>
      <age>00</age>
      <occupation>Developer</occupation>
   </person>
   <person id="2">
      <name>Bob</name>
      <age>00</age>
      <occupation>Developer</occupation>
   </person>
</root>

Admittedly that's a very lame example, there's a proper overview of what the class can really do on the Adobe Jrun guide There's a full overview of jrunx classes and packages here.

 

0 Comments

[Post comment]


Leave a comment








Captcha text