1 19 20 package org.netbeans.modules.xslt.model.impl; 21 22 import java.io.BufferedReader ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.net.URI ; 28 29 import javax.swing.text.BadLocationException ; 30 import javax.swing.text.Document ; 31 32 import org.netbeans.modules.xslt.model.XslModel; 33 import org.netbeans.modules.xslt.model.resources.ResourceMarker; 34 35 39 public class Utils { 40 41 public static File getTempDir(String path) throws Exception { 42 File tempdir = new File (System.getProperty("java.io.tmpdir"), path); 43 tempdir.mkdirs(); 44 return tempdir; 45 } 46 47 public static XslModel loadXslModel(String resourcePath) throws Exception { 48 return loadXslModel( resourcePath , false ); 49 } 50 51 52 public static XslModel loadXslModel(String resourcePath, boolean reload) 53 throws Exception 54 { 55 String location = resourcePath.substring(resourcePath.lastIndexOf('/')+1); 56 URI locationURI = new URI (location); 57 TestCatalogModel.getDefault().addURI(locationURI, 58 getResourceURI(resourcePath)); 59 return TestCatalogModel.getDefault().getXslModel(locationURI, reload); 60 } 61 62 public static XslModel loadXslModel(File schemaFile) throws Exception { 63 URI locationURI = new URI (schemaFile.getName()); 64 TestCatalogModel.getDefault().addURI(locationURI, schemaFile.toURI()); 65 return TestCatalogModel.getDefault().getXslModel(locationURI); 66 } 67 68 public static URI getResourceURI(String path) throws RuntimeException { 69 try { 70 return ResourceMarker.class.getResource(path).toURI(); 71 } catch (Exception ex) { 72 throw new RuntimeException (ex); 73 } 74 } 75 76 public static void setNewContent( XslModel model , String newContent ) 77 throws IOException 78 { 79 Document doc = (Document ) model.getModelSource().getLookup(). 80 lookup( Document .class ); 81 assert doc != null; 82 try { 83 doc.remove( 0 , doc.getLength()); 84 doc.insertString( 0 , newContent , null ); 85 } 86 catch (BadLocationException e) { 87 assert false; 88 } 89 model.sync(); 90 } 91 92 public static void setNewContentFromFile( XslModel model , String fileName ) 93 throws IOException 94 { 95 InputStream stream = ResourceMarker.class.getResourceAsStream( fileName ); 96 BufferedReader reader = new BufferedReader (new InputStreamReader ( stream)); 97 StringBuilder builder = new StringBuilder (); 98 String nextLine = null; 99 try { 100 while( (nextLine = reader.readLine()) != null ) { 101 builder.append( nextLine ); 102 builder.append(System.getProperty("line.separator")); 103 } 104 } 105 finally { 106 reader.close(); 107 } 108 setNewContent(model, builder.toString() ); 109 } 110 } 111 | Popular Tags |