FreeRDP
RDPFileParser.java
1 /*
2  Simple .RDP file parser
3 
4  Copyright 2013 Blaz Bacnik
5 
6  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7  If a copy of the MPL was not distributed with this file, You can obtain one at
8  http://mozilla.org/MPL/2.0/.
9 */
10 
11 package com.freerdp.freerdpcore.utils;
12 
13 import java.io.BufferedReader;
14 import java.io.FileReader;
15 import java.io.IOException;
16 import java.util.HashMap;
17 import java.util.Locale;
18 
19 public class RDPFileParser
20 {
21 
22  private static final int MAX_ERRORS = 20;
23  private static final int MAX_LINES = 500;
24 
25  private HashMap<String, Object> options;
26 
27  public RDPFileParser()
28  {
29  init();
30  }
31 
32  public RDPFileParser(String filename) throws IOException
33  {
34  init();
35  parse(filename);
36  }
37 
38  private void init()
39  {
40  options = new HashMap<>();
41  }
42 
43  public void parse(String filename) throws IOException
44  {
45  BufferedReader br = new BufferedReader(new FileReader(filename));
46  String line = null;
47 
48  int errors = 0;
49  int lines = 0;
50  boolean ok;
51 
52  while ((line = br.readLine()) != null)
53  {
54  lines++;
55  ok = false;
56 
57  if (errors > MAX_ERRORS || lines > MAX_LINES)
58  {
59  br.close();
60  throw new IOException("Parsing limits exceeded");
61  }
62 
63  String[] fields = line.split(":", 3);
64 
65  if (fields.length == 3)
66  {
67  if (fields[1].equals("s"))
68  {
69  options.put(fields[0].toLowerCase(Locale.ENGLISH), fields[2]);
70  ok = true;
71  }
72  else if (fields[1].equals("i"))
73  {
74  try
75  {
76  Integer i = Integer.parseInt(fields[2]);
77  options.put(fields[0].toLowerCase(Locale.ENGLISH), i);
78  ok = true;
79  }
80  catch (NumberFormatException e)
81  {
82  }
83  }
84  else if (fields[1].equals("b"))
85  {
86  ok = true;
87  }
88  }
89 
90  if (!ok)
91  errors++;
92  }
93  br.close();
94  }
95 
96  public String getString(String optionName)
97  {
98  if (options.get(optionName) instanceof String)
99  return (String)options.get(optionName);
100  else
101  return null;
102  }
103 
104  public Integer getInteger(String optionName)
105  {
106  if (options.get(optionName) instanceof Integer)
107  return (Integer)options.get(optionName);
108  else
109  return null;
110  }
111 }