001 /* 002 * Copyright (C) 2012 eXo Platform SAS. 003 * 004 * This is free software; you can redistribute it and/or modify it 005 * under the terms of the GNU Lesser General Public License as 006 * published by the Free Software Foundation; either version 2.1 of 007 * the License, or (at your option) any later version. 008 * 009 * This software is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * You should have received a copy of the GNU Lesser General Public 015 * License along with this software; if not, write to the Free 016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 018 */ 019 020 package org.crsh.cli.impl; 021 022 import java.io.IOException; 023 import java.lang.reflect.UndeclaredThrowableException; 024 025 public enum Delimiter { 026 027 EMPTY(' ') { 028 @Override 029 public void escape(CharSequence s, int start, int end, Appendable appendable) throws IOException { 030 while (start < end) { 031 char c = s.charAt(start++); 032 switch (c) { 033 case ' ': 034 case '"': 035 case '\'': 036 case '\\': 037 appendable.append('\\'); 038 } 039 appendable.append(c); 040 } 041 } 042 }, 043 044 SINGLE_QUOTE('\'') { 045 @Override 046 public void escape(CharSequence s, int start, int end, Appendable appendable) throws IOException { 047 while (start < end) { 048 while (start < end) { 049 char c = s.charAt(start++); 050 switch (c) { 051 case '\'': 052 case '\\': 053 appendable.append('\\'); 054 } 055 appendable.append(c); 056 } 057 } 058 } 059 }, 060 061 DOUBLE_QUOTE('"') { 062 @Override 063 public void escape(CharSequence s, int start, int end, Appendable appendable) throws IOException { 064 while (start < end) { 065 while (start < end) { 066 char c = s.charAt(start++); 067 switch (c) { 068 case '"': 069 case '\\': 070 appendable.append('\\'); 071 } 072 appendable.append(c); 073 } 074 } 075 } 076 }; 077 078 /** . */ 079 private final char value; 080 081 Delimiter(char value) { 082 this.value = value; 083 } 084 085 public char getValue() { 086 return value; 087 } 088 089 public final String escape(CharSequence s) { 090 try { 091 StringBuilder buffer = new StringBuilder(); 092 escape(s, buffer); 093 return buffer.toString(); 094 } 095 catch (IOException e) { 096 throw new UndeclaredThrowableException(e); 097 } 098 } 099 100 public final void escape(CharSequence s, Appendable appendable) throws IOException { 101 escape(s, 0, s.length(), appendable); 102 } 103 104 public abstract void escape(CharSequence s, int start, int end, Appendable appendable) throws IOException; 105 }