<?xml version="1.0" encoding="UTF-8"?>
<!--COPYRIGHT (C) 2004-2006 
	Laboratory of Advanced Information Technology and Ebiquity Group
	Department of Computer Science and Electronic Engineering
	University of Maryland Baltimore County
	1000 hilltop circle
	Baltimore, MD 21250
	ALL RIGHTS RESERVED.

	Author:  		Li Ding  ( http://www.cs.umbc.edu/~dingli1)
	Version:		1.2
	Last Update:	2004/05/16	
-->

<!-- Transforms an OWL ontology into a java file for jena inference  -->

<!DOCTYPE xsl:stylesheet [
  <!ENTITY rdf          "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <!ENTITY rdfs		"http://www.w3.org/2000/01/rdf-schema#">
  <!ENTITY xsd		"http://www.w3.org/2001/XMLSchema#">
  <!ENTITY owl		"http://www.w3.org/2002/07/owl#">
]>

  <xsl:stylesheet    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   version="0.8"    
    xmlns:rdf="&rdf;"
    xmlns:rdfs="&rdfs;"
    xmlns:xsd="&xsd;"
    xmlns:owl="&owl;"
    xmlns ='http://daml.umbc.edu/ontologies/webofbelief/xslt/owl2jena.xsl'>

  <xsl:output method="text" media-type="text/plain" encoding="ISO-8859-1"/>

  <!-- global variables and functions -->
  <!--   <xsl:variable name="namespace" select="concat(/rdf:RDF/@xml:base,'#')"/>  -->
  <xsl:variable name="namespace" select="/rdf:RDF/@xml:base"/>
<!--  <xsl:variable name="class-name" select="translate(//owl:Ontology/rdfs:label, $lowercase, $uppercase)"/>  -->
  <xsl:variable name="class-name" select="//owl:Ontology/rdfs:label"/>

  <xsl:variable name="package-name"> edu.umbc.trustweb.core.vocabulary </xsl:variable>
  <!-- TODO: setup your package name before conversion	e.g. edu.umbc.sharenet.vocabulary  -->


  <xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>
  <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>


  <xsl:variable name="nodeset-class-owl" select=".//*[rdf:type/@rdf:resource='&owl;Class' or (local-name()='Class' and namespace-uri()='&owl;')]" />
  <xsl:variable name="nodeset-class-rdfs" select=".//*[rdf:type/@rdf:resource='&rdfs;Class' or (local-name()='Class' and namespace-uri()='&rdfs;')]" />
  <xsl:variable name="nodeset-property-other" select=".//*[
				   (local-name()='Property' and namespace-uri()='&rdf;' and not (starts-with(rdfs:domain/@rdf:resource,'#')) ) 
				or (local-name()='DatatypeProperty' and namespace-uri()='&owl;' and not (starts-with(rdfs:domain/@rdf:resource,'#')) ) 
				or (local-name()='ObjectProperty' and namespace-uri()='&owl;'and not (starts-with(rdfs:domain/@rdf:resource,'#')) ) 
				or (local-name()='ConstraintProperty' and namespace-uri()='&rdfs;') ]" />
  <!-- main template: generate the java file here--> 


  <xsl:template match="/">
/*
 * <xsl:value-of select="$class-name"/>.java
 *
 */

package <xsl:value-of select="$package-name"/>;

import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

/**
 *
 *  This file is automatically generated by owl2jena.xsl,  
 *  @author: Li Ding (http://www.csee.umbc.edu/~dingli1/)
 *  
 *  Ontology information
    	<xsl:value-of select="//owl:Ontology/rdfs:comment"/>
	versionInfo: <xsl:value-of select="//owl:Ontology/owl:versionInfo"/>
 */
public class <xsl:value-of select="$class-name"/>{

    private static Model m_model = ModelFactory.createDefaultModel();

    protected static final String NS = "<xsl:value-of select="$namespace"/>";

    public static final String getURI(){ return NS; }

    public static final Resource NAMESPACE = m_model.createResource( NS );


    // Owl Classes(<xsl:value-of select="count($nodeset-class-owl) "/>)

	<xsl:apply-templates select="$nodeset-class-owl" mode="write-node">
		<xsl:with-param name="option" select="'resource-declare'"/>
		<xsl:sort select="@rdf:ID"/>
	</xsl:apply-templates>

    // Rdfs Classes(<xsl:value-of select="count($nodeset-class-rdfs) "/>)
	<xsl:apply-templates select="$nodeset-class-rdfs" mode="write-node">
		<xsl:with-param name="option" select="'resource-declare'"/>
	            <xsl:sort select="@rdf:ID"/>
	</xsl:apply-templates>

    // Other Properties(<xsl:value-of select="count($nodeset-property-other) "/>)
	<xsl:apply-templates select="$nodeset-property-other" mode="write-node">
		<xsl:with-param name="option" select="'property-declare'"/>
	            <xsl:sort select="@rdf:ID"/>
	</xsl:apply-templates>

}


 </xsl:template>



  <xsl:template name="write-declare">
    <xsl:param name="name"/>
    <xsl:param name="type"/>
    public static <xsl:value-of select="$type"/> <xsl:text> </xsl:text> <xsl:value-of select="$name"/> = m_model.createProperty("<xsl:value-of select="$namespace"/><xsl:value-of select="$name"/>");
 </xsl:template>


  <xsl:template match="*" mode="write-node">
   <xsl:param name="option"/>
   <xsl:if test="@rdf:ID or @ID or @rdf:about or @about">
     <xsl:variable name="ref">
       <xsl:choose>
         <xsl:when test="@rdf:ID">
           <xsl:value-of select="concat('#',@rdf:ID)"/>
         </xsl:when>
         <xsl:when test="@rdf:about">
           <xsl:value-of select="@rdf:about"/>
         </xsl:when>
         <xsl:when test="@ID and namespace-uri()='&rdf;'">
           <xsl:value-of select="concat('#',@ID)"/>
         </xsl:when>
         <xsl:when test="@about  and namespace-uri()='&rdf;'">
           <xsl:value-of select="@about"/>
         </xsl:when>
       </xsl:choose>
     </xsl:variable>
	<xsl:choose>
		<xsl:when test="$option='resource-declare'">
			<xsl:call-template name="write-declare">
				<xsl:with-param name="name" select="substring-after($ref,'#')"/>
				<xsl:with-param name="type" select="'Resource'"/>
			</xsl:call-template>
			<xsl:apply-templates select="//*[rdfs:domain/@rdf:resource=$ref]" mode="write-node">
				<xsl:with-param name="option" select="'property-declare'"/>
			            <xsl:sort select="@rdf:ID"/>
			</xsl:apply-templates>
<xsl:text>
</xsl:text>
		</xsl:when>
		<xsl:when test="$option='property-declare'">
			<xsl:call-template name="write-declare">
				<xsl:with-param name="name" select="substring-after($ref,'#')"/>
				<xsl:with-param name="type" select="'Property'"/>
			</xsl:call-template>
		</xsl:when>
	</xsl:choose>
   </xsl:if>
  </xsl:template>


</xsl:stylesheet>
