Find and Replace in ColdFusion - coldfusion

Here is the code which is troubling me. I need to find the code and replace with Fancybox code, but it is throwing an error: win is undefined
replace the mywin value
<cfsavecontent variable="x">
mywin = window.open (url,"win",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=ye‌s,width=878,height=810');
</cfsavecontent>
to this:
mywin = $.fancybox('href' :url,'width': '500');
I am trying like this
<cfset a = Replace(x,"mywin = window.open (url,"win",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');","$.fancybox()","one")>
Update Code:
<cfsavecontent variable="foo">
function setmycode() {
url = "http://myurl.com?thestep=9&sortBy=1&sortOrder=1";
mywin = window.open (url,"win",'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');
mywin.focus();
return false;
}
</cfsavecontent>
<cfset a = Replace(foo,"mywin = window.open (url,""win"",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');","$.fancybox()","one")>
<cfdump var="#a#">

You need to pay attention to your code. Even the syntax highlighting in your very question actually points out the error to you!
If an error says something about "win" and the text "win" is highlighted by a syntax highlighter... it's a bit of a give away that that is where the error lies. So that's what you should be scrutinising.
You have a string delimited with double quotes, but the string itself has double quotes in it:
mywin = window.open (url,"win",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');
So the CF parser sees the first double quote around "win" as the end of the string. And then what follows is just garbage (and will yield a syntax error, as you are seeing).
If your string has string-delimiters within it, you need to escape them. In CFML one does this by doubling them, eg: "" ("Special characters")
So your string becomes:
mywin = window.open (url,""win"",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');
And when delimited, it is now parseable by CF:
<cfset a = Replace(x,"mywin = window.open (url,""win"",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');","$.fancybox()","one")>

Related

Coldfusion opencsv csvWriter - I get writeNext method was not found

I have the following code:
<cfset csvFilename = 'myCSV.csv'>
<cfset fileWriter = createobject("java","java.io.FileWriter").init("#csvFileName#")>
<cfset csvWriter = createObject("java","com.opencsv.CSVWriter").init(fileWriter, ",")>
<cfset csvWriter.writeNext('{"1","2"}', true)>
<cfset csvWriter.flush()>
<cfset fileWriter.close()>
<cfset csvWriter.close()>
When I run the page I get this error message:
The writeNext method was not found.
Either there are no methods with the specified method name and
argument types or the writeNext method is overloaded with argument
types that ColdFusion cannot decipher reliably. ColdFusion found 0
methods that match the provided arguments. If this is a Java object
and you verified that the method exists, use the javacast function to
reduce ambiguity.
I have searched the internet and cannot seem to find any examples of using csvWriter with Coldfusion and I am not sure why it is not working. I have a working example of csvReader and ReadNext, but not WriteNext. Any ideas of what I am doing wrong? I have tried to do a javacast but that didn't work either.
<cfset csvWriter.writeNext(javacast("string",'1,2'))>
I am using Coldfusion 11 with opencsv-3.8.jar
According to the API, that overload of writeNext() expects a String[], or an array of Strings. CF arrays are a little different than Java's, but they are compatible. Either of these would work:
<cfset csvWriter.writeNext( ["1","2"], true)>
<cfset csvWriter.writeNext( javacast("String[]", ["3","4"]), javacast("boolean", true))>
As an aside, skip the call to fileWriter.close(). When you call CSVWriter.close(), it closes the underlying writer for you. Calling both will cause an error.
<cfset csvFilename = 'myCSV.csv'>
Without a full path, I am not sure where that file will end up. Always specifying a full path can save a lot of head scratching later on ;-)

fixing the url from the string using replace

I am doing a cfhttp call using a cfx_http5 tag as it is faster and better than cfhttp. so the links are coming as:
sort A
so i add the following script using the replace
http://mysubdomain.domain.com/http://mysubdomain.domain.com/e9.asp?rpttype=298&sortBy=1&sortOrder=2
<cfset lnk = ReplaceNoCase(objget, 'href="', 'href="http://mysubdomain.domain.com/', 'all')>
in few of the links, it is coming as correct but in few of the links it is coming as above appending one more to the already existing one,
so i want to make it conditional, if it exists, do not append or add or else add if there is no link
any ideas?
You can use regular expressions with negative lookahead like this:
<cfset lnk = reReplaceNoCase(objget, 'href=\"(?!http\:\/\/)','href="http://mysubdomain.domain.com', 'ALL')>
This will work for both type of links.
Simply create a conditional if statement
lnk = 'http://mysubdomain.domain.com/e9.asp?rpttype=298&sortBy=1&sortOrder=2';
if (!findNoCase('mysubdomain.domain.com', lnk)) {
lnk = ReplaceNoCase(objget, 'href="', 'href="http://mysubdomain.domain.com/', 'all');
}

how to get value of specifc string in coldfusion with regex

I am trying to get the url value of the following string with coldfusion, i am using list function but i am lost how do i get that please advice
<cfsavecontent variable="foo">
function modalwindow() {
url = "http://www.idea.com?mycode=9&pagenum=-1&sortBy=1&sortOrder=1";
mywin = window.open (url,"win",'toolbar=yes,location=yes,resizable=yes,copyhistory=yes,scrollbars=yes,width=878,height=810');
mywin.focus();
return false;
}
</cfsavecontent>
<cfset a = listgetat(foo,2,"url")>
<cfoutput>#a#</cfoutput>
but i am getting weird results, i need to fetch the URL value
This regex will return the URL and accounts for the whitespace being optional, however I should add the disclaimer that this is a little brittle and probably not a good way of going about whatever it is you're after for several reasons.
#reReplaceNoCase( foo, '.*url\s*=\s*"(.*?)".*', '\1' )#

Error while creating large dropdown in excel using ColdFusion

This code I have written for creating large dropdown in ColdFusion, but it is not working on my end. Could any one please help me rectify my problem. The new code is
<cfquery name="getPOP" datasource="l_webalc">
select distinct center_code from alc_pop
</cfquery>
<cfset countryName= ArrayNew(1)>
<cfloop query="getPOP">
<cfset arrayappend(countryName, getPOP.center_code)>
</cfloop>
<script>
workbook = new HSSFWorkbook();
realSheet = workbook.createSheet("Sheet xls");
hidden = workbook.createSheet("hidden");
for (int i = 0, length= countryName.length; i < length; i++) {
String name = countryName[i];
HSSFRow row = hidden.createRow(i);
HSSFCell cell = row.createCell(0);
cell.setCellValue(name);
}
namedCell = workbook.createName();
namedCell.setNameName("hidden");
namedCell.setRefersToFormula("hidden!A1:A" + countryName.length);
constraint = DVConstraint.createFormulaListConstraint("hidden");
addressList = new CellRangeAddressList(0, 0, 0, 0);
validation = new HSSFDataValidation(addressList, constraint);
workbook.setSheetHidden(1, true);
realSheet.addValidationData(validation);
stream = new FileOutputStream("c:\\range.xls");
workbook.write(stream);
stream.close();
</script>
Update 1:
(From other thread) I am getting this error message:
function keyword is missing in FUNCTION declaration. The CFML compiler
was processing: A script statement beginning with HSSFWorkbook on line
32, column 1. A script statement beginning with function on line 31,
column 9. A cfscript tag beginning on line 30, column 2.
Update 2:
Again I have modified this code and now the new error is
"The value hidden not A1:A cannot be converted to a number."
I edited the objects as mentioned in the comments and also changed the script to cfscript. Please help me to rectify this error.
<cfscript>
workbook = createObject("java", "org.apache.poi.hssf.usermodel.HSSFWorkbook");
realSheet = workbook.createSheet("Sheet xls");
hidden = workbook.createSheet("hidden");
for (i = 1; i <= arrayLen(countryName); i++){
name = countryName[i];
row = hidden.createRow(i);
cell = row.createCell(0);
cell.setCellValue(name);
}
namedCell = workbook.createName();
namedCell.setNameName("hidden");
namedCell.setRefersToFormula("hidden!A1:A"+arrayLen(countryName));
dv = createObject("java", "org.apache.poi.hssf.usermodel.DVConstraint");
constraint = dv.createFormulaListConstraint("hidden");
addressList = cellRangeList.init(0, 0, 0, 0);
validation = dataValidation.init(addressList, constraint);
workbook.setSheetHidden(1, true);
realSheet.addValidationData(validation);
stream = new FileOutputStream("c:\\range.xls");
workbook.write(stream);
stream.close();
</cfscript>
Update 3:
I have updated the code to fix the mentioned issues and and am now getting this error
"The setSheetHidden method was not found ..."
on the following line:
workbook.setSheetHidden(1, true);
There are several problems with your code. Though java syntax is similar, you cannot just copy and paste a java example and expect it to run in cfscript. You need to make some adjustments first. (Note: I am assuming script was just a typo for cfscript).
In java, you can instantiate an object using the keyword "new" ie new SomeClassName(). In CF, the new keyword can only be used with cfc's. To create a java object, you must use createObject instead. To instantiate it, call the init(...) method. It is a special method in CF that invoke's the constructor of a java class with whatever parameters you supply, ie
createObject("java", "path.to.SomeClassName").init();
To use static methods such as DVConstraint.createFormulaListConstraint(), you must also use createObject. While the java code does not create an new instance of that class, you must still use createObject to get a reference to the DVConstraint class, in CF, before you can invoke any of its methods. Note: Because it is static, no need to call init() first. ie
dv = createObject("java", "org.apache.poi.hssf.usermodel.DVConstraint");
dv.createFormulaListConstraint(...);
Java classes are organized into packages. In java classes, the full path to any referenced classes are imported at the top of the java code (not visible in the example you are using). In CF you need to use the full path in your createObject call. (Important: Paths are cAsE sEnsItIvE).
For example, instead of new HSSFWorkbook() use:
createObject("java", "org.apache.poi.hssf.usermodel.HSSFWorkbook");
If you are not sure of the path, just do a search on "POI TheClassName". Odds are the first result will be the POI JavaDocs, which show the full path at the top of each page like this:
java.lang.Object
|---org.apache.poi.ss.util.CellRangeAddressList
Unlike CF, java is strongly typed, which means you must declare a variable's type as well as it's name. For example, this line declares a variable row as type HSSFRow
HSSFRow row = hidden.createRow(i);
Since CF is typeless, it does not require a type. So running that same code in cfscript will cause the cryptic error "function keyword is missing...". The solution is to drop the variable type and just do a straight variable assignment:
row = hidden.createRow(i);
Java array indexes start at zero (0), while CF starts at one (1), so you need to fix the indexes in your for loop:
for (i = 1; i <= arrayLen(countryName); i++)
Java uses + to concatenate strings, whereas CF uses &. So you need to change the operator here "hidden!A1:A" + countryName.length. Otherwise CF will think you are trying to add two numbers, which will obviously throw an error because the first part is a string.
Assuming no version conflicts, the java example should work after you make those those adjustments.
"The setSheetHidden method was not found ..."
Just use Javacast function for boolean arguments:
workbook.setSheetHidden(1, javacast("boolean",true));

coldfusion Rereplace function

I am using the following ColdFusion REreplace function
<cfset variables.sAddress = #left(trim(REReplace(arguments.address, "[^-0-9a-zA-Z_/-/']", "", "ALL")),50)#/> where arguments.address is coming from the form.
I am using the rereplace function to remove the special characters, which is working fine on other pages, but in this case before using the rereplace function it is first converting the special characters into their html equivalent and then applying the rereplace function.
so if i pass 'test&test' as the arguments.address1, the result i am getting is 'testamptest'.
You may need a function to convert HTML encoded text into un-encoded (change HTML entities back to what they are).
Here's the one I'm usually using:
// function to reverse HTMLEditFormat
function HtmlUnEditFormat( str )
{
var lEntities = "&##xE7;,&##xF4;,&##xE2;,Î,Ç,È,Ó,Ê,&OElig,Â,«,»,À,É,≤,ý,χ,∑,′,ÿ,∼,β,⌈,ñ,ß,„,´,·,–,ς,®,†,⊕,õ,η,⌉,ó,­,>,φ,∠,‏,α,∩,↓,υ,ℑ,³,ρ,é,¹,<,¢,¸,π,⊃,÷,ƒ,¿,ê, ,∅,∀, ,γ,¡,ø,¬,à,ð,ℵ,º,ψ,⊗,δ,ö,°,≅,ª,‹,♣,â,ò,ï,♦,æ,∧,◊,è,¾,&,⊄,ν,“,∈,ç,ˆ,©,á,§,—,ë,κ,∉,⌊,≥,ì,↔,∗,ô,∞,¦,∫,¯,½,¤,≈,λ,⁄,‘,…,œ,£,♥,−,ã,ε,∇,∃,ä,μ,¼, ,≡,•,←,«,‾,∨,€,µ,≠,∪,å,ι,í,⊥,¶,→,»,û,ο,‚,ϑ,∋,∂,”,℘,‰,²,σ,⋅,š,¥,ξ,±,ℜ,þ,〉,ù,√,‍,∴,↑,×, ,θ,⌋,⊂,⊇,ü,’,ζ,™,î,ϖ,‌,〈,˜,ú,¨,∝,ϒ,ω,↵,τ,⊆,›,∏,",‎,♠";
var lEntitiesChars = "ç,ô,â,Î,Ç,È,Ó,Ê,Œ,Â,«,»,À,É,?,ý,?,?,?,Ÿ,?,?,?,ñ,ß,„,´,·,–,?,®,‡,?,õ,?,?,ó,­,>,?,?,?,?,?,?,?,?,³,?,é,¹,<,¢,¸,?,?,÷,ƒ,¿,ê,?,?,?,?,?,¡,ø,¬,à,ð,?,º,?,?,?,ö,°,?,ª,‹,?,â,ò,ï,?,æ,?,?,è,¾,&,?,?,“,?,ç,ˆ,©,á,§,—,ë,?,?,?,?,ì,?,?,ô,?,¦,?,¯,½,¤,?,?,?,‘,…,œ,£,?,?,ã,?,?,?,ä,?,¼, ,?,•,?,«,?,?,€,µ,?,?,å,?,í,?,¶,?,»,û,?,‚,?,?,?,”,?,‰,²,?,?,š,¥,?,±,?,þ,?,ù,?,?,?,?,×,?,?,?,?,?,ü,’,?,™,î,?,?,?,˜,ú,¨,?,?,?,?,?,?,›,?,"",?,?";
return ReplaceList(arguments.str, lEntities, lEntitiesChars);
}