View Javadoc

1   /*
2      Copyright 2007 Ivo Limmen
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.limmen.crs.storage.xml;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FilenameFilter;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.limmen.crs.model.Configuration;
27  import org.limmen.crs.storage.api.ConfigurationStorage;
28  import org.limmen.crs.storage.api.InputStreamResolver;
29  
30  
31  public class ConfigurationXMLStorage implements ConfigurationStorage, InputStreamResolver {
32  
33  	private final String configurationDirectory;
34  
35     public ConfigurationXMLStorage() {
36  
37     	super();
38  
39     	StringBuffer stringBuffer = new StringBuffer();
40  
41     	stringBuffer.append(System.getProperty("user.home"));
42     	stringBuffer.append(System.getProperty("file.separator"));
43     	stringBuffer.append(".crs");
44     	stringBuffer.append(System.getProperty("file.separator"));
45  
46     	this.configurationDirectory = stringBuffer.toString();
47     }
48  
49  	private String createFileName(Configuration configuration) {
50  
51  		return createFileName(configuration.getId());
52  	}
53  
54  	private String createFileName(String aConfigurationId) {
55  
56  		StringBuffer sb = new StringBuffer(this.configurationDirectory);
57  		sb.append(aConfigurationId);
58  		sb.append(".xml");
59  
60  		return sb.toString();
61  	}
62  
63  	@Override
64  	public void deleteConfiguration(String configurationId) {
65  
66  		File fileForDeletion = new File(createFileName(configurationId));
67  
68  		fileForDeletion.delete();
69  	}
70  
71  	@Override
72  	public List<String> getConfigurationIdList() {
73  
74  		File root = new File(configurationDirectory);
75  
76  		String[] fileList = root.list(new FilenameFilter() {
77  
78  			@Override
79  			public boolean accept(File dir, String name) {
80  
81  				if (name != null && name.endsWith(".xml")) {
82  
83  					return true;
84  				}
85  				else {
86  
87  					return false;
88  				}
89  			}
90  		});
91  
92  		List<String> listOfFiles = new ArrayList<String>();
93  
94  		if (fileList != null) {
95  
96          		for (String file : fileList) {
97  
98          			listOfFiles.add(file.substring(0, file.length() - 4));
99          		}
100 		}
101 
102 		return listOfFiles;
103 	}
104 
105 	@Override
106    public InputStream getInputStream(String configurationId) throws IOException {
107 
108       return new FileInputStream(createFileName(configurationId));
109    }
110 
111 	@Override
112 	public String getStorageType() {
113 
114 	   return "XML";
115 	}
116 
117 	@Override
118 	public Configuration loadConfiguration(String configurationId) {
119 
120 	   try {
121 
122 	      return ConfigurationXMLReader.read(createFileName(configurationId), this);
123       }
124       catch (IOException e) {
125 
126       	//TODO: handle exception
127       	e.printStackTrace();
128 
129       	return null;
130       }
131 	}
132 
133 	@Override
134 	public void storeConfiguration(Configuration configuration) {
135 
136 		try {
137 
138 			ConfigurationXMLWriter.write(createFileName(configuration), configuration);
139       }
140       catch (IOException e) {
141 
142       	//TODO: handle exception
143       	e.printStackTrace();
144       }
145 	}
146 }