FreeRDP
Loading...
Searching...
No Matches
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
11package com.freerdp.freerdpcore.utils;
12
13import java.io.BufferedReader;
14import java.io.IOException;
15import java.io.Reader;
16import java.util.HashMap;
17import java.util.Locale;
18
19public class RDPFileParser
20{
21
22 private static final int MAX_ERRORS = 20;
23 private static final int MAX_LINES = 500;
24
25 private final HashMap<String, Object> options;
26
27 public RDPFileParser()
28 {
29 options = new HashMap<>();
30 }
31
32 public void parse(Reader reader) throws IOException
33 {
34 BufferedReader br = new BufferedReader(reader);
35 String line = null;
36
37 int errors = 0;
38 int lines = 0;
39 boolean ok;
40
41 while ((line = br.readLine()) != null)
42 {
43 lines++;
44 ok = false;
45
46 if (errors > MAX_ERRORS || lines > MAX_LINES)
47 {
48 br.close();
49 throw new IOException("Parsing limits exceeded");
50 }
51
52 String[] fields = line.split(":", 3);
53
54 if (fields.length == 3)
55 {
56 if (fields[1].equals("s"))
57 {
58 options.put(fields[0].toLowerCase(Locale.ENGLISH), fields[2]);
59 ok = true;
60 }
61 else if (fields[1].equals("i"))
62 {
63 try
64 {
65 Integer i = Integer.parseInt(fields[2]);
66 options.put(fields[0].toLowerCase(Locale.ENGLISH), i);
67 ok = true;
68 }
69 catch (NumberFormatException e)
70 {
71 }
72 }
73 else if (fields[1].equals("b"))
74 {
75 ok = true;
76 }
77 }
78
79 if (!ok)
80 errors++;
81 }
82 br.close();
83 }
84
85 public String getString(String optionName)
86 {
87 if (options.get(optionName) instanceof String)
88 return (String)options.get(optionName);
89 else
90 return null;
91 }
92
93 public Integer getInteger(String optionName)
94 {
95 if (options.get(optionName) instanceof Integer)
96 return (Integer)options.get(optionName);
97 else
98 return null;
99 }
100}