package org.ellectra.language;

import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class languagexml {

	
	public String ReadLanFile(String szName, String szLang, String Filename,
			String Filepath) {
		String Error = "No Error";
		int s;
		String name;
		String label = "";
		String type;
		int s2;
		String similarTo = "";
		try {

			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(new File(Filepath + Filename + "_"
					+ szLang + ".xml"));

			// normalize text representation
			doc.getDocumentElement().normalize();

			NodeList listOfitems = doc.getElementsByTagName("item");

			for (s = 0; s < listOfitems.getLength(); s++) {

				Node firstItemNode = listOfitems.item(s);
				if (firstItemNode.getNodeType() == Node.ELEMENT_NODE) {

					Element firstItemElement = (Element) firstItemNode;

					// -------
					NodeList NameList = firstItemElement
							.getElementsByTagName("name");
					Element NameElement = (Element) NameList.item(0);

					NodeList textNList = NameElement.getChildNodes();
					name = ((Node) textNList.item(0)).getNodeValue().trim();
					if (szName.equalsIgnoreCase(name)) {
						// <>

						NodeList typeList = firstItemElement
								.getElementsByTagName("type");
						Element typeElement = (Element) typeList.item(0);

						NodeList texttypeList = typeElement.getChildNodes();
						type = ((Node) texttypeList.item(0)).getNodeValue()
								.trim();

						if (type.equalsIgnoreCase("String")) {
							NodeList labelList = firstItemElement
									.getElementsByTagName("label");
							Element labelElement = (Element) labelList.item(0);
							NodeList textlabelList = labelElement
									.getChildNodes();
							label = ((Node) textlabelList.item(0))
									.getNodeValue().trim();
							return label;
						}

						if (type.equalsIgnoreCase("Similar")) {
							NodeList similarToList = firstItemElement
									.getElementsByTagName("similarTo");
							Element similarToElement = (Element) similarToList
									.item(0);

							NodeList textsimilarToList = similarToElement
									.getChildNodes();
							similarTo = ((Node) textsimilarToList.item(0))
									.getNodeValue().trim();

							return ReadLanFile(similarTo, szLang, Filename,
									Filepath);
							// "Similar:"+similarTo;

						}

						// read list items
						if (type.equalsIgnoreCase("list")) {
							NodeList ENList1 = firstItemElement
									.getElementsByTagName("label");
							for (s2 = 0; s2 < ENList1.getLength(); s2++) {
								Element ENElement = (Element) ENList1.item(s2);
								NodeList textENList = ENElement.getChildNodes();
								String name222 = ((Node) textENList.item(0))
										.getNodeValue().trim();
								label = label + name222 + "|";
							}
							label = label.substring(0, label.length() - 1);
							return label;

						}
					}// end of if clause

				}// end of if clause

			}

		} catch (SAXParseException err) {
			Error = "** Parsing error" + ", line " + err.getLineNumber()
					+ ", uri " + err.getSystemId() + err.getMessage();

		} catch (SAXException e) {
			Error = "Error 2 " + e.getMessage();
			Exception x = e.getException();
			((x == null) ? e : x).printStackTrace();

		} catch (Throwable t) {
			Error = "File Missing for language " + szLang;
		}
		return Error;
	}
}
