|
| 1 | +package org.asteriskjava.config; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Iterator; |
| 5 | +import java.util.ArrayList; |
| 6 | + |
| 7 | +/** |
| 8 | + * |
| 9 | + */ |
| 10 | +public class Category extends ConfigElement |
| 11 | +{ |
| 12 | + private String name; |
| 13 | + private boolean template; |
| 14 | + private final List<Category> baseCategories = new ArrayList<Category>(); |
| 15 | + private final List<ConfigElement> elements = new ArrayList<ConfigElement>(); |
| 16 | + |
| 17 | + /** |
| 18 | + * The last object in the list will get assigned any trailing comments when EOF is hit. |
| 19 | + */ |
| 20 | + private String trailingComment; |
| 21 | + |
| 22 | + public Category(String name) |
| 23 | + { |
| 24 | + if (name == null) |
| 25 | + { |
| 26 | + throw new IllegalArgumentException("Name must not be null"); |
| 27 | + } |
| 28 | + this.name = name; |
| 29 | + } |
| 30 | + |
| 31 | + public Category(String filename, int lineno, String name) |
| 32 | + { |
| 33 | + super(filename, lineno); |
| 34 | + this.name = name; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns the name of this category. |
| 39 | + * |
| 40 | + * @return the name of this category. |
| 41 | + */ |
| 42 | + public String getName() |
| 43 | + { |
| 44 | + return name; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Checks if this category is a template. |
| 49 | + * |
| 50 | + * @return <code>true</code> if this category is a template, <code>false</code> otherwise. |
| 51 | + */ |
| 52 | + public boolean isTemplate() |
| 53 | + { |
| 54 | + return template; |
| 55 | + } |
| 56 | + |
| 57 | + void markAsTemplate() |
| 58 | + { |
| 59 | + template = true; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Returns a list of categories this category inherits from. |
| 64 | + * |
| 65 | + * @return a list of categories this category inherits from, never <code>null</code>. |
| 66 | + */ |
| 67 | + public List<Category> getBaseCategories() |
| 68 | + { |
| 69 | + return baseCategories; |
| 70 | + } |
| 71 | + |
| 72 | + void addBaseCategory(Category baseCategory) |
| 73 | + { |
| 74 | + baseCategories.add(baseCategory); |
| 75 | + } |
| 76 | + |
| 77 | + public List<ConfigElement> getElements() |
| 78 | + { |
| 79 | + return elements; |
| 80 | + } |
| 81 | + |
| 82 | + void addElement(ConfigElement element) |
| 83 | + { |
| 84 | + if (element instanceof Category) |
| 85 | + { |
| 86 | + throw new IllegalArgumentException("Nested categories are not allowed"); |
| 87 | + } |
| 88 | + |
| 89 | + elements.add(element); |
| 90 | + } |
| 91 | + |
| 92 | + public String format() |
| 93 | + { |
| 94 | + StringBuilder sb = new StringBuilder(); |
| 95 | + |
| 96 | + format(sb); |
| 97 | + for (ConfigElement e : elements) |
| 98 | + { |
| 99 | + sb.append("\n"); |
| 100 | + e.format(sb); |
| 101 | + } |
| 102 | + return sb.toString(); |
| 103 | + } |
| 104 | + |
| 105 | + protected StringBuilder rawFormat(StringBuilder sb) |
| 106 | + { |
| 107 | + sb.append("[").append(name).append("]"); |
| 108 | + if (isTemplate() || !getBaseCategories().isEmpty()) |
| 109 | + { |
| 110 | + sb.append("("); |
| 111 | + if (isTemplate()) |
| 112 | + { |
| 113 | + sb.append("!"); |
| 114 | + if (!getBaseCategories().isEmpty()) |
| 115 | + { |
| 116 | + sb.append(","); |
| 117 | + } |
| 118 | + } |
| 119 | + Iterator<Category> inheritsFromIterator = getBaseCategories().iterator(); |
| 120 | + while (inheritsFromIterator.hasNext()) |
| 121 | + { |
| 122 | + sb.append(inheritsFromIterator.next().getName()); |
| 123 | + if (inheritsFromIterator.hasNext()) |
| 124 | + { |
| 125 | + sb.append(","); |
| 126 | + } |
| 127 | + } |
| 128 | + sb.append(")"); |
| 129 | + } |
| 130 | + |
| 131 | + return sb; |
| 132 | + } |
| 133 | +} |
0 commit comments