001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.wsdlextensions.ftp.validation.test;
021:
022: import java.io.BufferedInputStream;
023: import java.io.BufferedOutputStream;
024: import java.io.BufferedReader;
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.FileOutputStream;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030: import java.io.OutputStream;
031: import java.io.PrintWriter;
032: import java.net.URI;
033: import java.util.Collection;
034: import javax.swing.text.Document;
035: import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
036: import org.netbeans.modules.xml.schema.model.SchemaModel;
037: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
038: import org.netbeans.modules.xml.wsdl.model.visitor.FindWSDLComponent;
039: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
040: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
041: import org.openide.filesystems.FileObject;
042: import org.openide.filesystems.FileUtil;
043: import org.openide.filesystems.FileLock;
044:
045: public class Util {
046: public static final String EMPTY_XSD = "resources/Empty.wsdl";
047:
048: // public static Document getResourceAsDocument(String path) throws Exception {
049: // InputStream in = Util.class.getResourceAsStream(path);
050: // return loadDocument(in);
051: // }
052:
053: // public static Document loadDocument(InputStream in) throws Exception {
054: // Document sd = new org.netbeans.editor.BaseDocument(
055: // org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
056: // BufferedReader br = new BufferedReader(new InputStreamReader(in));
057: // StringBuffer sbuf = new StringBuffer();
058: // try {
059: // String line = null;
060: // while ((line = br.readLine()) != null) {
061: // sbuf.append(line);
062: // sbuf.append(System.getProperty("line.separator"));
063: // }
064: // } finally {
065: // br.close();
066: // }
067: // sd.insertString(0,sbuf.toString(),null);
068: // return sd;
069: // }
070:
071: public static int count = 0;
072:
073: public static WSDLModel loadWSDLModel(String resourcePath)
074: throws Exception {
075: NamespaceLocation nl = NamespaceLocation
076: .valueFromResourcePath(resourcePath);
077: if (nl != null) {
078: return TestCatalogModel.getDefault().getWSDLModel(nl);
079: }
080: String location = resourcePath.substring(resourcePath
081: .lastIndexOf('/') + 1);
082: URI locationURI = new URI(location);
083: TestCatalogModel.getDefault().addURI(locationURI,
084: getResourceURI(resourcePath));
085: return TestCatalogModel.getDefault().getWSDLModel(locationURI);
086: }
087:
088: // public static WSDLModel createEmptyWSDLModel() throws Exception {
089: // return loadWSDLModel(EMPTY_XSD);
090: // }
091:
092: /*public static WSDLModel loadWSDLModel(Document doc) throws Exception {
093: return WSDLModelFactory.getDefault().getModel(doc);
094: }*/
095:
096: // public static void dumpToStream(Document doc, OutputStream out) throws Exception{
097: // PrintWriter w = new PrintWriter(out);
098: // w.print(doc.getText(0, doc.getLength()));
099: // 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: // public static Document loadDocument(File f) throws Exception {
127: // InputStream in = new BufferedInputStream(new FileInputStream(f));
128: // return loadDocument(in);
129: // }
130: public static URI getResourceURI(String path)
131: throws RuntimeException {
132: try {
133: return Util.class.getResource(path).toURI();
134: } catch (Exception ex) {
135: throw new RuntimeException(ex);
136: }
137: }
138:
139: public static File getTempDir(String path) throws Exception {
140: File tempdir = new File(System.getProperty("java.io.tmpdir"),
141: 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,
184: FileObject destFolder) throws Exception {
185: String filename = getFileName(path);
186:
187: FileObject dest = destFolder.getFileObject(filename);
188: if (dest == null) {
189: dest = destFolder.createData(filename);
190: }
191: FileLock lock = dest.lock();
192: OutputStream out = dest.getOutputStream(lock);
193: InputStream in = Util.class.getResourceAsStream(path);
194: try {
195: FileUtil.copy(in, out);
196: } finally {
197: out.close();
198: in.close();
199: if (lock != null)
200: lock.releaseLock();
201: }
202: return dest;
203: }
204:
205: public static String getFileName(String path) {
206: int i = path.lastIndexOf('/');
207: if (i > -1) {
208: return path.substring(i + 1);
209: } else {
210: return path;
211: }
212: }
213:
214: // public static <T extends WSDLComponent> T find(Class<T> type, WSDLModel model, String xpath) {
215: // return type.cast(FindWSDLComponent.findComponent(type, model.getDefinitions(), xpath));
216: // }
217: }
|