01: /*
02: * Copyright 2006 Pentaho Corporation. All rights reserved.
03: * This software was developed by Pentaho Corporation and is provided under the terms
04: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
05: * this file except in compliance with the license. If you need a copy of the license,
06: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
07: * BI Platform. The Initial Developer is Pentaho Corporation.
08: *
09: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11: * the license for the specific language governing your rights and limitations.
12: *
13: * @created Jun 21, 2005
14: * @author James Dixon
15: *
16: */
17:
18: package org.pentaho.util;
19:
20: import java.util.ArrayList;
21: import java.util.List;
22: import java.util.StringTokenizer;
23: import java.util.regex.Matcher;
24: import java.util.regex.Pattern;
25:
26: public class StringUtil {
27:
28: private StringUtil() {
29: super ();
30: // No need for a constructor since it should be all static utils
31: }
32:
33: // match "../stuff", "stuff/../stuff", "stuff/..", and same string with "\" instead of "/"
34: private static final String RE_CONTAINS_PARENT_PATH = "(^.*[/\\\\]|^)\\.\\.([/\\\\].*$|$)"; //$NON-NLS-1$
35: private static final Pattern CONTAINS_PARENT_PATH_PATTERN = Pattern
36: .compile(StringUtil.RE_CONTAINS_PARENT_PATH);
37:
38: /**
39: * Tokenize a string and return a String Array of the values seperated by the passed in token.
40: *
41: * @param tokenizedString The string to parse. If this is null, null will be returned.
42: * @param token The token used as a seperator. if this is null, the fill string will be returned in a 1 element array
43: * @return Array of Strings that were seperated by the token.
44: */
45: public static String[] tokenStringToArray(String tokenizedString,
46: String token) {
47: if (tokenizedString == null) {
48: return (null);
49: }
50: if (token == null) {
51: return (new String[] { tokenizedString });
52: }
53:
54: StringTokenizer st = new StringTokenizer(tokenizedString, token);
55: List strList = new ArrayList();
56: while (st.hasMoreTokens()) {
57: String tok = st.nextToken();
58: strList.add(tok);
59: }
60:
61: String[] rtnArray = new String[strList.size()];
62: return ((String[]) strList.toArray(rtnArray));
63: }
64:
65: /**
66: * Does the path contain a path-segment that is "..".
67: * @param path String
68: * @return boolean return true if path contains "..", else false.
69: */
70: public static boolean doesPathContainParentPathSegment(String path) {
71: Matcher m = CONTAINS_PARENT_PATH_PATTERN.matcher(path);
72: return m.matches();
73: }
74:
75: public static void main(String[] args) {
76: String[] testStrings = { "../bart/maggie.xml", "/bart/..",
77: "/bart/../maggie/homer.xml", "..//bart/maggie.xml",
78: "/bart//..", "/bart//../maggie/homer.xml", "/../",
79: "/..", "../", "..\\bart\\maggie.xml", "\\bart\\..",
80: "\\bart\\..\\maggie\\homer.xml", "I am clean",
81: "/go/not/parent.xml", "\\go\\not\\parent.xml", "..",
82: "should/not..not/match", "..should/not/match.xml",
83: "should/not..", "..." };
84:
85: for (int ii = 0; ii < testStrings.length; ++ii) {
86: String testString = testStrings[ii];
87: boolean matches = StringUtil
88: .doesPathContainParentPathSegment(testString);
89: if (matches) {
90: System.out.println(testString + " matches.");
91: } else {
92: System.out.println("--------" + testString
93: + " DOES NOT MATCH.");
94: }
95: }
96: }
97: }
|