001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.wizard.properties;
018:
019: import java.io.FileOutputStream;
020: import java.io.IOException;
021: import java.net.MalformedURLException;
022: import java.util.Map;
023: import java.util.Properties;
024:
025: import org.romaframework.core.io.virtualfile.VirtualFile;
026: import org.romaframework.core.io.virtualfile.VirtualFileFactory;
027: import org.romaframework.wizard.schema.NoDTDProperties;
028:
029: /**
030: * Handle the ROMA module properties. It load and save the configuration.
031: *
032: * @author Luca Garulli (luca.garulli@assetdata.it)
033: */
034: public class GenericProperties {
035:
036: protected Properties properties = null;
037: protected String fileName;
038: protected String comments;
039:
040: public GenericProperties(String iPath) throws IOException {
041: properties = new NoDTDProperties();
042: fileName = iPath;
043:
044: loadFromFile();
045: }
046:
047: public boolean existsFile() {
048: VirtualFile file;
049: try {
050: file = VirtualFileFactory.getFile(fileName);
051: } catch (MalformedURLException e) {
052: return false;
053: }
054: return file != null && file.exists();
055: }
056:
057: /**
058: * Update properties overwriting previous values.
059: *
060: * @param iInfo
061: * Map<String,String> containing all info to store
062: */
063: public synchronized void setProperties(Map<String, String> iInfo) {
064: for (Map.Entry<String, String> var : iInfo.entrySet()) {
065: properties.setProperty(var.getKey(), var.getValue());
066: }
067: saveToFile();
068: }
069:
070: /**
071: * Return the project information required.
072: *
073: * @param iVarName
074: * Name of variable to get
075: * @return
076: */
077: public synchronized String getProperty(String iVarName) {
078: return properties.getProperty(iVarName);
079: }
080:
081: /**
082: * Set property and save it.
083: *
084: * @param iVarName
085: * Variable's name to set
086: * @param iVarValue
087: * Variable's value to set
088: * @return
089: */
090: public synchronized void setProperty(String iVarName,
091: String iVarValue) {
092: properties.setProperty(iVarName, iVarValue);
093: saveToFile();
094: }
095:
096: private void saveToFile() {
097: try {
098: properties.storeToXML(new FileOutputStream(fileName),
099: comments);
100: } catch (Exception e) {
101: e.printStackTrace();
102: }
103: }
104:
105: private void loadFromFile() throws IOException {
106: VirtualFile file = VirtualFileFactory.getFile(fileName);
107: if (file != null && file.exists())
108: properties.loadFromXML(file.getInputStream());
109: }
110: }
|