source: trunk/gsdl3/src/java/org/greenstone/gsdl3/util/OID.java@ 3308

Last change on this file since 3308 was 3284, checked in by kjdon, 22 years ago

more functionality added along with Test classes

  • Property svn:keywords set to Author Date Id Revision
File size: 2.2 KB
Line 
1package org.greenstone.gsdl3.util;
2
3/** utitility class to handle greenstone OIDs
4 *
5 * based around OIDtools.h from gsdl
6 */
7public class OID {
8
9 /** returns everything up to the first dot
10 if no dot, returns oid */
11 public static String getTop(String oid) {
12 int pos = oid.indexOf('.');
13 if (pos == -1) {
14 return oid;
15 }
16 return oid.substring(0, pos);
17
18 }
19 /** returns true is oid is top level (ie has no dots)
20 returns false for an empty oid */
21 public static boolean isTop(String oid) {
22 if (oid.equals("")) {
23 return false;
24 }
25 return (oid.indexOf('.')==-1);
26 }
27
28 /** returns the parent of oid (everything up to last dot)
29 returns "" if oid has no parent */
30 public static String getParent(String oid) {
31 int pos = oid.lastIndexOf('.');
32 if (pos == -1) {
33 return "";
34 }
35 return oid.substring(0, pos);
36 }
37
38 /** returns the full name - replaces " with parent */
39 public static String translateParent(String oid, String parent) {
40 // this is the behaviour of the original - should only replace first
41 //perhaps?
42 return oid.replaceAll("\"", parent);
43 }
44 /** does the opposite to translate_parent */
45 public static String shrinkParent(String oid) {
46 int pos = oid.lastIndexOf('.');
47 if (pos==-1) return oid;
48 return "\""+oid.substring(pos);
49 }
50 /** returns true if oid uses .fc, .lc, .pr, .ns, .ps */
51 public static boolean needsTranslating(String oid) {
52 String tail = oid.substring(oid.length()-3);
53 return (tail.equals(".fc") || tail.equals(".lc") || tail.equals(".pr")
54 || tail.equals(".ns") || tail.equals(".ps"));
55 }
56 /** strips suffix from end */
57 public static String stripSuffix(String oid) {
58 String tail = oid.substring(oid.length()-3);
59 while (tail.equals(".fc") || tail.equals(".lc") || tail.equals(".pr")
60 || tail.equals(".ns") || tail.equals(".ps")) {
61 oid = oid.substring(0, oid.length()-3);
62 tail = oid.substring(oid.length()-3);
63 }
64 return oid;
65 }
66 /** returns true if child is a child of parent
67 an oid is not a child of itself */
68 public static boolean isChildOf(String parent, String child) {
69 if (parent.equals(child)) {
70 return false;
71 }
72 return child.startsWith(parent);
73 }
74}
Note: See TracBrowser for help on using the repository browser.