KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > model > 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.schema.model;
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.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20 import java.io.InputStreamReader JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23 import java.lang.management.ManagementFactory JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.util.Collection JavaDoc;
26 import javax.swing.text.Document JavaDoc;
27 import org.netbeans.modules.xml.schema.model.impl.SchemaModelImpl;
28 import org.netbeans.modules.xml.schema.model.visitor.FindSchemaComponentFromDOM;
29 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
30 import org.netbeans.modules.xml.xam.dom.DocumentModel;
31 import org.openide.filesystems.FileLock;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileUtil;
34 import org.openide.filesystems.Repository;
35 import org.w3c.dom.Element JavaDoc;
36 /**
37  *
38  * @author nn136682
39  */

40 public class Util {
41     public static final String JavaDoc EMPTY_XSD = "resources/Empty.xsd";
42     
43     static {
44         //JEditorPane.registerEditorKitForContentType(SchemaDataLoader.MIME_TYPE, XMLKit.class.getName());
45
registerXMLKit();
46     }
47     
48     public static void registerXMLKit() {
49         String JavaDoc[] path = new String JavaDoc[] { "Editors", "text", "x-xml" };
50         FileObject target = Repository.getDefault().getDefaultFileSystem().getRoot();
51         try {
52             for (int i=0; i<path.length; i++) {
53                 FileObject f = target.getFileObject(path[i]);
54                 if (f == null) {
55                     f = target.createFolder(path[i]);
56                 }
57                 target = f;
58             }
59             String JavaDoc name = "EditorKit.instance";
60             if (target.getFileObject(name) == null) {
61                 FileObject f = target.createData(name);
62                 f.setAttribute("instanceClass", "org.netbeans.modules.xml.text.syntax.XMLKit");
63             }
64         } catch(IOException JavaDoc ioe) {
65             ioe.printStackTrace();
66         }
67     }
68     
69     public static Document JavaDoc getResourceAsDocument(String JavaDoc path) throws Exception JavaDoc {
70         InputStream JavaDoc in = Util.class.getResourceAsStream(path);
71         return loadDocument(in);
72     }
73
74     public static Document JavaDoc loadDocument(InputStream JavaDoc in) throws Exception JavaDoc {
75 // Document sd = new PlainDocument();
76
Document JavaDoc sd = new org.netbeans.editor.BaseDocument(
77                 org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
78         return setDocumentContentTo(sd, in);
79     }
80     
81     public static Document JavaDoc setDocumentContentTo(Document JavaDoc doc, InputStream JavaDoc in) throws Exception JavaDoc {
82         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
83         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
84         try {
85             String JavaDoc line = null;
86             while ((line = br.readLine()) != null) {
87                 sbuf.append(line);
88                 sbuf.append(System.getProperty("line.separator"));
89             }
90         } finally {
91             br.close();
92         }
93         doc.remove(0, doc.getLength());
94         doc.insertString(0,sbuf.toString(),null);
95         return doc;
96     }
97     
98     public static Document JavaDoc setDocumentContentTo(Document JavaDoc doc, String JavaDoc resourcePath) throws Exception JavaDoc {
99         return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
100     }
101
102     public static void setDocumentContentTo(DocumentModel model, String JavaDoc resourcePath) throws Exception JavaDoc {
103         setDocumentContentTo(((AbstractDocumentModel)model).getBaseDocument(), resourcePath);
104     }
105     
106     public static int count = 0;
107     public static SchemaModel loadSchemaModel(String JavaDoc resourcePath) throws Exception JavaDoc {
108         NamespaceLocation nl = NamespaceLocation.valueFromResourcePath(resourcePath);
109         if (nl != null) {
110             return TestCatalogModel.getDefault().getSchemaModel(nl);
111         }
112         String JavaDoc location = resourcePath.substring(resourcePath.lastIndexOf('/')+1);
113         URI JavaDoc locationURI = new URI JavaDoc(location);
114         TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath));
115         return TestCatalogModel.getDefault().getSchemaModel(locationURI);
116     }
117     
118     public static SchemaModel loadSchemaModel(File JavaDoc schemaFile) throws Exception JavaDoc {
119         URI JavaDoc locationURI = new URI JavaDoc(schemaFile.getName());
120         TestCatalogModel.getDefault().addURI(locationURI, schemaFile.toURI());
121         return TestCatalogModel.getDefault().getSchemaModel(locationURI);
122     }
123     
124     public static SchemaModel createEmptySchemaModel() throws Exception JavaDoc {
125         return loadSchemaModel(EMPTY_XSD);
126     }
127     
128     public static void dumpToStream(Document JavaDoc doc, OutputStream JavaDoc out) throws Exception JavaDoc{
129         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
130         w.print(doc.getText(0, doc.getLength()));
131         w.close();
132         out.close();
133     }
134     
135     public static void dumpToFile(Document JavaDoc doc, File JavaDoc f) throws Exception JavaDoc {
136         OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
137         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
138         w.print(doc.getText(0, doc.getLength()));
139         w.close();
140         out.close();
141     }
142     
143     public static File JavaDoc dumpToTempFile(Document JavaDoc doc) throws Exception JavaDoc {
144         File JavaDoc f = File.createTempFile("xsm", "xsd");
145         dumpToFile(doc, f);
146         return f;
147     }
148     
149     public static Document JavaDoc loadDocument(File JavaDoc f) throws Exception JavaDoc {
150         InputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(f));
151         return loadDocument(in);
152     }
153     
154     public static SchemaComponent findComponent(Schema schema, String JavaDoc xpath) {
155         return (new FindSchemaComponentFromDOM().findComponent(schema, xpath));
156     }
157     
158     public static Sequence toSequence(SchemaComponent sc) {
159         return (sc instanceof Sequence) ? (Sequence) sc : null;
160     }
161     public static LocalElement toLocalElement(SchemaComponent sc) {
162         return (sc instanceof LocalElement) ? (LocalElement) sc : null;
163     }
164     public static SchemaModelImpl toSchemaModelImpl(SchemaModel sc) {
165         return sc instanceof SchemaModelImpl ? (SchemaModelImpl) sc : null;
166     }
167     
168     public static GlobalElement createGlobalElement(SchemaModel model, String JavaDoc name) {
169         GlobalElement ge = model.getFactory().createGlobalElement();
170         ge.setName(name); model.getSchema().addElement(ge);
171         return ge;
172     }
173     public static GlobalSimpleType createGlobalSimpleType(SchemaModel model, String JavaDoc name) {
174         GlobalSimpleType t = model.getFactory().createGlobalSimpleType();
175         t.setName(name); model.getSchema().addSimpleType(t);
176         return t;
177     }
178     
179     public static GlobalComplexType createGlobalComplexType(SchemaModel model, String JavaDoc name) {
180         GlobalComplexType t = model.getFactory().createGlobalComplexType();
181         t.setName(name); model.getSchema().addComplexType(t);
182         return t;
183     }
184     
185     public static Sequence createSequence(SchemaModel m, ComplexType gct) {
186         Sequence s = m.getFactory().createSequence(); gct.setDefinition(s);
187         return s;
188     }
189     
190     public static LocalElement createLocalElement(SchemaModel m, Sequence seq, String JavaDoc name, int i) {
191         LocalElement le = m.getFactory().createLocalElement();
192         seq.addContent(le, i); le.setName(name);
193         return le;
194     }
195     
196     public static LocalSimpleType createLocalSimpleType(SchemaModel m, LocalElement e) {
197         LocalSimpleType t = m.getFactory().createLocalSimpleType();
198         e.setInlineType(t);
199         return t;
200     }
201     
202     public static LocalComplexType createLocalComplexType(SchemaModel m, LocalElement e) {
203         LocalComplexType t = m.getFactory().createLocalComplexType();
204         e.setInlineType(t);
205         return t;
206     }
207     
208     public static LocalComplexType createLocalComplexType(SchemaModel m, GlobalElement e) {
209         LocalComplexType t = m.getFactory().createLocalComplexType();
210         e.setInlineType(t);
211         return t;
212     }
213     
214     public static GlobalSimpleType getPrimitiveType(String JavaDoc typeName){
215         SchemaModel primitiveModel = SchemaModelFactory.getDefault().getPrimitiveTypesModel();
216         Collection JavaDoc<GlobalSimpleType> primitives = primitiveModel.getSchema().getSimpleTypes();
217         for(GlobalSimpleType ptype: primitives){
218             if(ptype.getName().equals(typeName)){
219                 return ptype;
220             }
221         }
222         return null;
223     }
224     
225     public static Annotation createAnnotation(SchemaModel m, SchemaComponent p, String JavaDoc s) {
226         Annotation a = m.getFactory().createAnnotation();
227         Documentation d = m.getFactory().createDocumentation();
228         a.addDocumentation(d); p.setAnnotation(a);
229         Element e = d.getDocumentationElement(); m.getDocument().createTextNode(s);
230         d.setDocumentationElement(e);
231         return a;
232     }
233     
234     public static SimpleTypeRestriction createSimpleRestriction(SchemaModel m, SimpleType st) {
235         SimpleTypeRestriction csr = m.getFactory().createSimpleTypeRestriction();
236         st.setDefinition(csr);
237         return csr;
238     }
239     
240     public static URI JavaDoc getResourceURI(String JavaDoc path) throws RuntimeException JavaDoc {
241         try {
242             return Util.class.getResource(path).toURI();
243         } catch (Exception JavaDoc ex) {
244             throw new RuntimeException JavaDoc(ex);
245         }
246     }
247     
248     public static File JavaDoc getTempDir(String JavaDoc path) throws Exception JavaDoc {
249         File JavaDoc tempdir = new File JavaDoc(System.getProperty("java.io.tmpdir"), path);
250         tempdir.mkdirs();
251         return tempdir;
252     }
253     
254     public static void printMemoryUsage(String JavaDoc str){
255         long init = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getInit();
256         long cur = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
257         long max = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getMax();
258         System.out.printf("%s:\n@@@@@@MEMORY: %d/%d/%d\n",str, (init/(1024*1024)), (cur/(1024*1024)), (max/(1024*1024)));
259     }
260
261     public static SchemaModel dumpAndReloadModel(SchemaModel sm) throws Exception JavaDoc {
262         return dumpAndReloadModel((Document JavaDoc) sm.getModelSource().getLookup().lookup(Document JavaDoc.class));
263     }
264     
265     public static SchemaModel dumpAndReloadModel(Document JavaDoc doc) throws Exception JavaDoc {
266         File JavaDoc f = dumpToTempFile(doc);
267         URI JavaDoc dumpURI = new URI JavaDoc("dummyDump" + count++);
268         TestCatalogModel.getDefault().addURI(dumpURI, f.toURI());
269         return TestCatalogModel.getDefault().getSchemaModel(dumpURI);
270     }
271
272     public static GlobalSimpleType findGlobalSimpleType(Schema schema, String JavaDoc name) {
273         for (GlobalSimpleType t : schema.getSimpleTypes()) {
274             if (t.getName().equals(name)) {
275                 return t;
276             }
277         }
278         return null;
279     }
280
281     public static FileObject copyResource(String JavaDoc path, FileObject destFolder) throws Exception JavaDoc {
282         String JavaDoc filename = getFileName(path);
283         
284         FileObject dest = destFolder.getFileObject(filename);
285         if (dest == null) {
286             dest = destFolder.createData(filename);
287         }
288         FileLock lock = dest.lock();
289         OutputStream JavaDoc out = dest.getOutputStream(lock);
290         InputStream JavaDoc in = Util.class.getResourceAsStream(path);
291         try {
292             FileUtil.copy(in, out);
293         } finally {
294             out.close();
295             in.close();
296             if (lock != null) lock.releaseLock();
297         }
298         return dest;
299     }
300     
301     public static String JavaDoc getFileName(String JavaDoc path) {
302         int i = path.lastIndexOf('/');
303         if (i > -1) {
304             return path.substring(i+1);
305         } else {
306             return path;
307         }
308     }
309     
310 }
311
Popular Tags