1 19 20 28 29 package org.netbeans.modules.xml.wsdl.ui; 30 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.InputStream ; 34 import javax.swing.text.Document ; 35 import org.netbeans.modules.xml.xam.locator.CatalogModelException; 36 import org.netbeans.modules.xml.xam.ModelSource; 37 import org.netbeans.modules.xml.xam.spi.ModelAccessProvider; 38 import org.openide.filesystems.FileObject; 39 import org.openide.filesystems.FileUtil; 40 import org.openide.loaders.DataObject; 41 import org.openide.loaders.DataObjectNotFoundException; 42 import org.openide.util.Lookup; 43 import org.openide.util.lookup.Lookups; 44 45 49 50 public class TestUtil{ 51 52 public static ModelSource createModelSource(final FileObject thisFileObj, boolean editable) throws CatalogModelException{ 53 assert thisFileObj != null : "Null file object."; 54 final DataObject dobj; 55 try { 56 dobj = DataObject.find(thisFileObj); 57 } catch (DataObjectNotFoundException ex) { 58 throw new CatalogModelException(ex); 59 } 60 final Document document = getDocument(thisFileObj); 61 Lookup proxyLookup = Lookups.proxy( 62 new Lookup.Provider() { 63 public Lookup getLookup() { 64 return Lookups.fixed(new Object [] { 65 thisFileObj, 66 document, 67 dobj, 68 new ModelAccessProviderImpl(thisFileObj) 69 }); 70 } 71 } 72 ); 73 return new ModelSource(proxyLookup, editable); 74 } 75 76 private static Document getDocument(FileObject fo){ 77 Document result = null; 78 if (result != null) return result; 79 try { 80 81 InputStream fis = fo.getInputStream(); 82 byte buffer[] = new byte[fis.available()]; 83 result = new javax.swing.text.PlainDocument (); 86 result.remove(0, result.getLength()); 87 fis.read(buffer); 88 fis.close(); 89 String str = new String (buffer); 90 result.insertString(0,str,null); 91 92 } catch (Exception dObjEx) { 93 return null; 94 } 95 return result; 96 } 97 98 static class ModelAccessProviderImpl implements ModelAccessProvider { 99 private FileObject mFile; 100 101 public ModelAccessProviderImpl(FileObject file) { 102 this.mFile = file; 103 } 104 105 public Object getModelSourceKey(ModelSource source) { 106 return mFile; 107 } 108 109 } 110 } 111 112 | Popular Tags |