1 19 20 package org.netbeans.modules.xml.schema.core; 21 22 import java.io.BufferedInputStream ; 23 import java.io.BufferedOutputStream ; 24 import java.io.BufferedReader ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.InputStreamReader ; 31 import java.io.OutputStream ; 32 import java.io.PrintWriter ; 33 import java.net.URI ; 34 import javax.swing.JEditorPane ; 35 import javax.swing.text.Document ; 36 import org.netbeans.api.project.Project; 37 import org.netbeans.api.project.ProjectManager; 38 import org.netbeans.modules.xml.schema.model.ReferenceableSchemaComponent; 39 import org.netbeans.modules.xml.schema.model.Schema; 40 import org.netbeans.modules.xml.schema.model.SchemaComponent; 41 import org.netbeans.modules.xml.schema.model.visitor.FindSchemaComponentFromDOM; 42 import org.netbeans.modules.xml.text.syntax.XMLKit; 43 import org.netbeans.modules.xml.xam.dom.AbstractDocumentModel; 44 import org.netbeans.modules.xml.xam.dom.DocumentModel; 45 import org.openide.filesystems.FileLock; 46 import org.openide.filesystems.FileObject; 47 import org.openide.filesystems.FileUtil; 48 import org.openide.filesystems.Repository; 49 50 54 public class Util { 55 static { 56 registerXMLKit(); 58 } 59 60 public static void registerXMLKit() { 61 String [] path = new String [] { "Editors", "application", "x-schema+xml" }; 62 FileObject target = Repository.getDefault().getDefaultFileSystem().getRoot(); 63 try { 64 for (int i=0; i<path.length; i++) { 65 FileObject f = target.getFileObject(path[i]); 66 if (f == null) { 67 f = target.createFolder(path[i]); 68 } 69 target = f; 70 } 71 String name = "EditorKit.instance"; 72 if (target.getFileObject(name) == null) { 73 FileObject f = target.createData(name); 74 f.setAttribute("instanceClass", "org.netbeans.modules.xml.text.syntax.XMLKit"); 75 } 76 } catch(IOException ioe) { 77 ioe.printStackTrace(); 78 } 79 } 80 81 public static Document getResourceAsDocument(String path) throws Exception { 82 InputStream in = Util.class.getResourceAsStream(path); 83 return loadDocument(in); 84 } 85 86 public static Document loadDocument(InputStream in) throws Exception { 87 Document sd = new org.netbeans.editor.BaseDocument( 89 org.netbeans.modules.xml.text.syntax.XMLKit.class, false); 90 return setDocumentContentTo(sd, in); 91 } 92 93 public static Document setDocumentContentTo(Document doc, InputStream in) throws Exception { 94 BufferedReader br = new BufferedReader (new InputStreamReader (in)); 95 StringBuffer sbuf = new StringBuffer (); 96 try { 97 String line = null; 98 while ((line = br.readLine()) != null) { 99 sbuf.append(line); 100 sbuf.append(System.getProperty("line.separator")); 101 } 102 } finally { 103 br.close(); 104 } 105 doc.remove(0, doc.getLength()); 106 doc.insertString(0,sbuf.toString(),null); 107 return doc; 108 } 109 110 public static Document setDocumentContentTo(Document doc, String resourcePath) throws Exception { 111 return setDocumentContentTo(doc, Util.class.getResourceAsStream(resourcePath)); 112 } 113 114 public static void setDocumentContentTo(DocumentModel model, String resourcePath) throws Exception { 115 setDocumentContentTo(((AbstractDocumentModel)model).getBaseDocument(), resourcePath); 116 } 117 118 public static void dumpToStream(Document doc, OutputStream out) throws Exception { 119 PrintWriter w = new PrintWriter (out); 120 w.print(doc.getText(0, doc.getLength())); 121 w.close(); 122 out.close(); 123 } 124 125 public static void dumpToFile(Document doc, File f) throws Exception { 126 OutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 127 PrintWriter w = new PrintWriter (out); 128 w.print(doc.getText(0, doc.getLength())); 129 w.close(); 130 out.close(); 131 } 132 133 public static File dumpToTempFile(Document doc) throws Exception { 134 File f = File.createTempFile("xsm", "xsd"); 135 dumpToFile(doc, f); 136 return f; 137 } 138 139 public static Document loadDocument(File f) throws Exception { 140 InputStream in = new BufferedInputStream (new FileInputStream (f)); 141 return loadDocument(in); 142 } 143 144 public static SchemaComponent findComponent(Schema schema, String xpath) { 145 return (new FindSchemaComponentFromDOM().findComponent(schema, xpath)); 146 } 147 148 public static URI getResourceURI(String path) throws RuntimeException { 149 try { 150 return Util.class.getResource(path).toURI(); 151 } catch (Exception ex) { 152 throw new RuntimeException (ex); 153 } 154 } 155 156 public static File getTempDir(String path) throws Exception { 157 File tempdir = new File (System.getProperty("java.io.tmpdir"), path); 158 tempdir.mkdirs(); 159 return tempdir; 160 } 161 162 public static Project createJavaTestProject() throws Exception { 163 FileObject projectDir = FileUtil.toFileObject(getTempDir("TestJavaProject").getCanonicalFile()); 164 copyResource("resources/TestJavaProject/build.xml", projectDir); 165 copyResource("resources/TestJavaProject/manifest.mf", projectDir); 166 FileObject nbprojectDir = FileUtil.createFolder(projectDir, "nbproject"); 167 copyResource("resources/TestJavaProject/nbproject/build-impl.xml", nbprojectDir); 168 copyResource("resources/TestJavaProject/nbproject/project.xml", nbprojectDir); 169 copyResource("resources/TestJavaProject/nbproject/project.properties", nbprojectDir); 170 return ProjectManager.getDefault().findProject(projectDir); 171 } 172 173 public static FileObject populateProject(Project project, String xsd, String path) throws Exception { 174 FileObject srcDir = FileUtil.createFolder(project.getProjectDirectory(), path); 175 return copyResource(xsd, srcDir); 176 } 177 178 public static FileObject copyResource(String path, FileObject destFolder) throws Exception { 179 String filename = getFileName(path); 180 181 FileObject dest = destFolder.getFileObject(filename); 182 if (dest == null) { 183 dest = destFolder.createData(filename); 184 } 185 FileLock lock = dest.lock(); 186 OutputStream out = dest.getOutputStream(lock); 187 InputStream in = Util.class.getResourceAsStream(path); 188 try { 189 FileUtil.copy(in, out); 190 } finally { 191 out.close(); 192 in.close(); 193 if (lock != null) lock.releaseLock(); 194 } 195 return dest; 196 } 197 198 public static String getFileName(String path) { 199 int i = path.lastIndexOf('/'); 200 if (i > -1) { 201 return path.substring(i+1); 202 } else { 203 return path; 204 } 205 } 206 207 public static ReferenceableSchemaComponent findGlobalComponentByName(Schema schema, String name) { 208 for (ReferenceableSchemaComponent t : schema.getChildren(ReferenceableSchemaComponent.class)) { 209 if (t.getName().equals(name)) { 210 return t; 211 } 212 } 213 return null; 214 } 215 216 } 217 | Popular Tags |