001: /*
002: * Util.java
003: *
004: * Created on October 4, 2005, 7:48 PM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009:
010: package org.netbeans.modules.xml.xdm;
011:
012: import java.io.BufferedInputStream;
013: import java.io.BufferedOutputStream;
014: import java.io.BufferedReader;
015: import java.io.File;
016: import java.io.FileInputStream;
017: import java.io.FileOutputStream;
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.io.InputStreamReader;
021: import java.io.OutputStream;
022: import java.io.PrintWriter;
023: import java.util.ArrayList;
024: import java.util.List;
025: import javax.swing.text.Document;
026: import org.netbeans.editor.BaseDocument;
027: import org.netbeans.modules.xml.text.syntax.XMLKit;
028: import org.netbeans.modules.xml.xam.ModelSource;
029: import org.netbeans.modules.xml.xam.TestModel;
030: import org.netbeans.modules.xml.xam.dom.ElementIdentity;
031: import org.netbeans.modules.xml.xdm.diff.DefaultElementIdentity;
032: import org.netbeans.modules.xml.xdm.diff.DiffFinder;
033: import org.netbeans.modules.xml.xdm.diff.Difference;
034: import org.netbeans.modules.xml.xdm.nodes.Element;
035: import org.openide.filesystems.FileObject;
036: import org.openide.filesystems.Repository;
037: import org.openide.util.Lookup;
038: import org.openide.util.lookup.Lookups;
039: import org.w3c.dom.Node;
040: import org.w3c.dom.NodeList;
041:
042: /**
043: *
044: * @author nn136682
045: */
046: public class Util {
047:
048: static {
049: //JEditorPane.registerEditorKitForContentType(SchemaDataLoader.MIME_TYPE, XMLKit.class.getName());
050: registerXMLKit();
051: }
052:
053: public static void registerXMLKit() {
054: String[] path = new String[] { "Editors", "text", "x-xml" };
055: FileObject target = Repository.getDefault()
056: .getDefaultFileSystem().getRoot();
057: try {
058: for (int i = 0; i < path.length; i++) {
059: FileObject f = target.getFileObject(path[i]);
060: if (f == null) {
061: f = target.createFolder(path[i]);
062: }
063: target = f;
064: }
065: String name = "EditorKit.instance";
066: if (target.getFileObject(name) == null) {
067: FileObject f = target.createData(name);
068: f.setAttribute("instanceClass",
069: "org.netbeans.modules.xml.text.syntax.XMLKit");
070: }
071: } catch (IOException ioe) {
072: ioe.printStackTrace();
073: }
074: }
075:
076: public static Document getResourceAsDocument(String path)
077: throws Exception {
078: InputStream in = Util.class.getResourceAsStream(path);
079: return loadDocument(in);
080: }
081:
082: public static String getResourceAsString(String path)
083: throws Exception {
084: InputStream in = Util.class.getResourceAsStream(path);
085: BufferedReader br = new BufferedReader(new InputStreamReader(
086: in, "UTF-8"));
087: StringBuffer sbuf = new StringBuffer();
088: try {
089: String line = null;
090: while ((line = br.readLine()) != null) {
091: sbuf.append(line);
092: sbuf.append(System.getProperty("line.separator"));
093: }
094: } finally {
095: br.close();
096: }
097: return sbuf.toString();
098: }
099:
100: public static org.netbeans.modules.xml.xdm.nodes.Document loadXdmDocument(
101: String resourcePath) throws Exception {
102: XDMModel model = loadXDMModel(resourcePath);
103: return model.getDocument();
104: }
105:
106: public static Document loadDocument(InputStream in)
107: throws Exception {
108: Document sd = new BaseDocument(XMLKit.class, false);
109: BufferedReader br = new BufferedReader(new InputStreamReader(
110: in, "UTF-8"));
111: StringBuffer sbuf = new StringBuffer();
112: try {
113: // int c = 0;
114: // while((c = br.read()) != -1) {
115: // sbuf.append((char)c);
116: // }
117: String line = null;
118: while ((line = br.readLine()) != null) {
119: sbuf.append(line);
120: sbuf.append(System.getProperty("line.separator"));
121: }
122: } finally {
123: br.close();
124: }
125: sd.insertString(0, sbuf.toString(), null);
126: return sd;
127: }
128:
129: public static XDMModel loadXDMModel(String resourcePath)
130: throws Exception {
131: return loadXDMModel(getResourceAsDocument(resourcePath));
132: }
133:
134: public static XDMModel loadXDMModel(Document doc) throws Exception {
135: Lookup lookup = Lookups.singleton(doc);
136: ModelSource ms = new ModelSource(lookup, true);
137: XDMModel m = new XDMModel(ms);
138: m.sync();
139: return m;
140: }
141:
142: public static void dumpToStream(Document doc, OutputStream out)
143: throws Exception {
144: PrintWriter w = new PrintWriter(out);
145: w.print(doc.getText(0, doc.getLength()));
146: w.close();
147: out.close();
148: }
149:
150: public static void dumpToFile(Document doc, File f)
151: throws Exception {
152: OutputStream out = new BufferedOutputStream(
153: new FileOutputStream(f));
154: PrintWriter w = new PrintWriter(out);
155: w.print(doc.getText(0, doc.getLength()));
156: w.close();
157: out.close();
158: }
159:
160: public static File dumpToTempFile(Document doc) throws Exception {
161: File f = File.createTempFile("xdm-tester-", null);
162: dumpToFile(doc, f);
163: return f;
164: }
165:
166: public static Document loadDocument(File f) throws Exception {
167: InputStream in = new BufferedInputStream(new FileInputStream(f));
168: return loadDocument(in);
169: }
170:
171: public static void setDocumentContentTo(Document doc, File f)
172: throws Exception {
173: setDocumentContentTo(doc, new FileInputStream(f));
174: }
175:
176: public static Document setDocumentContentTo(Document doc,
177: InputStream in) throws Exception {
178: BufferedReader br = new BufferedReader(
179: new InputStreamReader(in));
180: StringBuffer sbuf = new StringBuffer();
181: try {
182: String line = null;
183: while ((line = br.readLine()) != null) {
184: sbuf.append(line);
185: sbuf.append(System.getProperty("line.separator"));
186: }
187: } finally {
188: br.close();
189: }
190: doc.remove(0, doc.getLength());
191: doc.insertString(0, sbuf.toString(), null);
192: return doc;
193: }
194:
195: public static Document setDocumentContentTo(Document doc,
196: String resourcePath) throws Exception {
197: return setDocumentContentTo(doc, Util.class
198: .getResourceAsStream(resourcePath));
199: }
200:
201: public static List<Element> getChildElementsByTag(Node parent,
202: String tag) {
203: List<Element> children = new ArrayList<Element>();
204: NodeList nl = parent.getChildNodes();
205: for (int i = 0; i < nl.getLength(); i++) {
206: if (nl.item(i) instanceof Element) {
207: Element e = (Element) nl.item(i);
208: if (e.getTagName().equals(tag))
209: children.add(e);
210: }
211: }
212: return children;
213: }
214:
215: public static Element getChildElementByTag(Node parent, String tag) {
216: List<Element> result = getChildElementsByTag(parent, tag);
217: return result.size() > 0 ? result.get(0) : null;
218: }
219:
220: public static ElementIdentity getDefaultElementIdentity() {
221: //establish DOM element identities
222: DefaultElementIdentity eID = new DefaultElementIdentity();
223: eID.addIdentifier("id");
224: eID.addIdentifier("index");
225: eID.addIdentifier("name");
226: eID.addIdentifier("ref");
227: return eID;
228: }
229:
230: public static List<Difference> diff(String path1, String path2)
231: throws Exception {
232: XDMModel m1 = loadXDMModel(path1);
233: XDMModel m2 = loadXDMModel(path2);
234: return diff(m1, m2);
235: }
236:
237: public static List<Difference> diff(XDMModel m1, XDMModel m2)
238: throws Exception {
239: return new DiffFinder(getDefaultElementIdentity()).findDiff(m1
240: .getDocument(), m2.getDocument());
241: }
242:
243: public static TestModel loadModel(Document doc) throws Exception {
244: return new TestModel(doc);
245: }
246:
247: public static TestModel loadModel(String path) throws Exception {
248: return new TestModel(getResourceAsDocument(path));
249: }
250:
251: public static TestModel loadModel(File f) throws Exception {
252: return new TestModel(loadDocument(f));
253: }
254:
255: public static TestModel dumpAndReloadModel(TestModel sm)
256: throws Exception {
257: Document doc = sm.getBaseDocument();
258: File f = dumpToTempFile(doc);
259: return loadModel(f);
260: }
261:
262: }
|