import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * @author Rizwan Sharif * */ public class CsvParser { /** * Original Regex without escaped qoutes: ("(([^"]|"")+)"|([^,]+)|,)(?=(,|$)) * Regex explained : * First Part : "(([^"]|"")+)" if value starts with " then it can either be followed by any character not " ([^"] or by "" , one or more times * Second Part : ([^,]+) for simple values : any character except , * Third Part : , can be only comma * Forth Part : all above three parts should be followed either by comma or by Endline. */ public static final String CSV_PATTERN = "(\"(([^\"]|\"\")+)\"|([^,]+)|,)(?=(,|$))"; private static Pattern csvRE; public CsvParser() { csvRE = Pattern.compile(CSV_PATTERN); } public List<String> parseLine(String line) { List<String> list = new ArrayList<String>(); Matcher m = csvRE.matcher(line); // For each field while (m.find()) { String match = m.group(); if (match == null) break; if (match.endsWith(",")) { // trim trailing , match = match.substring(0, match.length() - 1); } if (match.startsWith("\"")) { // assume also ends with match = match.substring(1, match.length() - 1); } if (match.length() == 0) match = null; list.add(match); } return list; } }
Thursday, September 1, 2011
My Csv Parser For Java
Saturday, June 11, 2011
Javascript: How Double And "&&" Works
Recently this question baffled us all.Coming from other languages we would expect that following line
assigns true or false to result variable (true in above case) but if you do "alert(result)" in next line , you see "Rizwan" in the alertbox instead of true.Confused? Read till end and it will be clear.
Javascript and Falsy Values
In javascript following are considered false
Everything else evaluates to true.
Keeping this in mind , we go back to our problem
In javascript && operator evaluates its first operand and if its true, it returns whatever is the second operand ("Rizwan" in the above case) .
And if first operand is falsy value , && operarot just returns that first operand.
Similary || operator returns its first operand if its true , otherwise it returns it second operator if first operator is a falsy value.
var result = true && "Rizwan" ;
assigns true or false to result variable (true in above case) but if you do "alert(result)" in next line , you see "Rizwan" in the alertbox instead of true.Confused? Read till end and it will be clear.
Javascript and Falsy Values
In javascript following are considered false
- false
- 0
- null
- undefined
- "" ,empty String
- NaN (Not a number , like 0/0)
Everything else evaluates to true.
Keeping this in mind , we go back to our problem
var result = true && "Rizwan";
In javascript && operator evaluates its first operand and if its true, it returns whatever is the second operand ("Rizwan" in the above case) .
And if first operand is falsy value , && operarot just returns that first operand.
var result = null && "Rizwan"; // result will contain null
Similary || operator returns its first operand if its true , otherwise it returns it second operator if first operator is a falsy value.
Friday, March 25, 2011
Hotlinking
Hotlinking:
Let's say i own example.com and i have webpage test.html accessible through www.example.com/test.html and test.html is codes as
<body>
<img src="rizwan.com/image1.jpg"/>
</body>
Now when you load example.com/test.html , image1 will be loaded from rizwan.com and not from example.com. So i have hotlinked image1 from my webpage to rizwan.com
Problem:
Since bandwidth is charged, and i have asked your browser to show that 500KB (lets assume) image1 from answer.com , answer.com is going to pay for that bandwidth , not example.com. Hence bandwidth theft.
Solution:
Http Referrer can be used to identify if image1 is being linked from any other site except answer.com. If yes , then we can simply reject the request or do whatever you like.
How can we check the referrer? Well Conditional Rewrite rules can help.
Well , first condition checks if referrer is anything except our website, if yes then next rewrite rule is excuted and alternate image is sent instead of image1.jpg, You can as well return 404 , a low bandwidth image etc
Let's say i own example.com and i have webpage test.html accessible through www.example.com/test.html and test.html is codes as
<body>
<img src="rizwan.com/image1.jpg"/>
</body>
Now when you load example.com/test.html , image1 will be loaded from rizwan.com and not from example.com. So i have hotlinked image1 from my webpage to rizwan.com
Problem:
Since bandwidth is charged, and i have asked your browser to show that 500KB (lets assume) image1 from answer.com , answer.com is going to pay for that bandwidth , not example.com. Hence bandwidth theft.
Solution:
Http Referrer can be used to identify if image1 is being linked from any other site except answer.com. If yes , then we can simply reject the request or do whatever you like.
How can we check the referrer? Well Conditional Rewrite rules can help.
RewriteCond %{HTTP_REFERRE] !^http://(www\.)?answer.com/.*$
RewriteRule \.*/image1.jpg$ /image/sucker.jpg [L]
Well , first condition checks if referrer is anything except our website, if yes then next rewrite rule is excuted and alternate image is sent instead of image1.jpg, You can as well return 404 , a low bandwidth image etc
Subscribe to:
Posts (Atom)