1 19 20 package org.netbeans.modules.xml.wsdl.refactoring; 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.ArrayList ; 34 import java.util.Collection ; 35 import java.util.List ; 36 import javax.swing.text.Document ; 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 55 public class Util { 56 public static final String EMPTY_XSD = "resources/Empty.wsdl"; 57 58 public static Document getResourceAsDocument(String path) throws Exception { 59 InputStream in = Util.class.getResourceAsStream(path); 60 return loadDocument(in); 61 } 62 63 public static Document loadDocument(InputStream in) throws Exception { 64 Document sd = new org.netbeans.editor.BaseDocument( 65 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 66 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 67 StringBuffer sbuf = new StringBuffer (); 68 try { 69 String 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 resourcePath) throws Exception { 83 NamespaceLocation nl = NamespaceLocation.valueFromResourcePath(resourcePath); 84 if (nl != null) { 85 return TestCatalogModel.getDefault().getWSDLModel(nl); 86 } 87 String location = resourcePath.substring(resourcePath.lastIndexOf('/')+1); 88 URI locationURI = new URI (location); 89 TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath)); 90 return TestCatalogModel.getDefault().getWSDLModel(locationURI); 91 } 92 93 public static WSDLModel createEmptyWSDLModel() throws Exception { 94 return loadWSDLModel(EMPTY_XSD); 95 } 96 97 100 101 public static void dumpToStream(Document doc, OutputStream out) throws Exception { 102 PrintWriter w = new PrintWriter (out); 103 w.print(doc.getText(0, doc.getLength())); 104 w.close(); 105 out.close(); 106 } 107 108 public static void dumpToFile(Document doc, File f) throws Exception { 109 if (! f.exists()) { 110 f.createNewFile(); 111 } 112 OutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 113 PrintWriter w = new PrintWriter (out); 114 w.print(doc.getText(0, doc.getLength())); 115 w.close(); 116 out.close(); 117 } 118 119 public static File dumpToTempFile(Document doc) throws Exception { 120 File f = File.createTempFile("xsm", "xsd"); 121 dumpToFile(doc, f); 122 return f; 123 } 124 125 public static WSDLModel dumpAndReloadModel(Document doc) throws Exception { 126 File f = dumpToTempFile(doc); 127 URI dumpURI = new URI ("dummyDump" + count++); 128 TestCatalogModel.getDefault().addURI(dumpURI, f.toURI()); 129 return TestCatalogModel.getDefault().getWSDLModel(dumpURI); 130 } 131 132 public static Document loadDocument(File f) throws Exception { 133 InputStream in = new BufferedInputStream (new FileInputStream (f)); 134 return loadDocument(in); 135 } 136 137 public static URI getResourceURI(String path) throws RuntimeException { 138 try { 139 return Util.class.getResource(path).toURI(); 140 } catch (Exception ex) { 141 throw new RuntimeException (ex); 142 } 143 } 144 145 public static File getTempDir(String path) throws Exception { 146 File tempdir = new File (System.getProperty("java.io.tmpdir"), path); 147 tempdir.mkdirs(); 148 return tempdir; 149 } 150 151 public static GlobalSimpleType getPrimitiveType(String typeName){ 152 SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel(); 153 Collection <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 setDocumentContentTo(Document doc, InputStream in) throws Exception { 163 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 164 StringBuffer sbuf = new StringBuffer (); 165 try { 166 String 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 setDocumentContentTo(Document doc, String resourcePath) throws Exception { 180 return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath)); 181 } 182 183 public static void setDocumentContentTo(WSDLModel model, String resourcePath) throws Exception { 184 Document doc = ((AbstractDocumentModel)model).getBaseDocument(); 185 setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath)); 186 } 187 188 public static FileObject copyResource(String path, FileObject destFolder) throws Exception { 189 String 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 out = dest.getOutputStream(lock); 197 InputStream 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 getFileName(String 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 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 xpath) { 227 return findByXpath(model.getDefinitions(), xpath); 228 } 229 230 public static WSDLComponent findByXpath(Definitions root, String xpath) { 231 return (WSDLComponent) new FindWSDLComponent().findComponent(root, xpath); 232 } 233 234 public static <T extends Object > List <T> toList(Collection <T> c) { 235 return new ArrayList <T>(c); 236 } 237 238 } 239 | Popular Tags |