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
2ronwalfpackage org.mindswap.utils;
2ronwalf
2ronwalfimport java.io.StringReader;
2ronwalf
2ronwalfimport org.mindswap.owl.OWLFactory;
2ronwalfimport org.mindswap.owl.OWLIndividual;
2ronwalfimport org.mindswap.owl.OWLKnowledgeBase;
2ronwalfimport org.mindswap.owl.OWLOntology;
2ronwalfimport org.mindswap.owl.vocabulary.RDF;
2ronwalf
2ronwalfpublic class RDFUtils {
2ronwalf public static String addRDFTag( String rdf ) {
2ronwalf return
2ronwalf "<rdf:RDF xmlns:rdf=\"" + RDF.getURI() + "\">" +
2ronwalf rdf +
2ronwalf "</rdf:RDF>";
2ronwalf }
2ronwalf
2ronwalf public static String removeRDFTag( String rdf ) {
2ronwalf int rdfTagBegin = rdf.indexOf( "<rdf:RDF" );
2ronwalf int rdfTagEnd = rdf.indexOf( ">", rdfTagBegin + 1 );
2ronwalf int rdfEndTag = rdf.indexOf( "</rdf:RDF>" );
2ronwalf
2ronwalf rdf = rdf.substring( rdfTagEnd + 1, rdfEndTag );
2ronwalf
2ronwalf return rdf;
2ronwalf }
2ronwalf
2ronwalf public static String removeRDFTag( String rdf, boolean keepNamespaces ) {
2ronwalf int rdfTagBegin = rdf.indexOf( "<rdf:RDF" );
2ronwalf int rdfTagEnd = rdf.indexOf( ">", rdfTagBegin + 1 );
2ronwalf int rdfEndTag = rdf.indexOf( "</rdf:RDF>" );
2ronwalf
2ronwalf if( keepNamespaces ) {
2ronwalf StringBuffer buffer = new StringBuffer();
2ronwalf buffer.append( rdf.substring( 0, rdfTagEnd + 1) );
2ronwalf buffer.append( "<rdf:Description><rdfs:label rdf:parseType=\"Literal\">");
2ronwalf buffer.append( rdf.substring( rdfTagEnd + 1, rdfEndTag ) );
2ronwalf buffer.append( "</rdfs:label></rdf:Description>");
2ronwalf buffer.append( rdf.substring( rdfEndTag ) );
2ronwalf
2ronwalf OWLKnowledgeBase kb = OWLFactory.createKB();
2ronwalf OWLOntology ont = kb.read( new StringReader( buffer.toString() ), null );
2ronwalf OWLIndividual ind = ont.getIndividuals().individualAt( 0 );
2ronwalf rdf = ind.getLabel();
2ronwalf }
2ronwalf else
2ronwalf rdf = rdf.substring( rdfTagEnd + 1, rdfEndTag );
2ronwalf
2ronwalf return rdf;
2ronwalf }
2ronwalf}