1 | /*
|
---|
2 | * OID.java
|
---|
3 | * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
|
---|
4 | *
|
---|
5 | * This program is free software; you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
18 | */
|
---|
19 | package org.greenstone.gsdl3.util;
|
---|
20 |
|
---|
21 | /** utility class to handle greenstone OIDs
|
---|
22 | *
|
---|
23 | * based around OIDtools.h from gsdl
|
---|
24 | */
|
---|
25 | public class OID {
|
---|
26 |
|
---|
27 | /** returns everything up to the first dot
|
---|
28 | if no dot, returns oid */
|
---|
29 | public static String getTop(String oid) {
|
---|
30 | int pos = oid.indexOf('.');
|
---|
31 | if (pos == -1) {
|
---|
32 | return oid;
|
---|
33 | }
|
---|
34 | return oid.substring(0, pos);
|
---|
35 |
|
---|
36 | }
|
---|
37 | /** returns true is oid is top level (ie has no dots)
|
---|
38 | returns false for an empty oid */
|
---|
39 | public static boolean isTop(String oid) {
|
---|
40 | if (oid.equals("")) {
|
---|
41 | return false;
|
---|
42 | }
|
---|
43 | return (oid.indexOf('.')==-1);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /** returns the parent of oid (everything up to last dot)
|
---|
47 | returns oid if oid has no parent */
|
---|
48 | public static String getParent(String oid) {
|
---|
49 | int pos = oid.lastIndexOf('.');
|
---|
50 | if (pos == -1) {
|
---|
51 | return oid;
|
---|
52 | }
|
---|
53 | return oid.substring(0, pos);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /** returns the full name - replaces all " with parent */
|
---|
57 | public static String translateParent(String oid, String parent) {
|
---|
58 | return oid.replaceAll("\"", parent);
|
---|
59 | }
|
---|
60 | /** does the opposite to translate_parent */
|
---|
61 | public static String shrinkParent(String oid) {
|
---|
62 | int pos = oid.lastIndexOf('.');
|
---|
63 | if (pos==-1) return oid;
|
---|
64 | return "\""+oid.substring(pos);
|
---|
65 | }
|
---|
66 | /** returns true if oid uses .fc, .lc, .pr, .ns, .ps .rt (root) .ss (specified sibling)*/
|
---|
67 | public static boolean needsTranslating(String oid) {
|
---|
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 | }
|
---|
76 | /** strips suffix from end */
|
---|
77 | public static String stripSuffix(String oid) {
|
---|
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 | }
|
---|
96 | /** returns true if child is a child of parent
|
---|
97 | an oid is not a child of itself */
|
---|
98 | public static boolean isChildOf(String parent, String child) {
|
---|
99 | if (parent.equals(child)) {
|
---|
100 | return false;
|
---|
101 | }
|
---|
102 | return child.startsWith(parent);
|
---|
103 | }
|
---|
104 | }
|
---|