View Javadoc

1   /*
2    *  $Id: Resource.java,v 1.4 2004/07/25 21:44:43 roku Exp $ 
3    *
4    *  Copyright 2004 University of Hannover
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 olr.rdf;
20  
21  import java.util.Iterator;
22  import java.util.List;
23  
24  
25  public class Resource
26  {
27      public Resource(String URI)
28      {
29          URI = Tools.correctURI(URI);
30          namespace = Tools.getNS(URI);
31          name = Tools.getName(URI);
32          attributes = new SortedAttributeList();
33      }
34  
35      public String getNS()
36      {
37          return namespace;
38      }
39  
40      public String getName()
41      {
42          return name;
43      }
44  
45      public String getURI()
46      {
47          return Tools.getURI(namespace, name);
48      }
49  
50      public boolean isEmpty()
51      {
52          return attributes.isEmpty();
53      }
54  
55      public void addAttribute(Attribute attribute)
56      {
57          attributes.add(attribute);
58      }
59  
60      public void removeAttribute(Attribute attribute)
61      {
62          for(Iterator it = attributes.iterator(); it.hasNext();)
63          {
64              Attribute a = (Attribute)it.next();
65              if(a.getID() == attribute.getID())
66              {
67                  attributes.remove(a);
68                  return;
69              }
70          }
71  
72      }
73  
74      public List getAttributes()
75      {
76          return attributes;
77      }
78  
79      public String toString()
80      {
81          return getURI();
82      }
83  
84      public boolean hasAttribute(String predicate, String object)
85      {
86          for(Iterator it = attributes.iterator(); it.hasNext();)
87          {
88              Attribute attribute = (Attribute)it.next();
89              if(predicate.equalsIgnoreCase(attribute.getPredicate()) && object.equalsIgnoreCase(attribute.getObject()))
90                  return true;
91          }
92  
93          return false;
94      }
95  
96      public List getObjects(String predicate)
97      {
98          return Tools.getSpecificObjects(this, predicate);
99      }
100 
101     public List getAttributes(String predicate)
102     {
103         return Tools.getSpecificAttributes(this, predicate);
104     }
105 
106     public String getType()
107     {
108         List types = getObjects(Definitions.RDF_TYPE);
109         if(types.size() > 0)
110             return types.get(0).toString();
111         else
112             return "";
113     }
114 
115     public List getLabel()
116     {
117         return getObjects(Definitions.RDF_LABEL);
118     }
119 
120     public List getComment()
121     {
122         return getObjects(Definitions.RDF_COMMENT);
123     }
124 
125     public List getRange()
126     {
127         return getObjects(Definitions.RDF_RANGE);
128     }
129 
130     public List getDomain()
131     {
132         return getObjects(Definitions.RDF_DOMAIN);
133     }
134 
135     public List getSubPropertyOf()
136     {
137         return getObjects(Definitions.RDF_SUBPROPERTYOF);
138     }
139 
140     public List getSubClass()
141     {
142         return getObjects(Definitions.MYRDF_SUBCLASS);
143     }
144 
145     public List getSubClassOf()
146     {
147         return getObjects(Definitions.RDF_SUBCLASSOF);
148     }
149 
150     public List getInstance()
151     {
152         return getObjects(Definitions.MYRDF_INSTANCE);
153     }
154 
155     public List getProperty()
156     {
157         return getObjects(Definitions.MYRDF_DOMAINFOR);
158     }
159 
160     public String getTitle()
161     {
162         List labels = getLabel();
163         if(!labels.isEmpty() && labels.get(0).toString().length() > 0)
164             return labels.get(0).toString();
165         else
166             return "<untitled>";
167     }
168 
169     protected String namespace;
170     protected String name;
171     protected List attributes;
172 }