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

« back to all changes in this revision

Viewing changes to TuxGuitar-converter/src/org/herac/tuxguitar/gui/tools/custom/converter/TGConverter.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.gui.tools.custom.converter;
 
2
 
 
3
import java.io.File;
 
4
import java.io.FileInputStream;
 
5
import java.io.FileNotFoundException;
 
6
import java.io.FileOutputStream;
 
7
import java.util.Iterator;
 
8
 
 
9
import org.herac.tuxguitar.io.base.TGFileFormatException;
 
10
import org.herac.tuxguitar.io.base.TGFileFormatManager;
 
11
import org.herac.tuxguitar.io.base.TGSongExporter;
 
12
import org.herac.tuxguitar.io.base.TGSongImporter;
 
13
import org.herac.tuxguitar.song.factory.TGFactory;
 
14
import org.herac.tuxguitar.song.managers.TGSongManager;
 
15
import org.herac.tuxguitar.song.models.TGSong;
 
16
 
 
17
public class TGConverter {
 
18
        // This value will delay the process something like 1 minute for 3000 files.
 
19
        public static final int SLEEP_TIME = 20;
 
20
        
 
21
        public static final int FILE_OK = 250;
 
22
        public static final int FILE_BAD = 403;
 
23
        public static final int FILE_COULDNT_WRITE = 401;
 
24
        public static final int FILE_NOT_FOUND = 404;
 
25
        public static final int OUT_OF_MEMORY = 500;
 
26
        public static final int EXPORTER_NOT_FOUND = 590;
 
27
        public static final int UNKNOWN_ERROR = 666;
 
28
        
 
29
        private String sourceFolder;
 
30
        private String destinationFolder;
 
31
        private String extension = null;
 
32
        
 
33
        private TGConverterListener listener;
 
34
        private boolean cancelled;
 
35
        
 
36
        public TGConverter(String sourceFolder,String destinationFolder){
 
37
                this.sourceFolder = sourceFolder;
 
38
                this.destinationFolder = destinationFolder;
 
39
        }
 
40
        
 
41
        public void convert(String fileName, String convertFileName) {
 
42
                try {
 
43
                        this.getListener().notifyFileProcess(convertFileName);
 
44
                        
 
45
                        TGSongManager manager = new TGSongManager();
 
46
                        TGSong song=null;
 
47
                        try {
 
48
                                song = TGFileFormatManager.instance().getLoader().load(manager.getFactory(),new FileInputStream(fileName));
 
49
                        } catch (TGFileFormatException e) {
 
50
                                song = importSong(manager.getFactory(), fileName);
 
51
                        }
 
52
                        
 
53
                        if (song != null){
 
54
                                manager.setSong(song);
 
55
                                manager.autoCompleteSilences();
 
56
                                manager.orderBeats();
 
57
                                
 
58
                                new File(new File(convertFileName).getParent()).mkdirs();
 
59
                                
 
60
                                boolean exporter = false;
 
61
                                try {
 
62
                                        TGFileFormatManager.instance().getWriter().write(manager.getFactory(),manager.getSong(), convertFileName);
 
63
                                } catch (TGFileFormatException ex) {
 
64
                                        exporter = true;
 
65
                                }
 
66
                                // then it's exporter
 
67
                                if (exporter==true) {
 
68
                                        TGSongExporter songExporter = findExporter();
 
69
                                        if (songExporter==null)
 
70
                                                this.getListener().notifyFileResult(this.extension,EXPORTER_NOT_FOUND);
 
71
                                        else {
 
72
                                                songExporter.configure(true);
 
73
                                                try {
 
74
                                                        songExporter.exportSong(new FileOutputStream(convertFileName), manager.getSong());
 
75
                                                } catch (FileNotFoundException ex) {
 
76
                                                        this.getListener().notifyFileResult(convertFileName,FILE_COULDNT_WRITE);
 
77
                                                }
 
78
                                        }
 
79
                                }
 
80
                                this.getListener().notifyFileResult(convertFileName,FILE_OK);
 
81
                        }
 
82
                        else{
 
83
                                this.getListener().notifyFileResult(fileName,FILE_BAD);
 
84
                        }
 
85
                } catch (FileNotFoundException ex) {
 
86
                        this.getListener().notifyFileResult(fileName,FILE_NOT_FOUND);
 
87
                } catch (OutOfMemoryError e) {
 
88
                        this.getListener().notifyFileResult(convertFileName,OUT_OF_MEMORY);
 
89
                } catch (Throwable throwable) {
 
90
                        this.getListener().notifyFileResult(convertFileName,UNKNOWN_ERROR);
 
91
                }
 
92
        }
 
93
        
 
94
        private String checkIfExists(String convertFileName, int level) {
 
95
                if (new File(convertFileName).exists()) {
 
96
                        String tmpName = convertFileName;
 
97
                        String tmpExtension = "";
 
98
                        String tmpLevel = "(" + (level + 1) + ")";
 
99
                        String lastLevel = "(" + (level ) + ")";
 
100
                        
 
101
                        int index = convertFileName.lastIndexOf( (level == 0 ? "." : lastLevel + ".") );
 
102
                        if (index!=-1) {
 
103
                                tmpExtension = tmpName.substring(index + ( level == 0 ? 0 : lastLevel.length() ), tmpName.length());
 
104
                                tmpName = tmpName.substring(0, index);
 
105
                        }
 
106
                        return checkIfExists( (tmpName + tmpLevel + tmpExtension)  , (level + 1) );
 
107
                }
 
108
                return convertFileName;
 
109
        }
 
110
        
 
111
        public void process() {
 
112
                this.getListener().notifyStart();
 
113
                this.process(new File(this.sourceFolder));
 
114
                this.getListener().notifyFinish();
 
115
        }
 
116
        
 
117
        private void process(File folder) {
 
118
                if(!isCancelled()){
 
119
                        String[] fileNames = folder.list();
 
120
                        if(fileNames != null){
 
121
                                for (int i = 0; i < fileNames.length; i++) {
 
122
                                        File file = new File(folder.getPath() + "/" + fileNames[i]);
 
123
                                        if (file.isDirectory()) {
 
124
                                                process(file);
 
125
                                        } else if(!isCancelled()){
 
126
                                                String fileName = file.getAbsolutePath();
 
127
                                                String convertFileName = getConvertFileName(fileName);
 
128
                                                convert(fileName, convertFileName);
 
129
                                                
 
130
                                                // Just release the thread some milliseconds
 
131
                                                sleep();
 
132
                                        }
 
133
                                        fileNames[i] = null;
 
134
                                }
 
135
                        }
 
136
                }
 
137
        }
 
138
        
 
139
        private String getConvertFileName(String path) {
 
140
                String convertPath = (this.destinationFolder + File.separator +path.substring(this.sourceFolder.length()));
 
141
                int lastDot = convertPath.lastIndexOf(".");
 
142
                if (lastDot!=-1) {
 
143
                        convertPath = convertPath.substring(0, lastDot) + this.extension;
 
144
                }
 
145
                return checkIfExists( new File(convertPath).getAbsolutePath() , 0 );
 
146
        }
 
147
        
 
148
        private TGSongExporter findExporter() {
 
149
                // find the exporter
 
150
                Iterator exporters = TGFileFormatManager.instance().getExporters();
 
151
                String wantedExtension = "*"+this.extension;
 
152
                while (exporters.hasNext()) {
 
153
                        TGSongExporter current = (TGSongExporter)exporters.next();
 
154
                        if (current.getFileFormat().getSupportedFormats().startsWith(wantedExtension))
 
155
                                return current;
 
156
                }
 
157
                return null;
 
158
        }
 
159
        
 
160
        private TGSong importSong(TGFactory factory, String filename) {
 
161
                Iterator importers = TGFileFormatManager.instance().getImporters();
 
162
                while (importers.hasNext() ) {
 
163
                        TGSongImporter currentImporter = (TGSongImporter)importers.next();
 
164
                        try {
 
165
                                currentImporter.configure(true);
 
166
                                if (isSupportedExtension(filename,currentImporter)) {
 
167
                                        FileInputStream input = new FileInputStream(filename);
 
168
                                        return currentImporter.importSong(factory, input);
 
169
                                }
 
170
                        } catch (Throwable throwable) {
 
171
                                throwable.printStackTrace();
 
172
                        }
 
173
                }
 
174
                return null;
 
175
        }
 
176
        
 
177
        private boolean isSupportedExtension(String filename, TGSongImporter currentImporter) {
 
178
                try {
 
179
                        String extension = filename.substring(filename.lastIndexOf("."),filename.length());
 
180
                        extension="*"+extension.toLowerCase();
 
181
                        String[] formats = currentImporter.getFileFormat().getSupportedFormats().split(";");
 
182
                        for (int i=0; i<formats.length; i++)
 
183
                                if (formats[i].toLowerCase().equals(extension))
 
184
                                        return true;
 
185
                } catch (Exception ex) {
 
186
                        return false;
 
187
                }
 
188
                
 
189
                return false;
 
190
        }
 
191
        
 
192
        private void sleep(){
 
193
                try {
 
194
                        Thread.sleep( SLEEP_TIME );
 
195
                } catch (Throwable throwable) {
 
196
                        throwable.printStackTrace();
 
197
                }
 
198
        }
 
199
        
 
200
        public String getExtension() {
 
201
                return this.extension;
 
202
        }
 
203
        
 
204
        public void setExtension(String extension) {
 
205
                this.extension = extension;
 
206
        }
 
207
        
 
208
        public TGConverterListener getListener() {
 
209
                return this.listener;
 
210
        }
 
211
        
 
212
        public void setListener(TGConverterListener listener) {
 
213
                this.listener = listener;
 
214
        }
 
215
        
 
216
        public boolean isCancelled() {
 
217
                return this.cancelled;
 
218
        }
 
219
        
 
220
        public void setCancelled(boolean cancelled) {
 
221
                this.cancelled = cancelled;
 
222
        }
 
223
}