KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xdm > 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.xdm;
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.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import javax.swing.text.Document JavaDoc;
26 import org.netbeans.editor.BaseDocument;
27 import org.netbeans.modules.xml.text.syntax.XMLKit;
28 import org.netbeans.modules.xml.xam.ModelSource;
29 import org.netbeans.modules.xml.xam.TestModel;
30 import org.netbeans.modules.xml.xam.dom.ElementIdentity;
31 import org.netbeans.modules.xml.xdm.diff.DefaultElementIdentity;
32 import org.netbeans.modules.xml.xdm.diff.DiffFinder;
33 import org.netbeans.modules.xml.xdm.diff.Difference;
34 import org.netbeans.modules.xml.xdm.nodes.Element;
35 import org.openide.filesystems.FileObject;
36 import org.openide.filesystems.Repository;
37 import org.openide.util.Lookup;
38 import org.openide.util.lookup.Lookups;
39 import org.w3c.dom.Node JavaDoc;
40 import org.w3c.dom.NodeList JavaDoc;
41 /**
42  *
43  * @author nn136682
44  */

45 public class Util {
46     
47     static {
48         //JEditorPane.registerEditorKitForContentType(SchemaDataLoader.MIME_TYPE, XMLKit.class.getName());
49
registerXMLKit();
50     }
51     
52     public static void registerXMLKit() {
53         String JavaDoc[] path = new String JavaDoc[] { "Editors", "text", "x-xml" };
54         FileObject target = Repository.getDefault().getDefaultFileSystem().getRoot();
55         try {
56             for (int i=0; i<path.length; i++) {
57                 FileObject f = target.getFileObject(path[i]);
58                 if (f == null) {
59                     f = target.createFolder(path[i]);
60                 }
61                 target = f;
62             }
63             String JavaDoc name = "EditorKit.instance";
64             if (target.getFileObject(name) == null) {
65                 FileObject f = target.createData(name);
66                 f.setAttribute("instanceClass", "org.netbeans.modules.xml.text.syntax.XMLKit");
67             }
68         } catch(IOException JavaDoc ioe) {
69             ioe.printStackTrace();
70         }
71     }
72
73     public static Document JavaDoc getResourceAsDocument(String JavaDoc path) throws Exception JavaDoc {
74         InputStream JavaDoc in = Util.class.getResourceAsStream(path);
75         return loadDocument(in);
76     }
77     
78     public static String JavaDoc getResourceAsString(String JavaDoc path) throws Exception JavaDoc {
79         InputStream JavaDoc in = Util.class.getResourceAsStream(path);
80         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in,"UTF-8"));
81         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
82         try {
83             String JavaDoc line = null;
84             while ((line = br.readLine()) != null) {
85                 sbuf.append(line);
86                 sbuf.append(System.getProperty("line.separator"));
87             }
88         } finally {
89             br.close();
90         }
91         return sbuf.toString();
92     }
93     
94     public static org.netbeans.modules.xml.xdm.nodes.Document loadXdmDocument(String JavaDoc resourcePath) throws Exception JavaDoc {
95         XDMModel model = loadXDMModel(resourcePath);
96         return model.getDocument();
97     }
98     
99     public static Document JavaDoc loadDocument(InputStream JavaDoc in) throws Exception JavaDoc {
100         Document JavaDoc sd = new BaseDocument(XMLKit.class, false);
101         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in,"UTF-8"));
102         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
103         try {
104 // int c = 0;
105
// while((c = br.read()) != -1) {
106
// sbuf.append((char)c);
107
// }
108
String JavaDoc line = null;
109             while ((line = br.readLine()) != null) {
110                 sbuf.append(line);
111                 sbuf.append(System.getProperty("line.separator"));
112             }
113         } finally {
114             br.close();
115         }
116         sd.insertString(0,sbuf.toString(),null);
117         return sd;
118     }
119     
120     public static XDMModel loadXDMModel(String JavaDoc resourcePath) throws Exception JavaDoc {
121         return loadXDMModel(getResourceAsDocument(resourcePath));
122     }
123     
124     public static XDMModel loadXDMModel(Document JavaDoc doc) throws Exception JavaDoc {
125     Lookup lookup = Lookups.singleton(doc);
126     ModelSource ms = new ModelSource(lookup, true);
127         XDMModel m = new XDMModel(ms);
128         m.sync();
129         return m;
130     }
131     
132     public static void dumpToStream(Document JavaDoc doc, OutputStream JavaDoc out) throws Exception JavaDoc{
133         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
134         w.print(doc.getText(0, doc.getLength()));
135         w.close();
136         out.close();
137     }
138     
139     public static void dumpToFile(Document JavaDoc doc, File JavaDoc f) throws Exception JavaDoc {
140         OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
141         PrintWriter JavaDoc w = new PrintWriter JavaDoc(out);
142         w.print(doc.getText(0, doc.getLength()));
143         w.close();
144         out.close();
145     }
146     
147     public static File JavaDoc dumpToTempFile(Document JavaDoc doc) throws Exception JavaDoc {
148         File JavaDoc f = File.createTempFile("xdm-tester-", null);
149         dumpToFile(doc, f);
150         return f;
151     }
152     
153     public static Document JavaDoc loadDocument(File JavaDoc f) throws Exception JavaDoc {
154         InputStream JavaDoc in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(f));
155         return loadDocument(in);
156     }
157
158     public static void setDocumentContentTo(Document JavaDoc doc, File JavaDoc f) throws Exception JavaDoc {
159         setDocumentContentTo(doc, new FileInputStream JavaDoc(f));
160     }
161     
162     public static Document JavaDoc setDocumentContentTo(Document JavaDoc doc, InputStream JavaDoc in) throws Exception JavaDoc {
163         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(in));
164         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
165         try {
166             String JavaDoc line = null;
167             while ((line = br.readLine()) != null) {
168                 sbuf.append(line);
169                 sbuf.append(System.getProperty("line.separator"));
170             }
171         } finally {
172             br.close();
173         }
174         doc.remove(0, doc.getLength());
175         doc.insertString(0,sbuf.toString(),null);
176         return doc;
177     }
178     
179     public static Document JavaDoc setDocumentContentTo(Document JavaDoc doc, String JavaDoc resourcePath) throws Exception JavaDoc {
180         return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath));
181     }
182     
183     public static List JavaDoc<Element> getChildElementsByTag(Node JavaDoc parent, String JavaDoc tag) {
184         List JavaDoc<Element> children = new ArrayList JavaDoc<Element>();
185         NodeList JavaDoc nl = parent.getChildNodes();
186         for (int i=0; i<nl.getLength(); i++) {
187             if (nl.item(i) instanceof Element) {
188                 Element e = (Element) nl.item(i);
189                 if (e.getTagName().equals(tag))
190                     children.add(e);
191             }
192         }
193         return children;
194     }
195         
196     public static Element getChildElementByTag(Node JavaDoc parent, String JavaDoc tag) {
197         List JavaDoc<Element> result = getChildElementsByTag(parent, tag);
198         return result.size() > 0 ? result.get(0) : null;
199     }
200
201     public static ElementIdentity getDefaultElementIdentity() {
202         //establish DOM element identities
203
DefaultElementIdentity eID = new DefaultElementIdentity();
204         eID.addIdentifier( "id" );
205         eID.addIdentifier( "index" );
206         eID.addIdentifier( "name" );
207         eID.addIdentifier( "ref" );
208         return eID;
209     }
210     
211     public static List JavaDoc<Difference> diff(String JavaDoc path1, String JavaDoc path2) throws Exception JavaDoc {
212         XDMModel m1 = loadXDMModel(path1);
213         XDMModel m2 = loadXDMModel(path2);
214         return diff(m1, m2);
215     }
216     
217     public static List JavaDoc<Difference> diff(XDMModel m1, XDMModel m2) throws Exception JavaDoc {
218         return new DiffFinder(getDefaultElementIdentity()).findDiff(
219             m1.getDocument(), m2.getDocument());
220     }
221     
222     public static TestModel loadModel(Document JavaDoc doc) throws Exception JavaDoc {
223         return new TestModel(doc);
224     }
225     
226     public static TestModel loadModel(String JavaDoc path) throws Exception JavaDoc {
227         return new TestModel(getResourceAsDocument(path));
228     }
229     
230     public static TestModel loadModel(File JavaDoc f) throws Exception JavaDoc {
231         return new TestModel(loadDocument(f));
232     }
233     
234     public static TestModel dumpAndReloadModel(TestModel sm) throws Exception JavaDoc {
235         Document JavaDoc doc = sm.getBaseDocument();
236         File JavaDoc f = dumpToTempFile(doc);
237         return loadModel(f);
238     }
239     
240 }
241
Popular Tags