001    package net.minecraftforge.common;
002    
003    import java.io.BufferedWriter;
004    import java.io.IOException;
005    import java.util.ArrayList;
006    import java.util.Collection;
007    import java.util.HashMap;
008    
009    import com.google.common.base.Splitter;
010    
011    import static net.minecraftforge.common.Configuration.*;
012    
013    public class ConfigCategory
014    {
015        private String name;
016        private String comment;
017        private ArrayList<ConfigCategory> children = new ArrayList<ConfigCategory>();
018        private HashMap<String, Property> properties = new HashMap<String, Property>();
019        public final ConfigCategory parent;
020    
021        public ConfigCategory(String name)
022        {
023            this(name, null);
024        }
025    
026        public ConfigCategory(String name, ConfigCategory parent)
027        {
028            this.name = name;
029            this.parent = parent;
030            if (parent != null)
031            {
032                parent.children.add(this);
033            }
034        }
035    
036        public boolean equals(Object obj)
037        {
038            if (obj instanceof ConfigCategory)
039            {
040                ConfigCategory cat = (ConfigCategory)obj;
041                return name.equals(cat.name) && children.equals(cat.children);  
042            }
043            
044            return false;
045        }
046    
047        public String getQualifiedName()
048        {
049            return getQualifiedName(name, parent);
050        }
051    
052        public static String getQualifiedName(String name, ConfigCategory parent)
053        {
054            return (parent == null ? name : parent.getQualifiedName() + Configuration.CATEGORY_SPLITTER + name);
055        }
056    
057        public ConfigCategory getFirstParent()
058        {
059            return (parent == null ? this : parent.getFirstParent());
060        }
061    
062        public boolean isChild()
063        {
064            return parent != null;
065        }
066    
067        public HashMap<String, Property> getValues()
068        {
069            return properties;
070        }
071    
072        public void setComment(String comment)
073        {
074            this.comment = comment;
075        }
076    
077        public boolean containsKey(String key)
078        {
079            return properties.containsKey(key);
080        }
081    
082        public Property get(String key)
083        {
084            return properties.get(key);
085        }
086    
087        public void set(String key, Property value)
088        {
089            properties.put(key, value);
090        }
091    
092        public void write(BufferedWriter out, int indent) throws IOException
093        {
094            String pad = getIndent(indent);
095    
096            out.write(pad + "####################" + NEW_LINE);
097            out.write(pad + "# " + name + NEW_LINE);
098    
099            if (comment != null)
100            {
101                out.write(pad + "#===================" + NEW_LINE);
102                Splitter splitter = Splitter.onPattern("\r?\n");
103    
104                for (String line : splitter.split(comment))
105                {
106                    out.write(pad + "# " + line + NEW_LINE);
107                }
108            }
109    
110            out.write(pad + "####################" + NEW_LINE + NEW_LINE);
111    
112            if (!allowedProperties.matchesAllOf(name))
113            {
114                name = '"' + name + '"';
115            }
116    
117            out.write(pad + name + " {" + NEW_LINE);
118    
119            pad = getIndent(indent + 1);
120    
121            Property[] props = properties.values().toArray(new Property[properties.size()]);
122    
123            for (int x = 0; x < props.length; x++)
124            {
125                Property prop = props[x];
126    
127                if (prop.comment != null)
128                {
129                    if (x != 0)
130                    {
131                        out.newLine();
132                    }
133    
134                    Splitter splitter = Splitter.onPattern("\r?\n");
135                    for (String commentLine : splitter.split(prop.comment))
136                    {
137                        out.write(pad + "# " + commentLine + NEW_LINE);
138                    }
139                }
140    
141                String propName = prop.getName();
142    
143                if (!allowedProperties.matchesAllOf(propName))
144                {
145                    propName = '"' + propName + '"';
146                }
147    
148                if (prop.isList())
149                {
150                    out.write(String.format(pad + "%s:%s <" + NEW_LINE, prop.getType().getID(), propName));
151                    pad = getIndent(indent + 2)
152    ;
153                    for (String line : prop.valueList)
154                    {
155                        out.write(pad + line + NEW_LINE);
156                    }
157    
158                    out.write(getIndent(indent + 1) + " >" + NEW_LINE);
159                }
160                else
161                {
162                    out.write(String.format(pad + "%s:%s=%s" + NEW_LINE, prop.getType().getID(), propName, prop.value));
163                }
164            }
165    
166            for (ConfigCategory child : children)
167            {
168                child.write(out, indent + 1);
169            }
170    
171            out.write(getIndent(indent) + "}" + NEW_LINE + NEW_LINE);
172        }
173    
174        private String getIndent(int indent)
175        {
176            StringBuilder buf = new StringBuilder("");
177            for (int x = 0; x < indent; x++)
178            {
179                buf.append("    ");
180            }
181            return buf.toString();
182        }
183    }