2ronwalf// The MIT License
2ronwalf//
2ronwalf// Copyright (c) 2004 Evren Sirin
2ronwalf//
2ronwalf// Permission is hereby granted, free of charge, to any person obtaining a copy
2ronwalf// of this software and associated documentation files (the "Software"), to
2ronwalf// deal in the Software without restriction, including without limitation the
2ronwalf// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
2ronwalf// sell copies of the Software, and to permit persons to whom the Software is
2ronwalf// furnished to do so, subject to the following conditions:
2ronwalf//
2ronwalf// The above copyright notice and this permission notice shall be included in
2ronwalf// all copies or substantial portions of the Software.
2ronwalf//
2ronwalf// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2ronwalf// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2ronwalf// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2ronwalf// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2ronwalf// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2ronwalf// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2ronwalf// IN THE SOFTWARE.
2ronwalf
2ronwalf/*
2ronwalf * Created on Jan 23, 2004
2ronwalf */
2ronwalfpackage examples;
2ronwalf
2ronwalfimport java.io.PrintWriter;
2ronwalfimport java.net.URI;
2ronwalfimport java.util.ArrayList;
2ronwalfimport java.util.Iterator;
2ronwalfimport java.util.List;
2ronwalfimport java.util.Map;
2ronwalf
2ronwalfimport org.mindswap.owl.OWLFactory;
2ronwalfimport org.mindswap.owl.OWLKnowledgeBase;
2ronwalfimport org.mindswap.owl.OWLOntology;
2ronwalfimport org.mindswap.owls.service.Service;
2ronwalfimport org.mindswap.utils.OutputFormatter;
2ronwalf
2ronwalf
2ronwalf/**
2ronwalf *
2ronwalf * Validate OWL-S service descriptions to find errors such as flaws in the data flow specification,
2ronwalf * grounding description and so on. Validator will catch common RDF errors and print them.
2ronwalf *
2ronwalf * @author Evren Sirin
2ronwalf */
2ronwalfpublic class OWLSValidator {
2ronwalf boolean parseRDFFinished = false;
2ronwalf
2ronwalf List serviceURIs = new ArrayList();
2ronwalf List errors = new ArrayList();
2ronwalf List warnings = new ArrayList();
2ronwalf List rdfProblems = new ArrayList();
2ronwalf
2ronwalf public OWLSValidator() {
2ronwalf
2ronwalf }
2ronwalf
2ronwalf public boolean validate(String fileURI) throws Exception {
2ronwalf return validate(fileURI, new PrintWriter(System.out));
2ronwalf }
2ronwalf
2ronwalf public boolean validate(String fileURI, PrintWriter output) throws Exception {
2ronwalf return validate(fileURI, output, false);
2ronwalf }
2ronwalf
2ronwalf public boolean validate(String fileURI, PrintWriter output, boolean isHTML) throws Exception {
2ronwalf return validate(fileURI, new OutputFormatter(output, isHTML));
2ronwalf }
2ronwalf
2ronwalf public boolean validate(String fileURI, OutputFormatter out) throws Exception {
2ronwalf URI uri = new URI(fileURI);
2ronwalf
2ronwalf OWLKnowledgeBase kb = OWLFactory.createKB();
2ronwalf
2ronwalf OWLOntology ont = kb.read(uri);
2ronwalf
2ronwalf List services = new ArrayList();
2ronwalf
2ronwalf out.printBold("Number of services found: ").println(ont.getServices().size() +"");
2ronwalf out.printBold("Number of valid services: ").println(services.size()+"");
2ronwalf out.println();
2ronwalf
2ronwalf printMesssages(out, "RDF Problems: ", rdfProblems.iterator());
2ronwalf printMesssages(out, "Errors: ", errors.iterator());
2ronwalf printMesssages(out, "Warnings: ", warnings.iterator());
2ronwalf
2ronwalf if(services != null) {
2ronwalf for(int i = 0; i < services.size(); i++) {
2ronwalf Service service = (Service) services.get(i);
2ronwalf out.printBold("Service: ").printLink(service.getURI().toString());
2ronwalf// out.print(" (Version: ").print(service.getOWLSVersion()).print(")").println();
2ronwalf out.printBold("Name: ").println(service.getLabel());
2ronwalf out.printBold("Description: ").println(service.getProfile().getTextDescription());
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf out.flush();
2ronwalf
2ronwalf // FIXME return value for validation
2ronwalf // return services.size() > 0 && errors.isEmpty();
2ronwalf return true;
2ronwalf }
2ronwalf
2ronwalf public void printMesssages(OutputFormatter out, String header, Iterator i) {
2ronwalf if(i.hasNext()) {
2ronwalf out.printBold(header).println();
2ronwalf
2ronwalf while(i.hasNext()) {
2ronwalf String error = (String) i.next();
2ronwalf
2ronwalf if(out.isFormatHTML()) out.print("<ul>");
2ronwalf if(out.isFormatHTML()) {
2ronwalf out.print("<li>");
2ronwalf out.print(format(error));
2ronwalf out.print("</li>");
2ronwalf }
2ronwalf else
2ronwalf out.println(error);
2ronwalf if(out.isFormatHTML()) out.print("</ul>");
2ronwalf }
2ronwalf
2ronwalf out.println();
2ronwalf }
2ronwalf }
2ronwalf
2ronwalf
2ronwalf
2ronwalf public String format(String str) {
2ronwalf return str;
2ronwalf// return str.replaceAll("\n", "<br>").replaceAll(" ", "&nbsp;");
2ronwalf// StringBuffer buffer = new StringBuffer();
2ronwalf// String link;
2ronwalf// int start = 0, finish;
2ronwalf// int index = str.indexOf("http://");
2ronwalf//
2ronwalf// while(index != -1) {
2ronwalf// buffer.append(str.substring(start, index));
2ronwalf//
2ronwalf// finish = Math.min(str.indexOf(' ', index), str.indexOf('\n', index));
2ronwalf// if(finish > str.indexOf('<', index)) finish = str.indexOf('\n', index);
2ronwalf// if(finish == -1) finish = str.length();
2ronwalf// link = str.substring(index, finish);
2ronwalf// buffer.append("<a href=\"").append(link).append("\">");
2ronwalf// buffer.append(link).append("</a>");
2ronwalf// start = index + link.length();
2ronwalf// index = str.indexOf("http://", start);
2ronwalf// }
2ronwalf// buffer.append(str.substring(start));
2ronwalf//
2ronwalf// return buffer.toString().replaceAll("\n", "<br>");
2ronwalf }
2ronwalf
2ronwalf public String getServiceURI() {
2ronwalf return (String) serviceURIs.get(serviceURIs.size() - 1);
2ronwalf }
2ronwalf
2ronwalf public void put(Map table, String key, String value) {
2ronwalf List list = (List) table.get(key);
2ronwalf
2ronwalf if(list == null) {
2ronwalf list = new ArrayList();
2ronwalf table.put(key, list);
2ronwalf }
2ronwalf
2ronwalf list.add(value);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.io.OWLSReaderListener#startService(java.lang.String)
2ronwalf */
2ronwalf public void startService(String serviceURI) {
2ronwalf serviceURIs.add(serviceURI);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.io.OWLSReaderListener#finishService(java.lang.String)
2ronwalf */
2ronwalf public void finishService(String serviceURI) {
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.io.OWLSReaderListener#warning(java.lang.String)
2ronwalf */
2ronwalf public void warning(String msg) {
2ronwalf if(parseRDFFinished)
2ronwalf warnings.add(msg);
2ronwalf else
2ronwalf rdfProblems.add(msg);
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.io.OWLSReaderListener#error(java.lang.String)
2ronwalf */
2ronwalf public void error(String msg) {
2ronwalf if(parseRDFFinished)
2ronwalf errors.add(msg);
2ronwalf else
2ronwalf rdfProblems.add(msg);
2ronwalf }
2ronwalf
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.io.OWLSReaderListener#startParseRDF()
2ronwalf */
2ronwalf public void startParseRDF() {
2ronwalf parseRDFFinished = false;
2ronwalf }
2ronwalf
2ronwalf /* (non-Javadoc)
2ronwalf * @see org.mindswap.owls.io.OWLSReaderListener#finishParseRDF()
2ronwalf */
2ronwalf public void finishParseRDF() {
2ronwalf parseRDFFinished = true;
2ronwalf }
2ronwalf
2ronwalf public static void main(String[] args) throws Exception {
2ronwalf if(args.length != 1) {
2ronwalf if(args.length < 1)
2ronwalf System.err.println("Not enough parameters");
2ronwalf if(args.length > 1)
2ronwalf System.err.println("Too many parameters");
2ronwalf System.err.println("usage: java OWLSValidator <serviceURI>");
2ronwalf System.exit(0);
2ronwalf }
2ronwalf
2ronwalf OWLSValidator validator = new OWLSValidator();
2ronwalf System.out.println("Valid: " + validator.validate(args[0]));
2ronwalf }
2ronwalf
2ronwalf}