All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.franz.agraph.jena.AGPrefixMapping Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
/******************************************************************************
** Copyright (c) 2008-2016 Franz Inc.
** All rights reserved. This program and the accompanying materials
** are made available under the terms of the Eclipse Public License v1.0
** which accompanies this distribution, and is available at
** http://www.eclipse.org/legal/epl-v10.html
******************************************************************************/

package com.franz.agraph.jena;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.xerces.util.XMLChar;
import org.openrdf.model.Namespace;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryResult;

import com.hp.hpl.jena.rdf.model.impl.Util;
import com.hp.hpl.jena.shared.PrefixMapping;

/**
 * Implements the Jena PrefixMapping interface for AllegroGraph.
 * 
 */
public class AGPrefixMapping implements PrefixMapping {

	AGGraph graph;
    protected boolean locked;
	
	public AGPrefixMapping(AGGraph graph) {
		this.graph = graph;
	}

	AGGraph getGraph() {
		return graph;
	}

	@Override
	public String expandPrefix(String prefixed) {
        int colon = prefixed.indexOf( ':' );
        if (colon < 0) {
            return prefixed;
        } else {
            String uri = getNsPrefixURI( prefixed.substring( 0, colon ) );
            return uri == null ? prefixed : uri + prefixed.substring( colon + 1 );
        }
	}

	@Override
	public Map getNsPrefixMap() {
		Map map = new HashMap();
		try {
			RepositoryResult result = getGraph().getConnection().getNamespaces();
			while (result.hasNext()) {
				Namespace ns = result.next();
				map.put(ns.getPrefix(), ns.getName());
			}
		} catch (RepositoryException e) {
			throw new RuntimeException(e);
		}
		return map;
	}

	@Override
	public String getNsPrefixURI(String prefix) {
		String uri = null;
		try {
			if (prefix!=null) {
				uri = getGraph().getConnection().getNamespace(prefix);
			}
		} catch (RepositoryException e) {
			throw new RuntimeException(e);
		}
		return uri;
	}

	@Override
	public String getNsURIPrefix(String uri) {
		// TODO speed this up!
		String prefix = null;
		try {
			RepositoryResult result = getGraph().getConnection().getNamespaces();
			while (prefix==null && result.hasNext()) {
				Namespace ns = result.next();
				if (uri.equalsIgnoreCase(ns.getName())) {
					prefix = ns.getPrefix();
				}
			}
		} catch (RepositoryException e) {
			throw new RuntimeException(e);
		}
		return prefix;
	}

	@Override
	public PrefixMapping lock() {
        locked = true; 
        return this;
	}

	@Override
	public String qnameFor(String uri) {
        int split = Util.splitNamespace( uri );
        String ns = uri.substring( 0, split ), local = uri.substring( split );
        if (local.equals( "" )) return null;
        String prefix = getNsURIPrefix(ns);
        return prefix == null ? null : prefix + ":" + local;
	}

	@Override
	public PrefixMapping removeNsPrefix(String prefix) {
        checkUnlocked();
		try {
			getGraph().getConnection().removeNamespace(prefix);
		} catch (RepositoryException e) {
			throw new RuntimeException(e);
		}
		return this;
	}

	@Override
	public boolean samePrefixMappingAs(PrefixMapping other) {
		return getNsPrefixMap().equals(other.getNsPrefixMap());
	}

	@Override
	public PrefixMapping setNsPrefix(String prefix, String uri) {
        checkUnlocked();
		// TODO support an empty prefix for the default namespace 
        if (prefix.length() > 0 && !XMLChar.isValidNCName( prefix ))
        	// required by AbstractTestPrefixMapping#testCheckNames()
            throw new PrefixMapping.IllegalPrefixException( prefix ); 
		if (uri==null)
			// required by AbstractTestPrefixMapping#testNullURITrapped()
			// TODO: why not an IllegalArgumentException?
			throw new NullPointerException("null URIs are prohibited as arguments to setNsPrefix");
		try {
			getGraph().getConnection().setNamespace(prefix, uri);
		} catch (RepositoryException e) {
			throw new RuntimeException(e);
		}
		return this;
	}

    protected void checkUnlocked()
    { if (locked) throw new PrefixMapping.JenaLockedException( this ); }

    @Override
	public PrefixMapping setNsPrefixes(PrefixMapping other) {
		return setNsPrefixes(other.getNsPrefixMap());
	}

	@Override
	public PrefixMapping setNsPrefixes(Map map) {
		checkUnlocked();
		// TODO do this in a single http request when that is available
		for (String key: map.keySet()) {
			setNsPrefix(key, map.get(key));
		}
		return this;
	}

	@Override
	public String shortForm(String uri) {
		// TODO speed this up
		Map map = getNsPrefixMap();
		for (Entry s: map.entrySet()) {
			if (uri.startsWith(s.getValue())) {
				return s.getKey() + ":" + uri.substring(s.getValue().length());
			}
		}
		return uri;
	}

	@Override
	public PrefixMapping withDefaultMappings(PrefixMapping map) {
		Map thisMap = getNsPrefixMap();
		Map otherMap = map.getNsPrefixMap();
		for (String key: otherMap.keySet()) {
			String value = otherMap.get(key);
			if (!thisMap.containsKey(key)&&!thisMap.containsValue(value)) {
				setNsPrefix(key,value);
			}
		}
		return this;
	}

	public String toString() {
		return toString(20);
	}
	
	public String toString(int max)
	   {
		Map map = getNsPrefixMap();
		int size = map.size();
		int count = 0; 
		Iterator it = map.keySet().iterator();
		StringBuffer b = new StringBuffer(this.getClass().getSimpleName()+"(size: " + size + "){" );
		String gap = "";
		for (; it.hasNext() && count < max; count++) {
			b.append( gap );
			gap = ", ";
			String key = it.next();
			b.append( key + "=" + map.get(key));
		    }
		if (count==max && it.hasNext()) {
			b.append(",...");
		}
		b.append( "}" );
		return b.toString();
	  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy