1
2
3
4
5
6
7
8
9
10
11
12 package org.mod4j.dslcommon.generator.helpers;
13
14
15
16
17
18
19 public class StringHelpers {
20
21 private StringHelpers() {
22 }
23
24 final static public String newLine = System.getProperty("line.separator", "\n");
25
26 final static public char newLineChar = newLine.charAt(0);
27
28 static public StringBuffer replaceAllSubstrings(StringBuffer orig, String origSub, String newSub) {
29 StringBuffer result = new StringBuffer();
30 result.append(replaceAllSubstrings(orig.toString(), origSub, newSub));
31 return result;
32 }
33
34 static public String replaceAllSubstrings(String orig, String origSub, String newSub) {
35 int first = orig.indexOf(origSub);
36 if (first == -1) {
37 return orig;
38 }
39 int last = first + origSub.length();
40 String result = orig.substring(0, first);
41 String rest = orig.substring(last);
42 result = result + newSub + replaceAllSubstrings(rest, origSub, newSub);
43 return result;
44 }
45
46
47
48
49
50
51
52
53
54 static public String replaceFirstSubstring(String orig, String origSub, String newSub) {
55 int first = orig.indexOf(origSub);
56 if (first == -1) {
57 return orig;
58 }
59 int last = first + origSub.length();
60 String result = orig.substring(0, first);
61 result = result + newSub + orig.substring(last);
62 return result;
63 }
64
65 static public String firstCharToUpper(String orig) {
66 String result = "";
67 String origFirst = orig.substring(0, 1);
68 result = origFirst.toUpperCase();
69 result = result + orig.substring(1, orig.length());
70 return result;
71 }
72
73 static public String firstCharToLower(String orig) {
74 String result = "";
75 String origFirst = orig.substring(0, 1);
76 result = origFirst.toLowerCase();
77 result = result + orig.substring(1, orig.length());
78 return result;
79 }
80
81 static public String indent(String in, int level) {
82 StringBuffer result = new StringBuffer();
83 String newIndent = "";
84 for (int i = 0; i < level; i++) {
85 newIndent = newIndent + "\t";
86 }
87 String temp = in.toString();
88 temp = temp.trim();
89 temp = newIndent + StringHelpers.replaceAllSubstrings(temp, "\n", "\n" + newIndent);
90 result.append(temp);
91 if (result.charAt(result.length() - 1) == '\t') {
92 result.deleteCharAt(result.length() - 1);
93 }
94 return result.toString();
95 }
96
97 static public StringBuffer indent(StringBuffer in, int level) {
98 StringBuffer result = new StringBuffer();
99 String newIndent = "";
100 for (int i = 0; i < level; i++) {
101 newIndent = newIndent + "\t";
102 }
103 String temp = in.toString();
104 temp = temp.trim();
105 temp = newIndent + StringHelpers.replaceAllSubstrings(temp, "\n", "\n" + newIndent);
106 result.append(temp);
107 if (result.charAt(result.length() - 1) == '\t') {
108 result.deleteCharAt(result.length() - 1);
109 }
110 return result;
111 }
112
113 public static String replaceLastSubstring(String orig, String origSub, String newSub) {
114 int first = orig.lastIndexOf(origSub);
115 if (first == -1) {
116 return orig;
117 }
118 int last = first + origSub.length();
119 String result = orig.substring(0, first);
120 result = result + newSub + orig.substring(last);
121 return result;
122 }
123
124
125
126
127 public static void trimTrailingWhiteSpace(StringBuffer innerBody) {
128 while (Character.isWhitespace(innerBody.charAt(innerBody.length() - 1))) {
129 innerBody.deleteCharAt(innerBody.length() - 1);
130 }
131
132 }
133
134
135
136
137
138
139
140 static public String addBrackets(String source) {
141 String temp = source;
142 temp = temp.trim();
143 if (temp.charAt(0) == '(' && temp.charAt(temp.length() - 1) == ')') {
144 return source;
145 } else if (source.indexOf(' ') != -1) {
146 source = "(" + source + ")";
147 }
148 return source;
149 }
150
151
152
153
154
155 public static String removeSurroundingQuotes(String source) {
156 if (source.charAt(0) == '\'' || source.charAt(0) == '\"') {
157 source = source.substring(1, source.length());
158 if (source.charAt(source.length() - 1) == '\'' || source.charAt(source.length() - 1) == '\"') {
159
160
161
162 source = source.substring(0, source.length() - 1);
163 }
164 }
165 return source;
166 }
167
168 }