tnsnames.ora is a configuration file which store the information about network service names and connect descriptor. These information are vital to connect the database. More details about tnsora can be found here. https://docs.oracle.com/database/121/NETRF/tnsnames.htm#NETRF259
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class TnsOraParser {
private static final String COMMENT = "#";
private Stack<Parameter> stack = new Stack<Parameter>();
private List<Parameter> netServiceNames = new ArrayList<Parameter>();
private boolean bracketOpen;
public TnsNamesOra parse(InputStream is) {
Scanner scan = new Scanner(is);
StringBuilder buffer = new StringBuilder();
boolean quote = false;
while (scan.hasNextLine()) {
String aLine = scan.nextLine();
aLine = aLine.trim();
if (aLine.startsWith(COMMENT)) {
continue;
}
for (int i = 0; i < aLine.length(); i++) {
char ch = aLine.charAt(i);
if (ch == '"')
{
quote = !quote;
}
else if (quote)
{
buffer.append(ch);
continue;
}
else if (ch == ' ' || ch == '\n' || ch == '\r') {
continue;
}
if (ch == '=')
{
gotNewParam(buffer);
}
else if (ch == '(')
{
bracketOpen = true;
}
else if (ch == ')')
{
bracketOpen = false;
Parameter cp = stack.pop();
if (cp != null)
{
if (buffer.length() > 0)
{
cp.setValue(buffer.toString());
buffer.delete(0, buffer.length());
}
if (!stack.isEmpty())
{
stack.peek().addParm(cp);
}
}
}
else
{
buffer.append(ch);
}
}
}
scan.close();
return new TnsNamesOra(netServiceNames);
}
private void gotNewParam(StringBuilder buffer) {
Parameter param = new Parameter(buffer.toString());
if (!bracketOpen && stack.size() == netServiceNames.size())
{
netServiceNames.add(param);
}
buffer.delete(0, buffer.length());
stack.push(param);
}
public static void main(String args[]) throws Exception {
FileInputStream testFile = new FileInputStream("D:\\Work\\tnsnames.ora");
TnsOraParser tnsOraParser = new TnsOraParser();
TnsNamesOra tnsOra = tnsOraParser.parse(testFile);
System.out.println(tnsOra.getServiceNames());
}
}
Parameter .java
==============
import java.util.ArrayList;
import java.util.List;
public class Parameter {
private String name;
private String value;
private List<Parameter> parameters;
public Parameter(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<Parameter> getParameters() {
return parameters;
}
public void setParameters(List<Parameter> parameters) {
this.parameters = parameters;
}
public void addParm(Parameter cp) {
if (parameters == null) {
parameters = new ArrayList<Parameter>();
}
parameters.add(cp);
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(name + "=(" );
if (value != null)
{
sb.append(value);
}
else if (parameters != null)
{
for (Parameter param : parameters)
{
sb.append(param);
}
}
sb.append(")");
return sb.toString();
}
}
TnsNamesOra.java
==================
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TnsNamesOra {
private List<Parameter> netServiceName;
private final String DESCRIPTION_LIST = "DESCRIPTION_LIST";
private final String DESCRIPTION = "DESCRIPTION";
private final String ADDRESS_LIST = "ADDRESS_LIST";
private final String ADDRESS = "ADDRESS";
private final String PROTOCOL = "PROTOCOL";
public TnsNamesOra(List<Parameter> netServiceNames) {
this.netServiceName = netServiceNames;
}
public List<String> getServiceNames() {
List<String> serviceNames = null;
if (netServiceName != null) {
serviceNames = new ArrayList<String>();
for (Parameter param : netServiceName) {
serviceNames.add(param.getName());
}
}
return serviceNames;
}
}
The configurations are defined key value manner. value can be literal or one or multiple key values again. Syntax rule can be found here https://docs.oracle.com/cd/A57673_01/DOC/net/doc/NWUS233/apb.htm
To parse the same in Java please refer below code
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
public class TnsOraParser {
private static final String COMMENT = "#";
private Stack<Parameter> stack = new Stack<Parameter>();
private List<Parameter> netServiceNames = new ArrayList<Parameter>();
private boolean bracketOpen;
public TnsNamesOra parse(InputStream is) {
Scanner scan = new Scanner(is);
StringBuilder buffer = new StringBuilder();
boolean quote = false;
while (scan.hasNextLine()) {
String aLine = scan.nextLine();
aLine = aLine.trim();
if (aLine.startsWith(COMMENT)) {
continue;
}
for (int i = 0; i < aLine.length(); i++) {
char ch = aLine.charAt(i);
if (ch == '"')
{
quote = !quote;
}
else if (quote)
{
buffer.append(ch);
continue;
}
else if (ch == ' ' || ch == '\n' || ch == '\r') {
continue;
}
if (ch == '=')
{
gotNewParam(buffer);
}
else if (ch == '(')
{
bracketOpen = true;
}
else if (ch == ')')
{
bracketOpen = false;
Parameter cp = stack.pop();
if (cp != null)
{
if (buffer.length() > 0)
{
cp.setValue(buffer.toString());
buffer.delete(0, buffer.length());
}
if (!stack.isEmpty())
{
stack.peek().addParm(cp);
}
}
}
else
{
buffer.append(ch);
}
}
}
scan.close();
return new TnsNamesOra(netServiceNames);
}
private void gotNewParam(StringBuilder buffer) {
Parameter param = new Parameter(buffer.toString());
if (!bracketOpen && stack.size() == netServiceNames.size())
{
netServiceNames.add(param);
}
buffer.delete(0, buffer.length());
stack.push(param);
}
public static void main(String args[]) throws Exception {
FileInputStream testFile = new FileInputStream("D:\\Work\\tnsnames.ora");
TnsOraParser tnsOraParser = new TnsOraParser();
TnsNamesOra tnsOra = tnsOraParser.parse(testFile);
System.out.println(tnsOra.getServiceNames());
}
}
Parameter .java
==============
import java.util.ArrayList;
import java.util.List;
public class Parameter {
private String name;
private String value;
private List<Parameter> parameters;
public Parameter(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<Parameter> getParameters() {
return parameters;
}
public void setParameters(List<Parameter> parameters) {
this.parameters = parameters;
}
public void addParm(Parameter cp) {
if (parameters == null) {
parameters = new ArrayList<Parameter>();
}
parameters.add(cp);
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append(name + "=(" );
if (value != null)
{
sb.append(value);
}
else if (parameters != null)
{
for (Parameter param : parameters)
{
sb.append(param);
}
}
sb.append(")");
return sb.toString();
}
}
TnsNamesOra.java
==================
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TnsNamesOra {
private List<Parameter> netServiceName;
private final String DESCRIPTION_LIST = "DESCRIPTION_LIST";
private final String DESCRIPTION = "DESCRIPTION";
private final String ADDRESS_LIST = "ADDRESS_LIST";
private final String ADDRESS = "ADDRESS";
private final String PROTOCOL = "PROTOCOL";
public TnsNamesOra(List<Parameter> netServiceNames) {
this.netServiceName = netServiceNames;
}
public List<String> getServiceNames() {
List<String> serviceNames = null;
if (netServiceName != null) {
serviceNames = new ArrayList<String>();
for (Parameter param : netServiceName) {
serviceNames.add(param.getName());
}
}
return serviceNames;
}
}