View Javadoc

1   /*
2    *  $Id: Tools.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.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  
26  public abstract class Tools 
27  {
28      public static boolean isURI(String ID)
29      {
30          return ID.indexOf("://") >= 0 && getSeparatorPos(ID) >= 0;
31      }
32  
33      public static String getNS(String URI)
34      {
35          int sep = getSeparatorPos(URI);
36          if(sep >= 0)
37              return URI.substring(0, sep + 1);
38          else
39              return "";
40      }
41  
42      public static String getName(String URI)
43      {
44          return URI.substring(getSeparatorPos(URI) + 1);
45      }
46  
47      private static int getSeparatorPos(String URI)
48      {
49          int sep = URI.lastIndexOf('#');
50          if(sep < 0)
51          {
52              sep = URI.lastIndexOf('/');
53              if(sep > 0 && URI.charAt(sep - 1) == '/')
54                  sep = -1;
55          }
56          return sep;
57      }
58  
59      public static String getURI(String ns, String name)
60      {
61          return String.valueOf(ns) + String.valueOf(name);
62      }
63  
64      public static String correctURI(String URI)
65      {
66          if(URI == null)
67              return "";
68          else
69              return URI;
70      }
71  
72      public static String URItoID(String URI)
73      {
74          return URI.replace('#', '*');
75      }
76  
77      public static String IDtoURI(String ID)
78      {
79          return ID.replace('*', '#');
80      }
81  
82      public static boolean isSameNS(String ns, String URI)
83      {
84          return ns.equals(getNS(URI));
85      }
86  
87      public static List getSpecificObjects(Resource resource, String predicate)
88      {
89          List specObjects = new ArrayList();
90          Iterator it = resource.getAttributes().iterator();
91          do
92          {
93              if(!it.hasNext())
94                  break;
95              Attribute attribute = (Attribute)it.next();
96              if(predicate.equals(attribute.getPredicate()))
97                  specObjects.add(attribute.getObject());
98          } while(true);
99          return specObjects;
100     }
101 
102     public static List getSpecificAttributes(Resource resource, String predicate)
103     {
104         List specAttributes = new ArrayList();
105         Iterator it = resource.getAttributes().iterator();
106         do
107         {
108             if(!it.hasNext())
109                 break;
110             Attribute attribute = (Attribute)it.next();
111             if(predicate.equals(attribute.getPredicate()))
112                 specAttributes.add(attribute);
113         } while(true);
114         return specAttributes;
115     }
116 }