Sunday, February 10, 2008

Url Encode function. Encode a string according to W3C standards

this is a function that turns all the chars in a string to the W3C Standards. 
/**
* Encode a string according to W3C standards.
*
*/

public static String urlEncoder(String s) {
if (s == null)
return s;
StringBuffer sb = new StringBuffer(s.length() * 3);
try {
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (c == '&') {
sb.append("&amp;");
} else if (c == ' ') {
sb.append('+');
} else if (
(c >= ',' && c <= ';')
(c >= 'A' && c <= 'Z')
(c >= 'a' && c <= 'z')
c == '_'
c == '?') {
sb.append(c);
} else {
sb.append('%');
if (c > 15) { // is it a non-control char, ie. >x0F so 2 chars
sb.append(Integer.toHexString((int)c)); // just add % and the string
} else {
sb.append("0" + Integer.toHexString((int)c));
// otherwise need to add a leading 0
}
}
}

} catch (Exception ex) {
return (null);
}
return (sb.toString());
}
 
source of this function : http://forum.java.sun.com/thread.jspa?threadID=594204&messageID=3596899

Monday, January 28, 2008

Replace relative links to absolute base links in HTML - Java

this is a function that can add a base url to all urls in an html page, this code was translated from this vb function displayed here :http://www.motobit.com/tips/detpg_replace-relative-links/

special thanks to the original coder...

the function (java) was tested and run successfully!

public static String ReplaceLinksToBase(String HTML, String baseURL)
// replaces all links in an html to base url
// By Ramik, 28/1/2008
{
HTML = ReplaceTagToBase(HTML, "a", "href", baseURL);
// replace <img src=...> links
HTML = ReplaceTagToBase(HTML, "img", "src", baseURL);
HTML = ReplaceTagToBase(HTML, "link", "href", baseURL);
return HTML;
}


public static String ReplaceTagToBase (String HTML, String TagName, String ValueName, String BaseURL)
// replace a specific tag url to base Url
//By Ramik, 28/01/2008
{
int pos1, pos2;
String p1, p2, html;
html= HTML;
//Find the tag In HTML.
String temp = "<"+TagName ;
pos1 = HTML.indexOf(temp);
while (pos1>0)
{
//The tag was found. Find closing > of the tag.
pos2 = HTML.indexOf(">",pos1+TagName.length());
if (pos2>-1)
{
//separate tag text from HTML
String tag, tag1;
tag = HTML.substring(pos1, pos2);
//rewrite realative URL links In the one tag.
tag1 = ReplaceParamToBase(tag, ValueName, BaseURL);
//The tag was changed - relative links found.
if (!tag1.equals(tag)) //there is some change In the tag.
{
//get parts before And after the tag
p1 = HTML.substring(0,pos1);
p2 = HTML.substring(pos2,HTML.length());
//Compute new POs2 position
pos2 = pos2 + tag1.length() - tag.length() ;
//replace old tag with relative links with the new version
html = p1 + tag1 + p2 ;
}
}
if (pos2>0)
pos1 = HTML.indexOf("<"+TagName,pos2+1);
else
pos1 = 0 ;
}
return html;
}

public static String ReplaceParamToBase( String Tag, String ValueName,String BaseURL)
{
String p1, p2 , c1, URL, tag=Tag;
int pos1, pos2, lenVal ;
lenVal = ValueName.length() ;
//find position of the tag value
pos1 = Tag.indexOf(ValueName+"=");
if (pos1>-1)
{
//get a first char after =
c1 = Tag.substring(pos1+lenVal+1,pos1+lenVal+2);
if (c1.equals("\""))
{
pos1 = pos1+lenVal+1 ;
pos2 = Tag.indexOf("\"",pos1+1);
}
else if (c1.equals("'"))
{
pos1 = pos1+lenVal+1;
pos2 = Tag.indexOf("'",pos1+1);
}
else//the value ends with Space
{
pos1 = pos1+lenVal;
pos2 = Tag.indexOf(" ",pos1);
if (pos2==-1)
pos2 = Tag.length();
}
p1 = Tag.substring(0,pos1+1);
p2 = Tag.substring(pos2,Tag.length());
URL = Tag.substring(pos1+1,pos2);
//is the value relative URL?
if (URL.indexOf("://")==-1 && URL.indexOf("mailto:")==-1)
{
//make the new absolute URL
if (BaseURL.charAt(BaseURL.length()-1)!='/' && BaseURL.charAt(BaseURL.length()-1)!='\\' )
{
BaseURL=BaseURL+'/';
}
URL = BaseURL+URL ;
tag = p1+URL+p2;
}

}
return tag;
}


public static String replaceAllTags (String HTML, String TagName, String ValueName, String BaseURL)
//Replace all tags in html to base url
// by ramik 30/1/2008
{
String html = HTML , allHtml="";
int pos1=0, pos2 = HTML.indexOf(">");
if (pos2>-1)
pos2++;
while (pos2>-1)
{
html = HTML.substring(pos1,pos2);
allHtml = allHtml + ReplaceTagToBase(html,TagName, ValueName, BaseURL);
pos1=pos2;
pos2=HTML.indexOf(">",pos2);
if (pos2>-1)
pos2++;
}
allHtml = allHtml + HTML.substring(pos1);
return allHtml;
}

Discuss this Post

Saturday, September 15, 2007

windows vista: access: local only

Ok, this is my first post of non programming issue but it still usefull for computers users.
Last week i got a new HP Laptop, with windows vista insite, it's cool looking, i was so happy with it untill i had the first problem, i couldn't surf the net.
well, i will not talk alot, because i guess some of you is reading here after a few days of searching.
well, what i had is like that:
i could connect to the network and see other computers but could not surf the net
that what you should do:
go to the network connection icon on the tray - > right click ->network and Sharing Center -> on the left press of the Manage network connection - > right click on the network that you want to log in to -> properties -> now you have some check boxes :
uncheck the Internet Protocol Version 6
stand on the Internet Protocol Version 4 and press properties :
check the Use the following IP Address :
and on the fields fill the following:
IP Address : [put an IP the is fine with your router]
Subnet mask : 255.255.255.0
Default gateway: the ip of your router

then check : use the following DNS server addresses
Prefered DNS server: the ip of your router.

press OK -> OK and restart your computer.

for me it worked..
thanks to the guys here: http://www.daniweb.com/forums/thread73818.html

Wednesday, July 04, 2007

sql query for getting your users email servers

this is a query that get you a list of the servers of the users emails that they registered with, for example if you have 100 people that has a hotmail account and 200 users with gmail you will get this table:
count server
-----------------
100 hotmail
200 gmail
...

here is the query:

select * from
(SELECT Count(users.UserID) AS Count, right(users.Useremail,len(users.Useremail)-instr(users.Useremail,'@')) as server
FROM users
where users.useremail <> ''
GROUP BY right(users.Useremail,len(users.Useremail)-instr(users.Useremail,'@')))
order by count

Monday, July 02, 2007

Selection.Find crash

you wrote an application that works with Word object?
the application crashes in the middle of the run, after the debug you find that it crashes on this line:
WordObj.Selection.Find

well, it's not your fault, it's a microsoft bug. now, how you resolve it??
it's a tricky way,
what you should do is late binding
u should dim your word object as an General object and create it manualy, not by "new word"
here is a usefull code:
...


Dim wrdDoc As Object ' setting the word object as a general object
Dim IsWordRunning As Boolean

IsWordRunning = ApplicationIsRunning("Word.Application")
If IsWordRunning = True Then ' do
Set wrdApp = GetObject(, "Word.Application")
Else
Set wrdApp = CreateObject("Word.Application")
End If
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open(filename)


...


and here is the applicationisrunning function:

Function ApplicationIsRunning(ApplicationClassName As String) As Boolean
' returns True if the application is running
' example: If Not ApplicationIsRunning("Outlook.Application") Then Exit Sub
Dim AnyApp As Object
On Error Resume Next
Set AnyApp = GetObject(, ApplicationClassName)
ApplicationIsRunning = Not AnyApp Is Nothing
Set AnyApp = Nothing
On Error GoTo 0
End Function

Thursday, June 28, 2007

adding adsense to phpbb 3 forum

do you want to add adsense advertising to your forum.
i could add adsense to the top of my forum by adding the code in the overall_header.htm file, i added it at the end of the file.
the important thing is that when i added it to the file manulay and then uploaded the file it didn't work, i succeded only when i changed the file through the admin system.
same i did with the google analytics code, i added it to the same file, but after the header tag.
if someone could do that after the first post only please write it here.

Monday, June 25, 2007

Beta Version for arabic language Google Adsense

Lately the google adsense for content started to support arabic language websites, the www.arabsites.co.il website administrator said that his site is utf-8 incoded and two weeks ago he started to show ads in his site, more he said, the pay per click still low, it's about 1-2 centes per click, but i guess when the time pass, more advertisers will know about the service and they will pay beter for a click.
the first report was on the arabic seo forum www.seoarabia.com