1
package org.herac.tuxguitar.gui.tools.custom.converter;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
6
import java.io.FileOutputStream;
7
import java.util.Iterator;
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;
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;
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;
29
private String sourceFolder;
30
private String destinationFolder;
31
private String extension = null;
33
private TGConverterListener listener;
34
private boolean cancelled;
36
public TGConverter(String sourceFolder,String destinationFolder){
37
this.sourceFolder = sourceFolder;
38
this.destinationFolder = destinationFolder;
41
public void convert(String fileName, String convertFileName) {
43
this.getListener().notifyFileProcess(convertFileName);
45
TGSongManager manager = new TGSongManager();
48
song = TGFileFormatManager.instance().getLoader().load(manager.getFactory(),new FileInputStream(fileName));
49
} catch (TGFileFormatException e) {
50
song = importSong(manager.getFactory(), fileName);
54
manager.setSong(song);
55
manager.autoCompleteSilences();
58
new File(new File(convertFileName).getParent()).mkdirs();
60
boolean exporter = false;
62
TGFileFormatManager.instance().getWriter().write(manager.getFactory(),manager.getSong(), convertFileName);
63
} catch (TGFileFormatException ex) {
68
TGSongExporter songExporter = findExporter();
69
if (songExporter==null)
70
this.getListener().notifyFileResult(this.extension,EXPORTER_NOT_FOUND);
72
songExporter.configure(true);
74
songExporter.exportSong(new FileOutputStream(convertFileName), manager.getSong());
75
} catch (FileNotFoundException ex) {
76
this.getListener().notifyFileResult(convertFileName,FILE_COULDNT_WRITE);
80
this.getListener().notifyFileResult(convertFileName,FILE_OK);
83
this.getListener().notifyFileResult(fileName,FILE_BAD);
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);
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 ) + ")";
101
int index = convertFileName.lastIndexOf( (level == 0 ? "." : lastLevel + ".") );
103
tmpExtension = tmpName.substring(index + ( level == 0 ? 0 : lastLevel.length() ), tmpName.length());
104
tmpName = tmpName.substring(0, index);
106
return checkIfExists( (tmpName + tmpLevel + tmpExtension) , (level + 1) );
108
return convertFileName;
111
public void process() {
112
this.getListener().notifyStart();
113
this.process(new File(this.sourceFolder));
114
this.getListener().notifyFinish();
117
private void process(File folder) {
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()) {
125
} else if(!isCancelled()){
126
String fileName = file.getAbsolutePath();
127
String convertFileName = getConvertFileName(fileName);
128
convert(fileName, convertFileName);
130
// Just release the thread some milliseconds
139
private String getConvertFileName(String path) {
140
String convertPath = (this.destinationFolder + File.separator +path.substring(this.sourceFolder.length()));
141
int lastDot = convertPath.lastIndexOf(".");
143
convertPath = convertPath.substring(0, lastDot) + this.extension;
145
return checkIfExists( new File(convertPath).getAbsolutePath() , 0 );
148
private TGSongExporter findExporter() {
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))
160
private TGSong importSong(TGFactory factory, String filename) {
161
Iterator importers = TGFileFormatManager.instance().getImporters();
162
while (importers.hasNext() ) {
163
TGSongImporter currentImporter = (TGSongImporter)importers.next();
165
currentImporter.configure(true);
166
if (isSupportedExtension(filename,currentImporter)) {
167
FileInputStream input = new FileInputStream(filename);
168
return currentImporter.importSong(factory, input);
170
} catch (Throwable throwable) {
171
throwable.printStackTrace();
177
private boolean isSupportedExtension(String filename, TGSongImporter currentImporter) {
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))
185
} catch (Exception ex) {
192
private void sleep(){
194
Thread.sleep( SLEEP_TIME );
195
} catch (Throwable throwable) {
196
throwable.printStackTrace();
200
public String getExtension() {
201
return this.extension;
204
public void setExtension(String extension) {
205
this.extension = extension;
208
public TGConverterListener getListener() {
209
return this.listener;
212
public void setListener(TGConverterListener listener) {
213
this.listener = listener;
216
public boolean isCancelled() {
217
return this.cancelled;
220
public void setCancelled(boolean cancelled) {
221
this.cancelled = cancelled;