KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui;
21
22 import java.io.BufferedInputStream JavaDoc;
23 import java.io.BufferedOutputStream JavaDoc;
24 import java.io.BufferedReader JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.lang.management.ManagementFactory JavaDoc;
33 import java.net.URI JavaDoc;
34 import java.util.Collection JavaDoc;
35 import javax.swing.text.Document JavaDoc;
36 import org.netbeans.modules.xml.schema.model.Annotation;
37 import org.netbeans.modules.xml.schema.model.ComplexType;
38 import org.netbeans.modules.xml.schema.model.Documentation;
39 import org.netbeans.modules.xml.schema.model.GlobalComplexType;
40 import org.netbeans.modules.xml.schema.model.GlobalElement;
41 import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
42 import org.netbeans.modules.xml.schema.model.LocalComplexType;
43 import org.netbeans.modules.xml.schema.model.LocalElement;
44 import org.netbeans.modules.xml.schema.model.LocalSimpleType;
45 import org.netbeans.modules.xml.schema.model.Schema;
46 import org.netbeans.modules.xml.schema.model.SchemaComponent;
47 import org.netbeans.modules.xml.schema.model.SchemaModel;
48 import org.netbeans.modules.xml.schema.model.SchemaModelFactory;
49 import org.netbeans.modules.xml.schema.model.Sequence;
50 import org.netbeans.modules.xml.schema.model.SimpleType;
51 import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction;
52 import org.netbeans.modules.xml.schema.model.impl.SchemaModelImpl;
53 import org.netbeans.modules.xml.schema.model.visitor.FindSchemaComponentFromDOM;
54 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
55 import org.netbeans.modules.xml.xam.dom.DocumentModel;
56 import org.openide.filesystems.FileLock;
57 import org.openide.filesystems.FileObject;
58 import org.openide.filesystems.FileUtil;
59 import org.w3c.dom.Element JavaDoc;
60
61 /**
62  *
63  * @author nn136682
64  */

65 public class Util {
66     public static final String JavaDoc PO_XSD = "resources/PurchaseOrder.xsd";
67     public static final String JavaDoc EMPTY_XSD = "resources/Empty.xsd";
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