Skip to content

Commit 546df43

Browse files
committed
Initial work on configuration file parser
1 parent 664da3c commit 546df43

17 files changed

Lines changed: 1150 additions & 0 deletions
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.asteriskjava.config;
2+
3+
public abstract class ConfigDirective extends ConfigElement
4+
{
5+
protected ConfigDirective()
6+
{
7+
}
8+
9+
protected ConfigDirective(String filename, int lineno)
10+
{
11+
super(filename, lineno);
12+
}
13+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.asteriskjava.config;
2+
3+
public abstract class ConfigElement
4+
{
5+
/**
6+
* Name of the file the category was read from.
7+
*/
8+
private String filename;
9+
10+
/**
11+
* Line number.
12+
*/
13+
private int lineno;
14+
private String preComment;
15+
private String samelineComment;
16+
17+
protected ConfigElement()
18+
{
19+
}
20+
21+
protected ConfigElement(String filename, int lineno)
22+
{
23+
this.filename = filename;
24+
this.lineno = lineno;
25+
}
26+
27+
public String getFileName()
28+
{
29+
return filename;
30+
}
31+
32+
public void setFileName(String filename)
33+
{
34+
this.filename = filename;
35+
}
36+
37+
public int getLineNumber()
38+
{
39+
return lineno;
40+
}
41+
42+
void setLineNumber(int lineno)
43+
{
44+
this.lineno = lineno;
45+
}
46+
47+
public String getPreComment()
48+
{
49+
return preComment;
50+
}
51+
52+
public void setPreComment(String preComment)
53+
{
54+
this.preComment = preComment;
55+
}
56+
57+
public String getComment()
58+
{
59+
return samelineComment;
60+
}
61+
62+
public void setComment(String samelineComment)
63+
{
64+
this.samelineComment = samelineComment;
65+
}
66+
67+
protected StringBuilder format(StringBuilder sb)
68+
{
69+
if (preComment != null && ! preComment.isEmpty())
70+
{
71+
sb.append(preComment);
72+
}
73+
74+
rawFormat(sb);
75+
76+
if (samelineComment != null && ! samelineComment.isEmpty())
77+
{
78+
sb.append(" ; ").append(samelineComment);
79+
}
80+
81+
return sb;
82+
}
83+
84+
protected abstract StringBuilder rawFormat(StringBuilder sb);
85+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2004-2006 Stefan Reuter
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
package org.asteriskjava.config;
18+
19+
import java.util.Map;
20+
import java.util.List;
21+
22+
/**
23+
* An Asterisk configuration file.
24+
*
25+
* @author srt
26+
* @version $Id$
27+
* @since 1.0.0
28+
*/
29+
public class ConfigFile
30+
{
31+
private final String filename;
32+
private final Map<String, List<String>> categories;
33+
34+
public ConfigFile(String filename, Map<String, List<String>> categories)
35+
{
36+
this.filename = filename;
37+
this.categories = categories;
38+
}
39+
40+
/**
41+
* Returns the filename.
42+
*
43+
* @return the filename, for example "voicemail.conf".
44+
*/
45+
public String getFilename()
46+
{
47+
return filename;
48+
}
49+
50+
/**
51+
* Returns the lines per category.
52+
*
53+
* @return the lines per category.
54+
*/
55+
public Map<String, List<String>> getCategories()
56+
{
57+
return categories;
58+
}
59+
}

0 commit comments

Comments
 (0)