001: /*
002: * Util.java
003: *
004: * Created on October 4, 2005, 7:48 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.netbeans.modules.iep.model.common;
011:
012: import java.io.BufferedInputStream;
013: import java.io.BufferedOutputStream;
014: import java.io.BufferedReader;
015: import java.io.File;
016: import java.io.FileInputStream;
017: import java.io.FileOutputStream;
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.InputStreamReader;
021: import java.io.OutputStream;
022: import java.io.PrintWriter;
023: import java.net.URI;
024: import java.util.Collection;
025: import javax.swing.text.Document;
026: import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
027: import org.netbeans.modules.xml.schema.model.SchemaModel;
028: import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
029: import org.netbeans.modules.xml.wsdl.model.WSDLComponent;
030: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
031: import org.netbeans.modules.xml.wsdl.model.visitor.FindWSDLComponent;
032: import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
033: import org.openide.filesystems.FileObject;
034: import org.openide.filesystems.FileUtil;
035: import org.openide.filesystems.FileLock;
036: import org.openide.filesystems.Repository;
037:
038: /**
039: *
040: * @author nn136682
041: */
042: public class Util {
043: public static final String EMPTY_XSD = "resources/Empty.wsdl";
044:
045: static {
046: registerXMLKit();
047: }
048:
049: public static void registerXMLKit() {
050: String[] path = new String[] { "Editors", "text", "x-xml" };
051: FileObject target = Repository.getDefault()
052: .getDefaultFileSystem().getRoot();
053: try {
054: for (int i = 0; i < path.length; i++) {
055: FileObject f = target.getFileObject(path[i]);
056: if (f == null) {
057: f = target.createFolder(path[i]);
058: }
059: target = f;
060: }
061: String name = "EditorKit.instance";
062: if (target.getFileObject(name) == null) {
063: FileObject f = target.createData(name);
064: f.setAttribute("instanceClass",
065: "org.netbeans.modules.xml.text.syntax.XMLKit");
066: }
067: } catch (IOException ioe) {
068: ioe.printStackTrace();
069: }
070: }
071:
072: public static Document getResourceAsDocument(String path)
073: throws Exception {
074: InputStream in = Util.class.getResourceAsStream(path);
075: return loadDocument(in);
076: }
077:
078: public static String getResourceAsString(String path)
079: throws Exception {
080: InputStream in = Util.class.getResourceAsStream(path);
081: BufferedReader br = new BufferedReader(new InputStreamReader(
082: in, "UTF-8"));
083: StringBuffer sbuf = new StringBuffer();
084: try {
085: String line = null;
086: while ((line = br.readLine()) != null) {
087: sbuf.append(line);
088: sbuf.append(System.getProperty("line.separator"));
089: }
090: } finally {
091: br.close();
092: }
093: return sbuf.toString();
094: }
095:
096: public static Document loadDocument(InputStream in)
097: throws Exception {
098: Document sd = new org.netbeans.editor.BaseDocument(
099: org.netbeans.modules.xml.text.syntax.XMLKit.class,
100: false);
101: BufferedReader br = new BufferedReader(
102: new InputStreamReader(in));
103: StringBuffer sbuf = new StringBuffer();
104: try {
105: String line = null;
106: while ((line = br.readLine()) != null) {
107: sbuf.append(line);
108: sbuf.append(System.getProperty("line.separator"));
109: }
110: } finally {
111: br.close();
112: }
113: sd.insertString(0, sbuf.toString(), null);
114: return sd;
115: }
116:
117: public static int count = 0;
118:
119: public static WSDLModel loadWSDLModel(String resourcePath)
120: throws Exception {
121: NamespaceLocation nl = NamespaceLocation
122: .valueFromResourcePath(resourcePath);
123: if (nl != null) {
124: return TestCatalogModel.getDefault().getWSDLModel(nl);
125: }
126: String location = resourcePath.substring(resourcePath
127: .lastIndexOf('/') + 1);
128: URI locationURI = new URI(location);
129: TestCatalogModel.getDefault().addURI(locationURI,
130: getResourceURI(resourcePath));
131: return TestCatalogModel.getDefault().getWSDLModel(locationURI);
132: }
133:
134: public static WSDLModel createEmptyWSDLModel() throws Exception {
135: return loadWSDLModel(EMPTY_XSD);
136: }
137:
138: /*public static WSDLModel loadWSDLModel(Document doc) throws Exception {
139: return WSDLModelFactory.getDefault().getModel(doc);
140: }*/
141:
142: public static void dumpToStream(Document doc, OutputStream out)
143: throws Exception {
144: PrintWriter w = new PrintWriter(out);
145: w.print(doc.getText(0, doc.getLength()));
146: w.close();
147: out.close();
148: }
149:
150: public static void dumpToFile(Document doc, File f)
151: throws Exception {
152: if (!f.exists()) {
153: f.createNewFile();
154: }
155: OutputStream out = new BufferedOutputStream(
156: new FileOutputStream(f));
157: PrintWriter w = new PrintWriter(out);
158: w.print(doc.getText(0, doc.getLength()));
159: w.close();
160: out.close();
161: }
162:
163: public static File dumpToTempFile(Document doc) throws Exception {
164: File f = File.createTempFile("xsm", "xsd");
165: dumpToFile(doc, f);
166: return f;
167: }
168:
169: public static WSDLModel dumpAndReloadModel(Document doc)
170: throws Exception {
171: File f = dumpToTempFile(doc);
172: URI dumpURI = new URI("dummyDump" + count++);
173: TestCatalogModel.getDefault().addURI(dumpURI, f.toURI());
174: return TestCatalogModel.getDefault().getWSDLModel(dumpURI);
175: }
176:
177: public static Document loadDocument(File f) throws Exception {
178: InputStream in = new BufferedInputStream(new FileInputStream(f));
179: return loadDocument(in);
180: }
181:
182: public static URI getResourceURI(String path)
183: throws RuntimeException {
184: try {
185: return Util.class.getResource(path).toURI();
186: } catch (Exception ex) {
187: throw new RuntimeException(ex);
188: }
189: }
190:
191: public static File getTempDir(String path) throws Exception {
192: File tempdir = new File(System.getProperty("java.io.tmpdir"),
193: path);
194: tempdir.mkdirs();
195: return tempdir;
196: }
197:
198: public static GlobalSimpleType getPrimitiveType(String typeName) {
199: SchemaModel primitiveModel = SchemaModelFactory.getDefault()
200: .getPrimitiveTypesModel();
201: Collection<GlobalSimpleType> primitives = primitiveModel
202: .getSchema().getSimpleTypes();
203: for (GlobalSimpleType ptype : primitives) {
204: if (ptype.getName().equals(typeName)) {
205: return ptype;
206: }
207: }
208: return null;
209: }
210:
211: public static Document setDocumentContentTo(Document doc,
212: InputStream in) throws Exception {
213: BufferedReader br = new BufferedReader(
214: new InputStreamReader(in));
215: StringBuffer sbuf = new StringBuffer();
216: try {
217: String line = null;
218: while ((line = br.readLine()) != null) {
219: sbuf.append(line);
220: sbuf.append(System.getProperty("line.separator"));
221: }
222: } finally {
223: br.close();
224: }
225: doc.remove(0, doc.getLength());
226: doc.insertString(0, sbuf.toString(), null);
227: return doc;
228: }
229:
230: public static Document setDocumentContentTo(Document doc,
231: String resourcePath) throws Exception {
232: return setDocumentContentTo(doc, Util.class
233: .getResourceAsStream(resourcePath));
234: }
235:
236: public static void setDocumentContentTo(WSDLModel model,
237: String resourcePath) throws Exception {
238: Document doc = ((AbstractDocumentModel) model)
239: .getBaseDocument();
240: setDocumentContentTo(doc, Util.class
241: .getResourceAsStream(resourcePath));
242: }
243:
244: public static FileObject copyResource(String path,
245: FileObject destFolder) throws Exception {
246: String filename = getFileName(path);
247:
248: FileObject dest = destFolder.getFileObject(filename);
249: if (dest == null) {
250: dest = destFolder.createData(filename);
251: }
252: FileLock lock = dest.lock();
253: OutputStream out = dest.getOutputStream(lock);
254: InputStream in = Util.class.getResourceAsStream(path);
255: try {
256: FileUtil.copy(in, out);
257: } finally {
258: out.close();
259: in.close();
260: if (lock != null)
261: lock.releaseLock();
262: }
263: return dest;
264: }
265:
266: public static String getFileName(String path) {
267: int i = path.lastIndexOf('/');
268: if (i > -1) {
269: return path.substring(i + 1);
270: } else {
271: return path;
272: }
273: }
274:
275: public static <T extends WSDLComponent> T find(Class<T> type,
276: WSDLModel model, String xpath) {
277: return type.cast(FindWSDLComponent.findComponent(type, model
278: .getDefinitions(), xpath));
279: }
280: }
|