KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > Util


1 /*
2  * Util.java
3  *
4  * Created on October 4, 2005, 7:48 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.xml.xam;
11
12 import java.io.BufferedInputStream JavaDoc;
13 import java.io.BufferedOutputStream JavaDoc;
14 import java.io.BufferedReader JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileInputStream JavaDoc;
17 import java.io.FileOutputStream JavaDoc;
18 import java.io.InputStream JavaDoc;
19 import java.io.InputStreamReader JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.net.URI JavaDoc;
23 import javax.swing.text.Document JavaDoc;
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 /**
30  *
31  * @author nn136682
32  */

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