KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > jackpotrules > JackpotUtils


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.apisupport.jackpotrules;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.Writer JavaDoc;
30 import java.net.URL JavaDoc;
31 import java.util.zip.GZIPInputStream JavaDoc;
32
33 import junit.framework.Assert;
34
35 import org.netbeans.jackpot.engine.*;
36 import org.netbeans.jackpot.transform.Transformer;
37
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.filesystems.Repository;
41
42 /**
43  *
44  * @author Jaroslav Tulach
45  */

46 public final class JackpotUtils {
47     
48     /** Creates a new instance of JackpotUtils */
49     private JackpotUtils() {
50     }
51
52     public static void apply(File JavaDoc dir, URL JavaDoc rules) throws Exception JavaDoc {
53         DefaultApplicationContext context = new DefaultApplicationContext();
54         JackpotEngine eng = EngineFactory.createEngine(context);
55
56         File JavaDoc rulesFile = extractResource(rules);
57
58         int errors = eng.initialize(dir.getPath(), System.getProperty("java.class.path"), "1.4");
59         Assert.assertEquals("No errors during compilation", 0, errors);
60         eng.runScript("q", "t", rulesFile.getPath());
61
62         Assert.assertTrue("There is something to commit", eng.needsCommit());
63         Assert.assertTrue("commit ok", eng.commit());
64     }
65
66     final static String JavaDoc readFile (java.io.File JavaDoc f, boolean gzip) throws java.io.IOException JavaDoc {
67         if (!gzip) {
68             int s = (int)f.length ();
69             byte[] data = new byte[s];
70             Assert.assertEquals ("Read all data", s, new FileInputStream JavaDoc (f).read (data));
71
72             return new String JavaDoc (data);
73         } else {
74             GZIPInputStream JavaDoc is = new GZIPInputStream JavaDoc(new FileInputStream JavaDoc(f));
75             byte[] arr = new byte[256 * 256];
76             int first = 0;
77             for(;;) {
78                 int len = is.read(arr, first, arr.length - first);
79                 if (first + len < arr.length) {
80                     return new String JavaDoc(arr, 0, first + len);
81                 }
82             }
83         }
84     }
85     
86     final static File JavaDoc extractString(String JavaDoc res) throws Exception JavaDoc {
87         File JavaDoc f = File.createTempFile("res", ".xml");
88         f.deleteOnExit ();
89         return extractString(f, res);
90     }
91
92     final static File JavaDoc extractString (File JavaDoc f, String JavaDoc res) throws Exception JavaDoc {
93         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
94         InputStream JavaDoc is = new ByteArrayInputStream JavaDoc(res.getBytes("UTF-8"));
95         for (;;) {
96             int ch = is.read ();
97             if (ch == -1) break;
98             os.write (ch);
99         }
100         os.close ();
101             
102         return f;
103     }
104     
105     final static File JavaDoc extractResource(String JavaDoc res) throws Exception JavaDoc {
106         URL JavaDoc u = JackpotUtils.class.getResource(res);
107         Assert.assertNotNull ("Resource should be found " + res, u);
108         return extractResource(u);
109     }
110
111     final static File JavaDoc extractResource(URL JavaDoc u) throws Exception JavaDoc {
112         Assert.assertNotNull ("Resource should be found " + u, u);
113         
114         File JavaDoc f = File.createTempFile("res", ".xml");
115         f.deleteOnExit ();
116         
117         FileOutputStream JavaDoc os = new FileOutputStream JavaDoc(f);
118         InputStream JavaDoc is = u.openStream();
119         for (;;) {
120             int ch = is.read ();
121             if (ch == -1) break;
122             os.write (ch);
123         }
124         os.close ();
125             
126         return f;
127     }
128
129 }
130
Popular Tags