KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > filesystems > xmlfs > XMLFSTest


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 package org.openide.filesystems.xmlfs;
20
21 import java.io.*;
22
23 import org.openide.*;
24 import org.openide.filesystems.*;
25 import org.openide.filesystems.localfs.LocalFSTest;
26 import org.openide.filesystems.data.SerialData;
27
28 /**
29  * Base class for testing XMLFileSystem.
30  */

31 public class XMLFSTest extends ReadOnlyFSTest {
32
33     public static final String JavaDoc PACKAGE = "org/openide/filesystems/data/";
34     private static final String JavaDoc MF_NAME = "mf-layer";
35     private static final String JavaDoc HEADER = "<?xml version=\"1.0\"?>";//\n<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.0//EN\" \"http://www.netbeans.org/dtds/filesystem-1_0.dtd\">";
36
private static final String JavaDoc FOLDER_START1 = " <folder name=\"org\">\n <folder name=\"openide\">\n <folder name=\"filesystems\">\n <folder name=\"data";
37     private static final String JavaDoc FOLDER_START2 = "\">";
38     private static final String JavaDoc FOLDER_END = " </folder>\n </folder>\n </folder>\n </folder>\n";
39     private static final int FOLDER_INDENT = FOLDER_END.indexOf('<') / 2;
40     private static final String JavaDoc INDENT_STEP = " ";
41     
42     /** Root folder for this test */
43     protected File tmp;
44     /** Working folder */
45     protected File destFolder;
46     /** Tested XMLFS */
47     protected XMLFileSystem xmlfs;
48     
49     /** Creates new XMLFSGenerator */
50     public XMLFSTest(String JavaDoc name) {
51         super(name);
52     }
53
54     /** Set up given number of FileObjects */
55     protected FileObject[] setUpFileObjects(int foCount) throws Exception JavaDoc {
56         tmp = createTempFolder();
57         destFolder = LocalFSTest.createFiles(foCount, 0, tmp);
58         File xmlbase = generateXMLFile(destFolder, new ResourceComposer(LocalFSTest.RES_NAME, LocalFSTest.RES_EXT, foCount, 0));
59         xmlfs = new XMLFileSystem();
60         xmlfs.setXmlUrl(xmlbase.toURL(), false);
61         
62         FileObject pkg = xmlfs.findResource(PACKAGE);
63         return pkg.getChildren();
64     }
65     
66     /** Disposes given FileObjects */
67     protected void tearDownFileObjects(FileObject[] fos) throws Exception JavaDoc {
68         destFolder = null;
69         delete(tmp);
70         tmp = null;
71     }
72     
73     /** Generates an XML file that describes a filesystem structure.
74      * @param folder - where to place the file
75      * @param composer a factory that assemblies resource strings
76      */

77     public static final File generateXMLFile(File folder, ResourceComposer composer) throws Exception JavaDoc {
78         String JavaDoc name = MF_NAME + '-' + String.valueOf(composer.getFileBase());
79         File dest = new File(folder, name.concat(".xml"));
80         
81         OutputStream os = new FileOutputStream(dest);
82         Writer writer = new OutputStreamWriter(os);
83         writer.write(generate(composer));
84         writer.close();
85         os.close();
86         
87         return dest;
88     }
89     
90     /** Generates an XML file that describes a filesystem structure.
91      * @return a String that is an xml document describing a filesystem
92      */

93     public static String JavaDoc generate(ResourceComposer composer) throws Exception JavaDoc {
94         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50000);
95         buffer.append(HEADER).append('\n');
96         buffer.append("<filesystem>").append('\n');
97         generateFolder(buffer, composer);
98         buffer.append("</filesystem>").append('\n');
99         
100         return buffer.toString();
101     }
102     
103     /** Generates an XML description of a folder inside a filesystem structure.
104      * @param buffer - where to place the description
105      */

106     private static final void generateFolder(StringBuffer JavaDoc buffer, ResourceComposer composer) throws Exception JavaDoc {
107         buffer.append(FOLDER_START1);
108         int base = composer.getFileBase();
109         if (base != 0) {
110             buffer.append(base);
111         }
112         buffer.append(FOLDER_START2);
113         generateFiles(buffer, composer);
114         buffer.append(FOLDER_END);
115     }
116
117     /** Generates an XML description of files inside a folder structure.
118      * @param buffer - where to place the description
119      */

120     private static final void generateFiles(StringBuffer JavaDoc buffer, ResourceComposer composer) throws Exception JavaDoc {
121         int base = composer.getFileBase();
122         int fileNo = composer.getFileCount();
123         for (int i = 0; i < fileNo; i++) {
124             composer.setFileBase(base + i);
125             generateOneFile(buffer, composer);
126         }
127     }
128     
129     /** Generates an XML description of a file inside a folder structure.
130      * @param buffer - where to place the description
131      */

132     private static void generateOneFile(StringBuffer JavaDoc buffer, ResourceComposer composer) throws Exception JavaDoc {
133         buffer.append('\n');
134         addFileHeader(buffer, composer);
135         generateAttributes(buffer, composer.getPaddingSize());
136         addFileEnd(buffer);
137     }
138     
139     /** Generates an XML description of attributes inside a file description.
140      * @param buffer - where to place the description
141      */

142     private static void generateAttributes(StringBuffer JavaDoc buffer, int paddingSize) throws Exception JavaDoc {
143         generateSerialAttr(buffer);
144         for (int i = 0; i < 5; i++) {
145             generateStringAttr(buffer, i, paddingSize);
146         }
147     }
148     
149     /** Generates a serial attribute inside a file description.
150      * @param buffer - where to place the description
151      */

152     private static void generateSerialAttr(StringBuffer JavaDoc buffer) throws Exception JavaDoc {
153         addIndent(buffer, FOLDER_INDENT + 2);
154         buffer.append("<attr name=\"NetBeansAttrSerial\" serialvalue=\"").append(SerialData.getSerialDataString()).append("\"/>");
155         buffer.append('\n');
156     }
157     
158     /** Generates i-th String attribute inside a file description.
159      * @param buffer - where to place the description
160      */

161     private static void generateStringAttr(StringBuffer JavaDoc buffer, int i, int paddingSize) {
162         addIndent(buffer, FOLDER_INDENT + 2);
163         buffer.append("<attr name=\"key_");
164         Utilities.appendNDigits(i, paddingSize, buffer);
165         buffer.append("\" stringvalue=\"val_");
166         Utilities.appendNDigits(i, paddingSize, buffer);
167         buffer.append("\"/>");
168         buffer.append('\n');
169     }
170     
171     /** Generates file end inside a folder description.
172      * @param buffer - where to place the description
173      */

174     private static void addFileEnd(StringBuffer JavaDoc buffer) {
175         addIndent(buffer, FOLDER_INDENT + 1);
176         buffer.append("</file>");
177         buffer.append('\n');
178     }
179     
180     /** Generates file start inside a folder description.
181      * @param buffer - where to place the description
182      */

183     private static void addFileHeader(StringBuffer JavaDoc buffer, ResourceComposer composer) {
184         addIndent(buffer, FOLDER_INDENT + 1);
185         buffer.append("<file name=\"");
186         composer.assemblyResourceString(buffer);
187         buffer.append("\" url=\"");
188         composer.assemblyResourceString(buffer);
189         buffer.append("\">").append('\n');
190     }
191     
192     /** Adds indent
193      * @param buffer - where to place the description
194      */

195     private static void addIndent(StringBuffer JavaDoc buffer, int howMuch) {
196         for (int i = 0; i < howMuch; i++) {
197             buffer.append(INDENT_STEP);
198         }
199     }
200     
201     /** Assemblies resource string */
202     public static final class ResourceComposer {
203         private final int paddingSize;
204         private int fileBase;
205         private final int foCount;
206         private String JavaDoc resName;
207         private String JavaDoc resExt;
208         
209         /** new ResourceComposer */
210         public ResourceComposer(String JavaDoc resName, String JavaDoc resExt, int foCount, int fileBase) {
211             this.foCount = foCount;
212             this.paddingSize = Utilities.expPaddingSize(foCount + fileBase - 1);
213             this.fileBase = fileBase;
214             this.resName = resName;
215             this.resExt = resExt;
216         }
217         
218         /** getter for paddingSize */
219         protected final int getPaddingSize() {
220             return paddingSize;
221         }
222         
223         /** getter for fileBase */
224         protected final int getFileBase() {
225             return fileBase;
226         }
227         
228         /** setter for fileBase */
229         protected final void setFileBase(int newBase) {
230             fileBase = newBase;
231         }
232         
233         /** getter for file count */
234         protected final int getFileCount() {
235             return foCount;
236         }
237         
238         /** Assembly fileBase (e.g. 13) with name (e.g. JavaSrc) and ext (e.g. .java) into sbuffer.
239          * Do not forget to take into account paddingSize.
240          * Result could be e.g. JavaSrc0675.java, with paddingSize 4 and fileBase 675.
241          */

242         public void assemblyResourceString(StringBuffer JavaDoc sbuffer) {
243             sbuffer.append(resName);
244             Utilities.appendNDigits(getFileBase(), getPaddingSize(), sbuffer);
245             sbuffer.append(resExt);
246         }
247     }
248
249     /*
250      public static void main(String[] args) throws Exception {
251         XMLFSTest xmlfstest = new XMLFSTest("first test");
252         xmlfstest.setUpFileObjects(500);
253     }
254     */

255 }
256
Popular Tags