From 44e6fd90c9510393d6859537f43159128170d21e Mon Sep 17 00:00:00 2001 From: LexManos Date: Fri, 10 Aug 2012 00:31:39 -0700 Subject: [PATCH] Moved over Configuration --- .../minecraftforge/common}/Configuration.java | 30 +++++++++++-------- .../net/minecraftforge/common}/Property.java | 21 +++++++++++-- 2 files changed, 37 insertions(+), 14 deletions(-) rename {forge_common/net/minecraft/src/forge => common/net/minecraftforge/common}/Configuration.java (94%) rename {forge_common/net/minecraft/src/forge => common/net/minecraftforge/common}/Property.java (81%) diff --git a/forge_common/net/minecraft/src/forge/Configuration.java b/common/net/minecraftforge/common/Configuration.java similarity index 94% rename from forge_common/net/minecraft/src/forge/Configuration.java rename to common/net/minecraftforge/common/Configuration.java index 55b332b378..c89a8e87f5 100644 --- a/forge_common/net/minecraft/src/forge/Configuration.java +++ b/common/net/minecraftforge/common/Configuration.java @@ -3,7 +3,7 @@ * License v1.0. */ -package net.minecraft.src.forge; +package net.minecraftforge.common; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -16,6 +16,7 @@ import java.io.OutputStreamWriter; import java.text.DateFormat; import java.util.Collection; import java.util.Date; +import java.util.Locale; import java.util.Map; import java.util.TreeMap; @@ -41,6 +42,7 @@ public class Configuration public TreeMap blockProperties = new TreeMap(); public TreeMap itemProperties = new TreeMap(); public TreeMap generalProperties = new TreeMap(); + public static final String ALLOWED_CHARS = "._-"; /** * Create a configuration file for the file given in parameter. @@ -82,7 +84,7 @@ public class Configuration { Property property = new Property(); properties.put(key, property); - property.name = key; + property.setName(key); if (Block.blocksList[defaultId] == null && !configBlocks[defaultId]) { @@ -125,7 +127,7 @@ public class Configuration public Property getOrCreateBooleanProperty(String key, String category, boolean defaultValue) { Property prop = getOrCreateProperty(key, category, Boolean.toString(defaultValue)); - if ("true".equals(prop.value.toLowerCase()) || "false".equals(prop.value.toLowerCase())) + if ("true".equals(prop.value.toLowerCase(Locale.ENGLISH)) || "false".equals(prop.value.toLowerCase(Locale.ENGLISH))) { return prop; } @@ -138,13 +140,13 @@ public class Configuration public Property getOrCreateProperty(String key, String category, String defaultValue) { - category = category.toLowerCase(); + category = category.toLowerCase(Locale.ENGLISH); Map source = categories.get(category); - + if(source == null) { source = new TreeMap(); - categories.put(category, source); + categories.put(category, source); } if (source.containsKey(key)) @@ -156,7 +158,7 @@ public class Configuration Property property = new Property(); source.put(key, property); - property.name = key; + property.setName(key); property.value = defaultValue; return property; @@ -204,7 +206,7 @@ public class Configuration for (int i = 0; i < line.length() && !skip; ++i) { - if (Character.isLetterOrDigit(line.charAt(i)) || line.charAt(i) == '.') + if (Character.isLetterOrDigit(line.charAt(i)) || ALLOWED_CHARS.indexOf(line.charAt(i)) != -1) { if (nameStart == -1) { @@ -224,6 +226,7 @@ public class Configuration case '#': skip = true; continue; + case '{': String scopeName = line.substring(nameStart, nameEnd + 1); @@ -231,13 +234,15 @@ public class Configuration if (currentMap == null) { currentMap = new TreeMap(); - categories.put(scopeName, currentMap); + categories.put(scopeName, currentMap); } break; + case '}': currentMap = null; break; + case '=': String propertyName = line.substring(nameStart, nameEnd + 1); @@ -247,13 +252,14 @@ public class Configuration } Property prop = new Property(); - prop.name = propertyName; + prop.setName(propertyName); prop.value = line.substring(i + 1); i = line.length(); currentMap.put(propertyName, prop); break; + default: throw new RuntimeException("unknown character " + line.charAt(i)); } @@ -300,7 +306,7 @@ public class Configuration buffer.write("# Configuration file\r\n"); buffer.write("# Generated on " + DateFormat.getInstance().format(new Date()) + "\r\n"); buffer.write("\r\n"); - + for(Map.Entry> category : categories.entrySet()) { buffer.write("####################\r\n"); @@ -331,7 +337,7 @@ public class Configuration buffer.write(" # " + property.comment + "\r\n"); } - buffer.write(" " + property.name + "=" + property.value); + buffer.write(" " + property.getName() + "=" + property.value); buffer.write("\r\n"); } } diff --git a/forge_common/net/minecraft/src/forge/Property.java b/common/net/minecraftforge/common/Property.java similarity index 81% rename from forge_common/net/minecraft/src/forge/Property.java rename to common/net/minecraftforge/common/Property.java index 471f47b5fa..745ad6a0d3 100644 --- a/forge_common/net/minecraft/src/forge/Property.java +++ b/common/net/minecraftforge/common/Property.java @@ -3,11 +3,11 @@ * License v1.0. */ -package net.minecraft.src.forge; +package net.minecraftforge.common; public class Property { - public String name; + private String name; public String value; public String comment; @@ -87,4 +87,21 @@ public class Property { return ("true".equals(value.toLowerCase()) || "false".equals(value.toLowerCase())); } + + public String getName() + { + return name; + } + + public void setName(String name) + { + for (char c : name.toCharArray()) + { + if (!Character.isLetterOrDigit(c) && Configuration.ALLOWED_CHARS.indexOf(c) == -1) + { + throw new IllegalArgumentException("Invalid property name: \"" + name + "\""); + } + } + this.name = name; + } }