001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.gast.lib;
025:
026: import java.io.BufferedReader;
027: import java.io.BufferedWriter;
028: import java.io.FileInputStream;
029: import java.io.FileNotFoundException;
030: import java.io.FileOutputStream;
031: import java.io.IOException;
032: import java.io.InputStreamReader;
033: import java.io.OutputStreamWriter;
034: import java.util.ArrayList;
035: import java.util.List;
036: import java.util.Map;
037: import jeeves.utils.Util;
038:
039: //=============================================================================
040:
041: public class TextLib {
042: //---------------------------------------------------------------------------
043: //---
044: //--- API methods
045: //---
046: //---------------------------------------------------------------------------
047:
048: public List<String> load(String file) throws FileNotFoundException,
049: IOException {
050: FileInputStream is = new FileInputStream(file);
051: BufferedReader ir = new BufferedReader(new InputStreamReader(
052: is, "ISO-8859-1"));
053:
054: ArrayList<String> al = new ArrayList<String>();
055:
056: String line;
057:
058: try {
059: while ((line = ir.readLine()) != null)
060: al.add(line);
061:
062: return al;
063: } finally {
064: ir.close();
065: }
066: }
067:
068: //---------------------------------------------------------------------------
069:
070: public void save(String file, List<String> lines)
071: throws FileNotFoundException, IOException {
072: FileOutputStream os = new FileOutputStream(file);
073: BufferedWriter ow = new BufferedWriter(new OutputStreamWriter(
074: os, "ISO-8859-1"));
075:
076: try {
077: for (String line : lines) {
078: ow.write(line);
079: ow.newLine();
080: }
081: } finally {
082: ow.close();
083: }
084: }
085:
086: //---------------------------------------------------------------------------
087:
088: public String getProperty(List<String> lines, String name) {
089: for (String line : lines)
090: if (!line.startsWith("#")) {
091: int pos = line.indexOf("=");
092:
093: if (pos != -1) {
094: String curName = line.substring(0, pos).trim();
095: String curValue = line.substring(pos + 1).trim();
096:
097: if (name.equals(curName))
098: return curValue;
099: }
100: }
101:
102: return null;
103: }
104:
105: //---------------------------------------------------------------------------
106:
107: public void setProperty(List<String> lines, String name,
108: String value) {
109: for (int i = 0; i < lines.size(); i++) {
110: String line = lines.get(i).trim();
111: int pos = line.indexOf("=");
112:
113: if (!line.startsWith("#") && pos != -1) {
114: String curName = line.substring(0, pos).trim();
115:
116: if (name.equals(curName)) {
117: lines.set(i, name + "=" + value);
118: return;
119: }
120: }
121: }
122:
123: lines.add(name + "=" + value);
124: }
125:
126: //---------------------------------------------------------------------------
127:
128: public String getRandomString(int length) {
129: StringBuffer sb = new StringBuffer();
130:
131: for (int i = 0; i < length; i++)
132: sb.append(getRandomChar());
133:
134: return sb.toString();
135: }
136:
137: //---------------------------------------------------------------------------
138:
139: public char getRandomChar() {
140: int pos = (int) (Math.random() * 62);
141:
142: if (pos < 26)
143: return (char) ('a' + pos);
144:
145: pos -= 26;
146:
147: if (pos < 26)
148: return (char) ('A' + pos);
149:
150: pos -= 26;
151:
152: return (char) ('0' + pos);
153: }
154:
155: //---------------------------------------------------------------------------
156:
157: public void replace(List<String> lines,
158: Map<String, ? extends Object> vars) {
159: for (int i = 0; i < lines.size(); i++) {
160: String line = lines.get(i);
161:
162: for (Map.Entry<String, ? extends Object> entry : vars
163: .entrySet())
164: line = Util.replaceString(line, entry.getKey(), entry
165: .getValue().toString());
166:
167: lines.set(i, line);
168: }
169: }
170: }
171:
172: //=============================================================================
|