anyone know how can i convert this java code to co...
# java-and-jvm
s
anyone know how can i convert this java code to coldfusion
Copy code
private static Data getdata(String url) {

        if (url != null) {

            String path = null;
            try {
                path = new URL(url).getPath().substring(1);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }

            if (path != null) {

                if (path.indexOf('/') == -1) {
                    return getData(url, "User");
                } else {
                    if (path.contains("music")) {
                        return getData(url, "Music");
                    } else if (path.contains("video")) {
                        return getData(url, "Video");
                    } else if (path.contains("tag")) {
                        return getData(url, "Tag");
                    }
                }
            }
        }

        return null;
    }
a
Which part of it are you struggling with? What have you got so far?
s
this one specific
Copy code
try {
                path = new URL(url).getPath().substring(1);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
string i can change to var
path.indexof('/')
for contains i can use path contains
specific this line
Copy code
path = new URL(url).getPath().substring(1)
and path.indexof('/')
a
OK, so same question, more focused: which part of that
try
/
catch
block are you struggling with? What have you got so far?
s
Copy code
path = new URL(url).getPath().substring(1)
i don't know what is the equivalent of aove in CF
a
What does it do?
s
Example : For URL -> http://localhost:8080/website/user so applying this function will return -> /website/user And then to remove 1st / i am using substring -> so final output will be website/user path.indexOf('/') == -1) => This function checks if character present in path variable. If not present this will return -1.
a
So you have a variable with
<http://localhost:8080/website/user>
in it. And you want to get
website/user
from it?
Or more to the point you have something like
[scheme]://[domain]:[port]/[the bit you want]
? I guess it won't always have the port?
But it always has "stuff before the third `/`" and you want "the stuff after the third `/`"?
f
If you want to actually use the Java’s URL class from CF, then that is possible to do: This provides a pretty good primer on integrating java / cfml: https://cfdocs.org/java take a look at Using a Constructor
a
@foundeo absolutely, but I think there's something to be said for solving the issue with CFML, not transliterating some code from another language. I find it's always good to go back to the human world problem: what am I actually trying to do, not what is this other code trying to do. But "it depends"
It's surprisingly difficult to tease this out of ppl sometime, though.
f
yeah, I generally agree and I almost wrote something to that same effect, but then I remembered that writing URL parsers is harder than it seems. In this case I might also rely on java’s URL implementation.
s
@foundeo how to use the java method in mycase which one/
f
• Take a look at the constructor example I provided… the tricky part is that in your java code they have
new URL()
but for someone who doesn’t know java, it’s probably not obvious what URL class that is… take a look at the import statements on the top of the file, you will probably find
import java.net.URL
that tells you the full name of the class, and you can use that in CFML’s
createObject()
function
so you have this in java:
new URL(url)
and you can write that in CFML as
createObject("java", "<http://java.net|java.net>.URL").init(url)
— if you can understand how I did that, then you can understand how to convert pretty much all java code to cfml
s
@foundeo Thanks In java i use this to connect to jSoup
Copy code
doc = Jsoup.connect(arguments.url).get();
now in CF i tried this but still not orking
Copy code
filePaths[1] = expandPath("../cfc/jar/jsoup-1.14.3.jar");
            
            loaderObj =createObject("component","jar.javaloader.JavaLoader").init(filePaths);
            Jsoup = loaderObj.create("org.jsoup.Jsoup");
anyone/
f
you’ll have to be more specific than not working - at the very least post the error message you are getting when you try to run the code. It’s also helpful if you post things you’ve tried, or verified - for example have you verified that the path to the jar file is correct (eg use fileExists on the path)?