⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ixprefsio.java

📁 一款即时通讯软件
💻 JAVA
字号:
package edu.ou.kmi.buddyspace.ixpanels;

/*
 * IXPrefsIO.java
 *
 * Project: BuddySpace
 * (C) Copyright Knowledge Media Institute 2002
 *
 *
 * Created on 11 October 2002, 8:26
 */

import java.util.*;
import java.io.*;

import org.jabber.jabberbeans.util.*;

import edu.ou.kmi.buddyspace.core.*;

/**
 * <code>IXPrefsIO</code> provides i/o from and to I-X process panels'
 * properties file, while getting/storing only relation information.
 *
 * @author  Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
 */
public class IXPrefsIO {
    
    /** starting words of interesting lines */
    private static String[] categories = {"peers", "superiors", "subordinates", 
                                          "contacts"};

    /** Returns hashtable of relations read from given file */
    public static Hashtable readRelations2(String filename) {
        Hashtable rels = new Hashtable();
        
        File file;
        FileReader input;
        try {
            file = new File(filename);
            input = new FileReader(file);
        } catch (NullPointerException e) {
            //throw new IOException("Cannot open input file");
            System.out.println("Cannot open input file");
            return rels;
        } catch (FileNotFoundException e) {
            //throw new IOException("Cannot open input file");
            System.out.println("Cannot open input file");
            return rels;
        }
        
        try {
            boolean readResult;
            do {
                char[] c = new char[1];
                String row = "";
                readResult = (1 == input.read(c, 0, 1));
                while (readResult && c[0] != '\n') {
                    row = row + c[0];
                    readResult = (1 == input.read(c, 0, 1));
                }
                
                for (int i=0; i<categories.length; i++) {
                    if (row.startsWith(categories[i])) {
                        int curPos = categories[i].length()+1;
                        int endPos = row.indexOf(',', curPos);
                        String jidStr;
                        while (endPos != -1) {
                            jidStr = row.substring(curPos, endPos);
                            curPos = endPos+1;
                            endPos = row.indexOf(',', curPos);
                            rels.put(jidStr, categories[i]);
                        }
                        jidStr = row.substring(curPos, row.length());
                        rels.put(jidStr, categories[i]);
                    }
                }
            } while (readResult);
            
            input.close();

        } catch (IOException e) {
            System.out.println("Input file reading error");
            return rels;
        }
        
        return rels;
    }
    
    
    /** Changes relations section in file according to given hashtable */
    public static void saveRelations2(Hashtable relations, String filename) {
        
        File file;
        String inputFileStr;
        
        FileReader input;
        try {
            file = new File(filename);
            input = new FileReader(file);
        } catch (NullPointerException e) {
            //throw new IOException("Cannot open input file");
            System.out.println("Cannot open input file");
            return;
        } catch (FileNotFoundException e) {
            //throw new IOException("Cannot open input file");
            System.out.println("Cannot open input file");
            return;
        }
        
        try {
            char[] c = new char[1024];
            inputFileStr = "";
            int charNum;
            do {
                charNum = input.read(c, 0, 1024);
                inputFileStr = inputFileStr + new String(c, 0, charNum);
            } while (charNum == 1024);
            input.close();
        } catch (IOException e) {
            //throw new IOException("Cannot open input file");
            System.out.println("Cannot read input file");
            return;
        }
                    
        FileWriter output;
        try {
            output = new FileWriter(file);
        } catch (NullPointerException e) {
            //throw new IOException("Cannot open input file");
            System.out.println("Cannot open output file");
            return;
        } catch (IOException e) {
            //throw new IOException("Cannot open input file");
            System.out.println("Cannot open output file");
            return;
        }
        
        try {
            boolean written = false;
            boolean relation = false;
            String row;
            int curPos = 0;
            int endPos = inputFileStr.indexOf('\n', curPos);
            while (endPos != -1) {
                
                row = inputFileStr.substring(curPos, endPos+1);
                curPos = endPos+1;
                endPos = inputFileStr.indexOf('\n', curPos);
                
                relation = false;
                for (int i=0; i<categories.length; i++) {
                    if (row.startsWith(categories[i]))
                        relation = true;
                }
     
                // if not any of categories
                if (!relation)
                    // just save
                    output.write(row);
                
                // otherwise if written==false
                else if (!written) {
                    // set written=true & save hashtable but don't save row
                    written = true;
                    writeRelations(relations, output);
                }
                
            }
            
            row = inputFileStr.substring(curPos, inputFileStr.length());
                
            relation = false;
            for (int i=0; i<categories.length; i++) {
                if (row.startsWith(categories[i]))
                    relation = true;
            }

            // if not any of categories
            if (!relation)
                // just save
                output.write(row);

            // otherwise if written==false
            else if (!written) {
                // set written=true & save hashtable but don't save row
                written = true;
                writeRelations(relations, output);
            }
            
            output.close();

        } catch (IOException e) {
            System.out.println("Output file writing error");
            return;
        }
    }
    
    /** Writes relations section */
    private static void writeRelations(Hashtable relations, FileWriter output) 
                                        throws IOException {
        try {
            for (int i=0; i<categories.length; i++) {
                boolean firstJID = true;
                Enumeration jids = relations.keys();
                while (jids.hasMoreElements()) {
                    Object j = jids.nextElement();
                    String category = (String) relations.get(j);
                    if (categories[i].equals(category)) {
                        if (firstJID) {
                            firstJID = false;
                            output.write(categories[i] + "=" + (String) j);
                        }
                        else {
                            output.write("," + (String) j);
                        }
                    }
                }
                if (!firstJID)
                    output.write("\n");
            }
        } catch (IOException e) {
            throw(e);
        }
    }
    
    
    /*public static Hashtable readRelations(String filename) {
        Hashtable rels = new Hashtable();
        
        File file;
        FileReader input;
        try {
            file = new File(filename);
            input = new FileReader(file);
        } catch (NullPointerException e) {
            //throw new IOException("Cannot open input file");
            System.out.println("Cannot open input file");
            return rels;
        } catch (FileNotFoundException e) {
            //throw new IOException("Cannot open input file");
            System.out.println("Cannot open input file");
            return rels;
        }
        
        try {
            char[] c = new char[1];
            boolean readResult = true;
            // reads all lines
            while (readResult && (readResult = (1 == input.read(c, 0, 1)))) {
                
                // reads category
                String category = "";
                while(readResult && c[0] != '=' && c[0] != '\n') {
                    category = category + c[0];
                    readResult = (1 == input.read(c, 0, 1));
                }
                
                if (!readResult) break;
                if (c[0] == '\n') continue;
                
                String jidStr = "";
                readResult = (1 == input.read(c, 0, 1));
                while (readResult && c[0] != '\n') {
                    if (c[0] != '\r')
                        jidStr = jidStr + c[0];
                    readResult = (1 == input.read(c, 0, 1));
                }
                
                //if (!readResult) break;
                
                if (!category.equals("peer") && !category.equals("subordinate") &&
                    !category.equals("superior"))
                    continue;
                
                JID jid = new JID(jidStr);
                if (jid != null) {
                    String str = BSPresenceBean.getJIDHashString(jid, false);
                    rels.put(str, category);
                }
            }
            
            input.close();
            
        } catch (IOException e) {
            System.out.println("Input file reading error");
            return rels;
        }
        
        return rels;
    }*/
    
    
    /*public static void saveRelations(Hashtable relations, String filename) {
        File file;
        FileWriter output;
        try {
            file = new File(filename);
            output = new FileWriter(file);
        } catch (NullPointerException e) {
            //throw new IOException("Cannot open input file");
            System.out.println("Cannot open output file");
            return;
        } catch (IOException e) {
            //throw new IOException("Cannot open input file");
            System.out.println("Cannot open output file");
            return;
        }
        
        try {
            Enumeration jids = relations.keys();
            // writes all records in hashtable
            while (jids.hasMoreElements()) {
                String jidStr = (String) jids.nextElement();
                String category = (String)relations.get(jidStr);
                
                output.write(category + "=" + jidStr + "\n");
            }
            
            output.close();
            
        } catch (IOException e) {
            System.out.println("Output file writing error");
            return;
        }
        
        return;
    }*/
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -