Java URI to extractURIFromText(String texto)

Here you can find the source of extractURIFromText(String texto)

Description

extract URI From Text

License

Open Source License

Declaration

public static String extractURIFromText(String texto) 

Method Source Code

//package com.java2s;
/*//from   ww w.j  a v  a2  s .c om
public static boolean save(Model modelo, String filename)
{
try {
BufferedWriter out = new BufferedWriter(new FileWriter(filename));
//            RDFDataMgr.write(System.out, modelo, Lang.TURTLE) ;
RDFDataMgr.write(out, modelo, Lang.TURTLE) ;
out.close();
} catch (IOException ex) {
Logger.getLogger("licenser").warn("Error saving file. " + ex.getMessage());
return false;
}
return true;
}*/

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static String extractURIFromText(String texto) {
        try {
            URI uri = new URI(texto);
            String suri = uri.toString();
            System.out.println(suri);
            return suri;
        } catch (Exception e) {
            List<String> uris = extractUrls(texto);
            for (String uri : uris) {
                System.out.println("Candidato: " + uri);
                return uri;
            }
        }
        return "";
    }

    public static List<String> extractUrls(String text) {
        List<String> containedUrls = new ArrayList<String>();
        String urlRegex = "((https?|ftp|gopher|telnet|file):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";
        Pattern pattern = Pattern.compile(urlRegex, Pattern.CASE_INSENSITIVE);
        Matcher urlMatcher = pattern.matcher(text);
        while (urlMatcher.find()) {
            containedUrls.add(text.substring(urlMatcher.start(0), urlMatcher.end(0)));
        }

        return containedUrls;
    }
}

Related

  1. extractLocalName(String uri)
  2. extractOntologyNameUriFromRDF(String rdfString, boolean generateURI)
  3. extractParamsFromURI(String uri)
  4. extractS3Key(URI uri)
  5. extractTargetSystemFromUri(URI uri)
  6. toDecodedString(URI uri)
  7. toExistingFile(URI uri)
  8. toExternalForm(URI u)
  9. toFile(java.net.URI uri)