1 9 10 package org.netbeans.modules.xml.xam; 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 javax.swing.text.Document ; 24 import org.netbeans.modules.xml.xam.dom.DocumentModel; 25 import org.netbeans.modules.xml.xam.dom.ReadOnlyAccess; 26 import org.openide.util.Lookup; 27 import org.openide.util.lookup.Lookups; 28 29 33 public class Util { 34 public static final String EMPTY_XSD = "resources/Empty.xml"; 35 36 public static javax.swing.text.Document getResourceAsDocument(String path) throws Exception { 37 InputStream in = Util.class.getResourceAsStream(path); 38 return loadDocument(in); 39 } 40 41 public static javax.swing.text.Document loadDocument(InputStream in) throws Exception { 42 javax.swing.text.Document sd = ReadOnlyAccess.Provider.getInstance().loadSwingDocument(in); 43 return sd; 44 } 45 46 public static TestModel2 loadModel(String path) throws Exception { 47 TestModel2 model = new TestModel2(getResourceAsDocument(path)); 48 return model; 49 } 50 51 public static void dumpToStream(Document doc, OutputStream out) throws Exception { 52 PrintWriter w = new PrintWriter (out); 53 w.print(doc.getText(0, doc.getLength())); 54 w.close(); 55 out.close(); 56 } 57 58 public static void dumpToFile(Document doc, File f) throws Exception { 59 OutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 60 PrintWriter w = new PrintWriter (out); 61 w.print(doc.getText(0, doc.getLength())); 62 w.close(); 63 out.close(); 64 } 65 66 public static File dumpToTempFile(Document doc) throws Exception { 67 File f = File.createTempFile("xsm", "xsd"); 68 dumpToFile(doc, f); 69 return f; 70 } 71 72 public static Document loadDocument(File f) throws Exception { 73 InputStream in = new BufferedInputStream (new FileInputStream (f)); 74 return loadDocument(in); 75 } 76 77 public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception { 78 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 79 StringBuffer sbuf = new StringBuffer (); 80 try { 81 String line = null; 82 while ((line = br.readLine()) != null) { 83 sbuf.append(line); 84 sbuf.append(System.getProperty("line.separator")); 85 } 86 } finally { 87 br.close(); 88 } 89 doc.remove(0, doc.getLength()); 90 doc.insertString(0,sbuf.toString(),null); 91 return doc; 92 } 93 94 public static Document setDocumentContentTo(Document doc, String resourcePath) throws Exception { 95 return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath)); 96 } 97 98 public static TestModel2 dumpAndReloadModel(DocumentModel sm) throws Exception { 99 Document doc = (Document ) sm.getModelSource().getLookup().lookup(Document .class); 100 File f = dumpToTempFile(doc); 101 return new TestModel2(loadDocument(f)); 102 } 103 104 public static URI getResourceURI(String path) throws Exception { 105 return Util.class.getResource(path).toURI(); 106 } 107 108 public static File getResourceFile(String path) throws Exception { 109 return new File (getResourceURI(path)); 110 } 111 112 public static ModelSource createModelSource(Document doc) { 113 Lookup lookup = Lookups.fixed(new Object [] { doc } ); 114 return new ModelSource(lookup, true); 115 } 116 117 public static ModelSource createModelSource(String path) throws Exception { 118 Document doc = Util.getResourceAsDocument(path); 119 File file = Util.getResourceFile(path); 120 Lookup lookup = Lookups.fixed(new Object [] { doc, file } ); 121 return new ModelSource(lookup, true); 122 } 123 124 } 125 | Popular Tags |