View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2009 Ordina and committers to Mod4j
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *     Jos Warmer & Anneke Kleppe - initial implementation taken from Octopus
10   *     Ordina - initial implementation
11   *******************************************************************************/
12  package org.mod4j.dslcommon.generator.helpers;
13  
14  /**
15   * 
16   * @author anneke
17   * @version $Id: StringHelpers.java,v 1.4 2005/01/21 15:08:21 anneke Exp $
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       * replaces 'origSub' with 'newSub' in the String 'orig'.
48       * 
49       * @param orig
50       * @param origSub
51       * @param newSub
52       * @return String
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(); // remove all whitespace from begin and end
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(); // remove all whitespace from begin and end
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      * @param innerBody
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      * Add brackets ( '(' and ')' ) around <code>source</code> when it contains any white space characters.
136      * 
137      * @param source
138      * @return
139      */
140     static public String addBrackets(String source) {
141         String temp = source;
142         temp = temp.trim(); // remove all whitespace from begin and end
143         if (temp.charAt(0) == '(' && temp.charAt(temp.length() - 1) == ')') {
144             return source;
145         } else if (source.indexOf(' ') != -1) { // string contains a space character
146             source = "(" + source + ")";
147         }
148         return source;
149     }
150 
151     /**
152      * @param toBeCalled
153      * @return
154      */
155     public static String removeSurroundingQuotes(String source) {
156         if (source.charAt(0) == '\'' || source.charAt(0) == '\"') { // string starts with a quote character
157             source = source.substring(1, source.length());
158             if (source.charAt(source.length() - 1) == '\'' || source.charAt(source.length() - 1) == '\"') { // string
159                 // ends with
160                 // a quote
161                 // character
162                 source = source.substring(0, source.length() - 1);
163             }
164         }
165         return source;
166     }
167 
168 }