001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.sdm.spa;
031
032import java.io.BufferedReader;
033import java.io.BufferedWriter;
034import java.io.File;
035import java.io.FileInputStream;
036import java.io.FileWriter;
037import java.io.IOException;
038import java.io.InputStreamReader;
039
040/**
041 * @author Zhengang Cheng
042 * 
043 *         To hold misc utility functions.
044 * 
045 */
046public class Util {
047        /**
048         * Method remove. Remove specified char from the string. Added because
049         * RepaceAll is not available for jdk before 1.4.
050         * 
051         * @param str
052         * @param rechar
053         * @return String
054         */
055        public static String remove(String str, char rechar) {
056                int len = str.length();
057                char buf[] = new char[len];
058
059                int i = 0;
060                int j = 0;
061                char c;
062                while (i < len) {
063                        c = str.charAt(i);
064                        if (c != rechar) {
065                                buf[j] = c;
066                                j++;
067                        }
068                        i++;
069                }
070                return new String(buf, 0, j);
071        }
072
073        /**
074         * Get Caching result It will be stored at file p1_p2;
075         */
076        public static String getFromCache(String md, String in) {
077                String p1, p2;
078                String fname = md.hashCode() + "_" + in.hashCode();
079                File file = new File(fname);
080                String res = null;
081                if (file.exists()) {
082                        res = getFile(file);
083                }
084                return res;
085
086        }
087
088        /**
089         * Save into Caching File It will be stored at file p1_p2;
090         */
091        public static void saveToCache(String md, String in, String res) {
092                String p1, p2;
093                String fname = md.hashCode() + "_" + in.hashCode();
094                try {
095                        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
096                                        fname)));
097                        bw.write(res);
098                        bw.flush();
099                        bw.close();
100                } catch (IOException e) {
101                        e.printStackTrace();
102                }
103
104        }
105
106        public static String getFile(File file) {
107                String content = null;
108
109                try {
110                        FileInputStream fis = new FileInputStream(file);
111                        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
112
113                        int size = fis.available();
114
115                        char[] buf = new char[size];
116                        int i = br.read(buf, 0, size);
117                        content = String.valueOf(buf, 0, size);
118                        // Debug.logInfo("Processing: size" + size + " read:" + i, module);
119                        fis.close();
120
121                } catch (Exception e) {
122                        e.printStackTrace();
123                }
124
125                return content;
126        }
127
128        public static boolean toCache = false;
129
130}