From aa75a82a5bfa4fae27e4a753a5e55b9ba2014ee2 Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Fri, 3 Jul 2015 14:27:09 +0800 Subject: [PATCH] make parser static --- .../zeus/service/model/PathRewriteParser.java | 30 +++++++++++-- .../model/handler/RewriteParseHandler.java | 2 - .../handler/impl/DefaultGroupValidator.java | 4 +- .../model/impl/DefaultRewriteParser.java | 45 ------------------- 4 files changed, 28 insertions(+), 53 deletions(-) delete mode 100644 src/main/java/com/ctrip/zeus/service/model/impl/DefaultRewriteParser.java diff --git a/src/main/java/com/ctrip/zeus/service/model/PathRewriteParser.java b/src/main/java/com/ctrip/zeus/service/model/PathRewriteParser.java index 83e86bf4..a61ca467 100644 --- a/src/main/java/com/ctrip/zeus/service/model/PathRewriteParser.java +++ b/src/main/java/com/ctrip/zeus/service/model/PathRewriteParser.java @@ -1,13 +1,37 @@ package com.ctrip.zeus.service.model; +import com.ctrip.zeus.service.model.handler.RewriteParseHandler; +import com.ctrip.zeus.service.model.handler.impl.ParseException; + +import java.util.ArrayList; import java.util.List; /** * Created by zhoumy on 2015/6/29. */ -public interface PathRewriteParser { +public class PathRewriteParser { - boolean validate(String value); + public static boolean validate(String value) { + return new RewriteParseHandler().validate(value.getBytes()); + } - List getValues(String value); + public static List getValues(String value) { + List output = new ArrayList<>(); + List result = new ArrayList<>(); + try { + new RewriteParseHandler().handleContent(value.getBytes(), output); + } catch (ParseException e) { + return null; + } + String oneRecord = ""; + for (int i = 0; i < output.size(); i++) { + if (i % 2 == 0) + oneRecord = "\"" + output.get(i) + "\""; + else { + oneRecord += " " + output.get(i) + ";"; + result.add(oneRecord); + } + } + return result; + } } diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/RewriteParseHandler.java b/src/main/java/com/ctrip/zeus/service/model/handler/RewriteParseHandler.java index 88536a37..fc217bfb 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/RewriteParseHandler.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/RewriteParseHandler.java @@ -4,14 +4,12 @@ package com.ctrip.zeus.service.model.handler; import com.ctrip.zeus.service.model.handler.impl.ParseException; import com.ctrip.zeus.service.model.handler.impl.ParserState; import com.ctrip.zeus.service.model.handler.impl.Tokenizer; -import org.springframework.stereotype.Component; import java.util.List; /** * Created by zhoumy on 2015/6/23. */ -@Component("rewriteParseHandler") public class RewriteParseHandler { ParserState currentState; diff --git a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java index 5bec588e..ec86678f 100644 --- a/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java +++ b/src/main/java/com/ctrip/zeus/service/model/handler/impl/DefaultGroupValidator.java @@ -24,8 +24,6 @@ public class DefaultGroupValidator implements GroupValidator { private ActiveConfService activeConfService; @Resource private SlbRepository slbRepository; - @Resource - private PathRewriteParser pathRewriteParser; @Override public void validate(Group group) throws Exception { @@ -49,7 +47,7 @@ public class DefaultGroupValidator implements GroupValidator { return false; Set virtualServerIds = new HashSet<>(); for (GroupSlb gs : group.getGroupSlbs()) { - if (gs.getRewrite() != null && !gs.getRewrite().isEmpty() && !pathRewriteParser.validate(gs.getRewrite())) { + if (gs.getRewrite() != null && !gs.getRewrite().isEmpty() && !PathRewriteParser.validate(gs.getRewrite())) { throw new ValidationException("Invalid rewrite value."); } VirtualServer vs = slbRepository.getVirtualServer(gs.getVirtualServer().getId(), diff --git a/src/main/java/com/ctrip/zeus/service/model/impl/DefaultRewriteParser.java b/src/main/java/com/ctrip/zeus/service/model/impl/DefaultRewriteParser.java deleted file mode 100644 index 850fa42a..00000000 --- a/src/main/java/com/ctrip/zeus/service/model/impl/DefaultRewriteParser.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.ctrip.zeus.service.model.impl; - -import com.ctrip.zeus.service.model.PathRewriteParser; -import com.ctrip.zeus.service.model.handler.RewriteParseHandler; -import com.ctrip.zeus.service.model.handler.impl.ParseException; -import org.springframework.stereotype.Component; - -import javax.annotation.Resource; -import java.util.ArrayList; -import java.util.List; - -/** - * Created by zhoumy on 2015/6/29. - */ -@Component("pathRewriteParser") -public class DefaultRewriteParser implements PathRewriteParser { - @Resource - private RewriteParseHandler rewriteParseHandler; - - @Override - public boolean validate(String value) { - return rewriteParseHandler.validate(value.getBytes()); - } - - @Override - public List getValues(String value) { - List output = new ArrayList<>(); - List result = new ArrayList<>(); - try { - rewriteParseHandler.handleContent(value.getBytes(), output); - } catch (ParseException e) { - return null; - } - String oneRecord = ""; - for (int i = 0; i < output.size(); i++) { - if (i % 2 == 0) - oneRecord = "\"" + output.get(i) + "\""; - else { - oneRecord += " " + output.get(i) + ";"; - result.add(oneRecord); - } - } - return result; - } -}