KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsitmodelext > util > Util


1 /*
2  * Util.java
3  *
4  * Created on October 4, 2005, 7:48 PM
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.websvc.wsitmodelext.util;
11
12 import java.io.BufferedInputStream JavaDoc;
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.BufferedReader JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.io.InputStreamReader JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.net.URI JavaDoc;
23 import java.util.Collection JavaDoc;
24 import javax.swing.text.Document JavaDoc;
25 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
26 import org.netbeans.modules.xml.schema.model.SchemaModel;
27 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
28 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
29 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.FileUtil;
32 import org.openide.filesystems.FileLock;
33
34 /**
35  *
36  * @author nn136682
37  */

38 public class Util {
39     public static final String JavaDoc EMPTY_XSD = "resources/Empty.wsdl";
40     
41     public static Document JavaDoc getResourceAsDocument(String JavaDoc path) throws Exception JavaDoc {
42         InputStream JavaDoc in = Util.class.getResourceAsStream(path);
43         return loadDocument(in);
44     }
45     
46     public static Document JavaDoc loadDocument(InputStream JavaDoc in) throws Exception JavaDoc {
47     Document JavaDoc sd = new org.netbeans.editor.BaseDocument(
48             org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
49         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
50         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
51         try {
52             String JavaDoc line = null;
53             while ((line = br.readLine()) != null) {
54                 sbuf.append(line);
55                 sbuf.append(System.getProperty("line.separator"));
56             }
57         } finally {
58             br.close();
59         }
60         sd.insertString(0,sbuf.toString(),null);
61         return sd;
62     }
63     
64     public static int count = 0;
65     public static WSDLModel loadWSDLModel(String JavaDoc resourcePath) throws Exception JavaDoc {
66         NamespaceLocation nl = NamespaceLocation.valueFromResourcePath(resourcePath);
67         if (nl != null) {
68             return TestCatalogModel.getDefault().getWSDLModel(nl);
69         }
70         String JavaDoc location = resourcePath.substring(resourcePath.lastIndexOf('/')+1);
71         URI JavaDoc locationURI = new URI JavaDoc(location);
72         TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath));
73         return TestCatalogModel.getDefault().getWSDLModel(locationURI);
74     }
75     
76     public static WSDLModel createEmptyWSDLModel() throws Exception JavaDoc {
77         return loadWSDLModel(EMPTY_XSD);
78     }
79     
80     /*public static WSDLModel loadWSDLModel(Document doc) throws Exception {
81         return WSDLModelFactory.getDefault().getModel(doc);
82     }*/

83     
84     public static void dumpToStream(Document JavaDoc doc, OutputStream JavaDoc out) throws Exception JavaDoc{
85         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
86         w.print(doc.getText(0, doc.getLength()));
87         w.close();
88         out.close();
89     }
90     
91     public static void dumpToFile(Document JavaDoc doc, File JavaDoc f) throws Exception JavaDoc {
92         if (! f.exists()) {
93             f.createNewFile();
94         }
95         OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
96         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
97         w.print(doc.getText(0, doc.getLength()));
98         w.close();
99         out.close();
100     }
101     
102     public static File JavaDoc dumpToTempFile(Document JavaDoc doc) throws Exception JavaDoc {
103         File JavaDoc f = File.createTempFile("xsm", "xsd");
104         dumpToFile(doc, f);
105         return f;
106     }
107     
108     public static WSDLModel dumpAndReloadModel(Document JavaDoc doc) throws Exception JavaDoc {
109         File JavaDoc f = dumpToTempFile(doc);
110         URI JavaDoc dumpURI = new URI JavaDoc("dummyDump" + count++);
111         TestCatalogModel.getDefault().addURI(dumpURI, f.toURI());
112         return TestCatalogModel.getDefault().getWSDLModel(dumpURI);
113     }
114     
115     public static Document JavaDoc loadDocument(File JavaDoc f) throws Exception JavaDoc {
116         InputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(f));
117         return loadDocument(in);
118     }
119     
120     public static URI JavaDoc getResourceURI(String JavaDoc path) throws RuntimeException JavaDoc {
121         try {
122             return Util.class.getResource(path).toURI();
123         } catch (Exception JavaDoc ex) {
124             throw new RuntimeException JavaDoc(ex);
125         }
126     }
127     
128     public static File JavaDoc getTempDir(String JavaDoc path) throws Exception JavaDoc {
129         File JavaDoc tempdir = new File JavaDoc(System.getProperty("java.io.tmpdir"), path);
130         tempdir.mkdirs();
131         return tempdir;
132     }
133
134     public static GlobalSimpleType getPrimitiveType(String JavaDoc typeName){
135         SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel();
136         Collection JavaDoc<GlobalSimpleType> primitives = primitiveModel.getSchema().getSimpleTypes();
137         for(GlobalSimpleType ptype: primitives){
138             if(ptype.getName().equals(typeName)){
139                 return ptype;
140             }
141         }
142         return null;
143     }
144
145     public static Document JavaDoc setDocumentContentTo(Document JavaDoc doc, InputStream JavaDoc in) throws Exception JavaDoc {
146         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
147         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
148         try {
149             String JavaDoc line = null;
150             while ((line = br.readLine()) != null) {
151                 sbuf.append(line);
152                 sbuf.append(System.getProperty("line.separator"));
153             }
154         } finally {
155             br.close();
156         }
157         doc.remove(0, doc.getLength());
158         doc.insertString(0,sbuf.toString(),null);
159         return doc;
160     }
161     
162     public static Document JavaDoc setDocumentContentTo(Document JavaDoc doc, String JavaDoc resourcePath) throws Exception JavaDoc {
163         return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
164     }
165
166     public static void setDocumentContentTo(WSDLModel model, String JavaDoc resourcePath) throws Exception JavaDoc {
167         Document JavaDoc doc = ((AbstractDocumentModel)model).getBaseDocument();
168         setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
169     }
170
171     public static FileObject copyResource(String JavaDoc path, FileObject destFolder) throws Exception JavaDoc {
172         String JavaDoc filename = getFileName(path);
173         
174         FileObject dest = destFolder.getFileObject(filename);
175         if (dest == null) {
176             dest = destFolder.createData(filename);
177         }
178         FileLock lock = dest.lock();
179         OutputStream JavaDoc out = dest.getOutputStream(lock);
180         InputStream JavaDoc in = Util.class.getResourceAsStream(path);
181         try {
182             FileUtil.copy(in, out);
183         } finally {
184             out.close();
185             in.close();
186             if (lock != null) lock.releaseLock();
187         }
188         return dest;
189     }
190     
191     public static String JavaDoc getFileName(String JavaDoc path) {
192         int i = path.lastIndexOf('/');
193         if (i > -1) {
194             return path.substring(i+1);
195         }
196         return path;
197     }
198     
199 }
200
Popular Tags