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 Jan 30, 2018
1 parent cb6aa62 commit f3b7800
Show file tree
Hide file tree
Showing 7 changed files with 624 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* 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 java.util.List;
import java.util.Map;

/**
* A configuration node that is capable of having attributes
*/
public interface AttributedConfigurationNode extends ConfigurationNode {

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

/**
* Sets the tag name of this node.
*
* <p>Will have no effect when called on nodes within a
* {@link #getChildrenMap() children map}.</p>
*
* @param name the name to set, cannot be null
* @return this
*/
AttributedConfigurationNode setTagName(String name);

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

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

/**
* Sets the attributes of this node
*
* @param attributes the attributes to set
* @return this
*/
AttributedConfigurationNode setAttributes(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
*/
String getAttribute(String name);

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

// Methods from superclass overridden to have correct return types

AttributedConfigurationNode getParent();
@Override
List<? extends AttributedConfigurationNode> getChildrenList();
@Override
Map<Object, ? extends AttributedConfigurationNode> getChildrenMap();
@Override
AttributedConfigurationNode setValue(Object value);
@Override
AttributedConfigurationNode mergeValuesFrom(ConfigurationNode other);
@Override
AttributedConfigurationNode getAppendedNode();
@Override
AttributedConfigurationNode getNode(Object... path);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
/**
* 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 java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Represents a configuration node containing comments
*/
public class SimpleAttributedConfigurationNode extends SimpleConfigurationNode implements AttributedConfigurationNode {
private String tagName;
private final Map<String, String> attributes = new HashMap<>();

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

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

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

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

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

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

this.tagName = tagName;
return this;
}

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

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

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

@Override
public SimpleAttributedConfigurationNode setAttributes(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();
}

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

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

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

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

@Override
public SimpleAttributedConfigurationNode setValue(Object value) {
if (value instanceof AttributedConfigurationNode && ((AttributedConfigurationNode) value).hasAttributes()) {
setAttributes(((AttributedConfigurationNode) value).getAttributes());
}
return (SimpleAttributedConfigurationNode)super.setValue(value);
}

@Override
public SimpleAttributedConfigurationNode mergeValuesFrom(ConfigurationNode other) {
if (other instanceof AttributedConfigurationNode && ((AttributedConfigurationNode) other).hasAttributes()) {
Map<String, String> attributes = ((AttributedConfigurationNode) other).getAttributes();
setAttributes(attributes);
}
return (SimpleAttributedConfigurationNode) super.mergeValuesFrom(other);
}

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

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

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

@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;
if (!attributes.equals(that.attributes)) return false;

return true;
}

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

@Override
public String toString() {
return "SimpleAttributedConfigurationNode{" +
"super=" + super.toString() + ", " +
"attributes=" + attributes +
'}';
}
}
41 changes: 41 additions & 0 deletions configurate-xml/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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">
<parent>
<groupId>ninja.leaping.configurate</groupId>
<artifactId>configurate-parent</artifactId>
<version>3.4-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

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

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

</project>
Loading

0 comments on commit f3b7800

Please sign in to comment.