KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > TestUtil


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 /*
21  * TestCatalogModel.java
22  *
23  * Created on April 2, 2006, 10:41 AM
24  *
25  * To change this template, choose Tools | Template Manager
26  * and open the template in the editor.
27  */

28
29 package org.netbeans.modules.xml.wsdl.ui;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.InputStream JavaDoc;
34 import javax.swing.text.Document JavaDoc;
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 /**
46  *
47  * @author girix
48  */

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 JavaDoc document = getDocument(thisFileObj);
61         Lookup proxyLookup = Lookups.proxy(
62                 new Lookup.Provider() {
63             public Lookup getLookup() {
64                 return Lookups.fixed(new Object JavaDoc[] {
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 JavaDoc getDocument(FileObject fo){
77         Document JavaDoc result = null;
78         if (result != null) return result;
79         try {
80             
81             InputStream JavaDoc fis = fo.getInputStream();
82             byte buffer[] = new byte[fis.available()];
83 // result = new org.netbeans.editor.BaseDocument(
84
// org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
85
result = new javax.swing.text.PlainDocument JavaDoc();
86             result.remove(0, result.getLength());
87             fis.read(buffer);
88             fis.close();
89             String JavaDoc str = new String JavaDoc(buffer);
90             result.insertString(0,str,null);
91             
92         } catch (Exception JavaDoc 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 JavaDoc getModelSourceKey(ModelSource source) {
106             return mFile;
107         }
108
109     }
110 }
111
112
Popular Tags