KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > blobstoreconvertor > BlobStoreConvertor


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package org.outerj.daisy.blobstoreconvertor;
17
18 import java.io.File JavaDoc;
19 import java.io.FileFilter JavaDoc;
20
21 public class BlobStoreConvertor {
22     private File JavaDoc blobDir;
23
24     private static final int DIRECTORY_DEPTH = 4;
25
26     private static final int DIRECTORY_NAME_LENGTH = 2;
27
28     private static final int KEYLENGTH = 20;
29
30     public static void main(String JavaDoc[] args) {
31         if (args.length == 1) {
32             try {
33                 new BlobStoreConvertor(args[0]).flatToHierarchical();
34             } catch (Exception JavaDoc e) {
35                 e.printStackTrace();
36                 System.exit(1);
37             }
38         }
39     }
40
41     public BlobStoreConvertor(String JavaDoc blobStorePath) throws Exception JavaDoc {
42         blobDir = new File JavaDoc(blobStorePath);
43         if (!blobDir.exists() || !blobDir.isDirectory())
44             throw new Exception JavaDoc(blobStorePath + " does not exist or is not a directory");
45     }
46
47     public void flatToHierarchical() throws Exception JavaDoc {
48         File JavaDoc[] files = blobDir.listFiles(new FlatBlobFilter());
49         for (int i = 0; i < files.length; i++) {
50             String JavaDoc id = files[i].getName();
51             System.out.println("Processing " + id);
52             String JavaDoc subdirName = "";
53             int position = 0;
54             for (int j = 0; j < DIRECTORY_DEPTH; j++)
55                 subdirName += id.substring(position, position += DIRECTORY_NAME_LENGTH) + File.separator;
56
57             File JavaDoc subdir = new File JavaDoc(blobDir, subdirName);
58             subdir.mkdirs();
59
60             File JavaDoc newFile = new File JavaDoc(subdir, id.substring(position));
61             if (!files[i].renameTo(newFile)) {
62                 throw new Exception JavaDoc("Could not rename file " + files[i].getName());
63             }
64         }
65         System.out.println("Finished successfully.");
66     }
67
68     private class FlatBlobFilter implements FileFilter JavaDoc {
69         public boolean accept(File JavaDoc pathname) {
70             return pathname.isFile() && pathname.getName().length() == KEYLENGTH * 2;
71         }
72     }
73 }
74
Popular Tags