blob: 06c8913c62b7dba33fb1e14b07481bcaedaa4f0d [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Andras Schmidt, Andras Balogh, Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.exports;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import org.eclipse.viatra2.core.IEntity;
import org.eclipse.viatra2.core.IModelElement;
import org.eclipse.viatra2.core.IModelSpace;
import org.eclipse.viatra2.core.IRelation;
import org.eclipse.viatra2.framework.IFramework;
import org.eclipse.viatra2.framework.properties.VPMProperties;
/**
* Exports the modelspace into an XML file.
*
* @author Andras Schmidt, Istvan Rath
*
*/
public class VPMExporter {
/**
* Filter out non-valid Unicode characters not allowed in XML 1.0 files.
*/
private final static String stripNonValidXMLCharacters(String in) {
if (in == null || ("".equals(in))) {
return "";
}
StringBuffer res = new StringBuffer();
char c;
int l = in.length();
for (int i = 0; i < l; i++) {
c = in.charAt(i);
if (isValidXMLUnicodeCharacter(c)) {
res.append(c);
}
}
return res.toString();
}
private final static boolean isValidXMLUnicodeCharacter(char c) {
return ((c == 0x9) || (c == 0xA) || (c == 0xD) || ((c >= 0x20) && (c <= 0xD7FF)) || ((c >= 0xE000) && (c <= 0xFFFD)) || ((c >= 0x10000) && (c <= 0x10FFFF)));
}
/**
* Rewritten by Istvan Rath. Based on original by Andras Schmidt.
* Aim is to create xml documents that contains only valid characters
* (mainly international characters), and to avoid xml parser
* mis-understanding <, >, & and [ ]
*
* @param in
* This string is converted to be valid xml text
* @return The string escaped to be valid xml
*/
public String escapeCharacters2(String _in)
{
// first, strip all Unicode characters that are not allowed in XML
String in = stripNonValidXMLCharacters(_in);
StringBuffer ret = new StringBuffer();
for (char ch : in.toCharArray() )
{
if (ch < ' ' || ch > 127
|| ch == '<' || ch == '>'
|| ch == '&' || ch == '['
|| ch == '"')
{
ret.append(char2escape(ch));
}
else {
ret.append(ch);
}
}
return ret.toString();
}
private String char2escape(char ch) {
return "&#" + (int) ch + ";";
}
public void export(IFramework fw, String fileName) throws IOException {
File f = new File(fileName);
// f.createNewFile();
FileOutputStream stream = new FileOutputStream(f);
export(fw, stream);
}
public void export(IFramework fw, OutputStream outputStream)
throws IOException {
IModelSpace ms = fw.getTopmodel();
// FileWriter fw = new FileWriter(fileName);
PrintWriter out = new PrintWriter(outputStream);
out.println("<?xml version=\"1.0\" ?>");
out.println("<VPM>");
boolean writeProperties = true;
if (writeProperties) {
out.println("<PROPERTIES>");
VPMProperties props = fw.getProperties();
// Export properties alphabetically ordered
TreeMap<Object, Object> ts = new TreeMap<Object, Object>(props
.getJavaProperties());
for (Map.Entry<Object, Object> entry : ts.entrySet()) {
String name = entry.getKey().toString();
String value = entry.getValue().toString();
name = escapeCharacters2(name);
value = escapeCharacters2(value);
out.println("<PROPERTY name=\"" + name + "\" value=\"" + value
+ "\"/>");
}
out.println("</PROPERTIES>");
}
out.println("<MODEL>");
out.println("<ROOTENTITY id=\""
+ ms.getModelManager().getRoot().getID() + "\"/>");
//out.println("<NOPARENTENTITY id=\""+ms.getModelManager().getNoParent()
// .getID()+"\"/>");
for (int i = 0; i < 2; ++i) {
Iterator<? extends IModelElement> it = null;
String type = "";
String tagClose = "";
if (i == 0) {
type = "ENTITY";
out.println("<ENTITIES>");
it = ms.getModelManager().getEntities().iterator();
tagClose = "</ENTITIES>";
}
if (i == 1) {
type = "RELATION";
out.println("<RELATIONS>");
it = ms.getModelManager().getRelations().iterator();
tagClose = "</RELATIONS>";
}
while (it.hasNext()) {
IModelElement e = it.next();
if (e.equals(ms.getModelManager().getRoot())) {
continue;
}
String isAny = "";
if (i == 1) {
// Relation
IRelation rel = (IRelation) e;
isAny = " isAnyFrom=\""
+ Boolean.toString(rel.getIsAnyFrom()) + "\""
+ " isAnyTo=\""
+ Boolean.toString(rel.getIsAnyTo()) + "\"";
}
out.println("<" + type + " id=\"" + e.getID() + "\"" + isAny
+ ">");
out.println("<NAME>" + escapeCharacters2(e.getName())
+ "</NAME>");
if (e.getViewInfo() != null)
out
.println("<VIEWINFO>"
+ escapeCharacters2(e.getViewInfo())
+ "</VIEWINFO>");
// added by Istvan Rath
out.println("<ISFINALTYPE>" + e.getIsFinalType()
+ "</ISFINALTYPE>");
if (e.getTypes() != null) {
Iterator<IModelElement> j = e.getTypes().iterator();
while (j.hasNext()) {
IModelElement me = j.next();
out.println("<TYPE><MODELELEMENT idref=\"" + me.getID()
+ "\"/></TYPE>");
}
}
if (e.getSupertypes() != null) {
Iterator<IModelElement> j = e.getSupertypes().iterator();
while (j.hasNext()) {
IModelElement me = j.next();
out.println("<SUPERTYPE><MODELELEMENT idref=\""
+ me.getID() + "\"/></SUPERTYPE>");
}
}
if (e instanceof IEntity) {
IEntity ent = (IEntity) e;
if (ent.getParent() != null) {
IModelElement me = ent.getParent();
out.println("<PARENT><ENTITY idref=\"" + me.getID()
+ "\"/></PARENT>");
}
}
if (e instanceof IEntity) {
IEntity ent = (IEntity) e;
out.println("<VALUE>" + escapeCharacters2(ent.getValue())
+ "</VALUE>");
}
if (e instanceof IRelation) {
IRelation r = (IRelation) e;
if (r.getFrom() != null)
out.println("<FROM><ENTITY idref=\""
+ r.getFrom().getID() + "\"/></FROM>");
if (r.getTo() != null)
out.println("<TO><ENTITY idref=\"" + r.getTo().getID()
+ "\"/></TO>");
//out.print("<MULTIPLICITY>"+r.getFromMinMultiplicity()+","+
// r
// .getFromMaxMultiplicity()+","+r.getToMinMultiplicity()+","
// +r.getToMaxMultiplicity()+"</MULTIPLICITY>");
out.print("<MULTIPLICITY>" + r.getMultiplicity().toString()
+ "</MULTIPLICITY>");
out.print("<ISAGGREGATION>" + r.getIsAggregation()
+ "</ISAGGREGATION>");
if (r.getInverse() != null) {
out.print("<INVERSE><RELATION idref=\""
+ r.getInverse().getID() + "\"/></INVERSE>");
}
}
out.println("</" + type + ">");
}
out.println(tagClose);
}
out.println("</MODEL>");
out.println("</VPM>");
out.flush();
out.close();
}
}