Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package org.genfork.quest_system.xml;
  2.  
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.genfork.quest_system.model.quests.ITargetCondition;
  5. import org.genfork.quest_system.model.quests.conditions.KillsCondition;
  6. import org.genfork.quest_system.model.quests.model.KillsModel;
  7.  
  8. import javax.xml.bind.annotation.adapters.XmlAdapter;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13.  
  14. /**
  15.  * User: <a href="http://gencloud.solutions">GenCloud</a>
  16.  * Date: 2018/04
  17.  */
  18. public class TargetConditionAdapter extends XmlAdapter<String, List<ITargetCondition>> {
  19.     @Override
  20.     public List<ITargetCondition> unmarshal(String v) throws Exception {
  21.         StringBuilder buffer = new StringBuilder(v);
  22.         Pattern pattern = Pattern.compile("\\s*?\\{([\\S; \n\t]*?)}", Pattern.DOTALL);
  23.         List<ITargetCondition> targetConditions = new ArrayList<>();
  24.         Matcher matcher = pattern.matcher(buffer);
  25.         while (matcher.find()) {
  26.             String elementBuffer = matcher.group(1);
  27.             buffer = buffer.delete(matcher.start(), matcher.end());
  28.             elementBuffer = elementBuffer.replaceAll("[\\[\\]\\n\\t ]+", "");
  29.             final String[] parts = elementBuffer.split(";");
  30.             doInitCondition(parts, targetConditions);
  31.             matcher = pattern.matcher(buffer);
  32.  
  33.         }
  34.  
  35.         return targetConditions;
  36.     }
  37.  
  38.     private void doInitCondition(String[] parts, List<ITargetCondition> targetConditions) {
  39.         for (String elems : parts) {
  40.             final String[] params = elems.split(",");
  41.             if (params[0].equals("KillsCondition")) {
  42.                 int npcId = -1;
  43.                 if (StringUtils.isNumeric(params[1]) && !params[1].equals("-")) {
  44.                     npcId = Integer.parseInt(params[1]);
  45.                 }
  46.  
  47.                 final String level = params[2];
  48.                 final int count = Integer.parseInt(params[3]);
  49.                 final KillsModel model = new KillsModel(npcId, level, count);
  50.                 final KillsCondition condition = new KillsCondition(model);
  51.                 targetConditions.add(condition);
  52.             }
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     public String marshal(List<ITargetCondition> v) throws Exception {
  58.         final StringBuilder builder = new StringBuilder();
  59.         builder.append('{');
  60.         if (v.size() > 1) {
  61.             for (ITargetCondition condition : v) {
  62.                 builder.append(condition.toString());
  63.                 builder.append(';');
  64.             }
  65.         } else {
  66.             for (ITargetCondition condition : v) {
  67.                 builder.append(condition.toString());
  68.             }
  69.         }
  70.  
  71.         return builder.toString() + "}";
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement