View Javadoc

1   /////////////////////////////////////////////////////////////////////
2   //
3   // © University of Southampton IT Innovation Centre, 2008
4   //
5   // Copyright in this software belongs to the IT Innovation Centre of
6   // 2 Venture Road, Chilworth Science Park, Southampton SO16 7NP, UK.
7   //
8   // This software may not be used, sold, licensed, transferred, copied
9   // or reproduced in whole or in part in any manner or form or in or
10  // on any media by any person other than in accordance with the terms
11  // of the Licence Agreement supplied with the software, or otherwise
12  // without the prior written consent of the copyright owners.
13  //
14  // This software is distributed WITHOUT ANY WARRANTY, without even the
15  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  // PURPOSE, except where stated in the Licence Agreement supplied with
17  // the software.
18  //
19  //      Created By :            Nikolaos Matskanis
20  //      Created Date :          08/01/2008
21  //      Created for Project :   NextGRID
22  //
23  /////////////////////////////////////////////////////////////////////////
24  //
25  //      Dependencies : none
26  //
27  /////////////////////////////////////////////////////////////////////////
28  //
29  //      Last commit info:       $Author: Nodens2k $
30  //                              $Date: 2008-01-24 19:56:06 +0100 (jue, 24 ene 2008) $
31  //                              $Revision: 158 $
32  //
33  /////////////////////////////////////////////////////////////////////////
34  
35  package  uk.ac.soton.itinnovation.utils;
36  
37  
38  public class URIUtils { 
39          
40          public static boolean isNameStartChar(char ch) { 
41                  return (Character.isLetter(ch) || ch == '_'); 
42          } 
43          
44          public static boolean isNameChar(char ch) { 
45                  return (isNameStartChar(ch) || Character.isDigit(ch) || ch == '.' || ch == '-'); 
46          }
47  
48          public static String getLocalName( String uri ) {
49                  int index = splitPos( uri );
50  
51                  if( index == -1 ) return null;
52                  return uri.substring( index + 1 );
53          } 
54          
55          public static boolean relaxedMatch( String uri1, String uri2 ) {
56                   if( uri1 == null || uri2 == null ) return false;
57                   if( uri1.equals( uri2 ) ) return true;
58  
59                   String name1 = getLocalName( uri1 );
60                   String name2 = getLocalName( uri2 );
61  
62                   if( name1 == null || name2 == null ) return false;
63                   return name1.equals( name2 );
64          }
65  
66          private static int splitPos( String uri ) {
67                  int pos = uri.indexOf( "#" );
68                  if( pos == -1 ) pos = findLastNameIndex( uri ) - 1;
69                  return pos;
70          }
71  
72         public static  int findLastNameIndex(String str) {
73                 char[] strChars = str.toCharArray();
74                 int nameIndex = -1;
75  
76                 for(int strIndex = strChars.length - 1; strIndex >= 0; strIndex--) {
77                         char letter = strChars[strIndex];
78                         if(isNameChar(letter)) {
79                                 nameIndex = strIndex;
80                         } else {
81                                 break;
82                         }
83                 }
84                 return nameIndex;
85         }
86  }
87  
88