Java URI to extractOntologyNameUriFromRDF(String rdfString, boolean generateURI)

Here you can find the source of extractOntologyNameUriFromRDF(String rdfString, boolean generateURI)

Description

Parses a RDF/XML-serialization and returns the base-URI if a value for 'xml:base' was set, null otherwise.

License

LGPL

Parameter

Parameter Description
rdfString RDF/XML-serialization to parse
generateURI TODO

Exception

Parameter Description
URISyntaxException an exception

Return

the base-URI

Declaration

public static URI extractOntologyNameUriFromRDF(String rdfString, boolean generateURI)
        throws URISyntaxException 

Method Source Code

//package com.java2s;
/*/*www. j av  a 2  s. co  m*/
 * IST-FP6-034763 QualiPSo: QualiPSo is a unique alliance
 * of European, Brazilian and Chinese ICT industry players,
 * SMEs, governments and academics to help industries and
 * governments fuel innovation and competitiveness with Open
 * Source software. To meet that goal, the QualiPSo consortium
 * intends to define and implement the technologies, processes
 * and policies to facilitate the development and use of Open
 * Source software components, with the same level of trust
 * traditionally offered by proprietary software. QualiPSo is
 * partially funded by the European Commission under EU?s sixth
 * framework program (FP6), as part of the Information Society
 * Technologies (IST) initiative. 
 *
 * This program has been created as part of the QualiPSo work
 * package on "Semantic Interoperability". The basic idea of this work    
 * package is to demonstrate how semantic technologies can be used to 
 * cope with the diversity and heterogeneity of software and services 
 * in the OSS domain.
 *
 * You can redistribute this program and/or modify it under the terms 
 * of the GNU Lesser General Public License version 3 (LGPL v3.0).
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 *
 * You should have received a copy of the LGPL
 * along with this program; if not, please have a look at
 * http://www.gnu.org/licenses/lgpl.html 
 * to obtain the full license text.
 *
 * Author of this program: 
 * Fraunhofer Institute FOKUS, http://www.fokus.fraunhofer.de
 *
 *
*/

import java.net.URI;
import java.net.URISyntaxException;

import java.util.Random;

public class Main {
    /**
     * Parses a RDF/XML-serialization and returns the base-URI if a value for
     * 'xml:base' was set, <code>null</code> otherwise.
     * 
     * @param rdfString
     *            RDF/XML-serialization to parse
     * @param generateURI TODO
     * @return the base-URI
     * @throws URISyntaxException
     */
    public static URI extractOntologyNameUriFromRDF(String rdfString, boolean generateURI)
            throws URISyntaxException {
        int startPos = -1;
        int endPos = -1;
        String uri = null;

        startPos = rdfString.indexOf("xml:base=\"") + 10;
        endPos = rdfString.indexOf("\"", startPos + 1);
        uri = rdfString.substring(startPos, endPos);

        // "xml:base" not found --> look for default namespace
        if (startPos == 9) {
            startPos = rdfString.indexOf("xmlns=\"") + 7;
            endPos = rdfString.indexOf("\"", startPos + 1) - 1; // -1 because protege puts a '#' after namespace-declaration
            uri = rdfString.substring(startPos, endPos);

            if (startPos != 6) {
                // write the uri as base-URI into the rdf:RDF-element, 
                // because it is necessary for importing into Protege
                rdfString = rdfString.substring(0, endPos + 2) + " xml:base=\"" + uri + "\" "
                        + rdfString.substring(endPos + 2);
            }
        }

        // default-namespace not found --> look for ontology name
        if (startPos == 6) {
            startPos = rdfString.indexOf("Ontology rdf:about=\"") + 20;
            endPos = rdfString.indexOf("\"", startPos + 1);
            uri = rdfString.substring(startPos, endPos);
        }

        if (startPos == 19) {
            // no URI found that can be used as the name
            int randomNumber = new Random().nextInt();
            if (randomNumber < 0) {
                randomNumber *= -1;
            }
            uri = "http://generatedOntologyName" + randomNumber + ".owl";
        }

        return new URI(uri);
    }
}

Related

  1. extractForwardURIFrom(URI requestURI)
  2. extractFromURIParams(String paramsRule, String uri)
  3. extractHostAddress(String uri)
  4. extractHostname(String uri)
  5. extractLocalName(String uri)
  6. extractParamsFromURI(String uri)
  7. extractS3Key(URI uri)
  8. extractTargetSystemFromUri(URI uri)
  9. extractURIFromText(String texto)