URL is getting displayed as String in table - java

I wanted to display one URL in a table.
Value I had was : www.yahoo.com
I converted into format as below and returning back to table :
www.yahoo.com</font>"> Link
I want it to be displayed in URL form. But it is displaying as a string like as it is above.
Below is the method used for conversion :
private static String validateString(String header) throws Exception
{
System.out.println("&&&&&&&&&& inside validateString ");
String displayString = "";
String colorValue = "";
try
{
if((header) != null && !"null".equalsIgnoreCase(header))
{
displayString = header;
colorValue = "red";
displayString = "<font color=\""+colorValue+"\">"+displayString+"</font>";
}
}
catch(Exception e)
{
displayString = " ";
}
if(!"".equals(displayString))
{
displayString = " Link ";
}
if ("".equals(displayString))
{
displayString = " ";
}
return displayString;
}
Please let me know how to display as URL.
Thanks.

In the attribute href for the tag A, you only should use the proper URL not ant other tag. Change it to something like this:
Link

Related

Xpages - Passing <ahref> in Arraylist

I'm trying to add a href to Arraylist and this adds nicely to the Arraylist, but the link is broken. Everything after the question mark (?) in the URL is not included in the link.
Is there anything that I'm missing, code below:
private String processUpdate(Database dbCurrent) throws NotesException {
int intCountSuccessful = 0;
View vwLookup = dbCurrent.getView("DocsDistribution");
ArrayList<String> listArray = new ArrayList<String>();
Document doc = vwLookup.getFirstDocument();
while (doc != null) {
String paperDistro = doc.getItemValueString("DistroRecords");
if (paperDistro.equals("")) {
String ref = doc.getItemValueString("ref");
String unid = doc.getUniversalID();
// the link generated when adding to Arraylist is broken
listArray.add("" + ref + "");
}
Document tmppmDoc = vwLookup.getNextDocument(doc);
doc.recycle();
doc = tmppmDoc;
}
Collections.sort(listArray);
String listString = "";
for (String s : listArray) {
listString += s + ", \t";
}
return listString;
}
You have a problem with " escaping around unid value due to which you URL becomes gandhi.w3schools.com/testbox.nsf/distro.xsp?documentId="+ unid + "&action=openDocument.
It would be easier to read if you use String.format() and single quotes to generate the a tag:
listArray.add(String.format(
"<a href='gandhi.w3schools.com/testbox.nsf/distro.xsp?documentId=%s&action=openDocument'>%s</a>",
unid, ref));

If string contains with get getContents (JXL)

UPDATE: POSSIBLE SOLUTION POSTED BELOW BY MYSELF
I need my program to read a cell. If it contains a specific string, lets say the name "Jacob", then it needs to execute a certain static function defined later in the code.
My IF statement will be nested in a while loop.
int currentRow = 1;
Cell cell;
Cell ncell;
while (!(cell = sheet.getCell(URL_COLUMN, currentRow)).getType().equals(CellType.EMPTY)) {
String url = cell.getContents();
System.out.println("Checking URL: " + url);
ncell = sheet.getCell(NAME_COLUMN, currentRow);
ncell.getContents();
ncell.toString();
if (ncell.contains("Jacob")) {
String FAV = JacobFavFood(url); // A static function will be defined later for this
System.out.println("Jacob's favorite food is " + name);
Label cellWithFAV = new Label(FAV_COLUMN, currentRow, FAV);
sheet.addCell(cellWithFAV);
}
currentRow++;
}
workbook.write();
workbook.close();
UPDATE: POSSIBLE SOLUTION POSTED BELOW BY MYSELF
int currentRow = 1;
Cell cell;
Cell ncell;
while (!(cell = sheet.getCell(URL_COLUMN, currentRow)).getType().equals(CellType.EMPTY)) {
String url = cell.getContents();
String NAME = ncell.getcontents();
System.out.println("Checking URL: " + url);
ncell = sheet.getCell(NAME_COLUMN, currentRow);
if (NAME.contains("Jacob")) {
String FAV = JacobFavFood(url); // A static function will be defined later for this
System.out.println("Jacob's favorite food is " + name);
Label cellWithFAV = new Label(FAV_COLUMN, currentRow, FAV);
sheet.addCell(cellWithFAV);
}
currentRow++;
}
workbook.write();
workbook.close();
I forgot I can define the string as was done with the url. This does work, and it is able to call on a static function. I am still wondering why .toString() didn't work, though.

android java code error

I am creating a keyboard but there is some error in local variable usage.
private void updateCandidateText(){
try{
ExtractedText r= getCurrentInputConnection().getExtractedText(new ExtractedTextRequest(),InputConnection.GET_EXTRACTED_TEXT_MONITOR);
String strbeforeCursor="";
String strafterCursor ="";
strbeforeCursor = getCurrentInputConnection().getTextBeforeCursor(1000000000, 0).toString();
strafterCursor = getCurrentInputConnection().getTextAfterCursor(1000000000, 0).toString();
String str = strbeforeCursor + "|"+strafterCursor;
if(mTamilPreviewView != null)
mTamilPreviewView.update(str, strbeforeCursor.length());
mTamilPreviewView.update(r.text.toString() , 0);
}
catch (Exception e) {
Log.e("t", "errr", e);
}
}
You test if mTamilPreviewView != null to call
mTamilPreviewView.update(str, strbeforeCursor.length());
but even if it's null, you'll do
mTamilPreviewView.update(r.text.toString() , 0);
and you'll get a NullPointerException. Is it really what you want to do? Don't you mean
if (mTamilPreviewView != null) {
mTamilPreviewView.update(str, strbeforeCursor.length());
mTamilPreviewView.update(r.text.toString() , 0);
}
Moreover, you initialize strbeforeCursor and strafterCursor with an empty string, and you give them other values at the next lines. You could simply remove
String strbeforeCursor="";
String strafterCursor ="";
and do
String strbeforeCursor = getCurrentInputConnection().getTextBeforeCursor(1000000000, 0).toString();
String strafterCursor = getCurrentInputConnection().getTextAfterCursor(1000000000, 0).toString();

Java regex pattern to remove a parameter from query string

I am looking for removing foo parameter and its value from all the possible following query strings in Java.
Is there a regex pattern to do this?
http://localhost/test?foo=abc&foobar=def
http://localhost/test?foobar=def&foo=abc
http://localhost/test?foo=abc
http://localhost/test?foobar=def&foo=abc&foobar2=def
The resulting strings would be
http://localhost/test?foobar=def
http://localhost/test?foobar=def
http://localhost/test
http://localhost/test?foobar=def&foobar2=def
This regex should match the GET param and its value...
(?<=[?&;])foo=.*?($|[&;])
RegExr.
Just replace it with an empty string.
For reference, there is a better (Perl) regex available in this other question: Regular expression to remove one parameter from query string
In Java, this can be implemented as follows:
public static String removeParams(String queryString, String... params) {
for (String param : params) {
String keyValue = param + "=[^&]*?";
queryString = queryString.replaceAll("(&" + keyValue + "(?=(&|$))|^" + keyValue + "(&|$))", "");
}
return queryString;
}
This is an extension of the answer provided by mchr.
It allows params to be removed from an url and from a query string, and shows how to execute the javascript test cases he mentions. Since it uses a regex, it will return the url with all the other parameters exactly where they were before. This is useful if you want to remove some parameters from an url when "signing" an url ;-) Note that this does not remove any completely empty parameters.
e.g. it will not remove foo from /test?foo&me=52
The test cases listed below remove the parameters, "foo" and "test" when found in the query string.
You can test it out line here at Repl.it
class Main {
public static void main(String[] args) {
runTests();
}
public static void runTests() {
test("foo=%2F{}/me/you&me=52", "me=52");
test("?foo=%2F{}/me/you&me=52", "?me=52");
test("?foo=52&me=able was i ere&test=2", "?me=able was i ere");
test("foo=", "");
test("?", "");
test("?foo=52", "");
test("test?", "test");
test("test?foo=23", "test");
test("foo=&bar=456", "bar=456");
test("bar=456&foo=", "bar=456");
test("abc=789&foo=&bar=456", "abc=789&bar=456");
test("foo=123", "");
test("foo=123&bar=456", "bar=456");
test("bar=456&foo=123", "bar=456");
test("abc=789&foo=123&bar=456", "abc=789&bar=456");
test("xfoo", "xfoo");
test("xfoo&bar=456", "xfoo&bar=456");
test("bar=456&xfoo", "bar=456&xfoo");
test("abc=789&xfoo&bar=456", "abc=789&xfoo&bar=456");
test("xfoo=", "xfoo=");
test("xfoo=&bar=456", "xfoo=&bar=456");
test("bar=456&xfoo=", "bar=456&xfoo=");
test("abc=789&xfoo=&bar=456", "abc=789&xfoo=&bar=456");
test("xfoo=123", "xfoo=123");
test("xfoo=123&bar=456", "xfoo=123&bar=456");
test("bar=456&xfoo=123", "bar=456&xfoo=123");
test("abc=789&xfoo=123&bar=456", "abc=789&xfoo=123&bar=456");
test("foox", "foox");
test("foox&bar=456", "foox&bar=456");
test("bar=456&foox", "bar=456&foox");
test("abc=789&foox&bar=456", "abc=789&foox&bar=456");
test("foox=", "foox=");
test("foox=&bar=456", "foox=&bar=456");
test("bar=456&foox=", "bar=456&foox=");
test("abc=789&foox=&bar=456", "abc=789&foox=&bar=456");
test("foox=123", "foox=123");
test("foox=123&bar=456", "foox=123&bar=456");
test("bar=456&foox=123", "bar=456&foox=123");
test("abc=789&foox=123&bar=456", "abc=789&foox=123&bar=456");
}
public static void test (String input, String expected) {
String result = removeParamsFromUrl(input, "foo", "test");
if (! result.equals(expected))
throw new RuntimeException("Failed:" + input);
System.out.println("Passed:" + input + ", output:" + result);
}
public static String removeParamsFromQueryString(String queryString, String... params) {
for (String param : params) {
String keyValue = param + "=[^&]*?";
queryString = queryString.replaceAll("(&" + keyValue + "(?=(&|$))|^" + keyValue + "(&|$))", "");
}
return queryString;
}
public static String removeParamsFromUrl(String url, String... params) {
String queryString;
String baseUrl;
int index = url.indexOf("?");
boolean wasFullUrl = (index != -1);
if (wasFullUrl)
{
baseUrl = url.substring(0, index);
queryString = url.substring(index+1);
}
else
{
baseUrl = "";
queryString = url;
}
String newQueryString = removeParamsFromQueryString(queryString, params);
String result;
if (wasFullUrl)
{
boolean isEmpty = newQueryString == null || newQueryString.equals("");
result = isEmpty ? baseUrl : baseUrl + "?" + newQueryString;
}
else
{
result = newQueryString;
}
return result;
}
}
url=url.replaceAll("(&"+param+"=[^&]*\$)|(\\?"+param+"=[^&]*\$)|("+param+"=[^&]*&)","")

Use JDT to get full method name

I am new to eclipse plugin development and I am trying to convert a IMethod to a string representation of the full method name. I.E.
my.full.package.ClassName.methodName(int param, String string)
so far I have had to hand roll my own solution. Is there a better way?
private static String getMethodFullName(IMethod iMethod)
{
String packageString = "[Default Package]";
try {
IPackageDeclaration[] declarations = iMethod.getCompilationUnit().getPackageDeclarations();
if(declarations.length > 0)
{
packageString = declarations[0].getElementName();
}
} catch (JavaModelException e) {
}
String classString = iMethod.getCompilationUnit().getElementName();
classString = classString.replaceAll(".java", "");
String methodString = iMethod.getElementName() + "(";
for (String type : iMethod.getParameterTypes()) {
methodString += type + ",";
}
methodString += ")";
return packageString + "." + classString + "." + methodString;
}
You can get the Fully qualified name for the type using
method.getDeclaringType().getFullyQualifiedName();
This is probably easier than accessing the package from the compilation unit. The rest of you function looks correct.
One small point: you should use StringBuilder to build up the string instead of adding to a standard String. Strings are immutable so addition creates loads of unrecesary temparary objects.
private static String getMethodFullName(IMethod iMethod)
{
StringBuilder name = new StringBuilder();
name.append(iMethod.getDeclaringType().getFullyQualifiedName());
name.append(".");
name.append(iMethod.getElementName());
name.append("(");
String comma = "";
for (String type : iMethod.getParameterTypes()) {
name.append(comma);
comma = ", ";
name.append(type);
}
name.append(")");
return name.toString();
}
Thanks to iain and some more research I have come up with this solution. It seems like something like this should be built into the JDT....
import org.eclipse.jdt.core.Signature;
private static String getMethodFullName(IMethod iMethod)
{
StringBuilder name = new StringBuilder();
name.append(iMethod.getDeclaringType().getFullyQualifiedName());
name.append(".");
name.append(iMethod.getElementName());
name.append("(");
String comma = "";
String[] parameterTypes = iMethod.getParameterTypes();
try {
String[] parameterNames = iMethod.getParameterNames();
for (int i=0; i<iMethod.getParameterTypes().length; ++i) {
name.append(comma);
name.append(Signature.toString(parameterTypes[i]));
name.append(" ");
name.append(parameterNames[i]);
comma = ", ";
}
} catch (JavaModelException e) {
}
name.append(")");
return name.toString();
}
I am not sure it would take into account all cases (method within an internal class, an anonymous class, with generic parameters...)
When it comes to methods signatures, the classes to look into are:
org.eclipse.jdt.internal.corext.codemanipulation.AddUnimplementedMethodsOperation
org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2
You need to get the jdt.core.dom.IMethodBinding, from which you can extract all what you need.
If you have a MethodInvocation, you can:
//MethodInvocation node
ITypeBinding type = node.getExpression().resolveTypeBinding();
IMethodBinding method=node.resolveMethodBinding();

Categories