001    /*
002     * Copyright (C) 2010 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.cmdline;
021    
022    import java.io.IOException;
023    
024    /**
025     * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
026     * @version $Revision$
027     */
028    public enum Delimiter {
029    
030      EMPTY(' ') {
031        @Override
032        public void escape(CharSequence s, int start, int end, Appendable appendable) throws IOException {
033          while (start < end) {
034            char c = s.charAt(start++);
035            switch (c) {
036              case ' ':
037              case '"':
038              case '\'':
039              case '\\':
040                appendable.append('\\');
041            }
042            appendable.append(c);
043          }
044        }
045      },
046    
047      SINGLE_QUOTE('\'') {
048        @Override
049        public void escape(CharSequence s, int start, int end, Appendable appendable) throws IOException {
050          while (start < end) {
051            while (start < end) {
052              char c = s.charAt(start++);
053              switch (c) {
054                case '\'':
055                case '\\':
056                  appendable.append('\\');
057              }
058              appendable.append(c);
059            }
060          }
061        }
062      },
063    
064      DOUBLE_QUOTE('"') {
065        @Override
066        public void escape(CharSequence s, int start, int end, Appendable appendable) throws IOException {
067          while (start < end) {
068            while (start < end) {
069              char c = s.charAt(start++);
070              switch (c) {
071                case '"':
072                case '\\':
073                  appendable.append('\\');
074              }
075              appendable.append(c);
076            }
077          }
078        }
079      };
080    
081      /** . */
082      private final char value;
083    
084      Delimiter(char value) {
085        this.value = value;
086      }
087    
088      public char getValue() {
089        return value;
090      }
091    
092      public final void escape(CharSequence s, Appendable appendable) throws IOException {
093        escape(s, 0, s.length(), appendable);
094      }
095    
096      public abstract void escape(CharSequence s, int start, int end, Appendable appendable) throws IOException;
097    }