1 9 10 package org.netbeans.modules.xml.xdm; 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.util.ArrayList ; 24 import java.util.List ; 25 import javax.swing.text.Document ; 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 ; 40 import org.w3c.dom.NodeList ; 41 45 public class Util { 46 47 static { 48 registerXMLKit(); 50 } 51 52 public static void registerXMLKit() { 53 String [] path = new String [] { "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 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 ioe) { 69 ioe.printStackTrace(); 70 } 71 } 72 73 public static Document getResourceAsDocument(String path) throws Exception { 74 InputStream in = Util.class.getResourceAsStream(path); 75 return loadDocument(in); 76 } 77 78 public static String getResourceAsString(String path) throws Exception { 79 InputStream in = Util.class.getResourceAsStream(path); 80 BufferedReader br = new BufferedReader (new InputStreamReader (in,"UTF-8")); 81 StringBuffer sbuf = new StringBuffer (); 82 try { 83 String 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 resourcePath) throws Exception { 95 XDMModel model = loadXDMModel(resourcePath); 96 return model.getDocument(); 97 } 98 99 public static Document loadDocument(InputStream in) throws Exception { 100 Document sd = new BaseDocument(XMLKit.class, false); 101 BufferedReader br = new BufferedReader (new InputStreamReader (in,"UTF-8")); 102 StringBuffer sbuf = new StringBuffer (); 103 try { 104 String 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 resourcePath) throws Exception { 121 return loadXDMModel(getResourceAsDocument(resourcePath)); 122 } 123 124 public static XDMModel loadXDMModel(Document doc) throws Exception { 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 doc, OutputStream out) throws Exception { 133 PrintWriter w = new PrintWriter (out); 134 w.print(doc.getText(0, doc.getLength())); 135 w.close(); 136 out.close(); 137 } 138 139 public static void dumpToFile(Document doc, File f) throws Exception { 140 OutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 141 PrintWriter w = new PrintWriter (out); 142 w.print(doc.getText(0, doc.getLength())); 143 w.close(); 144 out.close(); 145 } 146 147 public static File dumpToTempFile(Document doc) throws Exception { 148 File f = File.createTempFile("xdm-tester-", null); 149 dumpToFile(doc, f); 150 return f; 151 } 152 153 public static Document loadDocument(File f) throws Exception { 154 InputStream in = new BufferedInputStream (new FileInputStream (f)); 155 return loadDocument(in); 156 } 157 158 public static void setDocumentContentTo(Document doc, File f) throws Exception { 159 setDocumentContentTo(doc, new FileInputStream (f)); 160 } 161 162 public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception { 163 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 164 StringBuffer sbuf = new StringBuffer (); 165 try { 166 String 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 setDocumentContentTo(Document doc, String resourcePath) throws Exception { 180 return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath)); 181 } 182 183 public static List <Element> getChildElementsByTag(Node parent, String tag) { 184 List <Element> children = new ArrayList <Element>(); 185 NodeList 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 parent, String tag) { 197 List <Element> result = getChildElementsByTag(parent, tag); 198 return result.size() > 0 ? result.get(0) : null; 199 } 200 201 public static ElementIdentity getDefaultElementIdentity() { 202 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 <Difference> diff(String path1, String path2) throws Exception { 212 XDMModel m1 = loadXDMModel(path1); 213 XDMModel m2 = loadXDMModel(path2); 214 return diff(m1, m2); 215 } 216 217 public static List <Difference> diff(XDMModel m1, XDMModel m2) throws Exception { 218 return new DiffFinder(getDefaultElementIdentity()).findDiff( 219 m1.getDocument(), m2.getDocument()); 220 } 221 222 public static TestModel loadModel(Document doc) throws Exception { 223 return new TestModel(doc); 224 } 225 226 public static TestModel loadModel(String path) throws Exception { 227 return new TestModel(getResourceAsDocument(path)); 228 } 229 230 public static TestModel loadModel(File f) throws Exception { 231 return new TestModel(loadDocument(f)); 232 } 233 234 public static TestModel dumpAndReloadModel(TestModel sm) throws Exception { 235 Document doc = sm.getBaseDocument(); 236 File f = dumpToTempFile(doc); 237 return loadModel(f); 238 } 239 240 } 241 | Popular Tags |