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

Last change on this file since 14641 was 11277, checked in by kjdon, 18 years ago

pp and np are now valid extensions that need translating (previous and next page as if we are reading through the document)

  • Property svn:keywords set to Author Date Id Revision
File size: 2.6 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 if (oid.length()<4) return false;
51 String tail = oid.substring(oid.length()-3);
52 return (tail.equals(".fc") || tail.equals(".lc") ||
53 tail.equals(".pr") || tail.equals(".ns") ||
54 tail.equals(".ps") || tail.equals(".rt") ||
55 tail.equals(".ss") || tail.equals(".np") ||
56 tail.equals(".pp"));
57 }
58 /** strips suffix from end */
59 public static String stripSuffix(String oid) {
60 String tail = oid.substring(oid.length()-3);
61 while (tail.equals(".fc") || tail.equals(".lc") ||
62 tail.equals(".pr") || tail.equals(".ns") ||
63 tail.equals(".ps") || tail.equals(".ss") ||
64 tail.equals(".np") || tail.equals(".pp") ||
65 tail.equals(".rt") ) {
66 if (tail.equals(".ss")) { // have doc.sibnum.ss
67 oid = oid.substring(0, oid.length()-3);
68 int pos = oid.lastIndexOf('.');
69 //strip that too
70 oid = oid.substring(0, pos);
71 }
72 oid = oid.substring(0, oid.length()-3);
73 tail = oid.substring(oid.length()-3);
74 }
75
76 return oid;
77 }
78 /** returns true if child is a child of parent
79 an oid is not a child of itself */
80 public static boolean isChildOf(String parent, String child) {
81 if (parent.equals(child)) {
82 return false;
83 }
84 return child.startsWith(parent);
85 }
86}
Note: See TracBrowser for help on using the repository browser.