make parser static

This commit is contained in:
Mengyi Zhou 2015-07-03 14:27:09 +08:00
parent 31ca9524b3
commit aa75a82a5b
4 changed files with 28 additions and 53 deletions

View file

@ -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<String> getValues(String value);
public static List<String> getValues(String value) {
List<String> output = new ArrayList<>();
List<String> 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;
}
}

View file

@ -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;

View file

@ -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<Long> 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(),

View file

@ -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<String> getValues(String value) {
List<String> output = new ArrayList<>();
List<String> 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;
}
}