KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > util > StringUtil


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.util;
14
15 /**
16  * @author hh
17  *
18  */

19 public class StringUtil {
20
21   /**
22    * put brackets around string, if not yet there
23    * @param orig
24    * @return String
25    */

26   public static String JavaDoc bracketsAround(String JavaDoc orig) {
27     if (orig.startsWith("[") && orig.endsWith("]"))
28       return orig;
29     return "[" + orig + "]";
30   }
31
32   /**
33    * split a unique name
34    * @param uniqueName
35    * @return the name parts without brackets
36    */

37   public static String JavaDoc[] splitUniqueName(String JavaDoc uniqueName) {
38     // uniqueName = [Product].[All Products].[Drink]
39
String JavaDoc str = uniqueName.trim();
40     int l2 = str.length() - 1;
41     if (str.charAt(0)!= '[' || str.charAt(l2)!= ']' )
42       return new String JavaDoc[] {uniqueName}; // should not occur
43
// remove first opening bracket and last closing bracket
44
str = str.substring(1, l2);
45     // str = Product].[All Products].[Drink
46
String JavaDoc[] nameParts = str.split("\\]\\.\\[");
47     return nameParts;
48   }
49
50   /**
51    * create unique name from String array
52    * @param strs - name parts
53    * @param n - number of name parts, all if n <= 0
54    * @return unique name
55    */

56   public static String JavaDoc createUName(String JavaDoc[] strs, int n) {
57     if (n <= 0)
58       n = strs.length;
59     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
60     for (int i = 0; i < n; i++) {
61       if (i > 0)
62         sb.append('.');
63       sb.append('[');
64       sb.append(strs[i]);
65       sb.append(']');
66     }
67     return sb.toString();
68   }
69
70   /**
71    * extract dimension (first part) from unique name
72    * @param uName - unique name
73    * @return first name part, with brackets
74    */

75   public static String JavaDoc dimFromUName(String JavaDoc uName) {
76     String JavaDoc[] strs = splitUniqueName(uName);
77     return "[" + strs[0] + "]";
78   }
79
80   /**
81    * extract parent (all exept last part) from unique name
82    * @param uName - unique name
83    * @return all name parts except last, with brackets
84    */

85   public static String JavaDoc parentFromUName(String JavaDoc uName) {
86     String JavaDoc[] strs = splitUniqueName(uName);
87     int n = strs.length;
88     if (n < 3)
89       return null; // at least 3 parts required, if a parent exists
90
return createUName(strs, n - 1);
91   }
92  
93 } // StringUtil
94
Popular Tags