001 // This file is part of the Attempto Java Packages. 002 // Copyright 2008, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch). 003 // 004 // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the 005 // terms of the GNU Lesser General Public License as published by the Free Software Foundation, 006 // either version 3 of the License, or (at your option) any later version. 007 // 008 // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY 009 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 010 // PURPOSE. See the GNU Lesser General Public License for more details. 011 // 012 // You should have received a copy of the GNU Lesser General Public License along with the Attempto 013 // Java Packages. If not, see http://www.gnu.org/licenses/. 014 015 package ch.uzh.ifi.attempto.owl; 016 017 import org.apache.commons.httpclient.HttpClient; 018 import org.apache.commons.httpclient.HttpStatus; 019 import org.apache.commons.httpclient.NameValuePair; 020 import org.apache.commons.httpclient.methods.PostMethod; 021 022 /** 023 * This is a simple interface to the OWL verbalizer. 024 * OWL verbalizer is a tool that translates an OWL 2 ontology into an ACE text. 025 * It expects the ontology to be represented in the OWL 2 XML serialization syntax. 026 * The version of the OWL 2 XML syntax that the OWL verbalizer supports is described in 027 * 028 * <blockquote> 029 * <a href="http://www.w3.org/TR/2008/WD-owl2-xml-serialization-20080411/">http://www.w3.org/TR/2008/WD-owl2-xml-serialization-20080411/</a> 030 * </blockquote> 031 * 032 * The OWL verbalizer accepts one parameter, <code>xml</code>, the value of which 033 * is the complete ontology as string. 034 * The OWL verbalizer returns the ACE representation of the ontology as plain text (string). 035 * In case the verbalizer fails to process an OWL axiom in the ontology, 036 * an error message is returned between ACE comment symbols. 037 * 038 * Note that future versions of the OWL verbalizer (and as a result this interface) 039 * will expect a different input format (because the OWL 2 XML specification will be updated) 040 * and probably produce a different output format (a more structured one, where e.g. error 041 * messages are represented differently). 042 * 043 * @author Kaarel Kaljurand 044 * 045 */ 046 public class VerbalizerWebservice { 047 048 private HttpClient client = new HttpClient(); 049 private String url = null; 050 051 /** 052 * Constructs a new <code>VerbalizerWebservice</code> object 053 * on the basis of the URL of the OWL verbalizer webservice. 054 * 055 * @param url The URL of the OWL verbalizer webservice. 056 */ 057 public VerbalizerWebservice(String url) { 058 this.url = url; 059 } 060 061 /** 062 * Calls the OWL verbalizer webservice by giving the string representation of 063 * an XML-formatted ontology as input. Returns the corresponding ACE text 064 * with possible error messages as comments. 065 * 066 * @param xml XML-representation of an OWL ontology. 067 * @return ACE text that corresponds to the ontology. 068 */ 069 public String call(String xml) { 070 String acetextAsString = null; 071 072 PostMethod method = new PostMethod(url); 073 074 method.setRequestBody(new NameValuePair[] { 075 new NameValuePair("xml", xml) 076 }); 077 078 try { 079 int statusCode = client.executeMethod(method); 080 081 if (statusCode == HttpStatus.SC_OK) { 082 acetextAsString = method.getResponseBodyAsString(); 083 } 084 else { 085 throw new RuntimeException("HTTP request failed: " + method.getStatusLine()); 086 } 087 } catch (Exception e) { 088 throw new RuntimeException("Error accessing OWL->ACE webservice: " + e.getMessage()); 089 } finally { 090 method.releaseConnection(); 091 } 092 093 return acetextAsString; 094 } 095 }