View Javadoc
1   /*
2    * The basecode project
3    * 
4    * Copyright (c) 2007-2019 University of British Columbia
5    * 
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   */
19  package ubic.basecode.ontology.jena;
20  
21  import com.hp.hpl.jena.ontology.DatatypeProperty;
22  import com.hp.hpl.jena.ontology.OntProperty;
23  import com.hp.hpl.jena.ontology.OntResource;
24  import com.hp.hpl.jena.ontology.Restriction;
25  import com.hp.hpl.jena.rdf.model.Property;
26  
27  import java.util.Set;
28  
29  /**
30   * @author pavlidis
31   * 
32   */
33  public class PropertyFactory {
34  
35      /**
36       * Convert a Jena property.
37       * 
38       * @param property
39       * @param source
40       * @return new property or null if it could not be converted.
41       */
42      public static ubic.basecode.ontology.model.OntologyProperty asProperty( OntProperty property, Set<Restriction> additionalRestrictions ) {
43  
44          if ( property.isObjectProperty() ) {
45              return new ObjectPropertyImpl( property.asObjectProperty(), additionalRestrictions );
46          } else if ( property.isDatatypeProperty() ) {
47              return new DatatypePropertyImpl( property.asDatatypeProperty() );
48          } else {
49              return null;
50          }
51  
52      }
53  
54      public static Class<?> convertType( DatatypeProperty resource ) {
55          Class<?> type = java.lang.String.class;
56  
57          OntResource range = resource.getRange();
58          if ( range != null ) {
59              String uri = range.getURI();
60  
61              if ( uri == null ) {
62                  type = java.lang.String.class;
63              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;string" ) ) {
64                  type = java.lang.String.class;
65              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;boolean" ) ) {
66                  type = java.lang.Boolean.class;
67              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;float" ) ) {
68                  type = java.lang.Double.class;
69              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;double" ) ) {
70                  type = java.lang.Double.class;
71              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;int" ) ) {
72                  type = java.lang.Integer.class;
73              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;date" ) ) {
74                  type = java.util.Date.class;
75              }
76          }
77          return type;
78      }
79  }