001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.openide.filesystems.xmlfs;
042:
043: import java.io.*;
044:
045: import org.openide.*;
046: import org.openide.filesystems.*;
047: import org.openide.filesystems.localfs.LocalFSTest;
048: import org.openide.filesystems.data.SerialData;
049:
050: /**
051: * Base class for testing XMLFileSystem.
052: */
053: public class XMLFSTest extends ReadOnlyFSTest {
054:
055: public static final String PACKAGE = "org/openide/filesystems/data/";
056: private static final String MF_NAME = "mf-layer";
057: private static final String HEADER = "<?xml version=\"1.0\"?>";//\n<!DOCTYPE filesystem PUBLIC \"-//NetBeans//DTD Filesystem 1.0//EN\" \"http://www.netbeans.org/dtds/filesystem-1_0.dtd\">";
058: private static final String FOLDER_START1 = " <folder name=\"org\">\n <folder name=\"openide\">\n <folder name=\"filesystems\">\n <folder name=\"data";
059: private static final String FOLDER_START2 = "\">";
060: private static final String FOLDER_END = " </folder>\n </folder>\n </folder>\n </folder>\n";
061: private static final int FOLDER_INDENT = FOLDER_END.indexOf('<') / 2;
062: private static final String INDENT_STEP = " ";
063:
064: /** Root folder for this test */
065: protected File tmp;
066: /** Working folder */
067: protected File destFolder;
068: /** Tested XMLFS */
069: protected XMLFileSystem xmlfs;
070:
071: /** Creates new XMLFSGenerator */
072: public XMLFSTest(String name) {
073: super (name);
074: }
075:
076: /** Set up given number of FileObjects */
077: protected FileObject[] setUpFileObjects(int foCount)
078: throws Exception {
079: tmp = createTempFolder();
080: destFolder = LocalFSTest.createFiles(foCount, 0, tmp);
081: File xmlbase = generateXMLFile(destFolder,
082: new ResourceComposer(LocalFSTest.RES_NAME,
083: LocalFSTest.RES_EXT, foCount, 0));
084: xmlfs = new XMLFileSystem();
085: xmlfs.setXmlUrl(xmlbase.toURL(), false);
086:
087: FileObject pkg = xmlfs.findResource(PACKAGE);
088: return pkg.getChildren();
089: }
090:
091: /** Disposes given FileObjects */
092: protected void tearDownFileObjects(FileObject[] fos)
093: throws Exception {
094: destFolder = null;
095: delete(tmp);
096: tmp = null;
097: }
098:
099: /** Generates an XML file that describes a filesystem structure.
100: * @param folder - where to place the file
101: * @param composer a factory that assemblies resource strings
102: */
103: public static final File generateXMLFile(File folder,
104: ResourceComposer composer) throws Exception {
105: String name = MF_NAME + '-'
106: + String.valueOf(composer.getFileBase());
107: File dest = new File(folder, name.concat(".xml"));
108:
109: OutputStream os = new FileOutputStream(dest);
110: Writer writer = new OutputStreamWriter(os);
111: writer.write(generate(composer));
112: writer.close();
113: os.close();
114:
115: return dest;
116: }
117:
118: /** Generates an XML file that describes a filesystem structure.
119: * @return a String that is an xml document describing a filesystem
120: */
121: public static String generate(ResourceComposer composer)
122: throws Exception {
123: StringBuffer buffer = new StringBuffer(50000);
124: buffer.append(HEADER).append('\n');
125: buffer.append("<filesystem>").append('\n');
126: generateFolder(buffer, composer);
127: buffer.append("</filesystem>").append('\n');
128:
129: return buffer.toString();
130: }
131:
132: /** Generates an XML description of a folder inside a filesystem structure.
133: * @param buffer - where to place the description
134: */
135: private static final void generateFolder(StringBuffer buffer,
136: ResourceComposer composer) throws Exception {
137: buffer.append(FOLDER_START1);
138: int base = composer.getFileBase();
139: if (base != 0) {
140: buffer.append(base);
141: }
142: buffer.append(FOLDER_START2);
143: generateFiles(buffer, composer);
144: buffer.append(FOLDER_END);
145: }
146:
147: /** Generates an XML description of files inside a folder structure.
148: * @param buffer - where to place the description
149: */
150: private static final void generateFiles(StringBuffer buffer,
151: ResourceComposer composer) throws Exception {
152: int base = composer.getFileBase();
153: int fileNo = composer.getFileCount();
154: for (int i = 0; i < fileNo; i++) {
155: composer.setFileBase(base + i);
156: generateOneFile(buffer, composer);
157: }
158: }
159:
160: /** Generates an XML description of a file inside a folder structure.
161: * @param buffer - where to place the description
162: */
163: private static void generateOneFile(StringBuffer buffer,
164: ResourceComposer composer) throws Exception {
165: buffer.append('\n');
166: addFileHeader(buffer, composer);
167: generateAttributes(buffer, composer.getPaddingSize());
168: addFileEnd(buffer);
169: }
170:
171: /** Generates an XML description of attributes inside a file description.
172: * @param buffer - where to place the description
173: */
174: private static void generateAttributes(StringBuffer buffer,
175: int paddingSize) throws Exception {
176: generateSerialAttr(buffer);
177: for (int i = 0; i < 5; i++) {
178: generateStringAttr(buffer, i, paddingSize);
179: }
180: }
181:
182: /** Generates a serial attribute inside a file description.
183: * @param buffer - where to place the description
184: */
185: private static void generateSerialAttr(StringBuffer buffer)
186: throws Exception {
187: addIndent(buffer, FOLDER_INDENT + 2);
188: buffer.append(
189: "<attr name=\"NetBeansAttrSerial\" serialvalue=\"")
190: .append(SerialData.getSerialDataString())
191: .append("\"/>");
192: buffer.append('\n');
193: }
194:
195: /** Generates i-th String attribute inside a file description.
196: * @param buffer - where to place the description
197: */
198: private static void generateStringAttr(StringBuffer buffer, int i,
199: int paddingSize) {
200: addIndent(buffer, FOLDER_INDENT + 2);
201: buffer.append("<attr name=\"key_");
202: Utilities.appendNDigits(i, paddingSize, buffer);
203: buffer.append("\" stringvalue=\"val_");
204: Utilities.appendNDigits(i, paddingSize, buffer);
205: buffer.append("\"/>");
206: buffer.append('\n');
207: }
208:
209: /** Generates file end inside a folder description.
210: * @param buffer - where to place the description
211: */
212: private static void addFileEnd(StringBuffer buffer) {
213: addIndent(buffer, FOLDER_INDENT + 1);
214: buffer.append("</file>");
215: buffer.append('\n');
216: }
217:
218: /** Generates file start inside a folder description.
219: * @param buffer - where to place the description
220: */
221: private static void addFileHeader(StringBuffer buffer,
222: ResourceComposer composer) {
223: addIndent(buffer, FOLDER_INDENT + 1);
224: buffer.append("<file name=\"");
225: composer.assemblyResourceString(buffer);
226: buffer.append("\" url=\"");
227: composer.assemblyResourceString(buffer);
228: buffer.append("\">").append('\n');
229: }
230:
231: /** Adds indent
232: * @param buffer - where to place the description
233: */
234: private static void addIndent(StringBuffer buffer, int howMuch) {
235: for (int i = 0; i < howMuch; i++) {
236: buffer.append(INDENT_STEP);
237: }
238: }
239:
240: /** Assemblies resource string */
241: public static final class ResourceComposer {
242: private final int paddingSize;
243: private int fileBase;
244: private final int foCount;
245: private String resName;
246: private String resExt;
247:
248: /** new ResourceComposer */
249: public ResourceComposer(String resName, String resExt,
250: int foCount, int fileBase) {
251: this .foCount = foCount;
252: this .paddingSize = Utilities.expPaddingSize(foCount
253: + fileBase - 1);
254: this .fileBase = fileBase;
255: this .resName = resName;
256: this .resExt = resExt;
257: }
258:
259: /** getter for paddingSize */
260: protected final int getPaddingSize() {
261: return paddingSize;
262: }
263:
264: /** getter for fileBase */
265: protected final int getFileBase() {
266: return fileBase;
267: }
268:
269: /** setter for fileBase */
270: protected final void setFileBase(int newBase) {
271: fileBase = newBase;
272: }
273:
274: /** getter for file count */
275: protected final int getFileCount() {
276: return foCount;
277: }
278:
279: /** Assembly fileBase (e.g. 13) with name (e.g. JavaSrc) and ext (e.g. .java) into sbuffer.
280: * Do not forget to take into account paddingSize.
281: * Result could be e.g. JavaSrc0675.java, with paddingSize 4 and fileBase 675.
282: */
283: public void assemblyResourceString(StringBuffer sbuffer) {
284: sbuffer.append(resName);
285: Utilities.appendNDigits(getFileBase(), getPaddingSize(),
286: sbuffer);
287: sbuffer.append(resExt);
288: }
289: }
290:
291: /*
292: public static void main(String[] args) throws Exception {
293: XMLFSTest xmlfstest = new XMLFSTest("first test");
294: xmlfstest.setUpFileObjects(500);
295: }
296: */
297: }
|