/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package SongXMLHolder;
import Exceptions.BadSpecsException;
import Exceptions.MissingTitleTagException;
import XMLTags.AbstractBaseClass.ABCSongTag;
import XMLTags.AbstractBaseClass.ABCSongTagContainer;
import XMLTags.AbstractBaseClass.ABCSongTagSimple;
import XMLTags.BodyHeaderTag;
import XMLTags.HouseTag;
import XMLTags.LineTag;
import XMLTags.MetaHeaderTag;
import XMLTags.SongTagSpecs.SongTagSpecs;
import XMLTags.SongXMLHeaderTag;
import XMLTags.TitleTag;
import XMLTags.WritterTag;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
*
* @author Haim
*/
public class SongXMLHolder {
private SongXMLHeaderTag root;
public SongXMLHolder(String path, List<ABCSongTagSimple> metaData) throws FileNotFoundException, BadSpecsException, MissingTitleTagException, IOException {
File inputFile = new File(path);
// File outputFile = new File(inputFile.getAbsolutePath() + "output.txt");
if (!inputFile.exists()) {
throw new FileNotFoundException(inputFile.getName() + " Does not exists!");
}
boolean foundTitleMetaTag = false;
root = new SongXMLHeaderTag();
MetaHeaderTag meta = new MetaHeaderTag();//((MetaHeaderTag) root.getContent().get(0));
BodyHeaderTag body = new BodyHeaderTag();//((BodyHeaderTag) root.getContent().get(1));
root.add(meta);
root.add(body);
HouseTag currentHouse = new HouseTag(1);
body.add(currentHouse);
for (ABCSongTagSimple tag : metaData) {
if (tag instanceof TitleTag) {
foundTitleMetaTag = true;
}
meta.add(tag);
}
if (!foundTitleMetaTag) {
throw new MissingTitleTagException("Initialization list for songXMLHolder must contain a TitleTag!");
}
LineNumberReader lr = new LineNumberReader(new FileReader(inputFile));
// scan = new Scanner(file);
while (lr.ready()) {
String currentLine = lr.readLine();//scan.nextLine();
if (currentLine.compareToIgnoreCase("") == 0) {
do {
currentHouse.add(new LineTag(currentLine,lr.getLineNumber()));
currentLine = lr.readLine(); //scan.nextLine();
}while(currentLine.compareTo("") == 0);
// currentHouse.setSpecValue(SongTagSpecs.Keys.OFFSET, lr.getLineNumber() - Integer.parseInt(currentHouse.getSpecValue(SongTagSpecs.Keys.LINE))); // setting lower boundery of house
currentHouse = new HouseTag(lr.getLineNumber()); // makeing new house to continue reading lines
body.add(currentHouse);
}
currentHouse.add(new LineTag(currentLine, lr.getLineNumber()));
}
}
public void printXML(String path) throws IOException{
File output = new File(path);
FileWriter fw = new FileWriter(output);
String result = printXML(root);
fw.write(result);
fw.close();
}
private String printXML(ABCSongTag head){
return printXMLRecursive(head, 0);
}
/**
* This method recursively creates a String representation of the SongXML structure nested in head
* @param head the root of the XML structure tree
* @param depth
* @return
*/
private String printXMLRecursive(ABCSongTag head,int depth){
StringBuilder result = new StringBuilder();
StringBuilder spacer = new StringBuilder(); // padding with depth length of space befor line print
for(int i=0; i < depth ; i++)
spacer.append(" ");
if(head.isContainer()){
ABCSongTagContainer tempContainer = (ABCSongTagContainer)(head);
result.append(spacer + head.toString() + System.lineSeparator());
Iterator iterator = ((ABCSongTagContainer)(head)).iterator();
while(iterator.hasNext()){
result.append(printXMLRecursive(((ABCSongTag)iterator.next()), depth+1) + System.lineSeparator());
}
result.append(spacer + head.printCloser());
}else{
result.append(spacer + head.printOpner() + ((ABCSongTagSimple)head).getContent().toString() + head.printCloser());
}
return result.toString();
}
public static SongXMLHolder load(String path) throws FileNotFoundException{
File input = new File(path);
if(!input.exists()) throw new FileNotFoundException("Unable to find input file in path: " + path);
return null;
// TODO: first need to check for valid input by parser
}
public static void main(String args[]) throws BadSpecsException, MissingTitleTagException, IOException{
List<ABCSongTagSimple> meta = new ArrayList<ABCSongTagSimple>();
meta.add(new TitleTag("Smells Like Teen Spirit"));
meta.add(new WritterTag("Nirvana"));
SongXMLHolder xml = new SongXMLHolder("c:\\sample.txt", meta);
xml.printXML("c:\\output.txt");
}
}