001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program is distributed in the hope that it will be useful,
010: // but WITHOUT ANY WARRANTY; without even the implied warranty of
011: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: // GNU General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.personalization;
021:
022: import com.salmonllc.properties.Props;
023: import com.salmonllc.util.MessageLog;
024: import com.salmonllc.util.VectorSort;
025: import java.io.*;
026:
027: /**
028: * This class loads and saves skins to a text files. It uses the following key SKIN_PATH from the property file to determine the location of the files. The naming convention for each file should be skin_name.skin.
029: */
030:
031: public class FileSkinManager extends SkinManager {
032:
033: String _appName = null;
034: String _skinPath = null;
035:
036: void init(String appName) {
037: Props p = Props.getProps(appName, null);
038: _appName = appName;
039: _skinPath = p
040: .getProperty(Props.SKIN_PATH, Props.getPropsPath());
041: }
042:
043: /*Loads the data for a skin with the specified name and append its attributes to the current skin*/
044: public void load(String name, Skin skin) {
045: try {
046: File f = new File(_skinPath + File.separatorChar + name
047: + ".skin");
048: if (!f.exists())
049: return;
050: BufferedReader in = new BufferedReader(
051: new InputStreamReader(new FileInputStream(f)));
052: String line = in.readLine();
053: int pos = 0;
054: while (line != null) {
055: if (line.length() > 0 && line.charAt(0) != '#') {
056: pos = line.indexOf('=');
057: if (pos > -1) {
058: String att = line.substring(0, pos).trim();
059: String val = line.substring(pos + 1).trim();
060: boolean classAtt = false;
061: boolean instAtt = false;
062: if (att.startsWith("class."))
063: classAtt = true;
064: else if (att.startsWith("instance."))
065: instAtt = true;
066:
067: if (classAtt || instAtt) {
068: int ndx = att.lastIndexOf(".");
069: int ndx2 = att.indexOf(".");
070: if (ndx == ndx2)
071: continue;
072: String attName = att.substring(ndx + 1);
073: String attClass = att.substring(ndx2 + 1,
074: ndx);
075: if (classAtt)
076: skin.setClassAttribute(attClass,
077: attName, val);
078: else
079: skin.setInstanceAttribute(attClass,
080: attName, val);
081: } else {
082: skin.setProperty(att, val);
083: }
084:
085: }
086: }
087: line = in.readLine();
088: }
089: in.close();
090: return;
091: } catch (Exception e) {
092: MessageLog.writeErrorMessage("loadData", e, this );
093: }
094: }
095:
096: /*Saves a skin under the specified name, if it exists it is overwritten*/
097: public void save(String name, Skin skin) {
098: try {
099: File f = new File(_skinPath + File.separatorChar + name
100: + ".skin");
101: PrintWriter pw = new PrintWriter(new FileOutputStream(f));
102: Attribute att[] = skin.getAllAttributes();
103: for (int i = 0; i < att.length; i++)
104: pw.println(att[i].getAttribute() + "="
105: + att[i].getValue());
106: pw.close();
107: } catch (Exception e) {
108: MessageLog.writeErrorMessage("save()", e, this );
109: }
110: }
111:
112: /*Gets an array of available skin names*/
113: public String[] getSkinNames() {
114: VectorSort v = new VectorSort() {
115: public boolean compare(Object o1, Object o2) {
116: return ((String) o1).compareTo((String) o2) < 0;
117: };
118: };
119: File f = new File(_skinPath);
120: File list[] = f.listFiles();
121: for (int i = 0; i < list.length; i++) {
122: String name = list[i].getName();
123: if (name.endsWith(".skin"))
124: v.add(name.substring(0, name.length() - 5));
125: }
126: v.sort();
127: String ret[] = new String[v.size()];
128: v.copyInto(ret);
129: return ret;
130: }
131:
132: }
|