/[svn]/jlscp/trunk/src/org/linuxsampler/lscp/Parser.java
ViewVC logotype

Diff of /jlscp/trunk/src/org/linuxsampler/lscp/Parser.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1350 by iliev, Thu Sep 13 22:02:03 2007 UTC revision 1351 by iliev, Sun Sep 16 23:15:57 2007 UTC
# Line 560  public final class Parser { Line 560  public final class Parser {
560                  StringBuffer sb = new StringBuffer();                  StringBuffer sb = new StringBuffer();
561                  for(int i = 0; i < s.length(); i++) {                  for(int i = 0; i < s.length(); i++) {
562                          switch(s.charAt(i)) {                          switch(s.charAt(i)) {
563                                  case '/' : sb.append("\\/");  break;                                  case '/' : sb.append("\\x2f"); break;
564                                  case '\n': sb.append("\\n");  break;                                  case '\n': sb.append("\\n");   break;
565                                  case '\r': sb.append("\\r");  break;                                  case '\r': sb.append("\\r");   break;
566                                  case '\f': sb.append("\\f");  break;                                  case '\f': sb.append("\\f");   break;
567                                  case '\t': sb.append("\\t");  break;                                  case '\t': sb.append("\\t");   break;
568                                  case 0x0B: sb.append("\\v");  break;                                  case 0x0B: sb.append("\\v");   break;
569                                  case '\'': sb.append("\\'");  break;                                  case '\'': sb.append("\\'");   break;
570                                  case '\"': sb.append("\\\""); break;                                  case '\"': sb.append("\\\"");  break;
571                                  case '\\': sb.append("\\\\"); break;                                  case '\\': sb.append("\\\\");  break;
572                                  default  : sb.append(s.charAt(i));                                  default  : sb.append(s.charAt(i));
573                          }                          }
574                  }                  }
# Line 582  public final class Parser { Line 582  public final class Parser {
582           */           */
583          public static String          public static String
584          toNonEscapedFileName(Object obj) {          toNonEscapedFileName(Object obj) {
585                  String s = obj.toString();                  return toNonEscapedString(obj);
                 StringBuffer sb = new StringBuffer();  
                 for(int i = 0; i < s.length(); i++) {  
                         char c = s.charAt(i);  
                         if(c == '\\') {  
                                 if(i >= s.length()) {  
                                         Client.getLogger().info("Broken escape sequence");  
                                         break;  
                                 }  
                                 char c2 = s.charAt(++i);  
                                 if(c2 == '\'')      sb.append('\'');  
                                 else if(c2 == '"')  sb.append('"');  
                                 else if(c2 == '\\') sb.append('\\');  
                                 else if(c2 == 'r')  sb.append('\r');  
                                 else if(c2 == 'n')  sb.append('\n');  
                                 else if(c2 == '/') sb.append('/');  
                                 else Client.getLogger().info("Unknown escape sequence \\" + c2);  
                         } else {  
                                 sb.append(c);  
                         }  
                 }  
                   
                 return sb.toString();  
586          }          }
587                    
588          /**          /**
# Line 612  public final class Parser { Line 590  public final class Parser {
590           * @return The provided text with removed escape sequences.           * @return The provided text with removed escape sequences.
591           */           */
592          public static String          public static String
593          toNonEscapedText(Object obj) {          toNonEscapedString(Object obj) {
594                  String s = obj.toString();                  String s = obj.toString();
595                  StringBuffer sb = new StringBuffer();                  StringBuffer sb = new StringBuffer();
596                  for(int i = 0; i < s.length(); i++) {                  for(int i = 0; i < s.length(); i++) {
597                          char c = s.charAt(i);                          char c = s.charAt(i);
598                          if(c == '\\') {                          if(c == '\\') {
599                                  if(i >= s.length()) {                                  if(i >= s.length()) {
600                                          Client.getLogger().info("Broken escape sequence");                                          Client.getLogger().info("Broken escape sequence!");
601                                          break;                                          break;
602                                  }                                  }
603                                  char c2 = s.charAt(++i);                                  char c2 = s.charAt(++i);
# Line 628  public final class Parser { Line 606  public final class Parser {
606                                  else if(c2 == '\\') sb.append('\\');                                  else if(c2 == '\\') sb.append('\\');
607                                  else if(c2 == 'r')  sb.append('\r');                                  else if(c2 == 'r')  sb.append('\r');
608                                  else if(c2 == 'n')  sb.append('\n');                                  else if(c2 == 'n')  sb.append('\n');
609                                  else Client.getLogger().info("Unknown escape sequence \\" + c2);                                  else if(c2 == 'f')  sb.append('\f');
610                                    else if(c2 == 't')  sb.append('\t');
611                                    else if(c2 == 'v')  sb.append((char)0x0B);
612                                    else if(c2 == 'x') {
613                                            Character ch = getHexEscapeSequence(s, i + 1);
614                                            if(ch != null) sb.append(ch.charValue());
615                                            i += 2;
616                                    } else if(c2 >= '0' && c2 <= '9') {
617                                            Character ch = getOctEscapeSequence(s, i);
618                                            if(ch != null) sb.append(ch.charValue());
619                                            i += 2;
620                                    } else Client.getLogger().info("Unknown escape sequence \\" + c2);
621                          } else {                          } else {
622                                  sb.append(c);                                  sb.append(c);
623                          }                          }
# Line 637  public final class Parser { Line 626  public final class Parser {
626                  return sb.toString();                  return sb.toString();
627          }          }
628                    
629            private static Character
630            getHexEscapeSequence(String s, int index) {
631                    Character c = null;
632                    
633                    if(index + 1 >= s.length()) {
634                            Client.getLogger().info("Broken escape sequence");
635                            return c;
636                    }
637                    
638                    try { c = (char)Integer.parseInt(s.substring(index, index + 2), 16); }
639                    catch(Exception x) { Client.getLogger().info("Broken escape sequence!"); }
640                    
641                    return c;
642            }
643            
644            private static Character
645            getOctEscapeSequence(String s, int index) {
646                    Character c = null;
647                    
648                    if(index + 2 >= s.length()) {
649                            Client.getLogger().info("Broken escape sequence");
650                            return c;
651                    }
652                    
653                    try { c = (char)Integer.parseInt(s.substring(index, index + 3), 8); }
654                    catch(Exception x) { Client.getLogger().info("Broken escape sequence!"); }
655                    
656                    return c;
657            }
658            
659          /**          /**
660           * Determines whether the character at the specified position           * Determines whether the character at the specified position
661           * is escaped with backslash.           * is escaped with backslash.

Legend:
Removed from v.1350  
changed lines
  Added in v.1351

  ViewVC Help
Powered by ViewVC