KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > retriever > catalog > model > TestUtil


1 /*
2  * TestCatalogModel.java
3  *
4  * Created on April 2, 2006, 10:41 AM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.xml.retriever.catalog.model;
11
12 import java.io.File JavaDoc;
13 import java.io.FileInputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import javax.swing.text.Document JavaDoc;
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 /**
27  *
28  * @author girix
29  */

30
31 public class TestUtil{
32     
33     static {
34         registerXMLKit();
35     }
36     
37     public static void registerXMLKit() {
38         String JavaDoc[] path = new String JavaDoc[] { "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 JavaDoc 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 JavaDoc 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 JavaDoc document = getDocument(thisFileObj);
67         Lookup proxyLookup = Lookups.proxy(
68                 new Lookup.Provider() {
69             public Lookup getLookup() {
70                 return Lookups.fixed(new Object JavaDoc[] {
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 JavaDoc getDocument(FileObject fo){
83         Document JavaDoc result = null;
84         if (result != null) return result;
85         try {
86             
87             File JavaDoc file = FileUtil.toFile(fo);
88             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(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 JavaDoc str = new String JavaDoc(buffer);
96             result.insertString(0,str,null);
97             
98         } catch (Exception JavaDoc dObjEx) {
99             return null;
100         }
101         return result;
102     }
103     
104     
105 }
106
107
Popular Tags