⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 owlresourceimpl.java

📁 开发owl的API,提供了W3C规定标准接口,是目前比较少的API.
💻 JAVA
字号:
// The MIT License
//
// Copyright (c) 2004 Evren Sirin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

/*
 * Created on Dec 27, 2003
 *
 */
package org.mindswap.owl.impl;

import java.net.URI;

import org.mindswap.owl.OWLConfig;
import org.mindswap.owl.OWLFactory;
import org.mindswap.owl.OWLResource;
import org.mindswap.owl.OWLResourceWrapper;
import org.mindswap.owl.Util;
import org.mindswap.owl.vocabulary.RDFS;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.StmtIterator;

/**
 * @author Evren Sirin
 *
 */
public class OWLResourceImpl implements OWLResource {
	Resource resource;

	public OWLResourceImpl(Resource resource) {
		if(resource == null)
			this.resource = ResourceFactory.createResource();
		else
			this.resource = resource;
	}

	/* (non-Javadoc)
	 * @see org.mindswap.shared.Resource#isAnon()
	 */
	public boolean isAnon() {
		return resource.isAnon();
	}

	/* (non-Javadoc)
	 * @see org.mindswap.shared.Resource#getURI()
	 */
	public URI getURI() {
		if(isAnon()) return null;
		
		return Util.toURI(resource);
	}

	/* (non-Javadoc)
	 * @see org.mindswap.shared.Resource#getLabel()
	 */
	public String getLabel() {
		return getDataProperty(RDFS.label);
	}

	/* (non-Javadoc)
	 * @see org.mindswap.shared.Resource#getLabel(java.lang.String)
	 */
	public String getLabel(String lang) {		
		return getDataProperty(RDFS.label, lang);
	}

	/* (non-Javadoc)
	 * @see org.mindswap.shared.Resource#getProperty(java.lang.String)
	 */
	public String getDataProperty(URI propURI) {
		String value = null;
		
		
		for(int i = 0; value == null && i < OWLConfig.DEFAULT_LANGS.length; i++)
			value = getDataProperty(propURI, OWLConfig.DEFAULT_LANGS[i]);
		
		return value; 
	}

	/* (non-Javadoc)
	 * @see org.mindswap.shared.Resource#getProperty(java.lang.String, java.lang.String)
	 */
	public String getDataProperty(URI propURI, String lang) {
		try {
			Property prop = Util.toProperty(propURI);
	
			return getProperty(prop, lang).getLexicalForm();
		}
		catch(Exception e) {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see org.mindswap.shared.Resource#getProperty(java.lang.String, java.lang.String)
	 */
	private Literal getProperty(Property prop, String lang) {
		try {
			StmtIterator i = resource.listProperties(prop);
			while(i.hasNext()) {
				RDFNode node = i.nextStatement().getObject();
				
				if(node instanceof Literal) {
					Literal literal = (Literal) node;

					if(lang == null) 
						return literal;				
					else {
						if(lang.equals(literal.getLanguage()))
							return literal;			
					}					
				}
			}
		} catch (Exception e) {
		}
				
		return null;
	}

	private void removeProperty(Property prop, String lang) {
		try {
			StmtIterator i = resource.listProperties(prop);
			while(i.hasNext()) {
				RDFNode node = i.nextStatement().getObject();
				
				if(node instanceof Literal) {
					Literal literal = (Literal) node;

					if(lang == null) 
						i.remove();				
					else {
						if(lang.equals(literal.getLanguage()))
							i.remove();			
					}					
				}
			}
		} catch (Exception e) {
		}
	}
	
	public boolean equals(Object o) {
		if(o instanceof OWLResourceImpl)
			return resource.equals(((OWLResourceImpl)o).resource);
			
		return false;	
	}
	
	public int hashCode() {
		return resource.hashCode();
	}

	/* (non-Javadoc)
	 * @see org.mindswap.shared.Resource#debugString()
	 */
	public String debugString() {
		return toString();
	}

	/* (non-Javadoc)
	 * @see org.mindswap.shared.OWLResource#setLabel(java.lang.String)
	 */
	public void setLabel(String label) {
		setDataProperty(RDFS.label, label);
	}

	public void setLabel(String label, String lang) {
		setDataProperty(RDFS.label, label, lang);
	}
	
	
	public String toString() {
		String value = getLabel();
		if(value == null)
			value = isAnon() ? "Anonymous (" + resource + ")": getURI().getFragment();
		
		return value;
	}

	/* (non-Javadoc)
	 * @see org.mindswap.owl.OWLResource#getJenaResource()
	 */
	public Resource getJenaResource() {
		return resource;
	}

	/* (non-Javadoc)
	 * @see org.mindswap.owl.OWLResource#setProperty(java.net.URI)
	 */
	public void setDataProperty(URI propURI, String value) {
		setDataProperty(propURI, value, "");
	}

	/* (non-Javadoc)
	 * @see org.mindswap.owl.OWLResource#setProperty(java.net.URI, java.lang.String)
	 */
	public void setDataProperty(URI propURI, String value, String lang) {
		removeProperty(Util.toProperty(propURI), lang);
		Literal literal = resource.getModel().createLiteral(value, lang);
			
		resource.addProperty(Util.toProperty(propURI), literal);
	}

	/* (non-Javadoc)
	 * @see org.mindswap.owl.OWLResource#hasProperty(java.net.URI)
	 */
	public boolean hasProperty(URI propURI) {
		try {
			Property prop = Util.toProperty(propURI);
			
			return resource.hasProperty(prop);
		}
		catch(Exception e) {
			return false;
		}
	}
	
	/* (non-Javadoc)
	 * @see org.mindswap.owl.OWLResource#getProperty(java.net.URI)
	 */
	public OWLResource getProperty(URI propURI) {
		try {
			Property prop = Util.toProperty(propURI);
			
			return OWLFactory.createOWLResource(resource.getProperty(prop).getResource());
		}
		catch(Exception e) {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see org.mindswap.owl.OWLResource#as(java.lang.Class)
	 */
	public OWLResource wrap(Class view) {
		OWLResourceWrapper wrapper = OWLConfig.getWrapper(view);
		
		if(wrapper != null && wrapper.canWrap(this))
			return wrapper.wrap(this);
		
		return null;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -