1 19 20 package org.netbeans.upgrade.systemoptions; 21 22 import java.io.*; 23 import java.util.*; 24 import org.openide.filesystems.*; 25 26 30 public class Importer { 31 private static final String DEFINITION_OF_FILES = "systemoptionsimport"; 33 private static FileObject getRootOfSystemFileSystem() { 34 return Repository.getDefault().getDefaultFileSystem().getRoot(); 35 } 36 37 public static void doImport() throws IOException { 38 Set<FileObject> files = getImportFiles(loadImportFilesDefinition()); 39 for (Iterator<DefaultResult> it = parse(files).iterator(); it.hasNext();) { 40 saveResult(it.next()); 41 } 42 for (Iterator it = files.iterator(); it.hasNext();) { 43 FileObject fo = (FileObject) it.next(); 44 FileLock fLock = fo.lock(); 45 try { 46 fo.rename(fLock, fo.getName(), "imported"); } finally { 48 fLock.releaseLock(); 49 } 50 } 51 } 52 53 private static void saveResult(final DefaultResult result) throws IOException { 54 String absolutePath = "/"+result.getModuleName(); 55 PropertiesStorage ps = PropertiesStorage.instance(absolutePath); 56 Properties props = ps.load(); 57 String [] propertyNames = result.getPropertyNames(); 58 for (int i = 0; i < propertyNames.length; i++) { 59 String val = result.getProperty(propertyNames[i]); 60 if (val != null) { 61 props.put(propertyNames[i], val); 62 } 63 } 64 if (props.size() > 0) { 65 ps.save(props); 66 } 67 } 68 69 private static Set<DefaultResult> parse(final Set<FileObject> files) { 70 Set<DefaultResult> retval = new HashSet<DefaultResult>(); 71 for (FileObject f: files) { 72 try { 73 retval.add(SystemOptionsParser.parse(f, false)); 74 } catch (ClassNotFoundException ex) { 75 continue; 76 } catch (IOException ex) { 77 continue; 78 } 79 } 80 return retval; 81 } 82 83 84 static Properties loadImportFilesDefinition() throws IOException { 85 Properties props = new Properties(); 86 InputStream is = Importer.class.getResourceAsStream(DEFINITION_OF_FILES); 87 try { 88 props.load(is); 89 } finally { 90 is.close(); 91 } 92 return props; 93 } 94 95 private static Set<FileObject> getImportFiles(final Properties props) { 96 Set<FileObject> fileobjects = new HashSet<FileObject>(); 97 for (Iterator it = props.keySet().iterator(); it.hasNext();) { 98 String path = (String ) it.next(); 99 FileObject f = getRootOfSystemFileSystem().getFileObject(path); 100 if (f != null) { 101 fileobjects.add(f); 102 } 103 } 104 return fileobjects; 105 } 106 107 108 private Importer() {} 109 } 110 | Popular Tags |