Source code: com/flexstor/common/util/PropertyMapper.java
1 /*
2 * PropertyMapper.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:31 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.util;
12
13 import java.io.BufferedReader;
14 import java.io.IOException;
15 import java.io.InputStreamReader;
16 import java.net.URL;
17
18 import com.flexstor.common.io.xfile.FlexXFile;
19
20 /**
21 * PropertyMapper provides functionality to read sections, properties and
22 * values from an input file, keep them in memory and access each element of data.
23 *
24 * @author Jose Hernandez
25 * @version 2.2
26 */
27 public class PropertyMapper
28 extends PropertyMapperCore
29 {
30 public PropertyMapper()
31 {
32 super();
33 }
34
35 /**
36 * Reads the input source identifying sections, properties and values.
37 * If sFileName start with "http://" or "file://" we read the data
38 * from an URL otherwise via FlexXFile.
39 * This condition may be removed once XFile supports URLs itself.
40 *
41 * @param sFileName the input data source to get the data from.
42 * @exception IOException if there is any problem accessing the file.
43 */
44 public void readInputFile( String sFileName )
45 throws IOException
46 {
47 if ( sFileName.startsWith( "http://" ) )
48 {
49 readInputFile( new URL( StringUtil.encodeURLString( sFileName ) ) );
50 }
51 else if ( sFileName.startsWith( "file://" ) )
52 {
53 readInputFile( new URL( sFileName ) );
54 }
55 else
56 {
57 readInputFile( new FlexXFile( sFileName ) );
58 }
59 }
60
61 /**
62 * Reads the input file identifying sections, properties and values.
63 *
64 * @param sFileName the input file name to get the data from.
65 * @param bUseXFile If true then use the FlexXFile classes to access this file,
66 * else use the standard java classes.
67 * @exception IOException if there is any problem accessing the file.
68 */
69 public void readInputFile( String sFileName, boolean bUseXFile )
70 throws IOException
71 {
72 if ( bUseXFile )
73 readInputFile( new FlexXFile( sFileName ) );
74 else
75 readInputFile( new URL ( sFileName ) );
76 }
77
78 /**
79 * Reads the input file identifying sections, properties and values.
80 *
81 * @param xInputFile an FlexXFile object for the input file to get the data from.
82 * @exception IOException if there is any problem accessing the file.
83 */
84 public void readInputFile( FlexXFile xInputFile )
85 throws IOException
86 {
87 //BufferedReader inputReader = new BufferedReader( new XFileReader( xInputFile ), 512 );
88 BufferedReader inputReader = xInputFile.getBufferedReader(512);
89
90 readInputFile( inputReader );
91
92 // close input file
93 inputReader.close();
94 }
95
96
97 /**
98 * Reads the input file identifying sections, properties and values.
99 * This method uses the File class not the FlexXFile Class, due to problems with FlexXFile
100 *
101 * @param url an URL to get the data from.
102 * @exception IOException if there is any problem accessing the URL.
103 */
104 public void readInputFile( URL url )
105 throws IOException
106 {
107 BufferedReader inputReader = new BufferedReader( new InputStreamReader(url.openStream()), 512 );
108
109 readInputFile( inputReader );
110
111 inputReader.close();
112 }
113
114 } // end of class
115