4
* Created on October 4, 2005, 7:48 PM
6
* To change this template, choose Tools | Template Manager
7
* and open the template in the editor.
10
package org.sample.registry.model;
12
import java.io.BufferedInputStream;
13
import java.io.BufferedOutputStream;
14
import java.io.BufferedReader;
16
import java.io.FileInputStream;
17
import java.io.FileOutputStream;
18
import java.io.IOException;
19
import java.io.InputStream;
20
import java.io.InputStreamReader;
21
import java.io.OutputStream;
22
import java.io.PrintWriter;
24
import javax.swing.text.Document;
25
import org.netbeans.modules.xml.xam.ModelSource;
26
import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel;
27
import org.netbeans.modules.xml.xam.dom.DocumentModel;
28
import org.openide.filesystems.FileObject;
29
import org.openide.filesystems.Repository;
37
//JEditorPane.registerEditorKitForContentType(SchemaDataLoader.MIME_TYPE, XMLKit.class.getName());
41
public static void registerXMLKit() {
42
String[] path = new String[] { "Editors", "text", "x-xml" };
43
FileObject target = Repository.getDefault().getDefaultFileSystem().getRoot();
45
for (int i=0; i<path.length; i++) {
46
FileObject f = target.getFileObject(path[i]);
48
f = target.createFolder(path[i]);
52
String name = "EditorKit.instance";
53
if (target.getFileObject(name) == null) {
54
FileObject f = target.createData(name);
55
f.setAttribute("instanceClass", "org.netbeans.modules.xml.text.syntax.XMLKit");
57
} catch(IOException ioe) {
58
ioe.printStackTrace();
62
public static Document getResourceAsDocument(String path) throws Exception {
63
InputStream in = Util.class.getResourceAsStream(path);
64
return loadDocument(in);
67
public static Document loadDocument(InputStream in) throws Exception {
68
// Document sd = new PlainDocument();
69
Document sd = new org.netbeans.editor.BaseDocument(
70
org.netbeans.modules.xml.text.syntax.XMLKit.class, false);
71
return setDocumentContentTo(sd, in);
74
public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception {
75
BufferedReader br = new BufferedReader(new InputStreamReader(in));
76
StringBuffer sbuf = new StringBuffer();
79
while ((line = br.readLine()) != null) {
81
sbuf.append(System.getProperty("line.separator"));
86
doc.remove(0, doc.getLength());
87
doc.insertString(0,sbuf.toString(),null);
91
public static Document setDocumentContentTo(Document doc, String resourcePath) throws Exception {
92
return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
95
public static void setDocumentContentTo(DocumentModel model, String resourcePath) throws Exception {
96
setDocumentContentTo(((AbstractDocumentModel)model).getBaseDocument(), resourcePath);
100
public static int count = 0;
101
public static RegistryModel loadRegistryModel(String resourcePath) throws Exception {
102
URI locationURI = new URI(resourcePath);
103
TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath));
104
ModelSource ms = TestCatalogModel.getDefault().getModelSource(locationURI);
105
return RegistryModelFactory.getInstance().getModel(ms);
108
public static RegistryModel loadRegistryModel(File schemaFile) throws Exception {
109
URI locationURI = new URI(schemaFile.getName());
110
TestCatalogModel.getDefault().addURI(locationURI, schemaFile.toURI());
111
ModelSource ms = TestCatalogModel.getDefault().getModelSource(locationURI);
112
return RegistryModelFactory.getInstance().getModel(ms);
115
public static void dumpToStream(Document doc, OutputStream out) throws Exception{
116
PrintWriter w = new PrintWriter(out);
117
w.print(doc.getText(0, doc.getLength()));
122
public static void dumpToFile(DocumentModel model, File f) throws Exception {
123
dumpToFile(((AbstractDocumentModel)model).getBaseDocument(), f);
126
public static void dumpToFile(Document doc, File f) throws Exception {
127
OutputStream out = new BufferedOutputStream(new FileOutputStream(f));
128
PrintWriter w = new PrintWriter(out);
129
w.print(doc.getText(0, doc.getLength()));
134
public static RegistryModel dumpAndReloadModel(RegistryModel sm) throws Exception {
135
return dumpAndReloadModel((Document) sm.getModelSource().getLookup().lookup(Document.class));
138
public static File dumpToTempFile(Document doc) throws Exception {
139
File f = File.createTempFile("reg", "xml");
144
public static RegistryModel dumpAndReloadModel(Document doc) throws Exception {
145
File f = dumpToTempFile(doc);
146
URI dumpURI = new URI("dummyDump" + count++);
147
TestCatalogModel.getDefault().addURI(dumpURI, f.toURI());
148
ModelSource ms = TestCatalogModel.getDefault().getModelSource(dumpURI);
149
return RegistryModelFactory.getInstance().getModel(ms);
152
public static Document loadDocument(File f) throws Exception {
153
InputStream in = new BufferedInputStream(new FileInputStream(f));
154
return loadDocument(in);
157
public static URI getResourceURI(String path) throws RuntimeException {
159
return Util.class.getResource(path).toURI();
160
} catch (Exception ex) {
161
throw new RuntimeException(ex);
165
public static File getTempDir(String path) throws Exception {
166
File tempdir = new File(System.getProperty("java.io.tmpdir"), path);