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 org.jspecify.annotations.Nullable;
26  
27  import java.util.Set;
28  
29  /**
30   * @author pavlidis
31   */
32  class PropertyFactory {
33  
34      /**
35       * Convert a Jena property.
36       *
37       * @return new property or null if it could not be converted.
38       */
39      public static ubic.basecode.ontology.model.@Nullable OntologyProperty asProperty( OntProperty property, Set<Restriction> additionalRestrictions ) {
40  
41          if ( property.isObjectProperty() ) {
42              return new ObjectPropertyImpl( property.asObjectProperty(), additionalRestrictions );
43          } else if ( property.isDatatypeProperty() ) {
44              return new DatatypePropertyImpl( property.asDatatypeProperty() );
45          } else {
46              return null;
47          }
48  
49      }
50  
51      public static Class<?> convertType( DatatypeProperty resource ) {
52          Class<?> type = java.lang.String.class;
53  
54          OntResource range = resource.getRange();
55          if ( range != null ) {
56              String uri = range.getURI();
57  
58              if ( uri == null ) {
59                  type = java.lang.String.class;
60              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;string" ) ) {
61                  type = java.lang.String.class;
62              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;boolean" ) ) {
63                  type = java.lang.Boolean.class;
64              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;float" ) ) {
65                  type = java.lang.Double.class;
66              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;double" ) ) {
67                  type = java.lang.Double.class;
68              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;int" ) ) {
69                  type = java.lang.Integer.class;
70              } else if ( uri.equals( "http://www.w3.org/2001/XMLSchema#&xsd;date" ) ) {
71                  type = java.util.Date.class;
72              }
73          }
74          return type;
75      }
76  }