View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.pkg;
21  
22  import java.io.BufferedReader;
23  import java.io.IOException;
24  import java.util.Properties;
25  
26  /***
27   * 
28   */
29  public class PackageInfoReader
30  {
31  
32      //--- Constant(s) ---
33  
34      //--- Data field(s) ---
35  
36      //--- Constructor(s) ---
37  
38      //--- Method(s) ---
39  
40  	public static Properties readNext(BufferedReader in) throws IOException
41  	{
42  		Properties p = new Properties();
43  		String key = null;
44  		String value = null;
45  
46  		String s;
47  		while ((s = in.readLine()) != null) {
48  			if (s.startsWith("#")) {
49  				continue;
50  			}
51  			else if (s.length() == 0 || s.startsWith(" ")) {
52  				s = s.trim();
53  				if (s.length() == 0) {
54  					if (p.size() > 0) {
55  						// new package
56  						break;
57  					}
58  				}
59  				else if (s.equals(".")) {
60  					value += "\n";
61  				}
62  				else {
63  					value += " " + s;
64  				}
65  			}
66  			else {
67  				if (key != null && value != null) {
68  					// save key and value
69  					p.setProperty(key, value);
70  				}
71  
72  				// new key value pair
73  				key = value = null;
74  				int i = s.indexOf(':');
75  				if (i > 0) {
76  					key = s.substring(0, i);
77  					value = s.substring(i + 1).trim();
78  				}
79  				else {
80  					// malformed line
81  				}
82  			}
83  		}
84  
85  		if (key != null && value != null) {
86  			// save key and value
87  			p.setProperty(key, value);
88  		}
89  
90  		return (p.size() > 0) ? p : null;
91  	}
92  
93  }
94