4
* Created on October 4, 2005, 7:48 PM
6
* To change this template, choose Tools | Template Manager
7
* and open the template in the editor.
10
package org.netbeans.modules.xml.wsdl.model;
12
import java.io.BufferedInputStream;
13
import java.io.BufferedOutputStream;
14
import java.io.BufferedReader;
16
import java.io.FileInputStream;
17
import java.io.FileOutputStream;
18
import java.io.IOException;
19
import java.io.InputStream;
20
import java.io.InputStreamReader;
21
import java.io.OutputStream;
22
import java.io.PrintWriter;
24
import java.util.Collection;
25
import javax.swing.text.Document;
26
import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
27
import org.netbeans.modules.xml.schema.model.SchemaModel;
28
import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
29
import org.netbeans.modules.xml.wsdl.model.visitor.FindWSDLComponent;
30
import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
31
import org.openide.filesystems.FileObject;
32
import org.openide.filesystems.FileUtil;
33
import org.openide.filesystems.FileLock;
34
import org.openide.filesystems.Repository;
41
public static final String EMPTY_XSD = "resources/Empty.wsdl";
47
public static void registerXMLKit() {
48
String[] path = new String[] { "Editors", "text", "x-xml" };
49
FileObject target = Repository.getDefault().getDefaultFileSystem().getRoot();
51
for (int i=0; i<path.length; i++) {
52
FileObject f = target.getFileObject(path[i]);
54
f = target.createFolder(path[i]);
58
String name = "EditorKit.instance";
59
if (target.getFileObject(name) == null) {
60
FileObject f = target.createData(name);
61
f.setAttribute("instanceClass", "org.netbeans.modules.xml.text.syntax.XMLKit");
63
} catch(IOException ioe) {
64
ioe.printStackTrace();
68
public static Document getResourceAsDocument(String path) throws Exception {
69
InputStream in = Util.class.getResourceAsStream(path);
70
return loadDocument(in);
73
public static String getResourceAsString(String path) throws Exception {
74
InputStream in = Util.class.getResourceAsStream(path);
75
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
76
StringBuffer sbuf = new StringBuffer();
79
while ((line = br.readLine()) != null) {
81
sbuf.append(System.getProperty("line.separator"));
86
return sbuf.toString();
89
public static Document loadDocument(InputStream in) throws Exception {
90
Document sd = new org.netbeans.editor.BaseDocument(
91
org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
92
BufferedReader br = new BufferedReader(new InputStreamReader(in));
93
StringBuffer sbuf = new StringBuffer();
96
while ((line = br.readLine()) != null) {
98
sbuf.append(System.getProperty("line.separator"));
103
sd.insertString(0,sbuf.toString(),null);
107
public static int count = 0;
108
public static WSDLModel loadWSDLModel(String resourcePath) throws Exception {
109
NamespaceLocation nl = NamespaceLocation.valueFromResourcePath(resourcePath);
111
return TestCatalogModel.getDefault().getWSDLModel(nl);
113
String location = resourcePath.substring(resourcePath.lastIndexOf('/')+1);
114
URI locationURI = new URI(location);
115
TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath));
116
return TestCatalogModel.getDefault().getWSDLModel(locationURI);
119
public static WSDLModel createEmptyWSDLModel() throws Exception {
120
return loadWSDLModel(EMPTY_XSD);
123
/*public static WSDLModel loadWSDLModel(Document doc) throws Exception {
124
return WSDLModelFactory.getDefault().getModel(doc);
127
public static void dumpToStream(Document doc, OutputStream out) throws Exception{
128
PrintWriter w = new PrintWriter(out);
129
w.print(doc.getText(0, doc.getLength()));
134
public static void dumpToFile(Document doc, File f) throws Exception {
138
OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
139
PrintWriter w = new PrintWriter(out);
140
w.print(doc.getText(0, doc.getLength()));
145
public static File dumpToTempFile(Document doc) throws Exception {
146
File f = File.createTempFile("xsm", "xsd");
151
public static WSDLModel dumpAndReloadModel(Document doc) throws Exception {
152
File f = dumpToTempFile(doc);
153
URI dumpURI = new URI("dummyDump" + count++);
154
TestCatalogModel.getDefault().addURI(dumpURI, f.toURI());
155
return TestCatalogModel.getDefault().getWSDLModel(dumpURI);
158
public static Document loadDocument(File f) throws Exception {
159
InputStream in = new BufferedInputStream(new FileInputStream(f));
160
return loadDocument(in);
163
public static URI getResourceURI(String path) throws RuntimeException {
165
return Util.class.getResource(path).toURI();
166
} catch (Exception ex) {
167
throw new RuntimeException(ex);
171
public static File getTempDir(String path) throws Exception {
172
File tempdir = new File(System.getProperty("java.io.tmpdir"), path);
177
public static GlobalSimpleType getPrimitiveType(String typeName){
178
SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel();
179
Collection<GlobalSimpleType> primitives = primitiveModel.getSchema().getSimpleTypes();
180
for(GlobalSimpleType ptype: primitives){
181
if(ptype.getName().equals(typeName)){
188
public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception {
189
BufferedReader br = new BufferedReader(new InputStreamReader(in));
190
StringBuffer sbuf = new StringBuffer();
193
while ((line = br.readLine()) != null) {
195
sbuf.append(System.getProperty("line.separator"));
200
doc.remove(0, doc.getLength());
201
doc.insertString(0,sbuf.toString(),null);
205
public static Document setDocumentContentTo(Document doc, String resourcePath) throws Exception {
206
return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
209
public static void setDocumentContentTo(WSDLModel model, String resourcePath) throws Exception {
210
Document doc = ((AbstractDocumentModel)model).getBaseDocument();
211
setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
214
public static FileObject copyResource(String path, FileObject destFolder) throws Exception {
215
String filename = getFileName(path);
217
FileObject dest = destFolder.getFileObject(filename);
219
dest = destFolder.createData(filename);
221
FileLock lock = dest.lock();
222
OutputStream out = dest.getOutputStream(lock);
223
InputStream in = Util.class.getResourceAsStream(path);
225
FileUtil.copy(in, out);
229
if (lock != null) lock.releaseLock();
234
public static String getFileName(String path) {
235
int i = path.lastIndexOf('/');
237
return path.substring(i+1);
243
public static <T extends WSDLComponent> T find(Class<T> type, WSDLModel model, String xpath) {
244
return type.cast(FindWSDLComponent.findComponent(type, model.getDefinitions(), xpath));