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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package org.herac.tuxguitar.gui.tools.custom.converter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Iterator;

import org.herac.tuxguitar.io.base.TGFileFormatException;
import org.herac.tuxguitar.io.base.TGFileFormatManager;
import org.herac.tuxguitar.io.base.TGSongExporter;
import org.herac.tuxguitar.io.base.TGSongImporter;
import org.herac.tuxguitar.song.factory.TGFactory;
import org.herac.tuxguitar.song.managers.TGSongManager;
import org.herac.tuxguitar.song.models.TGSong;

public class TGConverter {
	// This value will delay the process something like 1 minute for 3000 files.
	public static final int SLEEP_TIME = 20;
	
	public static final int FILE_OK = 250;
	public static final int FILE_BAD = 403;
	public static final int FILE_COULDNT_WRITE = 401;
	public static final int FILE_NOT_FOUND = 404;
	public static final int OUT_OF_MEMORY = 500;
	public static final int EXPORTER_NOT_FOUND = 590;
	public static final int UNKNOWN_ERROR = 666;
	
	private String sourceFolder;
	private String destinationFolder;
	private String extension = null;
	
	private TGConverterListener listener;
	private boolean cancelled;
	
	public TGConverter(String sourceFolder,String destinationFolder){
		this.sourceFolder = sourceFolder;
		this.destinationFolder = destinationFolder;
	}
	
	public void convert(String fileName, String convertFileName) {
		try {
			this.getListener().notifyFileProcess(convertFileName);
			
			TGSongManager manager = new TGSongManager();
			TGSong song=null;
			try {
				song = TGFileFormatManager.instance().getLoader().load(manager.getFactory(),new FileInputStream(fileName));
			} catch (TGFileFormatException e) {
				song = importSong(manager.getFactory(), fileName);
			}
			
			if (song != null){
				manager.setSong(song);
				manager.autoCompleteSilences();
				manager.orderBeats();
				
				new File(new File(convertFileName).getParent()).mkdirs();
				
				boolean exporter = false;
				try {
					TGFileFormatManager.instance().getWriter().write(manager.getFactory(),manager.getSong(), convertFileName);
				} catch (TGFileFormatException ex) {
					exporter = true;
				}
				// then it's exporter
				if (exporter==true) {
					TGSongExporter songExporter = findExporter();
					if (songExporter==null)
						this.getListener().notifyFileResult(this.extension,EXPORTER_NOT_FOUND);
					else {
						songExporter.configure(true);
						try {
							songExporter.exportSong(new FileOutputStream(convertFileName), manager.getSong());
						} catch (FileNotFoundException ex) {
							this.getListener().notifyFileResult(convertFileName,FILE_COULDNT_WRITE);
						}
					}
				}
				this.getListener().notifyFileResult(convertFileName,FILE_OK);
			}
			else{
				this.getListener().notifyFileResult(fileName,FILE_BAD);
			}
		} catch (FileNotFoundException ex) {
			this.getListener().notifyFileResult(fileName,FILE_NOT_FOUND);
		} catch (OutOfMemoryError e) {
			this.getListener().notifyFileResult(convertFileName,OUT_OF_MEMORY);
		} catch (Throwable throwable) {
			this.getListener().notifyFileResult(convertFileName,UNKNOWN_ERROR);
		}
	}
	
	private String checkIfExists(String convertFileName, int level) {
		if (new File(convertFileName).exists()) {
			String tmpName = convertFileName;
			String tmpExtension = "";
			String tmpLevel = "(" + (level + 1) + ")";
			String lastLevel = "(" + (level ) + ")";
			
			int index = convertFileName.lastIndexOf( (level == 0 ? "." : lastLevel + ".") );
			if (index!=-1) {
				tmpExtension = tmpName.substring(index + ( level == 0 ? 0 : lastLevel.length() ), tmpName.length());
				tmpName = tmpName.substring(0, index);
			}
			return checkIfExists( (tmpName + tmpLevel + tmpExtension)  , (level + 1) );
		}
		return convertFileName;
	}
	
	public void process() {
		this.getListener().notifyStart();
		this.process(new File(this.sourceFolder));
		this.getListener().notifyFinish();
	}
	
	private void process(File folder) {
		if(!isCancelled()){
			String[] fileNames = folder.list();
			if(fileNames != null){
				for (int i = 0; i < fileNames.length; i++) {
					File file = new File(folder.getPath() + "/" + fileNames[i]);
					if (file.isDirectory()) {
						process(file);
					} else if(!isCancelled()){
						String fileName = file.getAbsolutePath();
						String convertFileName = getConvertFileName(fileName);
						convert(fileName, convertFileName);
						
						// Just release the thread some milliseconds
						sleep();
					}
					fileNames[i] = null;
				}
			}
		}
	}
	
	private String getConvertFileName(String path) {
		String convertPath = (this.destinationFolder + File.separator +path.substring(this.sourceFolder.length()));
		int lastDot = convertPath.lastIndexOf(".");
		if (lastDot!=-1) {
			convertPath = convertPath.substring(0, lastDot) + this.extension;
		}
		return checkIfExists( new File(convertPath).getAbsolutePath() , 0 );
	}
	
	private TGSongExporter findExporter() {
		// find the exporter
		Iterator exporters = TGFileFormatManager.instance().getExporters();
		String wantedExtension = "*"+this.extension;
		while (exporters.hasNext()) {
			TGSongExporter current = (TGSongExporter)exporters.next();
			if (current.getFileFormat().getSupportedFormats().startsWith(wantedExtension))
				return current;
		}
		return null;
	}
	
	private TGSong importSong(TGFactory factory, String filename) {
		Iterator importers = TGFileFormatManager.instance().getImporters();
		while (importers.hasNext() ) {
			TGSongImporter currentImporter = (TGSongImporter)importers.next();
			try {
				currentImporter.configure(true);
				if (isSupportedExtension(filename,currentImporter)) {
					FileInputStream input = new FileInputStream(filename);
					return currentImporter.importSong(factory, input);
				}
			} catch (Throwable throwable) {
				throwable.printStackTrace();
			}
		}
		return null;
	}
	
	private boolean isSupportedExtension(String filename, TGSongImporter currentImporter) {
		try {
			String extension = filename.substring(filename.lastIndexOf("."),filename.length());
			extension="*"+extension.toLowerCase();
			String[] formats = currentImporter.getFileFormat().getSupportedFormats().split(";");
			for (int i=0; i<formats.length; i++)
				if (formats[i].toLowerCase().equals(extension))
					return true;
		} catch (Exception ex) {
			return false;
		}
		
		return false;
	}
	
	private void sleep(){
		try {
			Thread.sleep( SLEEP_TIME );
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		}
	}
	
	public String getExtension() {
		return this.extension;
	}
	
	public void setExtension(String extension) {
		this.extension = extension;
	}
	
	public TGConverterListener getListener() {
		return this.listener;
	}
	
	public void setListener(TGConverterListener listener) {
		this.listener = listener;
	}
	
	public boolean isCancelled() {
		return this.cancelled;
	}
	
	public void setCancelled(boolean cancelled) {
		this.cancelled = cancelled;
	}
}