import java.util.*; /** * Provides one or two static methods to put some * semantic sugar on the meal of dealing with text * which may or may not have quote characters * surrounding it. * * @author http://bumble.sf.net * @see TextTool */ public class QuoteText extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private String text; //-------------------------------------------- public QuoteText() { this.text = ""; } //-------------------------------------------- public QuoteText(String sText) { this.text = sText; } //-------------------------------------------- /** determines if a character is a quote */ public static boolean isQuote(char cTest) { char cSingleQuote = '\''; char cDoubleQuote = '"'; if ((cTest == cSingleQuote) || (cTest == cDoubleQuote)) { return true; } else { return false; } } //-------------------------------------------- /** removes the quotes from around a string, or * nothing if there arent any */ public static String popQuotes(String sText) { String sReturn; if (!QuoteText.isQuoted(sText)) { return sText; } sReturn = sText.trim().substring(1, sText.trim().length() - 1); return sReturn; } //-- method: popQuotes //-------------------------------------------- /** determines if some text has quotes around it ignoring * leading and trailing space. The quotes do not have * to match */ public static boolean isQuoted(String sText) { char cFirst; char cLast; if (sText.trim().length() < 2) { return false; } cFirst = sText.trim().charAt(0); if (!QuoteText.isQuote(cFirst)) { return false; } cLast = sText.trim().charAt(sText.trim().length() - 1); if (!QuoteText.isQuote(cLast)) { return false; } return true; } //-- method: isQuoted //-------------------------------------------- /** determines if some text has quotes around it ignoring * leading and trailing space. The quotes do have * to match */ public static boolean isQuotedStrict(String sText) { char cFirst; char cLast; if (sText.trim().length() < 2) { return false; } cFirst = sText.trim().charAt(0); if (!QuoteText.isQuote(cFirst)) { return false; } cLast = sText.trim().charAt(sText.trim().length() - 1); if (!QuoteText.isQuote(cLast)) { return false; } if (cFirst != cLast) { return false; } return true; } //-- method: isQuotedStrict //-------------------------------------------- /** a main method for testing */ public static void main(String[] args) throws Exception { StringBuffer sbUsageMessage = new StringBuffer(""); sbUsageMessage.append("test usage: java TextTool text"); sbUsageMessage.append(NEWLINE); StringBuffer sbMessage = new StringBuffer(""); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } String[] ssTest = {"why", "Here", "now", "NO"}; String sCheck = args[0]; System.out.println("Using list:"); for (int ii = 0; ii < ssTest.length; ii++) { System.out.print(ssTest[ii] + ", "); } System.out.println(""); System.out.println("Test String: " + sCheck); System.out.print(".isQuoted() >"); System.out.println(QuoteText.isQuoted(sCheck)); System.out.print(".isQuotedStrict() >"); System.out.println(QuoteText.isQuotedStrict(sCheck)); System.out.print(".popQuotes() >"); System.out.println(QuoteText.popQuotes(sCheck)); } //-- main() } //-- QuoteText class