Java URI to extractBaseUriFromRDF(String rdfString)

Here you can find the source of extractBaseUriFromRDF(String rdfString)

Description

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

License

Open Source License

Parameter

Parameter Description
rdfString RDF/XML-serialization to parse

Exception

Parameter Description
URISyntaxException an exception

Return

the base-URI

Declaration

public static URI extractBaseUriFromRDF(String rdfString) throws URISyntaxException 

Method Source Code

//package com.java2s;
/*//  www .j av a  2  s  . c  o m
 * SWS Challenge 2008
 *
 * You can redistribute this program and/or modify it under the terms 
 * of the GNU General Public License version 3 (GPL 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 GPL
 * along with this program; if not, please have a look at
 * http://www.gnu.org/licenses/gpl.html 
 * to obtain the full license text.
 *
 * Author of this program: 
 * Ralf Weinand
 * Nils Barnickel 
 * Fraunhofer Institute FOKUS, http://www.fokus.fraunhofer.de
 *
 */

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

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
     * @return the base-URI
     * @throws URISyntaxException
     */
    public static URI extractBaseUriFromRDF(String rdfString) throws URISyntaxException {
        int startPos = -1;
        int endPos = -1;

        startPos = rdfString.indexOf("xml:base=\"") + 10;

        if (startPos > 10) {
            endPos = rdfString.indexOf("\"", startPos + 1);
        } else {
            startPos = rdfString.indexOf("xmlns=\"") + 7;
            endPos = rdfString.indexOf("\"", startPos + 1) - 1; // -1 because
            // protege puts
            // a '#' after
            // xml:base-declaration
        }

        String uri = rdfString.substring(startPos, endPos);

        return new URI(uri);
    }
}

Related

  1. convertToAbsPath(URI baseURI, String path)
  2. convertToEncodedURIString(String input)
  3. extractContentDigest(URI contentDigest)
  4. extractForwardURIFrom(URI requestURI)
  5. extractFromURIParams(String paramsRule, String uri)
  6. extractHostAddress(String uri)