- Timestamp:
- 07/04/13 20:29:50 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/communication/Request.java
r84 r96 1 1 package com.framsticks.communication; 2 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 3 6 import com.framsticks.communication.queries.*; 7 import com.framsticks.util.FramsticksException; 8 import com.framsticks.util.lang.Pair; 4 9 5 10 /** … … 8 13 public abstract class Request { 9 14 15 public abstract String getCommand(); 16 17 protected abstract StringBuilder construct(StringBuilder buffer); 18 19 20 21 10 22 public static void quoteValue(StringBuilder builder, String value) { 11 23 String quote = ((value.indexOf(' ') > 0) || (value.length() == 0) ? "\"" : ""); … … 13 25 } 14 26 15 public abstract String getCommand(); 16 17 protected abstract StringBuilder construct(StringBuilder buffer); 18 //private static Pattern queryPattern = Pattern.compile("^(\\S+)\\s+(\\S+)(?:\\s+(\\S+))?$"); 27 public static Request parse(CharSequence type, CharSequence rest) { 28 final Request request = Request.createRequestByTypeString(type.toString()); 29 request.parseRest(rest); 30 return request; 31 } 19 32 20 33 public static Request createRequestByTypeString(String type) { 21 if (type.equals("get")) { 22 return new GetRequest(); 34 switch (type) { 35 case "get": return new GetRequest(); 36 case "set": return new SetRequest(); 37 case "info": return new InfoRequest(); 38 case "call": return new CallRequest(); 39 case "reg": return new RegistrationRequest(); 40 case "use": return new UseRequest(); 41 case "version": return new VersionRequest(); 23 42 } 24 if (type.equals("set")) { 25 return new SetRequest(); 26 } 27 if (type.equals("info")) { 28 return new InfoRequest(); 29 } 30 if (type.equals("call")) { 31 return new CallRequest(); 32 } 33 if (type.equals("reg")) { 34 return new RegistrationRequest(); 35 } 36 if (type.equals("use")) { 37 return new UseRequest(); 38 } 39 if (type.equals("version")) { 40 return new VersionRequest(); 41 } 42 return null; 43 throw new FramsticksException().msg("unknown request type").arg("type", type); 43 44 } 44 45 45 public abstract void parseRest(Stringrest);46 public abstract CharSequence parseRest(CharSequence rest); 46 47 47 48 @Override 48 49 public String toString() { 49 return construct(new StringBuilder().append(getCommand())).toString();50 return getCommand() + " request"; 50 51 } 52 53 public String stringRepresentation() { 54 return construct(new StringBuilder().append(getCommand()).append(" ")).toString(); 55 } 56 57 public static final Pattern EVENT_PATTERN = Pattern.compile("^\\s*(\\S+)\\s*(\\S+)"); 58 59 public static final Pattern STRING_BREAKER_PATTERN = Pattern.compile("^\\s*(?:(?:\\\"([^\"]*)\\\")|([^ ]+))\\s*(.*)$"); 60 public static final Pattern IDENTIFIER_BREAKER_PATTERN = Pattern.compile("^\\s*([^ ]+)\\s*(.*)$"); 61 62 // public static Matcher match(Pattern pattern, CharSequence line) { 63 // Matcher matcher = pattern.matcher(line); 64 // if (!matcher.matches()) { 65 // return null; 66 // // throw new FramsticksException().msg("failed to take").arg("pattern", pattern).arg("line", line); 67 // } 68 // return matcher; 69 // } 70 71 public static Pair<CharSequence, CharSequence> takeIdentifier(CharSequence line) { 72 Matcher matcher = IDENTIFIER_BREAKER_PATTERN.matcher(line); 73 if (!matcher.matches()) { 74 return null; 75 } 76 return new Pair<CharSequence, CharSequence>(line.subSequence(matcher.start(1), matcher.end(1)), line.subSequence(matcher.start(2), matcher.end(2))); 77 } 78 79 public static CharSequence takeGroup(CharSequence input, Matcher matcher, int group) { 80 // return (matcher.start(group) == matcher.end(group)) ? null : input.subSequence(matcher.start(group), matcher.end(group)); 81 return input.subSequence(matcher.start(group), matcher.end(group)); 82 } 83 84 public static Pair<CharSequence, CharSequence> takeString(CharSequence line) { 85 Matcher matcher = STRING_BREAKER_PATTERN.matcher(line); 86 if (!matcher.matches()) { 87 return null; 88 } 89 assert ((matcher.start(1) == -1) != (matcher.start(2) == -1)); 90 return new Pair<CharSequence, CharSequence>(takeGroup(line, matcher, (matcher.start(1) != -1 ? 1 : 2)), takeGroup(line, matcher, 3)); 91 } 92 93 protected static final Pattern REQUEST_ID_BREAKER_PATTERN = Pattern.compile("^\\s*([0-9]+)\\s*(.*)$"); 94 95 protected final static Pair<Integer, CharSequence> takeRequestId(boolean withId, CharSequence line) { 96 if (withId) { 97 Matcher matcher = REQUEST_ID_BREAKER_PATTERN.matcher(line); 98 if (!matcher.matches()) { 99 return null; 100 } 101 return new Pair<Integer, CharSequence>(Integer.valueOf(takeGroup(line, matcher, 1).toString()), takeGroup(line, matcher, 2)); 102 } 103 return new Pair<Integer, CharSequence>(null, line); 104 } 105 106 public static StringBuilder quoteArgumentIfNeeded(StringBuilder builder, Object argument) { 107 String r = argument.toString(); 108 if (r.indexOf(' ') != -1) { 109 builder.append('"').append(r).append('"'); 110 } else { 111 builder.append(r); 112 } 113 return builder; 114 } 115 51 116 }
Note: See TracChangeset
for help on using the changeset viewer.