~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to src/org/herac/tuxguitar/io/TGConverter.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2007-02-04 01:41:23 UTC
  • Revision ID: james.westby@ubuntu.com-20070204014123-9pv7okph0iaiqkvw
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Created on 09-dic-2005
 
3
 *
 
4
 * TODO To change the template for this generated file go to
 
5
 * Window - Preferences - Java - Code Style - Code Templates
 
6
 */
 
7
package org.herac.tuxguitar.io;
 
8
 
 
9
import java.io.File;
 
10
import java.io.FileNotFoundException;
 
11
 
 
12
import org.herac.tuxguitar.io.gp.GPFormatException;
 
13
import org.herac.tuxguitar.io.tg.TGOutputStream;
 
14
import org.herac.tuxguitar.song.managers.SongManager;
 
15
import org.herac.tuxguitar.song.models.Song;
 
16
 
 
17
/**
 
18
 * @author julian
 
19
 * 
 
20
 * TODO To change the template for this generated type comment go to Window - Preferences - Java - Code Style - Code Templates
 
21
 */
 
22
public class TGConverter {
 
23
        private SongManager manager;
 
24
    private String sourceFolder;
 
25
    private String destinationFolder;
 
26
    
 
27
    public TGConverter(String sourceFolder,String destinationFolder){
 
28
        this.manager = new SongManager();
 
29
        this.sourceFolder = sourceFolder;
 
30
        this.destinationFolder = destinationFolder;
 
31
    }
 
32
    
 
33
    public void convert(String fileName, String convertFileName) {
 
34
        try {
 
35
                if(manager.open(fileName)){       
 
36
                        manager.autoCompleteSilences();                         
 
37
                        System.out.print(convertFileName);
 
38
                        new File(new File(convertFileName).getParent()).mkdirs();                       
 
39
                        new TGOutputStream(convertFileName).write(manager.getSong()); 
 
40
                        System.out.println(" [OK] ");
 
41
                }else if (fileName.endsWith(".gp3") || fileName.endsWith(".gp4") || fileName.endsWith(".gp5")) {
 
42
                        System.err.println(fileName + " [Unsupported Version]");
 
43
                }
 
44
        } catch (FileNotFoundException e) {
 
45
        } catch (GPFormatException e) {
 
46
            System.out.println(convertFileName + " [FAILED] ");
 
47
            System.out.println(" ********** REASON: " + e.getClass().getName() + " ********** ");
 
48
            e.printStackTrace();
 
49
            System.exit(1);             
 
50
        } catch (Exception e) {
 
51
            System.out.println(convertFileName + " [FAILED] ");
 
52
            System.out.println(" ********** REASON: " + e.getClass().getName() + " ********** ");
 
53
            e.printStackTrace();
 
54
            System.exit(1);
 
55
        } catch (OutOfMemoryError e) {
 
56
            System.out.println(convertFileName + "  [FAILED]  ");
 
57
            System.out.println(" ********** REASON: OutOfMemoryError ********** ");
 
58
            System.gc();
 
59
        }       
 
60
    }
 
61
 
 
62
    private void process(File folder) {
 
63
        String[] fileNames = folder.list();
 
64
        if(fileNames != null){
 
65
                for (int i = 0; i < fileNames.length; i++) {
 
66
                        File file = new File(folder.getPath() + "/" + fileNames[i]);
 
67
                        if (file.isDirectory()) {
 
68
                                process(file);
 
69
                        } else {
 
70
                                String fileName = file.getAbsolutePath();
 
71
                                String convertFileName = getConvertFileName(fileName);
 
72
                                convert(fileName, convertFileName);
 
73
                        }
 
74
                        fileNames[i] = null;
 
75
                }
 
76
        }
 
77
    }
 
78
 
 
79
    private String getConvertFileName(String path) {
 
80
        String convertPath = (destinationFolder + File.separator +path.substring(sourceFolder.length()));
 
81
        if (convertPath.endsWith(".gp3") || convertPath.endsWith(".gp4") || convertPath.endsWith(".gp5")) {
 
82
                convertPath = convertPath.replaceAll(".gp3", ".tg");
 
83
                convertPath = convertPath.replaceAll(".gp4", ".tg");
 
84
                convertPath = convertPath.replaceAll(".gp5", ".tg");
 
85
        }        
 
86
        return convertPath;
 
87
    }
 
88
 
 
89
    private void writeSong(String fileName, Song song) {
 
90
        try {
 
91
            new TGOutputStream(fileName).write(song);
 
92
        } catch (FileNotFoundException e) {
 
93
            e.printStackTrace();
 
94
        }
 
95
    }
 
96
    
 
97
 
 
98
    public static void main(String args[]) {
 
99
        if(args.length >= 2){ 
 
100
            String sourceFolder = args[0];
 
101
            String destinationFolder = args[1];
 
102
            
 
103
            TGConverter converter = new TGConverter(sourceFolder,destinationFolder);
 
104
            File initFolder = new File(sourceFolder);
 
105
            converter.process(initFolder);
 
106
        }else{
 
107
            showHelp();
 
108
        }
 
109
        System.exit(0);
 
110
    }
 
111
 
 
112
    private static void showHelp(){
 
113
        System.out.println("usage options:  [SOURCE FOLDER] [DESTINATION FOLDER]");
 
114
    }
 
115
        
 
116
 
 
117
}
 
 
b'\\ No newline at end of file'