Advertisement
Vaerys_Dawn

TagInterface

Oct 27th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package com.github.vaerys.interfaces;
  2.  
  3. import com.github.vaerys.commands.CommandObject;
  4. import com.github.vaerys.objects.ReplaceObject;
  5. import org.apache.commons.lang3.StringUtils;
  6.  
  7. import java.util.Arrays;
  8. import java.util.List;
  9. import java.util.regex.Pattern;
  10.  
  11. public abstract class TagInterface extends TagObject {
  12.  
  13.     public TagInterface(int priority) {
  14.         super(priority);
  15.     }
  16.  
  17.     public abstract String execute(String from, CommandObject command, String args);
  18.  
  19.     ReplaceObject executeReplace(String from, CommandObject command, String args) {
  20.         return null;
  21.     }
  22.  
  23.     public abstract String tagName();
  24.  
  25.     public abstract int argsRequired();
  26.  
  27.     public abstract String usage();
  28.  
  29.     public abstract String desc();
  30.  
  31.     private boolean cont(String from) {
  32.         if (argsRequired() > 0) {
  33.             return Pattern.compile(prefix() + "*." + suffix()).matcher(from).find();
  34.         } else {
  35.             return Pattern.compile(tagName()).matcher(from).find();
  36.         }
  37.     }
  38.  
  39.     private String contents(String from) {
  40.         if (argsRequired() > 0) {
  41.             return StringUtils.substringBetween(from, prefix(), suffix());
  42.         } else {
  43.             return "";
  44.         }
  45.     }
  46.  
  47.     private List<String> getSplit(String args) {
  48.         return Arrays.asList(args.split(splitter()));
  49.     }
  50.  
  51.     private String removeFirst(String from, String args) {
  52.         return StringUtils.replaceOnce(from, args, "");
  53.     }
  54.  
  55.     private String removeAll(String from, String args) {
  56.         return from.replace(args, "");
  57.     }
  58.  
  59.     private String replaceFirst(String from, String replace, String withThis) {
  60.         return StringUtils.replaceOnce(from, replace, withThis);
  61.     }
  62.  
  63.     private String replaceAll(String from, String replace, String withThis) {
  64.         return from.replace(replace, withThis);
  65.     }
  66.  
  67.     private String splitter() {
  68.         return ";;";
  69.     }
  70.  
  71.     private String prefix() {
  72.         if (argsRequired() == 0) {
  73.             return tagName();
  74.         } else {
  75.             return tagName() + "{";
  76.         }
  77.     }
  78.  
  79.     private String suffix() {
  80.         if (argsRequired() == 0) {
  81.             return "";
  82.         } else {
  83.             return "}";
  84.         }
  85.     }
  86.  
  87.     public String handleTag(String from, CommandObject command, String args) {
  88.         while (cont(from)) {
  89.             from = execute(from, command, args);
  90.         }
  91.         return from;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement