KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jemmyI18NWizard > wizardSupport > Utilities


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package jemmyI18NWizard.wizardSupport;
21
22 import org.openide.filesystems.FileSystem;
23 import org.openide.filesystems.Repository;
24
25 import java.io.*;
26 import org.openide.filesystems.FileObject;
27 import org.openide.loaders.DataObject;
28 import org.openide.cookies.SaveCookie;
29 import org.openide.util.RequestProcessor;
30 import java.util.Hashtable JavaDoc;
31
32
33 public class Utilities {
34
35     public static String JavaDoc getFilesystemPath() {
36
37         org.openide.filesystems.FileObject fileObject = null;
38         org.openide.filesystems.FileSystem fileSystemRoot = null;
39         org.openide.filesystems.Repository repository;
40         try {
41             repository = Repository.getDefault();
42             fileObject = repository.find("jemmyI18NWizard.wizardSupport", "Utilities","class");
43             fileSystemRoot = fileObject.getFileSystem();
44             
45         } catch(Exception JavaDoc e) {
46             System.out.println("Exception when identifying filesystem: " + e);
47         }
48         return fileSystemRoot.getDisplayName();
49     }
50
51     public static boolean compareBundles(String JavaDoc name1, String JavaDoc name2) throws Exception JavaDoc {
52         BufferedReader file1 = null;
53         BufferedReader file2 = null;
54         
55         try {
56             file1 = new BufferedReader(new FileReader(new File(name1)));
57             file2 = new BufferedReader(new FileReader(new File(name2)));
58         } catch(Exception JavaDoc e) {
59             System.out.println("Exception when opening file: " + e);
60             throw e;
61         }
62         
63         Hashtable JavaDoc hashtable = new Hashtable JavaDoc();
64         String JavaDoc line;
65         while((line = file1.readLine())!=null) hashtable.put(line, line);
66         
67         while(true) {
68             line = file2.readLine();
69
70             if(line == null) return (hashtable.size() == 0);
71            
72             if(hashtable.containsKey(line)) hashtable.remove(line);
73             else return false;
74         }
75     }
76     
77     public static void saveFile(String JavaDoc name) throws Exception JavaDoc {
78
79         final String JavaDoc extension = name.substring(name.lastIndexOf('.')+1,name.length());
80         final String JavaDoc filename;
81         final String JavaDoc path;
82         
83         String JavaDoc shorter = name.substring(0, name.lastIndexOf('.'));
84         shorter = shorter.replace('/','.');
85         shorter = shorter.replace('\\','.');
86         
87         int lastDot = shorter.lastIndexOf('.');
88         if(lastDot == -1) {
89             filename = shorter;
90             path = "";
91         }
92         else {
93             filename = shorter.substring(lastDot+1, shorter.length());
94             if(shorter.startsWith(".")) shorter = shorter.substring(1);
95             path = shorter.substring(0, lastDot);
96         }
97
98         final FileObject fObject = Repository.getDefault().find(path, filename, extension);
99         if(fObject == null) throw new Exception JavaDoc("Error finding fileobject");
100         final DataObject dObject = DataObject.find(fObject);
101
102         Thread JavaDoc waitForSave = new Thread JavaDoc() {
103             public void run() {
104                 SaveCookie sc = (SaveCookie)dObject.getCookie(SaveCookie.class);
105                 try {
106                     if(sc != null) sc.save();
107                 } catch(Exception JavaDoc e) {
108                     System.out.println("Error when saving file.");
109                 }
110             }
111         };
112         
113         RequestProcessor.Task task = RequestProcessor.postRequest(waitForSave);
114         task.waitFinished();
115     }
116
117 }
118
119
120
Popular Tags