001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.base;
019:
020: /**
021: * Helper class for component path conversions. Logically belongs to
022: * {@link ContelligentComponent}, but has been encapsulated here. <br>
023: * This class is used to implement component path related helper methods like
024: * converting a server component path (with trailing slash) to a client
025: * component path (w/o. trailing slash) or extracting the component name and the
026: * component dir from a given component path...
027: */
028: final public class ComponentPath {
029:
030: public final static char SEPARATOR = '/';
031:
032: /**
033: * Extract path in client fashion.
034: *
035: * @param path
036: * the full qulified path
037: * @return component's dir in server fashion, <code>null</code> path is
038: * <code>null</code> or root path if path is empty or root path.
039: */
040: public static String toComponentPath(String path) {
041: return ComponentPath.toClientComponentPath(path);
042: }
043:
044: /**
045: * Extract path in client fashion.
046: *
047: * @param path
048: * the full qulified path
049: * @return component's dir in server fashion, <code>null</code> path is
050: * <code>null</code> or root path if path is empty or root path.
051: */
052: public static String toClientComponentPath(String path) {
053:
054: if (path == null) {
055: return null;
056: } else if (path.equals("")
057: || path.equals(String.valueOf(ComponentPath.SEPARATOR))) {
058: return String.valueOf(ComponentPath.SEPARATOR);
059: } else {
060: // strip off following separator
061: int lastCharPos = path.length() - 1;
062: if (path.charAt(lastCharPos) == ComponentPath.SEPARATOR) {
063: return path.substring(0, lastCharPos);
064: } else {
065: return path;
066: }
067: }
068: }
069:
070: /**
071: * Extract path in server fashion.
072: *
073: * @param path
074: * the full qulified path
075: * @return component's dir in server fashion, <code>null</code> path is
076: * <code>null</code> or root path if path is empty or root path.
077: */
078: public static String toServerComponentPath(String path) {
079:
080: String clientComponentPath = ComponentPath
081: .toClientComponentPath(path);
082:
083: if (clientComponentPath == null) {
084: return null;
085: } else if (clientComponentPath.equals(String
086: .valueOf(ComponentPath.SEPARATOR))) {
087: return clientComponentPath;
088: } else {
089: return (clientComponentPath + ComponentPath.SEPARATOR);
090: }
091: }
092:
093: /**
094: * Extract dir in client fashion.
095: *
096: * @param path
097: * the full qulified path
098: * @return component's dir in server fashion, <code>null</code> path is
099: * <code>null</code> or root path if path is empty or root path.
100: */
101: public static String getComponentDir(String path) {
102: return ComponentPath.getClientComponentDir(path);
103: }
104:
105: /**
106: * Extract dir in client fashion.
107: *
108: * @param path
109: * the full qulified path
110: * @return component's dir in server fashion, <code>null</code> path is
111: * <code>null</code> or root path if path is empty or root path.
112: */
113: public static String getClientComponentDir(String path) {
114:
115: if (path == null) {
116: return null;
117: } else if (path.equals("")
118: || path.equals(String.valueOf(ComponentPath.SEPARATOR))) {
119: return String.valueOf(ComponentPath.SEPARATOR);
120: } else {
121: // first make it a client path to handle the case that a server path
122: // is passed
123: // e.g. "/path/thisIsTheComponent/" transforms to
124: // "/path/thisIsTheComponent"
125: path = ComponentPath.toComponentPath(path);
126: int lastSep = path.lastIndexOf(ComponentPath.SEPARATOR);
127: if (lastSep == -1 || lastSep == 0) {
128: return String.valueOf(ComponentPath.SEPARATOR);
129: } else {
130: return path.substring(0, lastSep);
131: }
132: }
133: }
134:
135: /**
136: * Extract dir in server fashion.
137: *
138: * @param path
139: * the full qulified path
140: * @return component's dir in server fashion, <code>null</code> path is<code>null</code>
141: * or root path if path is empty or root path.
142: */
143: public static String getServerComponentDir(String path) {
144:
145: String clientComponentDir = ComponentPath
146: .getClientComponentDir(path);
147:
148: if (clientComponentDir == null) {
149: return null;
150: } else if (clientComponentDir.equals(String
151: .valueOf(ComponentPath.SEPARATOR))) {
152: return clientComponentDir;
153: } else {
154: return (clientComponentDir + ComponentPath.SEPARATOR);
155: }
156: }
157:
158: /**
159: * Extract the component's name.
160: *
161: * @param path
162: * the full qulified path
163: * @return component's name or <code>null</code> if path contains no
164: * component
165: */
166: public static String getComponentName(String path) {
167:
168: // empty or root path
169: if (path == null || path.equals("")
170: || path.equals(String.valueOf(ComponentPath.SEPARATOR))) {
171: return null;
172: } else {
173: // first make it a client path to handle the case that a server path
174: // is passed
175: // e.g. "/path/thisIsTheComponent/" transforms to
176: // "/path/thisIsTheComponent"
177: path = ComponentPath.toComponentPath(path);
178: int lastSep = path.lastIndexOf(ComponentPath.SEPARATOR);
179: if (lastSep == -1) {
180: // as there is no separator the path is the component itself
181: return path;
182: } else {
183: return path.substring(lastSep + 1);
184: }
185: }
186: }
187: }
|