-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuotedString.java
More file actions
87 lines (75 loc) · 2.59 KB
/
QuotedString.java
File metadata and controls
87 lines (75 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* \file QuotedString.java
* \brief EO sample
*/
/**
* Notice the benefits I gained:
*
* - The object is lazy — it doesn’t perform any calculations if not needed.
* - It is easy to test.
* - It is small, so it is easy to maintain in the future.
* - It is easy to use in collections. You can wrap multiple items in a collection with the object,
* but in the end use just a few of them. This way you will not perform heavy calculations
* on the entire collection if not needed.
* - It is immutable.
* - It is cheap to create. Java is designed to create new objects.
*/
/**
* Put quotes around the given String if necessary.
* <p>
* If the argument doesn't include spaces or quotes, return it as is. If it
* contains double quotes, use single quotes - else surround the argument by
* double quotes.
* </p>
* <p>
* Copied from Apache Commons Exec: http://commons.apache.org/proper/commons-exec/
*/
class QuotedString
{
private static final String SINGLE_QUOTE = "\'";
private static final String DOUBLE_QUOTE = "\"";
private final String argument;
public QuotedString(String argument)
{
this.argument = argument;
}
public String toString()
{
String cleanedArgument = argument.trim();
// strip the quotes from both ends
while (cleanedArgument.startsWith(SINGLE_QUOTE) ||
cleanedArgument.startsWith(DOUBLE_QUOTE))
{
cleanedArgument = cleanedArgument.substring(1);
}
while (cleanedArgument.endsWith(SINGLE_QUOTE) ||
cleanedArgument.endsWith(DOUBLE_QUOTE))
{
cleanedArgument = cleanedArgument.substring(0, cleanedArgument.length() - 1);
}
final StringBuilder buf = new StringBuilder();
if (cleanedArgument.contains(DOUBLE_QUOTE)) {
if (cleanedArgument.contains(SINGLE_QUOTE)) {
throw new IllegalArgumentException("Can't handle single and double quotes in same argument");
}
return buf
.append(SINGLE_QUOTE)
.append(cleanedArgument)
.append(SINGLE_QUOTE)
.toString();
}
else if (cleanedArgument.contains(SINGLE_QUOTE) ||
cleanedArgument.contains(" "))
{
return buf
.append(DOUBLE_QUOTE)
.append(cleanedArgument)
.append(DOUBLE_QUOTE)
.toString();
}
else {
return cleanedArgument;
}
}
}
//--------------------------------------------------------------------------------------------------