1 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 31 public class XMLFSTest extends ReadOnlyFSTest { 32 33 public static final String PACKAGE = "org/openide/filesystems/data/"; 34 private static final String MF_NAME = "mf-layer"; 35 private static final String HEADER = "<?xml version=\"1.0\"?>"; private static final String FOLDER_START1 = " <folder name=\"org\">\n <folder name=\"openide\">\n <folder name=\"filesystems\">\n <folder name=\"data"; 37 private static final String FOLDER_START2 = "\">"; 38 private static final String 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 INDENT_STEP = " "; 41 42 43 protected File tmp; 44 45 protected File destFolder; 46 47 protected XMLFileSystem xmlfs; 48 49 50 public XMLFSTest(String name) { 51 super(name); 52 } 53 54 55 protected FileObject[] setUpFileObjects(int foCount) throws Exception { 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 67 protected void tearDownFileObjects(FileObject[] fos) throws Exception { 68 destFolder = null; 69 delete(tmp); 70 tmp = null; 71 } 72 73 77 public static final File generateXMLFile(File folder, ResourceComposer composer) throws Exception { 78 String 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 93 public static String generate(ResourceComposer composer) throws Exception { 94 StringBuffer buffer = new StringBuffer (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 106 private static final void generateFolder(StringBuffer buffer, ResourceComposer composer) throws Exception { 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 120 private static final void generateFiles(StringBuffer buffer, ResourceComposer composer) throws Exception { 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 132 private static void generateOneFile(StringBuffer buffer, ResourceComposer composer) throws Exception { 133 buffer.append('\n'); 134 addFileHeader(buffer, composer); 135 generateAttributes(buffer, composer.getPaddingSize()); 136 addFileEnd(buffer); 137 } 138 139 142 private static void generateAttributes(StringBuffer buffer, int paddingSize) throws Exception { 143 generateSerialAttr(buffer); 144 for (int i = 0; i < 5; i++) { 145 generateStringAttr(buffer, i, paddingSize); 146 } 147 } 148 149 152 private static void generateSerialAttr(StringBuffer buffer) throws Exception { 153 addIndent(buffer, FOLDER_INDENT + 2); 154 buffer.append("<attr name=\"NetBeansAttrSerial\" serialvalue=\"").append(SerialData.getSerialDataString()).append("\"/>"); 155 buffer.append('\n'); 156 } 157 158 161 private static void generateStringAttr(StringBuffer 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 174 private static void addFileEnd(StringBuffer buffer) { 175 addIndent(buffer, FOLDER_INDENT + 1); 176 buffer.append("</file>"); 177 buffer.append('\n'); 178 } 179 180 183 private static void addFileHeader(StringBuffer 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 195 private static void addIndent(StringBuffer buffer, int howMuch) { 196 for (int i = 0; i < howMuch; i++) { 197 buffer.append(INDENT_STEP); 198 } 199 } 200 201 202 public static final class ResourceComposer { 203 private final int paddingSize; 204 private int fileBase; 205 private final int foCount; 206 private String resName; 207 private String resExt; 208 209 210 public ResourceComposer(String resName, String 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 219 protected final int getPaddingSize() { 220 return paddingSize; 221 } 222 223 224 protected final int getFileBase() { 225 return fileBase; 226 } 227 228 229 protected final void setFileBase(int newBase) { 230 fileBase = newBase; 231 } 232 233 234 protected final int getFileCount() { 235 return foCount; 236 } 237 238 242 public void assemblyResourceString(StringBuffer sbuffer) { 243 sbuffer.append(resName); 244 Utilities.appendNDigits(getFileBase(), getPaddingSize(), sbuffer); 245 sbuffer.append(resExt); 246 } 247 } 248 249 255 } 256 | Popular Tags |