Skip to content

Commit

Permalink
Implement XML configuration loader
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Apr 29, 2018
1 parent 676e8ba commit 0d30d11
Show file tree
Hide file tree
Showing 8 changed files with 934 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Configurate
* Copyright (C) zml and Configurate contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ninja.leaping.configurate.attributed;

import ninja.leaping.configurate.ConfigurationNode;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.List;
import java.util.Map;

/**
* A configuration node that can have attributes attached to it.
*/
public interface AttributedConfigurationNode extends ConfigurationNode {

/**
* Gets the tag name of this node.
*
* @return The tag name
*/
@NonNull
String getTagName();

/**
* Sets the tag name of this node.
*
* <p>Will have no effect when called on nodes which are direct values of a
* {@link #getChildrenMap() child map}, as the corresponding key is used as
* the tag name.</p>
*
* @param name The name to set, cannot be null
* @return this
*/
@NonNull
AttributedConfigurationNode setTagName(@NonNull String name);

/**
* Adds an attribute to this node.
*
* @param name The name of the attribute
* @param value The value of the attribute
* @return this
*/
@NonNull
AttributedConfigurationNode addAttribute(@NonNull String name, @NonNull String value);

/**
* Removes an attribute from this node.
*
* @param name The name of the attribute to remove
* @return this
*/
@NonNull
AttributedConfigurationNode removeAttribute(@NonNull String name);

/**
* Sets the attributes of this node.
*
* @param attributes the attributes to set
* @return this
*/
@NonNull
AttributedConfigurationNode setAttributes(@NonNull Map<String, String> attributes);

/**
* Gets if this node has any attributes.
*
* @return true if this node has any attributes
*/
boolean hasAttributes();

/**
* Gets the value of an attribute, or null if this node doesn't have the
* given attribute.
*
* @param name The name of the attribute to get
* @return this
*/
@Nullable
String getAttribute(@NonNull String name);

/**
* Gets the attributes this node has.
*
* <p>The returned map is immutable.</p>
*
* @return The map of attributes
*/
@NonNull
Map<String, String> getAttributes();

// Methods from superclass overridden to have correct return types
@Nullable @Override AttributedConfigurationNode getParent();
@NonNull @Override List<? extends AttributedConfigurationNode> getChildrenList();
@NonNull @Override Map<Object, ? extends AttributedConfigurationNode> getChildrenMap();
@NonNull @Override AttributedConfigurationNode setValue(@Nullable Object value);
@NonNull @Override AttributedConfigurationNode mergeValuesFrom(@NonNull ConfigurationNode other);
@NonNull @Override AttributedConfigurationNode getAppendedNode();
@NonNull @Override AttributedConfigurationNode getNode(@NonNull Object... path);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/*
* Configurate
* Copyright (C) zml and Configurate contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ninja.leaping.configurate.attributed;

import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ConfigurationOptions;
import ninja.leaping.configurate.SimpleConfigurationNode;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Basic implementation of {@link AttributedConfigurationNode}.
*/
public class SimpleAttributedConfigurationNode extends SimpleConfigurationNode implements AttributedConfigurationNode {
private String tagName;
private final Map<String, String> attributes = new LinkedHashMap<>();

@NonNull
public static SimpleAttributedConfigurationNode root() {
return root("root", ConfigurationOptions.defaults());
}

@NonNull
public static SimpleAttributedConfigurationNode root(@NonNull String tagName) {
return root(tagName, ConfigurationOptions.defaults());
}

@NonNull
public static SimpleAttributedConfigurationNode root(@NonNull String tagName, @NonNull ConfigurationOptions options) {
return new SimpleAttributedConfigurationNode(tagName, null, null, options);
}

protected SimpleAttributedConfigurationNode(@NonNull String tagName, @Nullable Object path, @Nullable SimpleConfigurationNode parent, @NonNull ConfigurationOptions options) {
super(path, parent, options);
this.tagName = tagName;
}

@NonNull
@Override
public String getTagName() {
return tagName;
}

@NonNull
@Override
public SimpleAttributedConfigurationNode setTagName(@NonNull String tagName) {
if (Strings.isNullOrEmpty(tagName)) {
throw new IllegalArgumentException("Tag name cannot be null/empty");
}

this.tagName = tagName;
return this;
}

@NonNull
@Override
public SimpleAttributedConfigurationNode addAttribute(@NonNull String name, @NonNull String value) {
if (Strings.isNullOrEmpty(name)) {
throw new IllegalArgumentException("Attribute name cannot be null/empty");
}

attributes.put(name, value);
return this;
}

@NonNull
@Override
public SimpleAttributedConfigurationNode removeAttribute(@NonNull String name) {
attributes.remove(name);
return this;
}

@NonNull
@Override
public SimpleAttributedConfigurationNode setAttributes(@NonNull Map<String, String> attributes) {
for (String name : attributes.keySet()) {
if (Strings.isNullOrEmpty(name)) {
throw new IllegalArgumentException("Attribute name cannot be null/empty");
}
}

this.attributes.clear();
this.attributes.putAll(attributes);
return this;
}

@Override
public boolean hasAttributes() {
return !attributes.isEmpty();
}

@Nullable
@Override
public String getAttribute(@NonNull String name) {
return attributes.get(name);
}

@NonNull
@Override
public Map<String, String> getAttributes() {
return ImmutableMap.copyOf(attributes);
}

// Methods from superclass overridden to have correct return types

@Nullable
@Override
public SimpleAttributedConfigurationNode getParent() {
return (SimpleAttributedConfigurationNode) super.getParent();
}

@Override
protected SimpleAttributedConfigurationNode createNode(Object path) {
return new SimpleAttributedConfigurationNode("element", path, this, getOptions());
}

@NonNull
@Override
public SimpleAttributedConfigurationNode setValue(@Nullable Object value) {
if (value instanceof AttributedConfigurationNode) {
AttributedConfigurationNode node = (AttributedConfigurationNode) value;
setTagName(node.getTagName());
setAttributes(node.getAttributes());
}
return (SimpleAttributedConfigurationNode) super.setValue(value);
}

@NonNull
@Override
public SimpleAttributedConfigurationNode mergeValuesFrom(@NonNull ConfigurationNode other) {
if (other instanceof AttributedConfigurationNode) {
AttributedConfigurationNode node = (AttributedConfigurationNode) other;
setTagName(node.getTagName());
for (Map.Entry<String, String> attribute : node.getAttributes().entrySet()) {
addAttribute(attribute.getKey(), attribute.getValue());
}
}
return (SimpleAttributedConfigurationNode) super.mergeValuesFrom(other);
}

@NonNull
@Override
public SimpleAttributedConfigurationNode getNode(@NonNull Object... path) {
return (SimpleAttributedConfigurationNode) super.getNode(path);
}

@NonNull
@Override
@SuppressWarnings("unchecked")
public List<? extends SimpleAttributedConfigurationNode> getChildrenList() {
return (List<SimpleAttributedConfigurationNode>) super.getChildrenList();
}

@NonNull
@Override
@SuppressWarnings("unchecked")
public Map<Object, ? extends SimpleAttributedConfigurationNode> getChildrenMap() {
return (Map<Object, SimpleAttributedConfigurationNode>) super.getChildrenMap();
}

@NonNull
@Override
public SimpleAttributedConfigurationNode getAppendedNode() {
return (SimpleAttributedConfigurationNode) super.getAppendedNode();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SimpleAttributedConfigurationNode)) return false;
if (!super.equals(o)) return false;
SimpleAttributedConfigurationNode that = (SimpleAttributedConfigurationNode) o;
return tagName.equals(that.tagName) && attributes.equals(that.attributes);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + tagName.hashCode();
result = 31 * result + attributes.hashCode();
return result;
}

@Override
public String toString() {
return "SimpleAttributedConfigurationNode{" +
"super=" + super.toString() + ", " +
"tagName=" + tagName + ", " +
"attributes=" + attributes +
'}';
}
}
40 changes: 40 additions & 0 deletions configurate-xml/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Configurate
Copyright (C) zml and Configurate contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>ninja.leaping.configurate</groupId>
<artifactId>configurate-parent</artifactId>
<version>3.4-SNAPSHOT</version>
</parent>

<artifactId>configurate-xml</artifactId>
<name>Configurate XML</name>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>ninja.leaping.configurate</groupId>
<artifactId>configurate-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>

</project>
Loading

0 comments on commit 0d30d11

Please sign in to comment.