1 9 10 package org.netbeans.modules.websvc.wsitmodelext.util; 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.InputStream ; 19 import java.io.InputStreamReader ; 20 import java.io.OutputStream ; 21 import java.io.PrintWriter ; 22 import java.net.URI ; 23 import java.util.Collection ; 24 import javax.swing.text.Document ; 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 38 public class Util { 39 public static final String EMPTY_XSD = "resources/Empty.wsdl"; 40 41 public static Document getResourceAsDocument(String path) throws Exception { 42 InputStream in = Util.class.getResourceAsStream(path); 43 return loadDocument(in); 44 } 45 46 public static Document loadDocument(InputStream in) throws Exception { 47 Document sd = new org.netbeans.editor.BaseDocument( 48 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 49 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 50 StringBuffer sbuf = new StringBuffer (); 51 try { 52 String 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 resourcePath) throws Exception { 66 NamespaceLocation nl = NamespaceLocation.valueFromResourcePath(resourcePath); 67 if (nl != null) { 68 return TestCatalogModel.getDefault().getWSDLModel(nl); 69 } 70 String location = resourcePath.substring(resourcePath.lastIndexOf('/')+1); 71 URI locationURI = new URI (location); 72 TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath)); 73 return TestCatalogModel.getDefault().getWSDLModel(locationURI); 74 } 75 76 public static WSDLModel createEmptyWSDLModel() throws Exception { 77 return loadWSDLModel(EMPTY_XSD); 78 } 79 80 83 84 public static void dumpToStream(Document doc, OutputStream out) throws Exception { 85 PrintWriter w = new PrintWriter (out); 86 w.print(doc.getText(0, doc.getLength())); 87 w.close(); 88 out.close(); 89 } 90 91 public static void dumpToFile(Document doc, File f) throws Exception { 92 if (! f.exists()) { 93 f.createNewFile(); 94 } 95 OutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 96 PrintWriter w = new PrintWriter (out); 97 w.print(doc.getText(0, doc.getLength())); 98 w.close(); 99 out.close(); 100 } 101 102 public static File dumpToTempFile(Document doc) throws Exception { 103 File f = File.createTempFile("xsm", "xsd"); 104 dumpToFile(doc, f); 105 return f; 106 } 107 108 public static WSDLModel dumpAndReloadModel(Document doc) throws Exception { 109 File f = dumpToTempFile(doc); 110 URI dumpURI = new URI ("dummyDump" + count++); 111 TestCatalogModel.getDefault().addURI(dumpURI, f.toURI()); 112 return TestCatalogModel.getDefault().getWSDLModel(dumpURI); 113 } 114 115 public static Document loadDocument(File f) throws Exception { 116 InputStream in = new BufferedInputStream (new FileInputStream (f)); 117 return loadDocument(in); 118 } 119 120 public static URI getResourceURI(String path) throws RuntimeException { 121 try { 122 return Util.class.getResource(path).toURI(); 123 } catch (Exception ex) { 124 throw new RuntimeException (ex); 125 } 126 } 127 128 public static File getTempDir(String path) throws Exception { 129 File tempdir = new File (System.getProperty("java.io.tmpdir"), path); 130 tempdir.mkdirs(); 131 return tempdir; 132 } 133 134 public static GlobalSimpleType getPrimitiveType(String typeName){ 135 SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel(); 136 Collection <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 setDocumentContentTo(Document doc, InputStream in) throws Exception { 146 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 147 StringBuffer sbuf = new StringBuffer (); 148 try { 149 String 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 setDocumentContentTo(Document doc, String resourcePath) throws Exception { 163 return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath)); 164 } 165 166 public static void setDocumentContentTo(WSDLModel model, String resourcePath) throws Exception { 167 Document doc = ((AbstractDocumentModel)model).getBaseDocument(); 168 setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath)); 169 } 170 171 public static FileObject copyResource(String path, FileObject destFolder) throws Exception { 172 String 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 out = dest.getOutputStream(lock); 180 InputStream 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 getFileName(String 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 |