1 19 20 package org.netbeans.modules.xml.wsdl.model.extensions; 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.BufferedReader ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.InputStream ; 29 import java.io.InputStreamReader ; 30 import java.io.OutputStream ; 31 import java.io.PrintWriter ; 32 import java.net.URI ; 33 import java.util.Collection ; 34 import javax.swing.text.Document ; 35 import org.netbeans.modules.xml.schema.model.GlobalSimpleType; 36 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent; 37 import org.netbeans.modules.xml.schema.model.Schema; 38 import org.netbeans.modules.xml.schema.model.SchemaModel; 39 import org.netbeans.modules.xml.wsdl.model.WSDLModel; 40 import org.netbeans.modules.xml.schema.model.SchemaModelFactory; 41 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 42 import org.openide.filesystems.FileLock; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileUtil; 45 46 50 public class Util { 51 public static final String EMPTY_XSD = "resources/Empty.wsdl"; 52 53 public static Document getResourceAsDocument(String path) throws Exception { 54 InputStream in = Util.class.getResourceAsStream(path); 55 return loadDocument(in); 56 } 57 58 public static Document loadDocument(InputStream in) throws Exception { 59 Document sd = new org.netbeans.editor.BaseDocument( 60 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 61 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 62 StringBuffer sbuf = new StringBuffer (); 63 try { 64 String line = null; 65 while ((line = br.readLine()) != null) { 66 sbuf.append(line); 67 sbuf.append(System.getProperty("line.separator")); 68 } 69 } finally { 70 br.close(); 71 } 72 sd.insertString(0,sbuf.toString(),null); 73 return sd; 74 } 75 76 public static int count = 0; 77 public static WSDLModel loadWSDLModel(String resourcePath) throws Exception { 78 NamespaceLocation nl = NamespaceLocation.valueFromResourcePath(resourcePath); 79 if (nl != null) { 80 return TestCatalogModel.getDefault().getWSDLModel(nl); 81 } 82 String location = resourcePath.substring(resourcePath.lastIndexOf('/')+1); 83 URI locationURI = new URI (location); 84 TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath)); 85 return TestCatalogModel.getDefault().getWSDLModel(locationURI); 86 } 87 88 public static WSDLModel createEmptyWSDLModel() throws Exception { 89 return loadWSDLModel(EMPTY_XSD); 90 } 91 92 95 96 public static void dumpToStream(Document doc, OutputStream out) throws Exception { 97 PrintWriter w = new PrintWriter (out); 98 w.print(doc.getText(0, doc.getLength())); 99 w.close(); 100 out.close(); 101 } 102 103 public static void dumpToFile(Document doc, File f) throws Exception { 104 if (! f.exists()) { 105 f.createNewFile(); 106 } 107 OutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 108 PrintWriter w = new PrintWriter (out); 109 w.print(doc.getText(0, doc.getLength())); 110 w.close(); 111 out.close(); 112 } 113 114 public static File dumpToTempFile(Document doc) throws Exception { 115 File f = File.createTempFile("xsm", "xsd"); 116 dumpToFile(doc, f); 117 return f; 118 } 119 120 public static WSDLModel dumpAndReloadModel(Document doc) throws Exception { 121 File f = dumpToTempFile(doc); 122 URI dumpURI = new URI ("dummyDump" + count++); 123 TestCatalogModel.getDefault().addURI(dumpURI, f.toURI()); 124 return TestCatalogModel.getDefault().getWSDLModel(dumpURI); 125 } 126 127 public static Document loadDocument(File f) throws Exception { 128 InputStream in = new BufferedInputStream (new FileInputStream (f)); 129 return loadDocument(in); 130 } 131 132 public static URI getResourceURI(String path) throws RuntimeException { 133 try { 134 return Util.class.getResource(path).toURI(); 135 } catch (Exception ex) { 136 throw new RuntimeException (ex); 137 } 138 } 139 140 public static File getTempDir(String path) throws Exception { 141 File tempdir = new File (System.getProperty("java.io.tmpdir"), path); 142 tempdir.mkdirs(); 143 return tempdir; 144 } 145 146 public static GlobalSimpleType getPrimitiveType(String typeName){ 147 SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel(); 148 Collection <GlobalSimpleType> primitives = primitiveModel.getSchema().getSimpleTypes(); 149 for(GlobalSimpleType ptype: primitives){ 150 if(ptype.getName().equals(typeName)){ 151 return ptype; 152 } 153 } 154 return null; 155 } 156 157 public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception { 158 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 159 StringBuffer sbuf = new StringBuffer (); 160 try { 161 String line = null; 162 while ((line = br.readLine()) != null) { 163 sbuf.append(line); 164 sbuf.append(System.getProperty("line.separator")); 165 } 166 } finally { 167 br.close(); 168 } 169 doc.remove(0, doc.getLength()); 170 doc.insertString(0,sbuf.toString(),null); 171 return doc; 172 } 173 174 public static Document setDocumentContentTo(Document doc, String resourcePath) throws Exception { 175 return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath)); 176 } 177 178 public static void setDocumentContentTo(WSDLModel model, String resourcePath) throws Exception { 179 Document doc = ((AbstractDocumentModel)model).getBaseDocument(); 180 setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath)); 181 } 182 183 public static FileObject copyResource(String path, FileObject destFolder) throws Exception { 184 String filename = getFileName(path); 185 186 FileObject dest = destFolder.getFileObject(filename); 187 if (dest == null) { 188 dest = destFolder.createData(filename); 189 } 190 FileLock lock = dest.lock(); 191 OutputStream out = dest.getOutputStream(lock); 192 InputStream in = Util.class.getResourceAsStream(path); 193 try { 194 FileUtil.copy(in, out); 195 } finally { 196 out.close(); 197 in.close(); 198 if (lock != null) lock.releaseLock(); 199 } 200 return dest; 201 } 202 203 public static String getFileName(String path) { 204 int i = path.lastIndexOf('/'); 205 if (i > -1) { 206 return path.substring(i+1); 207 } else { 208 return path; 209 } 210 } 211 212 public static ReferenceableSchemaComponent findGlobalComponentByName(Schema schema, String name) { 213 for (ReferenceableSchemaComponent t : schema.getChildren(ReferenceableSchemaComponent.class)) { 214 if (t.getName().equals(name)) { 215 return t; 216 } 217 } 218 return null; 219 } 220 221 } 222 | Popular Tags |