~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/wsdl/api/test/unit/src/org/netbeans/modules/xml/wsdl/model/Util.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.xml.wsdl.model;
 
11
 
 
12
import java.io.BufferedInputStream;
 
13
import java.io.BufferedOutputStream;
 
14
import java.io.BufferedReader;
 
15
import java.io.File;
 
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;
 
23
import java.net.URI;
 
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;
 
35
 
 
36
/**
 
37
 *
 
38
 * @author nn136682
 
39
 */
 
40
public class Util {
 
41
    public static final String EMPTY_XSD = "resources/Empty.wsdl";
 
42
 
 
43
    static {
 
44
        registerXMLKit();
 
45
    }
 
46
    
 
47
    public static void registerXMLKit() {
 
48
        String[] path = new String[] { "Editors", "text", "x-xml" };
 
49
        FileObject target = Repository.getDefault().getDefaultFileSystem().getRoot();
 
50
        try {
 
51
            for (int i=0; i<path.length; i++) {
 
52
                FileObject f = target.getFileObject(path[i]);
 
53
                if (f == null) {
 
54
                    f = target.createFolder(path[i]);
 
55
                }
 
56
                target = f;
 
57
            }
 
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");
 
62
            }
 
63
        } catch(IOException ioe) {
 
64
            ioe.printStackTrace();
 
65
        }
 
66
    }
 
67
        
 
68
    public static Document getResourceAsDocument(String path) throws Exception {
 
69
        InputStream in = Util.class.getResourceAsStream(path);
 
70
        return loadDocument(in);
 
71
    }
 
72
    
 
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();
 
77
        try {
 
78
            String line = null;
 
79
            while ((line = br.readLine()) != null) {
 
80
                sbuf.append(line);
 
81
                sbuf.append(System.getProperty("line.separator"));
 
82
            }
 
83
        } finally {
 
84
            br.close();
 
85
        }
 
86
        return sbuf.toString();
 
87
    }
 
88
    
 
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();
 
94
        try {
 
95
            String line = null;
 
96
            while ((line = br.readLine()) != null) {
 
97
                sbuf.append(line);
 
98
                sbuf.append(System.getProperty("line.separator"));
 
99
            }
 
100
        } finally {
 
101
            br.close();
 
102
        }
 
103
        sd.insertString(0,sbuf.toString(),null);
 
104
        return sd;
 
105
    }
 
106
    
 
107
    public static int count = 0;
 
108
    public static WSDLModel loadWSDLModel(String resourcePath) throws Exception {
 
109
        NamespaceLocation nl = NamespaceLocation.valueFromResourcePath(resourcePath);
 
110
        if (nl != null) {
 
111
            return TestCatalogModel.getDefault().getWSDLModel(nl);
 
112
        }
 
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);
 
117
    }
 
118
    
 
119
    public static WSDLModel createEmptyWSDLModel() throws Exception {
 
120
        return loadWSDLModel(EMPTY_XSD);
 
121
    }
 
122
    
 
123
    /*public static WSDLModel loadWSDLModel(Document doc) throws Exception {
 
124
        return WSDLModelFactory.getDefault().getModel(doc);
 
125
    }*/
 
126
    
 
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()));
 
130
        w.close();
 
131
        out.close();
 
132
    }
 
133
    
 
134
    public static void dumpToFile(Document doc, File f) throws Exception {
 
135
        if (! f.exists()) {
 
136
            f.createNewFile();
 
137
        }
 
138
        OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
 
139
        PrintWriter w = new PrintWriter(out);
 
140
        w.print(doc.getText(0, doc.getLength()));
 
141
        w.close();
 
142
        out.close();
 
143
    }
 
144
    
 
145
    public static File dumpToTempFile(Document doc) throws Exception {
 
146
        File f = File.createTempFile("xsm", "xsd");
 
147
        dumpToFile(doc, f);
 
148
        return f;
 
149
    }
 
150
    
 
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);
 
156
    }
 
157
    
 
158
    public static Document loadDocument(File f) throws Exception {
 
159
        InputStream in = new BufferedInputStream(new FileInputStream(f));
 
160
        return loadDocument(in);
 
161
    }
 
162
    
 
163
    public static URI getResourceURI(String path) throws RuntimeException {
 
164
        try {
 
165
            return Util.class.getResource(path).toURI();
 
166
        } catch (Exception ex) {
 
167
            throw new RuntimeException(ex);
 
168
        }
 
169
    }
 
170
    
 
171
    public static File getTempDir(String path) throws Exception {
 
172
        File tempdir = new File(System.getProperty("java.io.tmpdir"), path);
 
173
        tempdir.mkdirs();
 
174
        return tempdir;
 
175
    }
 
176
 
 
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)){
 
182
                return ptype;
 
183
            }
 
184
        }
 
185
        return null;
 
186
    }
 
187
 
 
188
    public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception {
 
189
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
 
190
        StringBuffer sbuf = new StringBuffer();
 
191
        try {
 
192
            String line = null;
 
193
            while ((line = br.readLine()) != null) {
 
194
                sbuf.append(line);
 
195
                sbuf.append(System.getProperty("line.separator"));
 
196
            }
 
197
        } finally {
 
198
            br.close();
 
199
        }
 
200
        doc.remove(0, doc.getLength());
 
201
        doc.insertString(0,sbuf.toString(),null);
 
202
        return doc;
 
203
    }
 
204
    
 
205
    public static Document setDocumentContentTo(Document doc, String resourcePath) throws Exception {
 
206
        return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
 
207
    }
 
208
 
 
209
    public static void setDocumentContentTo(WSDLModel model, String resourcePath) throws Exception {
 
210
        Document doc = ((AbstractDocumentModel)model).getBaseDocument();
 
211
        setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
 
212
    }
 
213
 
 
214
    public static FileObject copyResource(String path, FileObject destFolder) throws Exception {
 
215
        String filename = getFileName(path);
 
216
        
 
217
        FileObject dest = destFolder.getFileObject(filename);
 
218
        if (dest == null) {
 
219
            dest = destFolder.createData(filename);
 
220
        }
 
221
        FileLock lock = dest.lock();
 
222
        OutputStream out = dest.getOutputStream(lock);
 
223
        InputStream in = Util.class.getResourceAsStream(path);
 
224
        try {
 
225
            FileUtil.copy(in, out);
 
226
        } finally {
 
227
            out.close();
 
228
            in.close();
 
229
            if (lock != null) lock.releaseLock();
 
230
        }
 
231
        return dest;
 
232
    }
 
233
    
 
234
    public static String getFileName(String path) {
 
235
        int i = path.lastIndexOf('/');
 
236
        if (i > -1) {
 
237
            return path.substring(i+1);
 
238
        } else {
 
239
            return path;
 
240
        }
 
241
    }
 
242
    
 
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));
 
245
    }
 
246
}