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

Last change on this file since 7444 was 6513, checked in by kjdon, 20 years ago

fixed a typo

  • Property svn:keywords set to Author Date Id Revision
File size: 2.4 KB
Line 
1package org.greenstone.gsdl3.util;
2
3/** utility 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 oid if oid has no parent */
30 public static String getParent(String oid) {
31 int pos = oid.lastIndexOf('.');
32 if (pos == -1) {
33 return oid;
34 }
35 return oid.substring(0, pos);
36 }
37
38 /** returns the full name - replaces all " with parent */
39 public static String translateParent(String oid, String parent) {
40 return oid.replaceAll("\"", parent);
41 }
42 /** does the opposite to translate_parent */
43 public static String shrinkParent(String oid) {
44 int pos = oid.lastIndexOf('.');
45 if (pos==-1) return oid;
46 return "\""+oid.substring(pos);
47 }
48 /** returns true if oid uses .fc, .lc, .pr, .ns, .ps .rt (root) .ss (specified sibling)*/
49 public static boolean needsTranslating(String oid) {
50 String tail = oid.substring(oid.length()-3);
51 return (tail.equals(".fc") || tail.equals(".lc") || tail.equals(".pr")
52 || tail.equals(".ns") || tail.equals(".ps") || tail.equals(".rt") || tail.equals(".ss"));
53 }
54 /** strips suffix from end */
55 public static String stripSuffix(String oid) {
56 String tail = oid.substring(oid.length()-3);
57 while (tail.equals(".fc") || tail.equals(".lc") || tail.equals(".pr")
58 || tail.equals(".ns") || tail.equals(".ps") || tail.equals(".rt") || tail.equals(".ss")) {
59 if (tail.equals(".ss")) { // have doc.sibnum.ss
60 oid = oid.substring(0, oid.length()-3);
61 int pos = oid.lastIndexOf('.');
62 //strip that too
63 oid = oid.substring(0, pos);
64 }
65 oid = oid.substring(0, oid.length()-3);
66 tail = oid.substring(oid.length()-3);
67 }
68
69 return oid;
70 }
71 /** returns true if child is a child of parent
72 an oid is not a child of itself */
73 public static boolean isChildOf(String parent, String child) {
74 if (parent.equals(child)) {
75 return false;
76 }
77 return child.startsWith(parent);
78 }
79}
Note: See TracBrowser for help on using the repository browser.