KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xslt > model > impl > Utils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xslt.model.impl;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.net.URI JavaDoc;
28
29 import javax.swing.text.BadLocationException JavaDoc;
30 import javax.swing.text.Document JavaDoc;
31
32 import org.netbeans.modules.xslt.model.XslModel;
33 import org.netbeans.modules.xslt.model.resources.ResourceMarker;
34
35 /**
36  *
37  * @author ads
38  */

39 public class Utils {
40     
41     public static File JavaDoc getTempDir(String JavaDoc path) throws Exception JavaDoc {
42         File JavaDoc tempdir = new File JavaDoc(System.getProperty("java.io.tmpdir"), path);
43         tempdir.mkdirs();
44         return tempdir;
45     }
46     
47     public static XslModel loadXslModel(String JavaDoc resourcePath) throws Exception JavaDoc {
48         return loadXslModel( resourcePath , false );
49     }
50     
51     
52     public static XslModel loadXslModel(String JavaDoc resourcePath, boolean reload)
53         throws Exception JavaDoc
54     {
55         String JavaDoc location = resourcePath.substring(resourcePath.lastIndexOf('/')+1);
56         URI JavaDoc locationURI = new URI JavaDoc(location);
57         TestCatalogModel.getDefault().addURI(locationURI,
58                 getResourceURI(resourcePath));
59         return TestCatalogModel.getDefault().getXslModel(locationURI, reload);
60     }
61     
62     public static XslModel loadXslModel(File JavaDoc schemaFile) throws Exception JavaDoc {
63         URI JavaDoc locationURI = new URI JavaDoc(schemaFile.getName());
64         TestCatalogModel.getDefault().addURI(locationURI, schemaFile.toURI());
65         return TestCatalogModel.getDefault().getXslModel(locationURI);
66     }
67     
68     public static URI JavaDoc getResourceURI(String JavaDoc path) throws RuntimeException JavaDoc {
69         try {
70             return ResourceMarker.class.getResource(path).toURI();
71         } catch (Exception JavaDoc ex) {
72             throw new RuntimeException JavaDoc(ex);
73         }
74     }
75     
76     public static void setNewContent( XslModel model , String JavaDoc newContent )
77         throws IOException JavaDoc
78     {
79         Document JavaDoc doc = (Document JavaDoc) model.getModelSource().getLookup().
80             lookup( Document JavaDoc.class );
81         assert doc != null;
82         try {
83             doc.remove( 0 , doc.getLength());
84             doc.insertString( 0 , newContent , null );
85         }
86         catch (BadLocationException JavaDoc e) {
87             assert false;
88         }
89         model.sync();
90     }
91     
92     public static void setNewContentFromFile( XslModel model , String JavaDoc fileName )
93         throws IOException JavaDoc
94     {
95         InputStream JavaDoc stream = ResourceMarker.class.getResourceAsStream( fileName );
96         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc( stream));
97         StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
98         String JavaDoc 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