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 | import org.apache.log4j.*;
|
---|
22 |
|
---|
23 | /** utility class to handle greenstone OIDs
|
---|
24 | *
|
---|
25 | * based around OIDtools.h from gsdl
|
---|
26 | */
|
---|
27 | public class OID {
|
---|
28 |
|
---|
29 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.OID.class.getName());
|
---|
30 |
|
---|
31 | public interface OIDTranslatable {
|
---|
32 | public String processOID(String doc_id, String top, String suffix, int sibling_num);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /** returns everything up to the first dot
|
---|
36 | if no dot, returns oid */
|
---|
37 | public static String getTop(String oid) {
|
---|
38 | int pos = oid.indexOf('.');
|
---|
39 | if (pos == -1) {
|
---|
40 | return oid;
|
---|
41 | }
|
---|
42 | return oid.substring(0, pos);
|
---|
43 |
|
---|
44 | }
|
---|
45 | /** returns true is oid is top level (ie has no dots)
|
---|
46 | returns false for an empty oid */
|
---|
47 | public static boolean isTop(String oid) {
|
---|
48 | if (oid.equals("")) {
|
---|
49 | return false;
|
---|
50 | }
|
---|
51 | return (oid.indexOf('.')==-1);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /** returns the parent of oid (everything up to last dot)
|
---|
55 | returns oid if oid has no parent */
|
---|
56 | public static String getParent(String oid) {
|
---|
57 | int pos = oid.lastIndexOf('.');
|
---|
58 | if (pos == -1) {
|
---|
59 | return oid;
|
---|
60 | }
|
---|
61 | return oid.substring(0, pos);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /** returns the full name - replaces all " with parent */
|
---|
65 | public static String translateParent(String oid, String parent) {
|
---|
66 | return oid.replaceAll("\"", parent);
|
---|
67 | }
|
---|
68 | /** does the opposite to translate_parent */
|
---|
69 | public static String shrinkParent(String oid) {
|
---|
70 | int pos = oid.lastIndexOf('.');
|
---|
71 | if (pos==-1) return oid;
|
---|
72 | return "\""+oid.substring(pos);
|
---|
73 | }
|
---|
74 | /** returns true if oid uses .fc, .lc, .pr, .ns, .ps .rt (root) .ss (specified sibling)*/
|
---|
75 | public static boolean needsTranslating(String oid) {
|
---|
76 | if (oid.length()<4) return false;
|
---|
77 | String tail = oid.substring(oid.length()-3);
|
---|
78 | return (tail.equals(".fc") || tail.equals(".lc") ||
|
---|
79 | tail.equals(".pr") || tail.equals(".ns") ||
|
---|
80 | tail.equals(".ps") || tail.equals(".rt") ||
|
---|
81 | tail.equals(".ss") || tail.equals(".np") ||
|
---|
82 | tail.equals(".pp"));
|
---|
83 | }
|
---|
84 | /** strips suffix from end */
|
---|
85 | public static String stripSuffix(String oid) {
|
---|
86 | String tail = oid.substring(oid.length()-3);
|
---|
87 | while (tail.equals(".fc") || tail.equals(".lc") ||
|
---|
88 | tail.equals(".pr") || tail.equals(".ns") ||
|
---|
89 | tail.equals(".ps") || tail.equals(".ss") ||
|
---|
90 | tail.equals(".np") || tail.equals(".pp") ||
|
---|
91 | tail.equals(".rt") ) {
|
---|
92 | if (tail.equals(".ss")) { // have doc.sibnum.ss
|
---|
93 | oid = oid.substring(0, oid.length()-3);
|
---|
94 | int pos = oid.lastIndexOf('.');
|
---|
95 | //strip that too
|
---|
96 | oid = oid.substring(0, pos);
|
---|
97 | }
|
---|
98 | oid = oid.substring(0, oid.length()-3);
|
---|
99 | tail = oid.substring(oid.length()-3);
|
---|
100 | }
|
---|
101 |
|
---|
102 | return oid;
|
---|
103 | }
|
---|
104 | /** returns true if child is a child of parent
|
---|
105 | an oid is not a child of itself */
|
---|
106 | public static boolean isChildOf(String parent, String child) {
|
---|
107 | if (parent.equals(child)) {
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 | return child.startsWith(parent);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /** translates relative oids into proper oids:
|
---|
115 | * .pr (parent), .rt (root) .fc (first child), .lc (last child),
|
---|
116 | * .ns (next sibling), .ps (previous sibling)
|
---|
117 | * .np (next page), .pp (previous page) : links sections in the order that you'd read the document
|
---|
118 | * a suffix is expected to be present so test before using
|
---|
119 | * Other classes can implement the method processOID of interface OIDTranslatable
|
---|
120 | * to process the oid further to handle siblings.
|
---|
121 | */
|
---|
122 | public static String translateOID(OIDTranslatable translator, String oid) {
|
---|
123 | int p = oid.lastIndexOf('.');
|
---|
124 | if (p != oid.length()-3) {
|
---|
125 | logger.info("translateoid error: '.' is not the third to last char!!");
|
---|
126 | return oid;
|
---|
127 | }
|
---|
128 |
|
---|
129 | String top = oid.substring(0, p);
|
---|
130 | String suff = oid.substring(p+1);
|
---|
131 |
|
---|
132 | // just in case we have multiple extensions, we must translate
|
---|
133 | // we process inner ones first
|
---|
134 | if (OID.needsTranslating(top)) {
|
---|
135 | top = OID.translateOID(translator, top);
|
---|
136 | }
|
---|
137 | if (suff.equals("pr")) {
|
---|
138 | return OID.getParent(top);
|
---|
139 | }
|
---|
140 | if (suff.equals("rt")) {
|
---|
141 | return OID.getTop(top);
|
---|
142 | }
|
---|
143 | if (suff.equals("np")) {
|
---|
144 | // try first child
|
---|
145 |
|
---|
146 | String node_id = OID.translateOID(translator, top+".fc");
|
---|
147 | if (!node_id.equals(top)) {
|
---|
148 | return node_id;
|
---|
149 | }
|
---|
150 |
|
---|
151 | // try next sibling
|
---|
152 | node_id = OID.translateOID(translator, top+".ns");
|
---|
153 | if (!node_id.equals(top)) {
|
---|
154 | return node_id;
|
---|
155 | }
|
---|
156 | // otherwise we keep trying parents sibling
|
---|
157 | String child_id = top;
|
---|
158 | String parent_id = OID.getParent(child_id);
|
---|
159 | while(!parent_id.equals(child_id)) {
|
---|
160 | node_id = OID.translateOID(translator, parent_id+".ns");
|
---|
161 | if (!node_id.equals(parent_id)) {
|
---|
162 | return node_id;
|
---|
163 | }
|
---|
164 | child_id = parent_id;
|
---|
165 | parent_id = OID.getParent(child_id);
|
---|
166 | }
|
---|
167 | return top; // we couldn't get a next page, so just return the original
|
---|
168 | }
|
---|
169 | if (suff.equals("pp")) {
|
---|
170 | String prev_sib = OID.translateOID(translator, top+".ps");
|
---|
171 | if (prev_sib.equals(top)) {
|
---|
172 | // no previous sibling, so return the parent
|
---|
173 | return OID.getParent(top);
|
---|
174 | }
|
---|
175 | // there is a previous sibling, so it's either this section, or the last child of the last child
|
---|
176 | String last_child = OID.translateOID(translator, prev_sib+".lc");
|
---|
177 | while (!last_child.equals(prev_sib)) {
|
---|
178 | prev_sib = last_child;
|
---|
179 | last_child = OID.translateOID(translator, prev_sib+".lc");
|
---|
180 | }
|
---|
181 | return last_child;
|
---|
182 | }
|
---|
183 |
|
---|
184 | int sibling_num = 0;
|
---|
185 | if (suff.equals("ss")) {
|
---|
186 | // we have to remove the sib num before we get top
|
---|
187 | p = top.lastIndexOf('.');
|
---|
188 | sibling_num = Integer.parseInt(top.substring(p+1));
|
---|
189 | top = top.substring(0, p);
|
---|
190 | }
|
---|
191 |
|
---|
192 | // need to get info out of Fedora
|
---|
193 | String doc_id = top;
|
---|
194 | if (suff.endsWith("s")) {
|
---|
195 | doc_id = OID.getParent(top);
|
---|
196 | if (doc_id.equals(top)) {
|
---|
197 | // i.e. we are already at the top
|
---|
198 | return top;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | return translator.processOID(doc_id, top, suff, sibling_num);
|
---|
203 |
|
---|
204 | }
|
---|
205 | }
|
---|