1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package helper;
29 
30 public class StringHelper
31 {
32 
33     public static String doubleQuote(String _sStr)
34         {
35             return "\"" + _sStr + "\"";
36         }
37 
38     public static String singleQuote(String _sStr)
39         {
40             return "'" + _sStr + "'";
41         }
42 
43     /**
44      * removes quotes if both exists at start and at end
45      */
46     public static String removeSurroundQuoteIfExists(String _sPath)
47         {
48             String sNewPath = _sPath;
49             boolean bRemoveQuotes = false;
50             if (
51                 (_sPath.startsWith("\"") && _sPath.endsWith("\"")) ||
52                 (_sPath.startsWith("'") && _sPath.endsWith("'"))
53                 )
54             {
55                 // remove trailing quotes, if exists
56                 sNewPath = sNewPath.substring(1);
57 
58                 // remove trailing quotes, if exists
59                 sNewPath = sNewPath.substring(0, sNewPath.length() - 1);
60             }
61             return sNewPath;
62         }
63 
64     public static String removeQuoteIfExists(String _sPath)
65         {
66             String sNewPath = _sPath;
67 
68             if (_sPath.startsWith("\"") ||
69                 _sPath.startsWith("'"))
70             {
71                 // remove trailing quotes, if exists
72                 sNewPath = sNewPath.substring(1);
73             }
74 
75             if (_sPath.endsWith("\"") ||
76                 _sPath.endsWith("'"))
77             {
78                 // remove trailing quotes, if exists
79                 sNewPath = sNewPath.substring(0, sNewPath.length() - 1);
80             }
81             return sNewPath;
82         }
83 
84     public static String doubleQuoteIfNeed(String _sStr)
85         {
86             if (_sStr.startsWith("\"") && _sStr.endsWith("\""))
87             {
88                 // don't quote twice
89                 return _sStr;
90             }
91             if (_sStr.indexOf(" ") == -1)
92             {
93                 // don't quote, if there is no space in name
94                 return _sStr;
95             }
96             if (_sStr.indexOf("%") != -1)
97             {
98                 return singleQuote(_sStr);
99             }
100 
101             return doubleQuote(_sStr);
102         }
103 
104     /**
105      * Convert a value to a string with a given length, if the len is greater the len of the value string representation
106      * fill it's front with '0'
107      * So ("5", 4) will result in a string "0005"
108      * @param _nValue
109      * @param _nLen
110      * @return
111      */
112     public static String createValueString(int _nValue, int _nLen)
113         {
114             String sValue = String.valueOf(_nValue);
115             StringBuffer a = new StringBuffer();
116             while (_nLen > sValue.length())
117             {
118                 a.append('0');
119                 _nLen --;
120             }
121             a.append(sValue);
122             return a.toString();
123         }
124 
125 }
126