org.jdom2.Element - java examples

Here are the examples of the java api org.jdom2.Element taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

155 Examples 7

19 View Complete Implementation : Node.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static List<Node> fromChildrenOrAttrs(List<Node> nodes, Element el, String name, String... aliases) throws InvalidXMLException {
    return fromChildren(fromAttrs(new ArrayList<Node>(), el, name, aliases), el, name, aliases);
}

19 View Complete Implementation : FeatureDefinitionContext.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
/**
 * Add the given feature to the context, parsing its ID from the "id" attribute of the given
 * element, if present.
 */
public void addFeature(@Nullable Element node, FeatureDefinition definition) throws InvalidXMLException {
    addFeature(node, parseId(node), definition);
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public Filter parseRequiredFilterProperty(Element el, String name) throws InvalidXMLException {
    Filter filter = this.parseFilterProperty(el, name);
    if (filter == null)
        throw new InvalidXMLException("Missing required filter '" + name + "'", el);
    return filter;
}

19 View Complete Implementation : LegacyFilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
protected boolean isReference(Element el) {
    return el.getName().equalsIgnoreCase("filter") && el.getChildren().isEmpty() && el.getAttribute("parents") == null && el.getAttribute("name") != null;
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
@MethodParser("flying")
public FlyingFilter parseFlying(Element el) throws InvalidXMLException {
    return new FlyingFilter();
}

19 View Complete Implementation : Node.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static Node fromRequiredChildOrAttr(Element el, String name, String... aliases) throws InvalidXMLException {
    Node node = fromChildOrAttr(el, name, aliases);
    if (node == null) {
        throw new InvalidXMLException("attribute or child element '" + name + "' is required", el);
    }
    return node;
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
@MethodParser("flag-captured")
public FlagStateFilter parseFlagCaptured(Element el) throws InvalidXMLException {
    return this.parseFlagState(el, Captured.clreplaced);
}

19 View Complete Implementation : Node.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static List<Node> fromAttrs(Element el, String name, String... aliases) throws InvalidXMLException {
    return fromAttrs(new ArrayList<Node>(), el, name, aliases);
}

19 View Complete Implementation : Node.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
private static String describe(Element el) {
    return "'" + el.getName() + "' element";
}

19 View Complete Implementation : ProximityMetric.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static ProximityMetric parse(Element el, String prefix, ProximityMetric def) throws InvalidXMLException {
    if (!prefix.isEmpty())
        prefix = prefix + "-";
    return new ProximityMetric(XMLUtils.parseEnum(Node.fromAttr(el, prefix + "proximity-metric"), ProximityMetric.Type.clreplaced, "proximity metric", def.type), XMLUtils.parseBoolean(el.getAttribute(prefix + "proximity-horizontal"), def.horizontal));
}

19 View Complete Implementation : KitParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public ResetEnderPearlsKit parseEnderPearlKit(Element parent) throws InvalidXMLException {
    return XMLUtils.parseBoolean(parent.getAttribute("reset-ender-pearls"), false) ? new ResetEnderPearlsKit() : null;
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static Boolean parseBoolean(@Nullable Element el, Boolean def) throws InvalidXMLException {
    return el == null ? def : parseBoolean(new Node(el));
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public Filter parseChild(Element parent) throws InvalidXMLException {
    if (parent.getChildren().isEmpty()) {
        throw new InvalidXMLException("Expected a child filter", parent);
    } else if (parent.getChildren().size() > 1) {
        throw new InvalidXMLException("Expected only one child filter, not multiple", parent);
    }
    return this.parse(parent.getChildren().get(0));
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static Duration parseDuration(Element el, Duration def) throws InvalidXMLException {
    return parseDuration(Node.fromNullable(el), def);
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static Element getRequiredUniqueChild(Element parent, String name, String... aliases) throws InvalidXMLException {
    aliases = ArrayUtils.append(aliases, name);
    List<Element> children = new ArrayList<>();
    for (String alias : aliases) {
        children.addAll(parent.getChildren(alias));
    }
    if (children.size() > 1) {
        throw new InvalidXMLException("multiple '" + name + "' tags not allowed", parent);
    } else if (children.isEmpty()) {
        throw new InvalidXMLException("child tag '" + name + "' is required", parent);
    }
    return children.get(0);
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static List<Element> flattenElements(Element root, Set<String> parentTagNames) {
    return flattenElements(root, parentTagNames, null);
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
/**
 * Parse a piece of formatted text, which can be either plain text with legacy formatting codes,
 * or JSON chat components.
 */
public static Component parseFormattedText(Element parent, String property) throws InvalidXMLException {
    return parseFormattedText(Node.fromChildOrAttr(parent, property));
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static <T extends Enum<T>> T parseEnum(Element el, Clreplaced<T> type) throws InvalidXMLException {
    return parseEnum(new Node(el), type, type.getSimpleName());
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static Iterable<Attribute> getAttributes(Element parent, String... names) {
    final Set<String> nameSet = new HashSet<>(Arrays.asList(names));
    return Iterables.filter(parent.getAttributes(), new Predicate<Attribute>() {

        @Override
        public boolean apply(Attribute child) {
            return nameSet.contains(child.getName());
        }
    });
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
@MethodParser("spawn")
public SpawnReasonFilter parseSpawnReason(Element el) throws InvalidXMLException {
    return new SpawnReasonFilter(XMLUtils.parseEnum(new Node(el), SpawnReason.clreplaced, "spawn reason"));
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
@MethodParser("carrying")
public CarryingItemFilter parseHasItem(Element el) throws InvalidXMLException {
    return new CarryingItemFilter(context.getKitParser().parseRequiredItem(el));
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
@MethodParser("wearing")
public WearingItemFilter parseWearingItem(Element el) throws InvalidXMLException {
    return new WearingItemFilter(context.getKitParser().parseRequiredItem(el));
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static Attribute getRequiredAttribute(Element el, String name, String... aliases) throws InvalidXMLException {
    aliases = ArrayUtils.append(aliases, name);
    Attribute attr = null;
    for (String alias : aliases) {
        Attribute a = el.getAttribute(alias);
        if (a != null) {
            if (attr == null) {
                attr = a;
            } else {
                throw new InvalidXMLException("attributes '" + attr.getName() + "' and '" + alias + "' are aliases for the same thing, and cannot be combined", el);
            }
        }
    }
    if (attr == null) {
        throw new InvalidXMLException("attribute '" + name + "' is required", el);
    }
    return attr;
}

19 View Complete Implementation : KitParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
protected boolean maybeReference(Element el) {
    return "kit".equals(el.getName()) && el.getAttribute("parent") == null && el.getAttribute("parents") == null && el.getChildren().isEmpty();
}

19 View Complete Implementation : PointParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
/**
 * Parse the given element as a container for {@link PointProvider}s. The given element is not
 * itself parsed as a PointProvider, but its attributes are inherited by any contained
 * PointProviders.
 */
public List<PointProvider> parseChildren(Element el, PointProviderAttributes attributes) throws InvalidXMLException {
    return parseChildren(new ArrayList<PointProvider>(), el, attributes);
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static Clreplaced<? extends Enreplacedy> parseEnreplacedyType(Element el) throws InvalidXMLException {
    return parseEnreplacedyType(new Node(el));
}

19 View Complete Implementation : RegionParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public Region parseRequiredRegionProperty(Element rootElement, String... names) throws InvalidXMLException {
    return parseRequiredRegionProperty(rootElement, null, names);
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static <T extends Number & Comparable<T>> T parseNumber(Element el, Clreplaced<T> type, Range<T> range) throws InvalidXMLException {
    T value = parseNumber(el, type);
    if (!range.contains(value)) {
        throw new InvalidXMLException(value + " is not in the range " + range, el);
    }
    return value;
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
@MethodParser("one")
public Filter parseOne(Element el) throws InvalidXMLException {
    return new OneFilter(parseChildren(el));
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
/**
 * Generates a list of child elements from a parent / child tree.
 *
 * @param root Root element to start with.
 * @param parentTagNames Parent element names.
 * @param childTagNames Child element names. All elements returned will have one of these names.
 *     If this is null then all non-parent elements found are included as children.
 * @param minChildDepth Minimum number of parent tags that children must be wrapped in. If this is
 *     zero then children of the root tag can be included in the result, outside of any parent
 *     tag.
 * @return List of child elements in the tree, in the order they appear, with attributes inherited
 *     from all of their ancestors.
 */
public static List<Element> flattenElements(Element root, Set<String> parentTagNames, @Nullable Set<String> childTagNames, int minChildDepth) {
    // Walk the tree in-order to preserve the child ordering
    List<Element> result = Lists.newArrayList();
    for (Element child : root.getChildren()) {
        if (parentTagNames.contains(child.getName())) {
            result.addAll(flattenElements(new InheritingElement(child), parentTagNames, childTagNames, minChildDepth - 1));
        } else if (minChildDepth <= 0 && (childTagNames == null || childTagNames.contains(child.getName()))) {
            result.add(new InheritingElement(child));
        }
    }
    return result;
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
@MethodParser("sprinting")
public PlayerMovementFilter parseSprinting(Element el) throws InvalidXMLException {
    return new PlayerMovementFilter(true, false);
}

19 View Complete Implementation : Node.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static List<Node> fromChildren(Element el, String name, String... aliases) throws InvalidXMLException {
    return fromChildren(new ArrayList<Node>(), el, name, aliases);
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static <T extends Number> T parseNumber(Element el, Clreplaced<T> type) throws InvalidXMLException {
    return parseNumber(new Node(el), type);
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static Element getUniqueChild(Element parent, String name, String... aliases) throws InvalidXMLException {
    aliases = ArrayUtils.append(aliases, name);
    List<Element> children = new ArrayList<>();
    for (String alias : aliases) {
        children.addAll(parent.getChildren(alias));
    }
    if (children.size() > 1) {
        throw new InvalidXMLException("multiple '" + aliases[0] + "' tags not allowed", parent);
    }
    return children.isEmpty() ? null : children.get(0);
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static <T extends Number> T parseNumber(Element el, Clreplaced<T> type, T def) throws InvalidXMLException {
    if (el == null) {
        return def;
    } else {
        return parseNumber(el, type);
    }
}

19 View Complete Implementation : InheritingElement.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
protected int indexInParent() {
    if (indexInParent < -1) {
        final Element parent = getParentElement();
        indexInParent = parent == null ? -1 : parent.indexOf(this);
    }
    return indexInParent;
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static SingleMaterialMatcher parseMaterialPattern(Element el) throws InvalidXMLException {
    return parseMaterialPattern(new Node(el));
}

19 View Complete Implementation : ProximityMetric.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static ProximityMetric parse(Element el, ProximityMetric def) throws InvalidXMLException {
    return parse(el, "", def);
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
@MethodParser("team")
public TeamFilter parseTeam(Element el) throws InvalidXMLException {
    return new TeamFilter(Teams.getTeamRef(new Node(el), this.context));
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static String getNullableAttribute(Element el, String... attrs) {
    String text = null;
    for (String attr : attrs) {
        text = el.getAttributeValue(attr);
        if (text != null)
            break;
    }
    return text;
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
@MethodParser("structural-load")
public StructuralLoadFilter parseStructuralLoad(Element el) throws InvalidXMLException {
    return new StructuralLoadFilter(XMLUtils.parseNumber(el, Integer.clreplaced));
}

19 View Complete Implementation : FeatureDefinitionContext.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
/**
 * Add the given feature to the context with an optional ID. If no ID is given, there is no way to
 * retrieve the feature from the context. However, it can still be preplaceded to {@link #getNode} to
 * retrieve the element preplaceded here.
 */
public void addFeature(@Nullable Element node, @Nullable String id, FeatureDefinition definition) throws InvalidXMLException {
    if (definitions.add(definition)) {
        if (id != null) {
            FeatureDefinition old = byId.put(id, definition);
            if (old != null && old != definition) {
                byId.put(id, old);
                throw new InvalidXMLException("The ID '" + id + "' is already in use by a different feature", node);
            }
        }
        if (node != null) {
            definitionNodes.put(definition, node);
        }
    }
}

19 View Complete Implementation : KitParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public AttributeKit parseAttributeKit(Element el) throws InvalidXMLException {
    SetMultimap<String, AttributeModifier> modifiers = parseAttributeModifiers(el);
    attributeModifiers.addAll(modifiers.values());
    return modifiers.isEmpty() ? null : new AttributeKit(modifiers);
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static List<Element> flattenElements(Element root, String parentTagName, @Nullable String childTagName) {
    return flattenElements(root, parentTagName, childTagName, 1);
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
/**
 * Parse a piece of formatted text, which can be either plain text with legacy formatting codes,
 * or JSON chat components.
 */
public static Component parseFormattedText(Element parent, String property, Component def) throws InvalidXMLException {
    return parseFormattedText(Node.fromChildOrAttr(parent, property), def);
}

19 View Complete Implementation : Node.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
/**
 * Return a new Node wrapping the named Attribute of the given Element. If the Attribute does not
 * exist, throw an InvalidXMLException complaining about it.
 */
public static Node fromRequiredAttr(Element el, String name, String... aliases) throws InvalidXMLException {
    Node node = fromAttr(el, name, aliases);
    if (node == null) {
        throw new InvalidXMLException("attribute '" + name + "' is required", el);
    }
    return node;
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static Iterable<Element> getChildren(Element parent, String... names) {
    final Set<String> nameSet = new HashSet<>(Arrays.asList(names));
    return Iterables.filter(parent.getChildren(), new Predicate<Element>() {

        @Override
        public boolean apply(Element child) {
            return nameSet.contains(child.getName());
        }
    });
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
@MethodParser("clreplaced")
public PlayerClreplacedFilter parseClreplaced(Element el) throws InvalidXMLException {
    ClreplacedModule clreplacedes = this.context.getModule(ClreplacedModule.clreplaced);
    if (clreplacedes == null) {
        throw new InvalidXMLException("No clreplacedes defined", el);
    } else {
        PlayerClreplaced playerClreplaced = StringUtils.bestFuzzyMatch(el.getTextNormalize(), clreplacedes.getPlayerClreplacedes(), 0.9);
        if (playerClreplaced == null) {
            throw new InvalidXMLException("Could not find player-clreplaced: " + el.getTextNormalize(), el);
        } else {
            return new PlayerClreplacedFilter(playerClreplaced);
        }
    }
}

19 View Complete Implementation : XMLUtils.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
public static Pair<String, AttributeModifier> parseAttributeModifier(Element el) throws InvalidXMLException {
    String attribute = parseAttribute(new Node(el)).getName();
    double amount = parseNumber(Node.fromRequiredAttr(el, "amount"), Double.clreplaced);
    AttributeModifier.Operation operation = parseAttributeOperation(Node.fromAttr(el, "operation"), AttributeModifier.Operation.ADD_NUMBER);
    return Pair.create(attribute, new AttributeModifier("FromXML", amount, operation));
}

19 View Complete Implementation : FilterParser.java
Copyright GNU Affero General Public License v3.0
Author : Electroid
@MethodParser("flag-carried")
public FlagStateFilter parseFlagCarried(Element el) throws InvalidXMLException {
    return this.parseFlagState(el, Carried.clreplaced);
}