Example usage for org.apache.commons.validator.routines UrlValidator UrlValidator

List of usage examples for org.apache.commons.validator.routines UrlValidator UrlValidator

Introduction

In this page you can find the example usage for org.apache.commons.validator.routines UrlValidator UrlValidator.

Prototype

public UrlValidator() 

Source Link

Document

Create a UrlValidator with default properties.

Usage

From source file:org.zaizi.manifoldcf.agents.transformation.stanbol.StanbolEnhancer.java

private String getURILocalName(String uri) {
    UrlValidator urlValidator = new UrlValidator();
    if (urlValidator.isValid(uri)) {
        URIImpl uriImpl = new URIImpl(uri);
        return uriImpl.getLocalName();
    } else {//w w w .j a v  a  2  s  .  c  o m
        return uri;
    }
}

From source file:org.zaizi.sensefy.api.utils.PrimaryDocumentUtils.java

/**
 * Return the namespace of a URI, that is everything before the last /
 * @param uri//www.jav  a 2  s  .  c o  m
 * @return
 * @throws java.net.MalformedURLException
 */
public static String getNamespace(String uri) throws MalformedURLException {
    UrlValidator urlValidator = new UrlValidator();
    if (uri.contains("/")) {
        int lastSlashPosition = uri.lastIndexOf("/");
        return uri.substring(0, lastSlashPosition + 1);
    } else {
        return new String(uri);
    }
}

From source file:pt.ua.bioinformatics.coeus.nanopub.NanopubParser.java

/**
 * Do the nanopublication generation/parsing
 *
 *///from  w  ww.ja  v  a 2s.  co m
public void parse() {

    for (String string : concept_childs) {
        System.err.println("\t[COEUS][NanopubParser] " + string);
    }
    Logger.getLogger(NanopubParser.class.getName()).log(Level.INFO, "[COEUS][NanopubParser] Start parsing: {0}",
            concept_root);

    Boot.start();

    String queryConcepts = "SELECT * {?item coeus:hasConcept " + concept_root + " FILTER NOT EXISTS {"
            + concept_root + " coeus:builtNp true }}";
    ResultSet rs = Boot.getAPI().selectRS(queryConcepts, false);
    if (!rs.hasNext() && Config.isDebug()) {
        Logger.getLogger(NanopubParser.class.getName()).log(Level.INFO,
                "[COEUS][NanopubParser] Already parsed the concept {0}", concept_root);
    }
    UrlValidator urlValidator = new UrlValidator();
    while (rs.hasNext()) {
        QuerySolution row = rs.next();

        String item = row.get("item").toString();
        //System.out.println(item);
        String queryItems = "SELECT * { <" + item + "> ?p ?o}";
        ResultSet rs_item = Boot.getAPI().selectRS(queryItems, false);

        //Build Assertion
        String np_item = item + "_Nanopub";
        Assertion a = new Assertion(np_item + "_Assertion");
        //Add Items to Assertion field
        while (rs_item.hasNext()) {
            QuerySolution row_item = rs_item.next();
            String p = row_item.get("p").toString();
            String o = row_item.get("o").toString();
            //System.out.println(item + " " + p + " " + o);

            if (urlValidator.isValid(o)) {
                a.add(new Triple(Node.createURI(item), Node.createURI(p), Node.createURI(o)));
            } else {
                a.add(new Triple(Node.createURI(item), Node.createURI(p), Node.createLiteral(o)));
            }
            if (p.endsWith("isAssociatedTo")) {
                a = addAssociations(a, o);
            }
        }

        //Build Provenance
        Provenance p = new Provenance(np_item + "_Provenance");
        p.add(new Triple(Node.createURI(a.getUri()), Node.createURI("http://www.w3.org/ns/prov#wasDerivedFrom"),
                Node.createURI("http://bioinformatics.ua.pt/coeus/")));
        for (JSONObject json : prov) {
            String predicate = (String) json.get("predicate");
            String object = (String) json.get("object");
            if (urlValidator.isValid(object)) {
                p.add(new Triple(Node.createURI(a.getUri()), Node.createURI(PrefixFactory.decode(predicate)),
                        Node.createURI(object)));
            } else {
                p.add(new Triple(Node.createURI(a.getUri()), Node.createURI(PrefixFactory.decode(predicate)),
                        Node.createLiteral(object)));
            }
        }

        //Build PublicationInfo
        PublicationInfo i = new PublicationInfo(np_item + "_PubInfo");
        i.add(new Triple(Node.createURI(np_item), Node.createURI("http://www.w3.org/ns/prov#generatedAtTime"),
                Node.createLiteral(new Date().toString(), null, XSDDatatype.XSDdateTime)));
        i.add(new Triple(Node.createURI(np_item), Node.createURI("http://www.w3.org/ns/prov#wasAttributedTo"),
                Node.createLiteral(Config.getName())));
        i.add(new Triple(Node.createURI(np_item), Node.createURI("http://www.w3.org/ns/prov#wasGeneratedBy"),
                Node.createURI(PrefixFactory.decode(concept_root))));
        for (JSONObject json : info) {
            String predicate = (String) json.get("predicate");
            String object = (String) json.get("object");
            if (urlValidator.isValid(object)) {
                i.add(new Triple(Node.createURI(np_item), Node.createURI(PrefixFactory.decode(predicate)),
                        Node.createURI(object)));
            } else {
                i.add(new Triple(Node.createURI(np_item), Node.createURI(PrefixFactory.decode(predicate)),
                        Node.createLiteral(object)));
            }
        }

        /**/
        Nanopublication np = new Nanopublication(np_item, a, p, i);
        //System.out.println(np.writeNQuads());
        Boot.getAPI().storeNanopub(np, concept_root);

    }
    save();
}

From source file:pt.ua.bioinformatics.coeus.nanopub.NanopubParser.java

/**
 * Add all objects (recursive function) with the property
 * (coeus:isAssociatedTo) to the Assertion
 *
 * @param a//from  w ww .j  a v a 2s.  c  o  m
 * @param object
 * @return
 */
public Assertion addAssociations(Assertion a, String object) {
    String queryItems = "SELECT * { <" + object + "> ?p ?o}";
    ResultSet rs_item = Boot.getAPI().selectRS(queryItems, false);
    ResultSet rs_item_test = Boot.getAPI().selectRS(queryItems, false);
    Boolean load = false;

    //Test if the data concept is to load into the assertion
    while (rs_item_test.hasNext()) {
        QuerySolution row_item = rs_item_test.next();
        String p = row_item.get("p").toString();
        String o = row_item.get("o").toString();

        if ((p.endsWith("hasConcept")) && (concept_childs.contains(PrefixFactory.encode(o).split(":")[1]))) {
            load = true;
            Logger.getLogger(NanopubParser.class.getName()).log(Level.INFO,
                    PrefixFactory.encode(o).split(":")[1]);
        }
    }

    while (rs_item.hasNext()) {
        QuerySolution row_item = rs_item.next();
        String p = row_item.get("p").toString();
        String o = row_item.get("o").toString();
        //Add all or add only the association
        if (load || p.endsWith("isAssociatedTo")) {
            UrlValidator urlValidator = new UrlValidator();
            if (urlValidator.isValid(o)) {
                a.add(new Triple(Node.createURI(object), Node.createURI(p), Node.createURI(o)));
            } else {
                a.add(new Triple(Node.createURI(object), Node.createURI(p), Node.createLiteral(o)));
            }
        }
        if (p.endsWith("isAssociatedTo")) {
            Triple t = new Triple(Node.createURI(o), Node.createURI(p), Node.createURI(object));
            if (!a.getContent().contains(t)) {
                a = addAssociations(a, o);
            }
        }
    }
    return a;
}

From source file:pt.ua.scaleus.api.API.java

/**
 * Removes the given triple statement in the database.
 *
 * @param database//from  ww w.  j  a v  a 2s.  com
 * @param triple
 */
public void removeStatement(String database, NTriple triple) {
    Dataset dataset = getDataset(database);
    dataset.begin(ReadWrite.WRITE);
    try {
        Model model = dataset.getDefaultModel();

        Resource s = model.createResource(triple.getS());
        Property p = model.createProperty(triple.getP());

        UrlValidator urlValidator = new UrlValidator();
        if (urlValidator.isValid(triple.getO())) {
            Resource o = model.createResource(triple.getO());
            Statement stat = model.createStatement(s, p, o);
            model.remove(stat);
        } else {
            Statement stat = model.createLiteralStatement(s, p, triple.getO());
            if (model.contains(stat)) {
                model.remove(stat);
            }
        }

        dataset.commit();
        //model.close();
    } finally {
        dataset.end();
    }
}

From source file:pt.ua.scaleus.api.API.java

/**
 * Adds the given quad statement to the database.
 *
 * @param database/*from ww  w . j a  va2s.c  o m*/
 * @param quad
 * @return success of the operation.
 */
public boolean addStatement(String database, NQuad quad) {
    Dataset dataset = getDataset(database);
    dataset.begin(ReadWrite.WRITE);

    try {

        DatasetGraph ds = dataset.asDatasetGraph();
        Node c = NodeFactory.createURI(quad.getC());
        Node s = NodeFactory.createURI(quad.getS());
        Node p = NodeFactory.createURI(quad.getP());

        UrlValidator urlValidator = new UrlValidator();
        if (urlValidator.isValid(quad.getO())) {
            Node o = NodeFactory.createURI(quad.getO());
            ds.add(c, s, p, o);
        } else {
            Node o = NodeFactory.createLiteral(quad.getO());
            ds.add(c, s, p, o);
        }

        dataset.commit();
        //model.close();
    } catch (Exception e) {
        log.error("Add statement failed", e);
    } finally {
        dataset.end();
    }
    return true;
}

From source file:pt.ua.scaleus.api.API.java

/**
 * Adds the given triple statement to the database.
 *
 * @param database//from w  ww .j  av a2 s .c o  m
 * @param triple
 * @return success of the operation.
 */
public boolean addStatement(String database, NTriple triple) {
    Dataset dataset = getDataset(database);
    dataset.begin(ReadWrite.WRITE);

    try {
        Model model = dataset.getDefaultModel();
        Resource s = model.createResource(triple.getS());
        Property p = model.createProperty(triple.getP());

        UrlValidator urlValidator = new UrlValidator();
        if (urlValidator.isValid(triple.getO())) {
            Resource o = model.createResource(triple.getO());
            model.add(s, p, o);
        } else {
            model.add(s, p, triple.getO());
        }

        dataset.commit();
        //model.close();
    } catch (Exception e) {
        log.error("Add statement failed", e);
    } finally {
        dataset.end();
    }
    return true;
}

From source file:scrape.Scraper.java

public Scraper() {
    newUrls = new HashSet<>();
    scrapedUrls = new HashSet<>();
    json = new JSONArray();
    urlVal = new UrlValidator();
}

From source file:sk.svec.jan.acb.main.Main.java

private static boolean validateUrl(String value) {
    StringBuilder errors = new StringBuilder();
    UrlValidator urlValidator = new UrlValidator();
    if (!value.startsWith("http://")) {
        value = "http://" + value;
    }/*from   www  .  j a  v  a 2  s  . com*/
    boolean valid = urlValidator.isValid(value);
    if (value == null || value.isEmpty()) {
        errors.append("Zadajte prosm url.");
        return false;
    } else if (!valid) {
        errors.append("Url ").append(value).append(" je neplatn. Zadajte prosm platn url.");
        return false;
    }
    return true;
}

From source file:sk.svec.jan.acb.web.InputForm.java

private static String validateValue(String value, String fieldName, StringBuilder errors) {
    UrlValidator urlValidator = new UrlValidator();
    if (!value.startsWith("http://")) {
        value = "http://" + value;
    }/*from   w  ww .j ava 2 s.  c  o  m*/
    boolean valid = urlValidator.isValid(value);
    if (value == null || value.isEmpty()) {
        errors.append("Zadajte prosm url. <br />");
    } else if (!valid) {
        errors.append("Url ").append(value).append(" je neplatn. Zadajte prosm platn url. <br />");
    }

    return value;
}