KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > refactoring > Util


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 package org.netbeans.modules.xml.wsdl.refactoring;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.net.URI JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.List JavaDoc;
36 import javax.swing.text.Document JavaDoc;
37 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
38 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
39 import org.netbeans.modules.xml.schema.model.Schema;
40 import org.netbeans.modules.xml.schema.model.SchemaModel;
41 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
42 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
43 import org.netbeans.modules.xml.wsdl.model.Definitions;
44 import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
45 import org.netbeans.modules.xml.wsdl.model.visitor.FindWSDLComponent;
46 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
47 import org.openide.filesystems.FileLock;
48 import org.openide.filesystems.FileObject;
49 import org.openide.filesystems.FileUtil;
50
51 /**
52  *
53  * @author nn136682
54  */

55 public class Util {
56     public static final String JavaDoc EMPTY_XSD = "resources/Empty.wsdl";
57     
58     public static Document JavaDoc getResourceAsDocument(String JavaDoc path) throws Exception JavaDoc {
59         InputStream JavaDoc in = Util.class.getResourceAsStream(path);
60         return loadDocument(in);
61     }
62     
63     public static Document JavaDoc loadDocument(InputStream JavaDoc in) throws Exception JavaDoc {
64     Document JavaDoc sd = new org.netbeans.editor.BaseDocument(
65             org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
66         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
67         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
68         try {
69             String JavaDoc line = null;
70             while ((line = br.readLine()) != null) {
71                 sbuf.append(line);
72                 sbuf.append(System.getProperty("line.separator"));
73             }
74         } finally {
75             br.close();
76         }
77         sd.insertString(0,sbuf.toString(),null);
78         return sd;
79     }
80     
81     public static int count = 0;
82     public static WSDLModel loadWSDLModel(String JavaDoc resourcePath) throws Exception JavaDoc {
83         NamespaceLocation nl = NamespaceLocation.valueFromResourcePath(resourcePath);
84         if (nl != null) {
85             return TestCatalogModel.getDefault().getWSDLModel(nl);
86         }
87         String JavaDoc location = resourcePath.substring(resourcePath.lastIndexOf('/')+1);
88         URI JavaDoc locationURI = new URI JavaDoc(location);
89         TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath));
90         return TestCatalogModel.getDefault().getWSDLModel(locationURI);
91     }
92     
93     public static WSDLModel createEmptyWSDLModel() throws Exception JavaDoc {
94         return loadWSDLModel(EMPTY_XSD);
95     }
96     
97     /*public static WSDLModel loadWSDLModel(Document doc) throws Exception {
98         return WSDLModelFactory.getDefault().getModel(doc);
99     }*/

100     
101     public static void dumpToStream(Document JavaDoc doc, OutputStream JavaDoc out) throws Exception JavaDoc{
102         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
103         w.print(doc.getText(0, doc.getLength()));
104         w.close();
105         out.close();
106     }
107     
108     public static void dumpToFile(Document JavaDoc doc, File JavaDoc f) throws Exception JavaDoc {
109         if (! f.exists()) {
110             f.createNewFile();
111         }
112         OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
113         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
114         w.print(doc.getText(0, doc.getLength()));
115         w.close();
116         out.close();
117     }
118     
119     public static File JavaDoc dumpToTempFile(Document JavaDoc doc) throws Exception JavaDoc {
120         File JavaDoc f = File.createTempFile("xsm", "xsd");
121         dumpToFile(doc, f);
122         return f;
123     }
124     
125     public static WSDLModel dumpAndReloadModel(Document JavaDoc doc) throws Exception JavaDoc {
126         File JavaDoc f = dumpToTempFile(doc);
127         URI JavaDoc dumpURI = new URI JavaDoc("dummyDump" + count++);
128         TestCatalogModel.getDefault().addURI(dumpURI, f.toURI());
129         return TestCatalogModel.getDefault().getWSDLModel(dumpURI);
130     }
131     
132     public static Document JavaDoc loadDocument(File JavaDoc f) throws Exception JavaDoc {
133         InputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(f));
134         return loadDocument(in);
135     }
136     
137     public static URI JavaDoc getResourceURI(String JavaDoc path) throws RuntimeException JavaDoc {
138         try {
139             return Util.class.getResource(path).toURI();
140         } catch (Exception JavaDoc ex) {
141             throw new RuntimeException JavaDoc(ex);
142         }
143     }
144     
145     public static File JavaDoc getTempDir(String JavaDoc path) throws Exception JavaDoc {
146         File JavaDoc tempdir = new File JavaDoc(System.getProperty("java.io.tmpdir"), path);
147         tempdir.mkdirs();
148         return tempdir;
149     }
150
151     public static GlobalSimpleType getPrimitiveType(String JavaDoc typeName){
152         SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel();
153         Collection JavaDoc<GlobalSimpleType> primitives = primitiveModel.getSchema().getSimpleTypes();
154         for(GlobalSimpleType ptype: primitives){
155             if(ptype.getName().equals(typeName)){
156                 return ptype;
157             }
158         }
159         return null;
160     }
161
162     public static Document JavaDoc setDocumentContentTo(Document JavaDoc doc, InputStream JavaDoc in) throws Exception JavaDoc {
163         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
164         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
165         try {
166             String JavaDoc line = null;
167             while ((line = br.readLine()) != null) {
168                 sbuf.append(line);
169                 sbuf.append(System.getProperty("line.separator"));
170             }
171         } finally {
172             br.close();
173         }
174         doc.remove(0, doc.getLength());
175         doc.insertString(0,sbuf.toString(),null);
176         return doc;
177     }
178     
179     public static Document JavaDoc setDocumentContentTo(Document JavaDoc doc, String JavaDoc resourcePath) throws Exception JavaDoc {
180         return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
181     }
182
183     public static void setDocumentContentTo(WSDLModel model, String JavaDoc resourcePath) throws Exception JavaDoc {
184         Document JavaDoc doc = ((AbstractDocumentModel)model).getBaseDocument();
185         setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
186     }
187
188     public static FileObject copyResource(String JavaDoc path, FileObject destFolder) throws Exception JavaDoc {
189         String JavaDoc filename = getFileName(path);
190         
191         FileObject dest = destFolder.getFileObject(filename);
192         if (dest == null) {
193             dest = destFolder.createData(filename);
194         }
195         FileLock lock = dest.lock();
196         OutputStream JavaDoc out = dest.getOutputStream(lock);
197         InputStream JavaDoc in = Util.class.getResourceAsStream(path);
198         try {
199             FileUtil.copy(in, out);
200         } finally {
201             out.close();
202             in.close();
203             if (lock != null) lock.releaseLock();
204         }
205         return dest;
206     }
207     
208     public static String JavaDoc getFileName(String JavaDoc path) {
209         int i = path.lastIndexOf('/');
210         if (i > -1) {
211             return path.substring(i+1);
212         } else {
213             return path;
214         }
215     }
216    
217     public static ReferenceableSchemaComponent findGlobalComponentByName(Schema schema, String JavaDoc name) {
218         for (ReferenceableSchemaComponent t : schema.getChildren(ReferenceableSchemaComponent.class)) {
219             if (t.getName().equals(name)) {
220                 return t;
221             }
222         }
223         return null;
224     }
225
226     public static WSDLComponent findByXpath(WSDLModel model, String JavaDoc xpath) {
227         return findByXpath(model.getDefinitions(), xpath);
228     }
229
230     public static WSDLComponent findByXpath(Definitions root, String JavaDoc xpath) {
231         return (WSDLComponent) new FindWSDLComponent().findComponent(root, xpath);
232     }
233
234     public static <T extends Object JavaDoc> List JavaDoc<T> toList(Collection JavaDoc<T> c) {
235         return new ArrayList JavaDoc<T>(c);
236     }
237     
238 }
239
Popular Tags