Changeset 18503
- Timestamp:
- 2009-02-11T15:44:57+13:00 (15 years ago)
- Location:
- greenstone3/trunk/src/java/org/greenstone/gsdl3/util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
greenstone3/trunk/src/java/org/greenstone/gsdl3/util/OID.java
r16869 r18503 26 26 27 27 /** returns everything up to the first dot 28 28 if no dot, returns oid */ 29 29 public static String getTop(String oid) { 30 31 32 33 34 35 30 int pos = oid.indexOf('.'); 31 if (pos == -1) { 32 return oid; 33 } 34 return oid.substring(0, pos); 35 36 36 } 37 37 /** returns true is oid is top level (ie has no dots) 38 38 returns false for an empty oid */ 39 39 public static boolean isTop(String oid) { 40 41 42 43 40 if (oid.equals("")) { 41 return false; 42 } 43 return (oid.indexOf('.')==-1); 44 44 } 45 45 46 46 /** returns the parent of oid (everything up to last dot) 47 47 returns oid if oid has no parent */ 48 48 public static String getParent(String oid) { 49 50 51 52 53 49 int pos = oid.lastIndexOf('.'); 50 if (pos == -1) { 51 return oid; 52 } 53 return oid.substring(0, pos); 54 54 } 55 55 56 56 /** returns the full name - replaces all " with parent */ 57 57 public static String translateParent(String oid, String parent) { 58 58 return oid.replaceAll("\"", parent); 59 59 } 60 60 /** does the opposite to translate_parent */ 61 61 public static String shrinkParent(String oid) { 62 63 64 62 int pos = oid.lastIndexOf('.'); 63 if (pos==-1) return oid; 64 return "\""+oid.substring(pos); 65 65 } 66 66 /** returns true if oid uses .fc, .lc, .pr, .ns, .ps .rt (root) .ss (specified sibling)*/ 67 67 public static boolean needsTranslating(String oid) { 68 69 70 71 72 73 74 68 if (oid.length()<4) return false; 69 String tail = oid.substring(oid.length()-3); 70 return (tail.equals(".fc") || tail.equals(".lc") || 71 tail.equals(".pr") || tail.equals(".ns") || 72 tail.equals(".ps") || tail.equals(".rt") || 73 tail.equals(".ss") || tail.equals(".np") || 74 tail.equals(".pp")); 75 75 } 76 76 /** strips suffix from end */ 77 77 public static String stripSuffix(String oid) { 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 78 String tail = oid.substring(oid.length()-3); 79 while (tail.equals(".fc") || tail.equals(".lc") || 80 tail.equals(".pr") || tail.equals(".ns") || 81 tail.equals(".ps") || tail.equals(".ss") || 82 tail.equals(".np") || tail.equals(".pp") || 83 tail.equals(".rt") ) { 84 if (tail.equals(".ss")) { // have doc.sibnum.ss 85 oid = oid.substring(0, oid.length()-3); 86 int pos = oid.lastIndexOf('.'); 87 //strip that too 88 oid = oid.substring(0, pos); 89 } 90 oid = oid.substring(0, oid.length()-3); 91 tail = oid.substring(oid.length()-3); 92 } 93 94 return oid; 95 95 } 96 96 /** returns true if child is a child of parent 97 97 an oid is not a child of itself */ 98 98 public static boolean isChildOf(String parent, String child) { 99 100 101 102 99 if (parent.equals(child)) { 100 return false; 101 } 102 return child.startsWith(parent); 103 103 } 104 104 } -
greenstone3/trunk/src/java/org/greenstone/gsdl3/util/OIDTest.java
r16869 r18503 37 37 38 38 public OIDTest(String name) { 39 39 super(name); 40 40 } 41 41 42 42 /** test suite that dynamically runs all the tests */ 43 43 public static Test suite() { 44 44 return new TestSuite(OIDTest.class); 45 45 } 46 46 47 47 public void testGetTop() { 48 49 assertEquals(oid1, OID.getTop(oid1)); 50 assertEquals(oid1, OID.getTop(oid2)); 51 assertEquals(empty, OID.getTop(empty)); 48 assertEquals(oid1, OID.getTop(oid1)); 49 assertEquals(oid1, OID.getTop(oid2)); 50 assertEquals(empty, OID.getTop(empty)); 52 51 } 53 52 54 53 public void testIsTop() { 55 56 57 54 assertTrue(!OID.isTop(empty)); 55 assertTrue(OID.isTop(oid1)); 56 assertTrue(!OID.isTop(oid2)); 58 57 } 59 58 60 59 public void testGetParent() { 61 62 assertEquals(empty, OID.getParent(oid1)); 63 assertEquals(oid1, OID.getParent(oid2)); 64 assertEquals(empty, OID.getParent(empty)); 65 60 assertEquals(empty, OID.getParent(oid1)); 61 assertEquals(oid1, OID.getParent(oid2)); 62 assertEquals(empty, OID.getParent(empty)); 66 63 } 67 64 68 65 public void testTranslateParent() { 69 String short_id = "\".1"; 70 assertEquals(oid2, OID.translateParent(short_id, oid1)); 71 // should return the original oid if its not a shortened form 72 assertEquals(oid2, OID.translateParent(oid2, oid1)); 73 66 String short_id = "\".1"; 67 assertEquals(oid2, OID.translateParent(short_id, oid1)); 68 // should return the original oid if its not a shortened form 69 assertEquals(oid2, OID.translateParent(oid2, oid1)); 74 70 } 75 71 76 72 public void testShrinkParent() { 77 78 73 String short_oid2 = "\".1"; 74 String short_oid3 = "\".2"; 79 75 80 81 82 76 assertEquals(oid1, OID.shrinkParent(oid1)); 77 assertEquals(short_oid2, OID.shrinkParent(oid2)); 78 assertEquals(short_oid3, OID.shrinkParent(oid3)); 83 79 } 84 80 85 81 public void testNeedsTranslating() { 86 87 88 89 90 91 92 82 assertTrue(!OID.needsTranslating(oid1)); 83 assertTrue(!OID.needsTranslating(oid2)); 84 assertTrue(OID.needsTranslating(oidfc)); 85 assertTrue(OID.needsTranslating(oidlc)); 86 assertTrue(OID.needsTranslating(oidpr)); 87 assertTrue(OID.needsTranslating(oidns)); 88 assertTrue(OID.needsTranslating(oidps)); 93 89 94 90 } 95 91 96 92 public void testStripSuffix() { 97 98 99 100 101 93 assertEquals(oid1, OID.stripSuffix(oidfc)); 94 assertEquals(oid1, OID.stripSuffix(oidlc)); 95 assertEquals(oid1, OID.stripSuffix(oidpr)); 96 assertEquals(oid1, OID.stripSuffix(oidns)); 97 assertEquals(oid1, OID.stripSuffix(oidps)); 102 98 } 103 99 104 100 public void testIsChildOf() { 105 106 107 108 109 101 assertTrue(OID.isChildOf(oid1, oid2)); 102 assertTrue(OID.isChildOf(oid1, oid3)); 103 assertTrue(OID.isChildOf(oid2, oid3)); 104 assertTrue(!OID.isChildOf(oid3, oid1)); 105 assertTrue(!OID.isChildOf(oid1, oid1)); 110 106 } 111 107
Note:
See TracChangeset
for help on using the changeset viewer.