1 9 10 package org.netbeans.modules.xml.retriever.catalog.model; 11 12 import java.io.File ; 13 import java.io.FileInputStream ; 14 import java.io.IOException ; 15 import javax.swing.text.Document ; 16 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 17 import org.netbeans.modules.xml.xam.ModelSource; 18 import org.openide.filesystems.FileObject; 19 import org.openide.filesystems.FileUtil; 20 import org.openide.filesystems.Repository; 21 import org.openide.loaders.DataObject; 22 import org.openide.loaders.DataObjectNotFoundException; 23 import org.openide.util.Lookup; 24 import org.openide.util.lookup.Lookups; 25 26 30 31 public class TestUtil{ 32 33 static { 34 registerXMLKit(); 35 } 36 37 public static void registerXMLKit() { 38 String [] path = new String [] { "Editors", "text", "x-xml" }; 39 FileObject target = Repository.getDefault().getDefaultFileSystem().getRoot(); 40 try { 41 for (int i=0; i<path.length; i++) { 42 FileObject f = target.getFileObject(path[i]); 43 if (f == null) { 44 f = target.createFolder(path[i]); 45 } 46 target = f; 47 } 48 String name = "EditorKit.instance"; 49 if (target.getFileObject(name) == null) { 50 FileObject f = target.createData(name); 51 f.setAttribute("instanceClass", "org.netbeans.modules.xml.text.syntax.XMLKit"); 52 } 53 } catch(IOException ioe) { 54 ioe.printStackTrace(); 55 } 56 } 57 58 public static ModelSource createModelSource(final FileObject thisFileObj, boolean editable) throws CatalogModelException{ 59 assert thisFileObj != null : "Null file object."; 60 final DataObject dobj; 61 try { 62 dobj = DataObject.find(thisFileObj); 63 } catch (DataObjectNotFoundException ex) { 64 throw new CatalogModelException(ex); 65 } 66 final Document document = getDocument(thisFileObj); 67 Lookup proxyLookup = Lookups.proxy( 68 new Lookup.Provider() { 69 public Lookup getLookup() { 70 return Lookups.fixed(new Object [] { 71 FileUtil.toFile(thisFileObj), 72 thisFileObj, 73 document, 74 dobj 75 }); 76 } 77 } 78 ); 79 return new ModelSource(proxyLookup, editable); 80 } 81 82 private static Document getDocument(FileObject fo){ 83 Document result = null; 84 if (result != null) return result; 85 try { 86 87 File file = FileUtil.toFile(fo); 88 FileInputStream fis = new FileInputStream (file); 89 byte buffer[] = new byte[fis.available()]; 90 result = new org.netbeans.editor.BaseDocument( 91 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 92 result.remove(0, result.getLength()); 93 fis.read(buffer); 94 fis.close(); 95 String str = new String (buffer); 96 result.insertString(0,str,null); 97 98 } catch (Exception dObjEx) { 99 return null; 100 } 101 return result; 102 } 103 104 105 } 106 107 | Popular Tags |