import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class Translator {
private String Inputfile;
private String Outputfile;
private StreamResult out;
private TransformerHandler th;
private AttributesImpl atts;
private List<FunkzellenObjekt> listeDerFZO = new LinkedList<FunkzellenObjekt>();
public Translator(){
}
public void setInputFilename(String s){
Inputfile = s;
}
public String getInputFilename(){
return Inputfile;
}
public void setOutputFilename(String s){
Outputfile = s;
}
public String getOutputFilename(){
return Outputfile;
}
public List<String> einlesen() {
// Einlesen des Files und spliten
FileReader myFile = null;
BufferedReader buff = null;
final List<String> lines = new ArrayList<String>();
try {
myFile = new FileReader("test_BTS_Betriebsort.csv");
buff = new BufferedReader(myFile);
String line;
while ((line = buff.readLine()) != null) {
// System.out.println(line); // kontrolle was eingelesen
line = line.replaceAll(",", ".");
String[] splitted = line.split(";");
FunkzellenObjekt fzo1 = new FunkzellenObjekt(splitted[0], splitted[1],
Integer.valueOf(splitted[2]), Integer.valueOf(splitted[3]),
Double.valueOf(splitted[4]), Double.valueOf(splitted[5]),
Double.valueOf(splitted[6]));
boolean nichtDoppelt = false;
for (FunkzellenObjekt funkzellenObjekt : listeDerFZO) {
if (funkzellenObjekt.equals(fzo1)) {
nichtDoppelt = true;
/*
* Bestehende Liste wird abgerufen, neue Liste in diese eingefuegt
* und diese wieder gespeichert.
*/
/*
* Cell-ID in List speichern
*/
List<Integer> tmpI = funkzellenObjekt.getCellID();
tmpI.addAll(fzo1.getCellID());
funkzellenObjekt.setCellID(tmpI);
/*
* CI in List speichern
*/
List<Double> tmpD = funkzellenObjekt.getCI();
tmpD.addAll(fzo1.getCI());
funkzellenObjekt.setCI(tmpD);
break;
}
}
if (!nichtDoppelt) {
listeDerFZO.add(fzo1);
}
}
} catch (IOException e) {
System.err.println("Error2 :" + e);
} finally {
try {
buff.close();
myFile.close();
} catch (IOException e) {
System.err.println("Error2 :" + e);
}
// System.out.println(listeDerFZO);
}
return lines;
}
//Umwandeln der Liste in GPX(XML) Code
public void translate() {
try {
out = new StreamResult("data.xml");
initXML();
process();
closeXML();
} catch (TransformerConfigurationException ex) {
Logger.getLogger(Translator.class.getName()).log(Level.SEVERE, null, ex);
} catch (SAXException ex) {
Logger.getLogger(Translator.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void initXML() throws TransformerConfigurationException, SAXException {
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
th = tf.newTransformerHandler();
Transformer serializer = th.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
th.setResult(out);
th.startDocument();
atts = new AttributesImpl();
th.startElement("", "", "gpx", atts);
}
private void process() throws SAXException {
FunkzellenObjekt[] elements = new FunkzellenObjekt[listeDerFZO.size()];
listeDerFZO.toArray(elements);
for (int i=0; i<listeDerFZO.size(); i++){
atts.clear();
atts.addAttribute("", "", "lat", "", "" + elements[i].getKoordinate_b());
atts.addAttribute("", "", "lon", "", "" + elements[i].getKoordinate_l());
th.startElement("", "", "wpt", atts);
atts.clear();
th.startElement("", "", "name", atts);
String name = "Node-ID: " + elements[i].getCellID();
th.characters(name.toCharArray(), 0, name.length());
th.endElement("", "", "name");
th.startElement("", "", "cmt", atts);
// th.characters(elements[i].toCharArray(), 0, elements[i].length());
th.endElement("", "", "cmt");
th.startElement("", "", "desc", atts);
String desc = "Standortname: " + elements[i].getName() + "\r\n"+ "Netztyp: " + elements[i].getNetztyp() + "\r\n" + "Cell ID: " + elements[i].getCellID(); //+ "Releasecause: " + elements[5] + " Anzahl: " + elements[6];
th.characters(desc.toCharArray(), 0, desc.length());
th.endElement("", "", "desc");
th.endElement("", "", "wpt");
}
}
private void closeXML() throws SAXException {
th.endElement("", "", "gpx");
th.endDocument();
}
public boolean start() {
try{
translate();
}catch (ArrayIndexOutOfBoundsException ex){
return false;
}
return true;
}
public static void main(String[] args){
Translator t = new Translator();
t.start();
}
}
class FunkzellenObjekt {
private String Netztyp;
private String Name;
private int nummer;
private List<Integer> CellID = new LinkedList<Integer>();
private List<Double> CI = new LinkedList<Double>();;
private double koordinate_l;
private double koordinate_b;
public FunkzellenObjekt(String netztyp, String name, int nummer,
int cellID, double cI, double koordinate_l, double koordinate_b) {
super();
Netztyp = netztyp;
Name = name;
this.nummer = nummer;
CellID.add(cellID);
CI.add(cI);
this.koordinate_l = koordinate_l;
this.koordinate_b = koordinate_b;
}
public String getNetztyp() {
return Netztyp;
}
public void setNetztyp(String netztyp) {
Netztyp = netztyp;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getNummer() {
return nummer;
}
public void setNummer(int nummer) {
this.nummer = nummer;
}
public List<Integer> getCellID() {
return CellID;
}
public void setCellID(List<Integer> cellID) {
CellID = cellID;
}
public List<Double> getCI() {
return CI;
}
public void setCI(List<Double> cI) {
CI = cI;
}
public double getKoordinate_l() {
return koordinate_l;
}
public void setKoordinate_l(double koordinate_l) {
this.koordinate_l = koordinate_l;
}
public double getKoordinate_b() {
return koordinate_b;
}
public void setKoordinate_b(double koordinate_b) {
this.koordinate_b = koordinate_b;
}
@Override
public boolean equals(Object o) {
FunkzellenObjekt fzo = (FunkzellenObjekt) o;
/* System.out.println(this.koordinate_l + " / " + fzo.getKoordinate_l());
System.out.println(this.koordinate_b == fzo.getKoordinate_b());
System.out.println(this.koordinate_l == fzo.getKoordinate_l());*/
if (this.koordinate_l == fzo.getKoordinate_l()
&& this.koordinate_b == fzo.koordinate_b) {
return true;
}
return false;
}
}