[3235] | 1 | /*
|
---|
| 2 | * GSPath.java
|
---|
| 3 | * Copyright (C) 2002 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 | */
|
---|
[3222] | 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 22 | * GSPath - utility class for greenstone
|
---|
| 23 | *
|
---|
| 24 | * modifies and examines message address paths and names
|
---|
| 25 | *
|
---|
[25727] | 26 | * @author Katherine Don
|
---|
[3235] | 27 | * @version $Revision: 25727 $
|
---|
| 28 | *
|
---|
[3222] | 29 | */
|
---|
| 30 |
|
---|
| 31 | public class GSPath {
|
---|
| 32 |
|
---|
| 33 | /** adds new_link to the end of path
|
---|
| 34 | *
|
---|
| 35 | * @return the new path
|
---|
| 36 | */
|
---|
| 37 | static public String appendLink(String path, String new_link) {
|
---|
| 38 | if (path.equals("")) {
|
---|
| 39 | return new_link;
|
---|
| 40 | }
|
---|
| 41 | return path +"/"+new_link;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | /** adds new_link to the front of path
|
---|
| 45 | *
|
---|
| 46 | * @return the new path
|
---|
| 47 | */
|
---|
| 48 | static public String prependLink(String path, String new_link) {
|
---|
| 49 | if (path.equals("")) {
|
---|
| 50 | return new_link;
|
---|
| 51 | }
|
---|
| 52 | return new_link+"/"+path;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[9434] | 55 | /** replaces the first link in the path with the specified name
|
---|
| 56 | */
|
---|
| 57 | static public String replaceFirstLink(String path, String new_first_link) {
|
---|
| 58 | path = removeFirstLink(path);
|
---|
| 59 | path = prependLink(path, new_first_link);
|
---|
| 60 | return path;
|
---|
| 61 |
|
---|
| 62 | }
|
---|
[3222] | 63 | /** returns the first name in the path
|
---|
| 64 | *
|
---|
| 65 | */
|
---|
| 66 | static public String getFirstLink(String path) {
|
---|
| 67 |
|
---|
| 68 | int i = path.indexOf("/");
|
---|
| 69 | if (i==0) { // have a '/' on the front
|
---|
| 70 | path = path.substring(1);
|
---|
| 71 | i= path.indexOf("/");
|
---|
| 72 | }
|
---|
| 73 | if (i==-1) { // '/' not found
|
---|
| 74 | return path;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return path.substring(0,i);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /** removes the first name in the path
|
---|
| 81 | *
|
---|
| 82 | * @return the modified path
|
---|
| 83 | */
|
---|
| 84 | static public String removeFirstLink(String path) {
|
---|
| 85 | int i = path.indexOf("/");
|
---|
| 86 | if (i==0) { // have a '/' on the front
|
---|
| 87 | path = path.substring(1);
|
---|
| 88 | i= path.indexOf("/");
|
---|
| 89 | }
|
---|
| 90 | if (i==-1) { // '/' not found - remove the whole path
|
---|
| 91 | return "";
|
---|
| 92 | }
|
---|
| 93 | return path.substring(i+1); // remove the '/' char
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[5186] | 96 | /** removes the last name in the path
|
---|
| 97 | *
|
---|
| 98 | * @return the modified path
|
---|
| 99 | */
|
---|
| 100 | static public String removeLastLink(String path) {
|
---|
| 101 | int i = path.lastIndexOf("/");
|
---|
| 102 | if (i==-1) { // '/' not found - remove the whole path
|
---|
| 103 | return "";
|
---|
| 104 | }
|
---|
| 105 | return path.substring(0, i); // remove the '/' char
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | static public String getLastLink(String path) {
|
---|
| 109 | int i = path.lastIndexOf("/");
|
---|
[13977] | 110 | if (i==-1) { // '/' not found - return the whole path
|
---|
[5186] | 111 | return path;
|
---|
| 112 | }
|
---|
| 113 | return path.substring(i+1); // remove the last link
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
[4020] | 117 | static public String createPath(String [] links) {
|
---|
| 118 | String path = links[0];
|
---|
| 119 | for (int i=1; i<links.length; i++) {
|
---|
| 120 | path = appendLink(path, links[i]);
|
---|
| 121 | }
|
---|
| 122 | return path;
|
---|
| 123 | }
|
---|
[9912] | 124 |
|
---|
| 125 | static public int getIndex(String link) {
|
---|
| 126 | int i = link.indexOf("[");
|
---|
| 127 | if (i==-1) {
|
---|
| 128 | return -1;
|
---|
| 129 | }
|
---|
| 130 | int j = link.indexOf("]");
|
---|
| 131 | String num = link.substring(i+1,j);
|
---|
| 132 | try {
|
---|
| 133 | int index = Integer.parseInt(num);
|
---|
| 134 | return index;
|
---|
| 135 | } catch (Exception e) {
|
---|
| 136 | return -1;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | static public String removeIndex(String link) {
|
---|
| 141 | int i = link.indexOf("[");
|
---|
| 142 | if (i==-1) {
|
---|
| 143 | return link;
|
---|
| 144 | }
|
---|
| 145 | return link.substring(0, i);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[3222] | 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|