All pastes #180161 Raw Edit

Anonymous

public text v1 · immutable
#180161 ·published 2006-09-22 22:22 UTC
rendered paste body
//Version 0.2.4
//changelog: arreglado problema con algunas versiones de archivos que contenían un namespace específico
//it does not even writes the song's name that seems to be stores in a comments just after the MELODY tag on the XML
//but it does all the dirty job except setting the correct GAP time, that needs to be manually entered

import org.jdom.*;
import org.jdom.input.SAXBuilder;
import org.jdom.output.*;
import java.io.*;
import java.util.*;
import java.io.IOException;
			 
public class ss2us{
	
	private String url;
	Document xmldoc;
	Element root;
	public StringBuffer txtdoc;
	private int conversionTime;
	private final boolean withDashes = true; //to maintain the dash-like SS lyrics
	private boolean conversionStarted;	//the first note is our GAP tag
	

	static public void main(String[] argv){
		try{
			if(argv.length != 2){
				System.out.println("Usage:\n\t%1: source file\n\t%2: target file");
				System.exit(1);
			}
			System.out.println("Starting conversion of "+argv[0]);
			ss2us song = new ss2us(argv[0]);
			song.convert();
			DataOutputStream dos = new DataOutputStream(new FileOutputStream(argv[1]));
			dos.writeBytes(song.txtdoc.toString());	
		}catch(IOException e){e.printStackTrace();}
		catch(Exception e){e.printStackTrace();}
	}
	
	public ss2us(String file) throws Exception{
		SAXBuilder builder = new SAXBuilder();
		FileInputStream fis;
		try{
			fis = new FileInputStream(file);
			this.xmldoc = builder.build(fis); //a ver si funciona esta sintaxis
			root = xmldoc.getRootElement();
			conversionTime = 0;
			conversionStarted = false;
		}catch(JDOMException e){e.printStackTrace();throw new Exception("Error en JDOM");}
		catch(IOException e){e.printStackTrace();throw new Exception("Error en IO");}

	}
		
		
	public void convert(){
		String artist;
		Float tempo;
		Element el;
		if (root == null) return;		
		Element melody = root;
		conversionTime = 0;		
		//TEMPO
		//bpm has to be doubled
		tempo = new Float(Float.parseFloat(melody.getAttributeValue("Tempo")));
		if (melody.getAttributeValue("Resolution").equals("Demisemiquaver")) tempo=new Float(tempo.floatValue()*2);
		//ARTIST
		//some xml files does not have any TRACK tag
		if (melody.getChild("TRACK")!=null)
			artist = melody.getChild("TRACK").getAttributeValue("Artist");
		else
			artist = new String("Unknown");
		//GAP
		System.out.println(melody);
		System.out.println(melody.getChild("SENTENCE", melody.getNamespace()));
		int gap = Integer.parseInt(melody.getChild("SENTENCE", melody.getNamespace()).getChild("NOTE", melody.getNamespace()).getAttributeValue("Duration"));
		gap *= 60000;
		gap /= (tempo.intValue());
		
		//HEADER
		txtdoc = new StringBuffer();		
		txtdoc.append("#TITLE:\n");
		txtdoc.append("#ARTIST:"+artist+"\n");
		txtdoc.append("#MP3:\n");
		txtdoc.append("#BPM:"+tempo.toString().replace('.',',')+"\n");
		txtdoc.append("#GAP:"+gap);
		//We iterate through the SENTENCE tags
		Iterator sentIt = melody.getChildren("SENTENCE", melody.getNamespace()).iterator();
		while(sentIt.hasNext())
			txtdoc.append(sentenceConvert((Element) sentIt.next()));
		//End
		txtdoc.append("\nE");
		//Credits
		txtdoc.append("\nConverted using SS2US by UltraStar Spanish User Grup (http://ultrastar.webcindario.com");
	}
	
	
	private String sentenceConvert(Element sentenceSS){
		boolean oneNoteAtLeast = false;
		StringBuffer sentence = new StringBuffer();
		Element el;
		String lyric;
		if (!sentenceSS.getName().equals("SENTENCE")) return null;
		Iterator it = sentenceSS.getChildren("NOTE", root.getNamespace()).iterator();
		while(it.hasNext()){
			oneNoteAtLeast = true;
			el = (Element) it.next();
			if (el.getAttributeValue("MidiNote").equals("0")){
				if(conversionStarted)
					this.conversionTime += Integer.parseInt(el.getAttributeValue("Duration"));
				else
					conversionStarted = true;
			}
			else{
				conversionStarted = true;
				/*if (withDashes)
					lyric = el.getAttributeValue("Lyric");
				else
					lyric = el.getAttributeValue("Lyric").replaceAll("-", "");*/
				lyric = el.getAttributeValue("Lyric");
				lyric = new String(lyric+" ");
				String bonus = new String("");
				if (el.getAttribute("bonus")!=null)
					if(el.getAttributeValue("Bonus").equals("Yes"))
						bonus = "*";
				if (lyric.indexOf(" - ") > 0)
					lyric = lyric.substring(0, lyric.indexOf(" - "));
				
				sentence.append("\n"+bonus+": " + conversionTime + " " + el.getAttributeValue("Duration") +
							" " +el.getAttributeValue("MidiNote")+ " "+lyric);
				this.conversionTime += Integer.parseInt(el.getAttributeValue("Duration"));
			}
		}
		if (oneNoteAtLeast) sentence.append("\n-"+this.conversionTime);
		return sentence.toString();
	}
}