org.apache.marmotta.ldclient.provider.xml.AbstractXMLDataProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ldclient-provider-xml
Show all versions of ldclient-provider-xml
Provides base functionality for all Linked Data resources offering data in various XML formats. Specific
data providers / parsers can derive from these base classes.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.marmotta.ldclient.provider.xml;
import org.apache.marmotta.commons.sesame.model.Namespaces;
import org.apache.marmotta.ldclient.exception.DataRetrievalException;
import org.apache.marmotta.ldclient.provider.xml.mapping.XPathValueMapper;
import org.apache.marmotta.ldclient.services.provider.AbstractHttpProvider;
import org.jdom2.*;
import org.jdom2.Namespace;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders;
import org.jdom2.xpath.XPathExpression;
import org.openrdf.model.*;
import org.openrdf.model.impl.ValueFactoryImpl;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* Abstract implementation of a data provider based on XML documents. Implementing classes need to provide
* a mapping table mapping from RDF property URIs to XPath Value Mappers that are evaluated on the XML document
* (getXPathMappings method), as well as a list of URIs used as types for the created resource.
*
* Author: Sebastian Schaffert
*/
public abstract class AbstractXMLDataProvider extends AbstractHttpProvider {
/**
* Return a mapping table mapping from RDF properties to XPath Value Mappers. Each entry in the map is evaluated
* in turn; in case the XPath expression yields a result, the property is added for the processed resource.
*
* @return
* @param requestUrl
*/
protected abstract Map getXPathMappings(String requestUrl);
/**
* Return a list of URIs that should be added as types for each processed resource.
*
* @return
* @param resource
*/
protected abstract List getTypes(URI resource);
/**
* Provide namespace mappings for the XPath expressions from namespace prefix to namespace URI. May be overridden
* by subclasses as appropriate, the default implementation returns an empty map.
*
* @return
*/
protected Map getNamespaceMappings() {
return Collections.emptyMap();
}
/**
* Parse the HTTP response entity returned by the web service call and return its contents as a Sesame RDF
* repository. The content type returned by the web service is passed as argument to help the implementation
* decide how to parse the data.
*
*
*
* @param resource the subject of the data retrieval
* @param triples
*@param in input stream as returned by the remote webservice
* @param contentType content type as returned in the HTTP headers of the remote webservice @return an RDF repository containing an RDF representation of the dataset located at the remote resource.
* @throws java.io.IOException in case an error occurs while reading the input stream
*/
@Override
public List parseResponse(String resource, String requestUrl, Model triples, InputStream in, String contentType) throws DataRetrievalException {
// build a JDOM document
try {
SAXBuilder parser = new SAXBuilder(XMLReaders.NONVALIDATING);
Document doc = parser.build(in);
Set namespaces = new HashSet();
for(Map.Entry ns : getNamespaceMappings().entrySet()) {
namespaces.add(Namespace.getNamespace(ns.getKey(), ns.getValue()));
}
ValueFactory vf = new ValueFactoryImpl();
Resource subject = vf.createURI(resource);
for(Map.Entry mapping : getXPathMappings(requestUrl).entrySet()) {
XPathExpression © 2015 - 2025 Weber Informatics LLC | Privacy Policy