View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  
21  package base.user.jcard;
22  
23  import java.io.BufferedWriter;
24  import java.io.File;
25  import java.io.FileInputStream;
26  import java.io.FileWriter;
27  import java.io.IOException;
28  import java.io.Reader;
29  import java.io.Writer;
30  import java.util.ArrayList;
31  import java.util.Properties;
32  
33  import org.apache.log4j.Logger;
34  
35  import base.user.User;
36  
37  public class JCard implements IVCard {
38  	
39  	private static transient Logger logger = Logger.getLogger(JCard.class.getName());
40  	
41  	ArrayList<Property> containedProperties;
42  	
43  	private Properties availableProperties; 
44  	
45  	public JCard() throws JCardException {
46  		logger.info("New JCard created (in JCard ctor)");
47  		try {
48  			availableProperties = new Properties();
49  			availableProperties.loadFromXML(new FileInputStream("jcard-properties.xml"));
50  		
51  			containedProperties = new ArrayList<Property>();
52  		}
53  		catch(Exception e) {
54  			throw new JCardException("Error reading properties file", e);
55  		}
56  	}
57  	
58  	public Property addProperty(String propertyName) throws JCardException {
59  		logger.info("Adding " + propertyName + " to JCard");
60  		try {
61  //			System.out.println(propertyName);
62  //			System.out.println(availableProperties.getProperty(propertyName));
63  //			System.out.println(Class.forName(availableProperties.getProperty(propertyName)));
64  			Property newProp = (Property) Class.forName(availableProperties.getProperty(propertyName)).newInstance();
65  			//newProp.setPropertyName();
66  			containedProperties.add(newProp);
67  			return newProp;	
68  		}
69  		catch(Exception e) {
70  			throw new JCardException("Exception in creating property object", e);
71  		}
72  	}
73  	
74  	public void write(String fileName) throws JCardException {
75  		File file = new File(fileName);
76  		write(file);
77  	}
78  	
79  	public void write(File file) throws JCardException {
80  		try {
81  			FileWriter fWriter = new FileWriter(file);
82  			write(fWriter);
83  		}
84  		catch(IOException e) {
85  			throw new JCardException("Exception in creating Writer object", e);
86  		}
87  	}
88  	
89  
90  	/* (non-Javadoc)
91  	 * @see test.base.user.jcard.IVCard#write(java.io.Writer)
92  	 */
93  	public void write(Writer writer) throws JCardException {
94  		try {
95  			
96  			StringBuffer stringCard = new StringBuffer();
97  			stringCard.append("BEGIN:VCARD");
98  			stringCard.append(Property.CRLF);
99  			stringCard.append("VERSION:2.1");
100 			stringCard.append(Property.CRLF);
101 			
102 			for(Property property : containedProperties) {
103 				property.doValidate();
104 				stringCard.append(property.PROPERTY_NAME);
105 				stringCard.append(property.toString());
106 				stringCard.append(Property.CRLF);
107 			}
108 			
109 			stringCard.append("END:VCARD");
110 			
111 			BufferedWriter buffWriter = new BufferedWriter(writer);
112 			buffWriter.write(stringCard.toString());
113 			buffWriter.flush();
114 			buffWriter.close();
115 		}
116 		catch(IOException e) {
117 			throw new JCardException("Exception in writing file", e);
118 		}	
119 	}
120 
121 	public void read(Reader reader) {
122 		// TODO Auto-generated method stub
123 		
124 	}
125 
126 	public User newUser(JCard jcard) {
127 		// TODO Auto-generated method stub
128 		return null;
129 	}
130 
131 	public JCard newJCard(User user) {
132 		// TODO Auto-generated method stub
133 		return null;
134 	}
135 }