~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/xam/kitmodel/test/unit/src/org/sample/registry/model/Util.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.sample.registry.model;
 
11
 
 
12
import java.io.BufferedInputStream;
 
13
import java.io.BufferedOutputStream;
 
14
import java.io.BufferedReader;
 
15
import java.io.File;
 
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;
 
23
import java.net.URI;
 
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;
 
30
 
 
31
/**
 
32
 *
 
33
 * @author nn136682
 
34
 */
 
35
public class Util {
 
36
    static {
 
37
        //JEditorPane.registerEditorKitForContentType(SchemaDataLoader.MIME_TYPE, XMLKit.class.getName());
 
38
        registerXMLKit();
 
39
    }
 
40
    
 
41
    public static void registerXMLKit() {
 
42
        String[] path = new String[] { "Editors", "text", "x-xml" };
 
43
        FileObject target = Repository.getDefault().getDefaultFileSystem().getRoot();
 
44
        try {
 
45
            for (int i=0; i<path.length; i++) {
 
46
                FileObject f = target.getFileObject(path[i]);
 
47
                if (f == null) {
 
48
                    f = target.createFolder(path[i]);
 
49
                }
 
50
                target = f;
 
51
            }
 
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");
 
56
            }
 
57
        } catch(IOException ioe) {
 
58
            ioe.printStackTrace();
 
59
        }
 
60
    }
 
61
 
 
62
    public static Document getResourceAsDocument(String path) throws Exception {
 
63
        InputStream in = Util.class.getResourceAsStream(path);
 
64
        return loadDocument(in);
 
65
    }
 
66
 
 
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);
 
72
    }
 
73
    
 
74
    public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception {
 
75
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
 
76
        StringBuffer sbuf = new StringBuffer();
 
77
        try {
 
78
            String line = null;
 
79
            while ((line = br.readLine()) != null) {
 
80
                sbuf.append(line);
 
81
                sbuf.append(System.getProperty("line.separator"));
 
82
            }
 
83
        } finally {
 
84
            br.close();
 
85
        }
 
86
        doc.remove(0, doc.getLength());
 
87
        doc.insertString(0,sbuf.toString(),null);
 
88
        return doc;
 
89
    }
 
90
    
 
91
    public static Document setDocumentContentTo(Document doc, String resourcePath) throws Exception {
 
92
        return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
 
93
    }
 
94
 
 
95
    public static void setDocumentContentTo(DocumentModel model, String resourcePath) throws Exception {
 
96
        setDocumentContentTo(((AbstractDocumentModel)model).getBaseDocument(), resourcePath);
 
97
        model.sync();
 
98
    }
 
99
    
 
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);
 
106
    }
 
107
    
 
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);
 
113
    }
 
114
    
 
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()));
 
118
        w.close();
 
119
        out.close();
 
120
    }
 
121
    
 
122
    public static void dumpToFile(DocumentModel model, File f) throws Exception {
 
123
        dumpToFile(((AbstractDocumentModel)model).getBaseDocument(), f);
 
124
    }
 
125
    
 
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()));
 
130
        w.close();
 
131
        out.close();
 
132
    }
 
133
    
 
134
    public static RegistryModel dumpAndReloadModel(RegistryModel sm) throws Exception {
 
135
        return dumpAndReloadModel((Document) sm.getModelSource().getLookup().lookup(Document.class));
 
136
    }
 
137
    
 
138
    public static File dumpToTempFile(Document doc) throws Exception {
 
139
        File f = File.createTempFile("reg", "xml");
 
140
        dumpToFile(doc, f);
 
141
        return f;
 
142
    }
 
143
    
 
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);
 
150
    }
 
151
    
 
152
    public static Document loadDocument(File f) throws Exception {
 
153
        InputStream in = new BufferedInputStream(new FileInputStream(f));
 
154
        return loadDocument(in);
 
155
    }
 
156
        
 
157
    public static URI getResourceURI(String path) throws RuntimeException {
 
158
        try {
 
159
            return Util.class.getResource(path).toURI();
 
160
        } catch (Exception ex) {
 
161
            throw new RuntimeException(ex);
 
162
        }
 
163
    }
 
164
    
 
165
    public static File getTempDir(String path) throws Exception {
 
166
        File tempdir = new File(System.getProperty("java.io.tmpdir"), path);
 
167
        tempdir.mkdirs();
 
168
        return tempdir;
 
169
    }
 
170
}