1 9 10 package org.netbeans.modules.web.jsf.xdm.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.web.jsf.api.facesmodel.JSFConfigModel; 26 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigModelFactory; 27 import org.netbeans.modules.xml.xam.ModelSource; 28 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 29 import org.netbeans.modules.xml.xam.dom.DocumentModel; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.Repository; 32 33 37 public class Util { 38 static { 39 registerXMLKit(); 41 } 42 43 public static void registerXMLKit() { 44 String [] path = new String [] { "Editors", "text", "x-xml" }; 45 FileObject target = Repository.getDefault().getDefaultFileSystem().getRoot(); 46 try { 47 for (int i=0; i<path.length; i++) { 48 FileObject f = target.getFileObject(path[i]); 49 if (f == null) { 50 f = target.createFolder(path[i]); 51 } 52 target = f; 53 } 54 String name = "EditorKit.instance"; 55 if (target.getFileObject(name) == null) { 56 FileObject f = target.createData(name); 57 f.setAttribute("instanceClass", "org.netbeans.modules.xml.text.syntax.XMLKit"); 58 } 59 } catch(IOException ioe) { 60 ioe.printStackTrace(); 61 } 62 } 63 64 public static Document getResourceAsDocument(String path) throws Exception { 65 InputStream in = Util.class.getResourceAsStream(path); 66 return loadDocument(in); 67 } 68 69 public static Document loadDocument(InputStream in) throws Exception { 70 Document sd = new org.netbeans.editor.BaseDocument( 72 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 73 return setDocumentContentTo(sd, in); 74 } 75 76 public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception { 77 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 78 StringBuffer sbuf = new StringBuffer (); 79 try { 80 String line = null; 81 while ((line = br.readLine()) != null) { 82 sbuf.append(line); 83 sbuf.append(System.getProperty("line.separator")); 84 } 85 } finally { 86 br.close(); 87 } 88 doc.remove(0, doc.getLength()); 89 doc.insertString(0,sbuf.toString(),null); 90 return doc; 91 } 92 93 public static Document setDocumentContentTo(Document doc, String resourcePath) throws Exception { 94 return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath)); 95 } 96 97 public static void setDocumentContentTo(DocumentModel model, String resourcePath) throws Exception { 98 setDocumentContentTo(((AbstractDocumentModel)model).getBaseDocument(), resourcePath); 99 model.sync(); 100 } 101 102 public static int count = 0; 103 public static JSFConfigModel loadRegistryModel(String resourcePath) throws Exception { 104 URI locationURI = new URI (resourcePath); 105 TestCatalogModel.getDefault().addURI(locationURI, getResourceURI(resourcePath)); 106 ModelSource ms = TestCatalogModel.getDefault().getModelSource(locationURI); 107 return JSFConfigModelFactory.getInstance().getModel(ms); 108 } 109 110 111 112 public static JSFConfigModel loadRegistryModel(File schemaFile) throws Exception { 113 URI locationURI = new URI (schemaFile.getName()); 114 TestCatalogModel.getDefault().addURI(locationURI, schemaFile.toURI()); 115 ModelSource ms = TestCatalogModel.getDefault().getModelSource(locationURI); 116 return JSFConfigModelFactory.getInstance().getModel(ms); 117 } 118 119 public static void dumpToStream(Document doc, OutputStream out) throws Exception { 120 PrintWriter w = new PrintWriter (out); 121 w.print(doc.getText(0, doc.getLength())); 122 w.close(); 123 out.close(); 124 } 125 126 public static void dumpToFile(DocumentModel model, File f) throws Exception { 127 dumpToFile(((AbstractDocumentModel)model).getBaseDocument(), f); 128 } 129 130 public static void dumpToFile(Document doc, File f) throws Exception { 131 OutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 132 PrintWriter w = new PrintWriter (out); 133 w.print(doc.getText(0, doc.getLength())); 134 w.close(); 135 out.close(); 136 } 137 138 public static JSFConfigModel dumpAndReloadModel(JSFConfigModel sm) throws Exception { 139 return dumpAndReloadModel((Document ) sm.getModelSource().getLookup().lookup(Document .class)); 140 } 141 142 public static File dumpToTempFile(Document doc) throws Exception { 143 File f = File.createTempFile("faces-config-tmp", "xml"); 144 System.out.println("file: " + f.getAbsolutePath()); 145 dumpToFile(doc, f); 146 return f; 147 } 148 149 public static JSFConfigModel dumpAndReloadModel(Document doc) throws Exception { 150 File f = dumpToTempFile(doc); 151 URI dumpURI = new URI ("dummyDump" + count++); 152 TestCatalogModel.getDefault().addURI(dumpURI, f.toURI()); 153 ModelSource ms = TestCatalogModel.getDefault().getModelSource(dumpURI); 154 return JSFConfigModelFactory.getInstance().getModel(ms); 155 } 156 157 public static Document loadDocument(File f) throws Exception { 158 InputStream in = new BufferedInputStream (new FileInputStream (f)); 159 return loadDocument(in); 160 } 161 162 public static URI getResourceURI(String path) throws RuntimeException { 163 try { 164 return Util.class.getResource(path).toURI(); 165 } catch (Exception ex) { 166 throw new RuntimeException (ex); 167 } 168 } 169 170 public static File getTempDir(String path) throws Exception { 171 File tempdir = new File (System.getProperty("java.io.tmpdir"), path); 172 tempdir.mkdirs(); 173 return tempdir; 174 } 175 } 176 | Popular Tags |