package de.FeatureModellingTool.DictEditor.store;
import java.util.*;
import java.io.*;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.ProcessingInstruction;
import org.jdom.input.DOMBuilder;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import de.FeatureModellingTool.DictEditor.TerminologyModel.Terminology;
import de.FeatureModellingTool.DictEditor.TerminologyModel.TerminologyModel;
import de.FeatureModellingTool.DictEditor.TerminologyModel.TerminologyImpl;
/**
* Recoder: Raymond
* Date: 2003-10-8
* Time: 2:10:58
*/
public class TmdlParserImpl implements TmdlParser
{
public TmdlParserImpl()
{
}
public void newTmdl(File file, TerminologyModel terminologyModel)
throws IOException, JDOMException
{
//set TMDL namespace
Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Namespace tmd = Namespace.getNamespace("fmd", "http://sei.pku.edu.cn/DomainEngineering/terminologymodel");
//creat a new TMDL document including a root Element named terminologyModel
Document doc = new Document(new Element("terminologyModel", tmd));
//String fileName=file.getName();
//String featureModelName=fileName.substring(0,fileName.indexOf('.'));
//get the root Element��and attribute schemaLocation and name��
Element root = doc.getRootElement();
if (terminologyModel.getName() == null)
root.setAttribute("schemaLocation", "http://sei.pku.edu.cn/DomainEngineering/terminologymodel terminologymodel.xsd", xsi)
.setAttribute("name", "Untitled")
.setAttribute("description", "no description");
else
root.setAttribute("schemaLocation", "http://sei.pku.edu.cn/DomainEngineering/terminologymodel terminologymodel.xsd", xsi)
.setAttribute("name", terminologyModel.getName())
.setAttribute("description", _encode(terminologyModel.getDescription()));
//add terminologyList Element
Element terminologyList = new Element("terminologyList", tmd);
root.addContent(terminologyList);
//Output the TMDL document as XML type
XMLOutputter tmt = new XMLOutputter(" ", true, "GB2312");
tmt.output(doc, System.out);
}
public void saveTmdl(File file, TerminologyModel terminologyModel)
throws IOException, JDOMException
{
//set TMDL namespace
Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Namespace tmd = Namespace.getNamespace("tmd", "http://sei.pku.edu.cn/DomainEngineering/terminologymodel");
//creat a new TMDL document including a root Element named terminologyModel
Document doc = new Document(new Element("terminologyModel", tmd));
//get the root Element��and attribute schemaLocation and name��
Element root = doc.getRootElement();
if (terminologyModel.getName() == null || terminologyModel.getDescription() == null)
root.setAttribute("schemaLocation", "http://sei.pku.edu.cn/DomainEngineering/terminologymodel terminologymodel.xsd", xsi)
.setAttribute("name", "Untitled")
.setAttribute("description", "no description");
else
root.setAttribute("schemaLocation", "http://sei.pku.edu.cn/DomainEngineering/terminologymodel terminologymodel.xsd", xsi)
.setAttribute("name", terminologyModel.getName())
.setAttribute("description", _encode(terminologyModel.getDescription()));
//add terminologyList Element
Element terminologyList = new Element("terminologyList", tmd);
root.addContent(terminologyList);
//get all terminologys form terminologyModel
Map allTerminology = terminologyModel.getAllTerminology();
//get every attribute according terminorlogy��creat terminology Element and add all attributes
if (allTerminology != null)
{
Iterator terminologyIterator = allTerminology.keySet().iterator();
Terminology terminology;
while (terminologyIterator.hasNext())
{
terminology = (Terminology) allTerminology.get(terminologyIterator.next());
String id = terminology.getID();
String name = terminology.getName();
String description = terminology.getDescription();
String source = terminology.getSource();
String synonym = terminology.getSynonym();
description = _encode(description);
source = _encode(source);
synonym = _encode(synonym);
Element terminologyElement = new Element("terminology", tmd);
terminologyList.addContent(terminologyElement);
terminologyElement.setAttribute("id", id)
.addContent(new Element("name").addContent(name))
.addContent(new Element("description").addContent(description))
.addContent(new Element("source").addContent(source))
.addContent(new Element("synonym").addContent(synonym)); //addContent(new Element("synonym"))
}
}
//terminologyElement.getChild("synonym")
// .addContent(new Element("terminologyID")
// .setAttribute("idRef",synonymID));
//Output the TMDL document as XML type
XMLOutputter fmt = new XMLOutputter(" ", true, "GB2312");
fmt.output(doc, System.out);
FileOutputStream fileout = new FileOutputStream(file);
fmt.output(doc, fileout);
fileout.flush();
fileout.close();
}
public void openTmdl(File file, TerminologyModel terminologyModel)
throws IOException, JDOMException
{
Namespace tmd = Namespace.getNamespace("tmd", "http://sei.pku.edu.cn/DomainEngineering/terminologymodel");
SAXBuilder saxBuilder = new SAXBuilder(false);
Document doc = saxBuilder.build(file);
//ger attribute of terminologyModel
Element root = doc.getRootElement();
terminologyModel.setName(root.getAttributeValue("name"));
terminologyModel.setDescription(_decode(root.getAttributeValue("description")));
Element terminologyList = root.getChild("terminologyList", tmd);
Iterator terminologyIterator = terminologyList.getChildren("terminology", tmd).iterator();
// add every terminology into model
while (terminologyIterator.hasNext())
{
Terminology terminology = new TerminologyImpl();
Element terminologyElement = (Element) terminologyIterator.next();
terminology.setID(terminologyElement.getAttributeValue("id"));
terminology.setName(terminologyElement.getChild("name").getTextNormalize());
terminology.setDescription(_decode(terminologyElement.getChild("description").getTextNormalize()));
terminology.setSource(_decode(terminologyElement.getChild("source").getTextNormalize()));
terminology.setSynonym(_decode(terminologyElement.getChild("synonym").getTextNormalize()));
terminologyModel.addTerminology(terminology);
}
}
//encode:��String�еĿո�,tab,���лس��滻��
protected static String _encode(String in)
{
String out = in;
out = out.replaceAll("\u0020", "#x20");
out = out.replaceAll("\u0009", "#x09");
out = out.replaceAll("\n", "#x0a");
out = out.replaceAll("\r", "#x0d");
return out;
}
// decode:��ԭXML����
protected static String _decode(String in)
{
String out = in;
out = out.replaceAll("#x20", "\u0020");
out = out.replaceAll("#x09", "\u0009");
out = out.replaceAll("#x0a", "\n");
out = out.replaceAll("#x0d", "\r");
return out;
}
}