|
| 1 | +package in.twizmwaz.cardinal.module; |
| 2 | + |
| 3 | +import in.twizmwaz.cardinal.match.Match; |
| 4 | +import in.twizmwaz.cardinal.module.id.IdModule; |
| 5 | +import in.twizmwaz.cardinal.module.repository.LoadedMap; |
| 6 | +import in.twizmwaz.cardinal.util.ParseUtil; |
| 7 | +import in.twizmwaz.cardinal.util.document.XML; |
| 8 | +import lombok.Getter; |
| 9 | +import lombok.NonNull; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import org.jdom2.Attribute; |
| 12 | +import org.jdom2.Element; |
| 13 | + |
| 14 | +@RequiredArgsConstructor |
| 15 | +public class ModuleReporter { |
| 16 | + |
| 17 | + private final Module module; |
| 18 | + private final LoadedMap map; |
| 19 | + @Getter |
| 20 | + private final Match match; |
| 21 | + |
| 22 | + public ModuleReporter(Module module, Match match) { |
| 23 | + this(module, match.getMap(), match); |
| 24 | + } |
| 25 | + |
| 26 | + @Getter |
| 27 | + private boolean canLoad = false; |
| 28 | + |
| 29 | + public void reset() { |
| 30 | + canLoad = true; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Gets an attribute value from an element. If it fails to get a value, en error will be thrown. |
| 35 | + * @param element The element to get the attribute from. |
| 36 | + * @param attr The attribute to get. |
| 37 | + * @param type The class of the type to convert the attribute to. |
| 38 | + * @param <T> The type to convert the attribute value to. |
| 39 | + * @return An object of the correct type, or null if argument is missing or invalid. |
| 40 | + */ |
| 41 | + public <T> T getAttr(Element element, String attr, Class<T> type) { |
| 42 | + return getAttr(element, attr, type, null, true); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Gets an attribute value from an element. Defaults to fallback if not found. |
| 47 | + * @param element The element to get the attribute from. |
| 48 | + * @param attr The attribute to get. |
| 49 | + * @param type The class of the type to convert the attribute to. |
| 50 | + * @param fallback The fallback in case it's null or an error is thrown, null can be used as fallback. |
| 51 | + * @param <T> The type to convert the attribute value to. |
| 52 | + * @return An object of the correct type, or null if argument is missing or invalid. |
| 53 | + */ |
| 54 | + public <T> T getAttr(Element element, String attr, Class<T> type, T fallback) { |
| 55 | + return getAttr(element, attr, type, fallback, false); |
| 56 | + } |
| 57 | + |
| 58 | + private <T> T getAttr(Element element, String attr, Class<T> type, T fallback, boolean required) { |
| 59 | + Attribute attribute = element.getAttribute(attr); |
| 60 | + if (required && attribute == null) { |
| 61 | + module.getErrors().add(new ModuleError(module, map, element, "Missing required attribute:'" + attr + "'", false)); |
| 62 | + return null; |
| 63 | + } |
| 64 | + T result = getAttribute(attribute, type); |
| 65 | + if (required && result == null) { |
| 66 | + canLoad = false; |
| 67 | + } |
| 68 | + return result != null ? result : fallback; |
| 69 | + } |
| 70 | + |
| 71 | + private <T> T getAttribute(Attribute attr, Class<T> type) { |
| 72 | + try { |
| 73 | + return XML.getAttribute(attr, type); |
| 74 | + } catch (Exception e) { |
| 75 | + module.getErrors().add(new ModuleError(module, map, attr, e.getMessage(), false)); |
| 76 | + } |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Gets an attribute value from an element. If it fails to get a value, en error will be thrown. |
| 82 | + * @param element The element to get the object from. |
| 83 | + * @param type The class of the type to convert the element to. |
| 84 | + * @param <T> The type to convert the attribute value to. |
| 85 | + * @return An object of the correct type, or null if argument is missing or invalid. |
| 86 | + */ |
| 87 | + public <T> T getEl(Element element, Class<T> type) { |
| 88 | + return getEl(element, type, null, true); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Gets an attribute value from an element. |
| 93 | + * @param element The element to get the object from. |
| 94 | + * @param type The class of the type to convert the element to. |
| 95 | + * @param fallback The fallback in case it's null or an error is thrown, null can be used as fallback. |
| 96 | + * @param <T> The type to convert the attribute value to. |
| 97 | + * @return An object of the correct type, or null if argument is missing or invalid. |
| 98 | + */ |
| 99 | + public <T> T getEl(@NonNull Element element, Class<T> type, T fallback) { |
| 100 | + return getEl(element, type, fallback, false); |
| 101 | + } |
| 102 | + |
| 103 | + private <T> T getEl(@NonNull Element element, Class<T> type, T fallback, boolean required) { |
| 104 | + T result = getElement(element, type); |
| 105 | + if (required && result == null) { |
| 106 | + canLoad = false; |
| 107 | + } |
| 108 | + return result != null ? result : fallback; |
| 109 | + } |
| 110 | + |
| 111 | + private <T> T getElement(Element el, Class<T> type) { |
| 112 | + try { |
| 113 | + return XML.getElementObject(el, type); |
| 114 | + } catch (Exception e) { |
| 115 | + module.getErrors().add(new ModuleError(module, map, el, e.getMessage(), false)); |
| 116 | + } |
| 117 | + return null; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Gets a property value from an element. It will try to get the object from the attribute, |
| 122 | + * if null, from child element. |
| 123 | + * @param element The element to get the property from. |
| 124 | + * @param prop The property to get. |
| 125 | + * @param type The class of the type to convert the property to. |
| 126 | + * @param <T> The type to convert the attribute value to. |
| 127 | + * @return An object of the correct type, or null if argument is missing or invalid. |
| 128 | + */ |
| 129 | + public <T> T getProp(Element element, String prop, Class<T> type) { |
| 130 | + return getProp(element, prop, type, null, true); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Gets a property value from an element. It will try to get the object from the attribute, |
| 135 | + * if missing, from child element. |
| 136 | + * @param element The element to get the property from. |
| 137 | + * @param prop The property to get. |
| 138 | + * @param type The class of the type to convert the property to. |
| 139 | + * @param fallback The fallback in case it's null or an error is thrown, null can be used as fallback. |
| 140 | + * @param <T> The type to convert the attribute value to. |
| 141 | + * @return An object of the correct type, or null if argument is missing or invalid. |
| 142 | + */ |
| 143 | + public <T> T getProp(Element element, String prop, Class<T> type, T fallback) { |
| 144 | + return getProp(element, prop, type, fallback, false); |
| 145 | + } |
| 146 | + |
| 147 | + private <T> T getProp(Element element, String prop, Class<T> type, T fallback, boolean required) { |
| 148 | + T result; |
| 149 | + Attribute attribute = element.getAttribute(prop); |
| 150 | + Element child = element.getChild(prop); |
| 151 | + if (attribute == null && child == null && required) { |
| 152 | + module.getErrors().add(new ModuleError(module, map, element, "Missing required property:'" + prop + "'", false)); |
| 153 | + return null; |
| 154 | + } |
| 155 | + if (attribute != null) { |
| 156 | + result = getAttribute(attribute, type); |
| 157 | + } else { |
| 158 | + result = getElement(element, type); |
| 159 | + } |
| 160 | + if (required && result == null) { |
| 161 | + canLoad = false; |
| 162 | + } |
| 163 | + return result != null ? result : fallback; |
| 164 | + } |
| 165 | + |
| 166 | + public void checkId(Attribute attribute, String id) { |
| 167 | + if (!IdModule.get().canAdd(match, id)) { |
| 168 | + module.getErrors().add(new ModuleError(module, map, attribute, "Invalid or duplicated ID specified", false)); |
| 169 | + canLoad = false; |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments