0
我想解析一行一行的BIND配置,但它不起作用。因爲BIND使用Java解析BIND DNS配置文件?
關鍵字1 KEY_NAME的結構{
1關鍵字 key_value ;
};
來存儲配置。
如何解析Java的ISC BIND DNS配置文件?
例子:
logging {
channel "default" {
file "C:\named\etc\default.log" versions 5 size 5m;
severity dynamic;
print-time yes;
print-severity yes;
print-category yes;
};
channel "general" {
severity dynamic;
file "C:\named\etc\general.log" versions 5 size 5m;
print-time yes;
print-severity yes;
print-category yes;
};
category "default" {
"default";
};
category "general" {
"general";
};
};
這裏是我的代碼:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ScannerTest {
public static void main(String[] args) throws IOException {
FileReader file = new FileReader("C:/named.conf");
BufferedReader br = new BufferedReader(file);
String line;
boolean isChannel = false;
while ((line = br.readLine()) != null) {
if (line.length() <= 0)
continue; // skip empty line
if (line.charAt(0) == '#' | line.charAt(0) == '/')
continue; // skip comment line
if (isChannel) {
String endChannel = "};"; // end of block
String regexEndChannel = endChannel;
Pattern patternEndChannel = Pattern.compile(regexEndChannel);
Matcher matcherEndChannel = patternEndChannel.matcher(line);
if (matcherEndChannel.find() == false) {
// get the file name
String fileKey = "file";
String regex = "\\b" + fileKey + "\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(line);
while (matcher.find() == true) {
String fileName = null;
StringTokenizer channelToken = new StringTokenizer(line);
if (channelToken.hasMoreTokens())
fileKey = channelToken.nextToken();
if (channelToken.hasMoreTokens())
fileName = channelToken.nextToken();
// get the file name
System.out.println("file Name: " + fileName);
}
// get the severity name
String severityKey = "severity";
// don't know why boundary the word "severity" with the \b
// but it still match the word print-severity
String regexseverity = "\\b" + severityKey + "\\b";
Pattern patternseverity = Pattern.compile(regexseverity);
Matcher matcherseverity = patternseverity.matcher(line);
while (matcherseverity.find() == true) {
String severityName = null;
StringTokenizer channelToken = new StringTokenizer(line);
if (channelToken.hasMoreTokens())
severityKey = channelToken.nextToken();
if (channelToken.hasMoreTokens())
severityName = channelToken.nextToken();
// get the file name
System.out.println("severity Name: " + severityName);
}
// get the last things
// print-time no;
// print-severity yes;
// print-category yes;
} else {
isChannel = false;
}
}
String channelKey = "channel";
String regex = "\\b" + channelKey + "\\b";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(line);
while (matcher.find() == true) {
isChannel = true;
String channelName = null;
StringTokenizer channelToken = new StringTokenizer(line);
if (channelToken.hasMoreTokens())
channelKey = channelToken.nextToken();
if (channelToken.hasMoreTokens())
channelName = channelToken.nextToken();
// get the channel name
System.out.println("Channel Name: " + channelName);
}
}
br.close();
}
}
我看不出有任何的Java代碼在這裏。 – EJP
@EJP謝謝,我添加了代碼。 – Tuan
好,它的問題是什麼,到底是什麼? – EJP