KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > core > 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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.core;
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.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.InputStreamReader JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33 import java.net.URI JavaDoc;
34 import javax.swing.JEditorPane JavaDoc;
35 import javax.swing.text.Document JavaDoc;
36 import org.netbeans.api.project.Project;
37 import org.netbeans.api.project.ProjectManager;
38 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent;
39 import org.netbeans.modules.xml.schema.model.Schema;
40 import org.netbeans.modules.xml.schema.model.SchemaComponent;
41 import org.netbeans.modules.xml.schema.model.visitor.FindSchemaComponentFromDOM;
42 import org.netbeans.modules.xml.text.syntax.XMLKit;
43 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
44 import org.netbeans.modules.xml.xam.dom.DocumentModel;
45 import org.openide.filesystems.FileLock;
46 import org.openide.filesystems.FileObject;
47 import org.openide.filesystems.FileUtil;
48 import org.openide.filesystems.Repository;
49
50 /**
51  *
52  * @author nn136682
53  */

54 public class Util {
55     static {
56         //JEditorPane.registerEditorKitForContentType(SchemaDataLoader.MIME_TYPE, XMLKit.class.getName());
57
registerXMLKit();
58     }
59     
60     public static void registerXMLKit() {
61         String JavaDoc[] path = new String JavaDoc[] { "Editors", "application", "x-schema+xml" };
62         FileObject target = Repository.getDefault().getDefaultFileSystem().getRoot();
63         try {
64             for (int i=0; i<path.length; i++) {
65                 FileObject f = target.getFileObject(path[i]);
66                 if (f == null) {
67                     f = target.createFolder(path[i]);
68                 }
69                 target = f;
70             }
71             String JavaDoc name = "EditorKit.instance";
72             if (target.getFileObject(name) == null) {
73                 FileObject f = target.createData(name);
74                 f.setAttribute("instanceClass", "org.netbeans.modules.xml.text.syntax.XMLKit");
75             }
76         } catch(IOException JavaDoc ioe) {
77             ioe.printStackTrace();
78         }
79     }
80     
81     public static Document JavaDoc getResourceAsDocument(String JavaDoc path) throws Exception JavaDoc {
82         InputStream JavaDoc in = Util.class.getResourceAsStream(path);
83         return loadDocument(in);
84     }
85
86     public static Document JavaDoc loadDocument(InputStream JavaDoc in) throws Exception JavaDoc {
87 // Document sd = new PlainDocument();
88
Document JavaDoc sd = new org.netbeans.editor.BaseDocument(
89                 org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
90         return setDocumentContentTo(sd, in);
91     }
92     
93     public static Document JavaDoc setDocumentContentTo(Document JavaDoc doc, InputStream JavaDoc in) throws Exception JavaDoc {
94         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
95         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
96         try {
97             String JavaDoc line = null;
98             while ((line = br.readLine()) != null) {
99                 sbuf.append(line);
100                 sbuf.append(System.getProperty("line.separator"));
101             }
102         } finally {
103             br.close();
104         }
105         doc.remove(0, doc.getLength());
106         doc.insertString(0,sbuf.toString(),null);
107         return doc;
108     }
109     
110     public static Document JavaDoc setDocumentContentTo(Document JavaDoc doc, String JavaDoc resourcePath) throws Exception JavaDoc {
111         return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
112     }
113
114     public static void setDocumentContentTo(DocumentModel model, String JavaDoc resourcePath) throws Exception JavaDoc {
115         setDocumentContentTo(((AbstractDocumentModel)model).getBaseDocument(), resourcePath);
116     }
117     
118     public static void dumpToStream(Document JavaDoc doc, OutputStream JavaDoc out) throws Exception JavaDoc{
119         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
120         w.print(doc.getText(0, doc.getLength()));
121         w.close();
122         out.close();
123     }
124     
125     public static void dumpToFile(Document JavaDoc doc, File JavaDoc f) throws Exception JavaDoc {
126         OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
127         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
128         w.print(doc.getText(0, doc.getLength()));
129         w.close();
130         out.close();
131     }
132     
133     public static File JavaDoc dumpToTempFile(Document JavaDoc doc) throws Exception JavaDoc {
134         File JavaDoc f = File.createTempFile("xsm", "xsd");
135         dumpToFile(doc, f);
136         return f;
137     }
138     
139     public static Document JavaDoc loadDocument(File JavaDoc f) throws Exception JavaDoc {
140         InputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(f));
141         return loadDocument(in);
142     }
143     
144     public static SchemaComponent findComponent(Schema schema, String JavaDoc xpath) {
145         return (new FindSchemaComponentFromDOM().findComponent(schema, xpath));
146     }
147     
148     public static URI JavaDoc getResourceURI(String JavaDoc path) throws RuntimeException JavaDoc {
149         try {
150             return Util.class.getResource(path).toURI();
151         } catch (Exception JavaDoc ex) {
152             throw new RuntimeException JavaDoc(ex);
153         }
154     }
155     
156     public static File JavaDoc getTempDir(String JavaDoc path) throws Exception JavaDoc {
157         File JavaDoc tempdir = new File JavaDoc(System.getProperty("java.io.tmpdir"), path);
158         tempdir.mkdirs();
159         return tempdir;
160     }
161
162     public static Project createJavaTestProject() throws Exception JavaDoc {
163         FileObject projectDir = FileUtil.toFileObject(getTempDir("TestJavaProject").getCanonicalFile());
164         copyResource("resources/TestJavaProject/build.xml", projectDir);
165         copyResource("resources/TestJavaProject/manifest.mf", projectDir);
166         FileObject nbprojectDir = FileUtil.createFolder(projectDir, "nbproject");
167         copyResource("resources/TestJavaProject/nbproject/build-impl.xml", nbprojectDir);
168         copyResource("resources/TestJavaProject/nbproject/project.xml", nbprojectDir);
169         copyResource("resources/TestJavaProject/nbproject/project.properties", nbprojectDir);
170         return ProjectManager.getDefault().findProject(projectDir);
171     }
172
173     public static FileObject populateProject(Project project, String JavaDoc xsd, String JavaDoc path) throws Exception JavaDoc {
174         FileObject srcDir = FileUtil.createFolder(project.getProjectDirectory(), path);
175         return copyResource(xsd, srcDir);
176     }
177     
178     public static FileObject copyResource(String JavaDoc path, FileObject destFolder) throws Exception JavaDoc {
179         String JavaDoc filename = getFileName(path);
180         
181         FileObject dest = destFolder.getFileObject(filename);
182         if (dest == null) {
183             dest = destFolder.createData(filename);
184         }
185         FileLock lock = dest.lock();
186         OutputStream JavaDoc out = dest.getOutputStream(lock);
187         InputStream JavaDoc in = Util.class.getResourceAsStream(path);
188         try {
189             FileUtil.copy(in, out);
190         } finally {
191             out.close();
192             in.close();
193             if (lock != null) lock.releaseLock();
194         }
195         return dest;
196     }
197     
198     public static String JavaDoc getFileName(String JavaDoc path) {
199         int i = path.lastIndexOf('/');
200         if (i > -1) {
201             return path.substring(i+1);
202         } else {
203             return path;
204         }
205     }
206     
207     public static ReferenceableSchemaComponent findGlobalComponentByName(Schema schema, String JavaDoc name) {
208         for (ReferenceableSchemaComponent t : schema.getChildren(ReferenceableSchemaComponent.class)) {
209             if (t.getName().equals(name)) {
210                 return t;
211             }
212         }
213         return null;
214     }
215     
216 }
217
Popular Tags