[isidorus-cvs] r500 - in branches/gdl-frontend/src/anaToMia/GDL_TmEngine: src/us/isidor/gdl/anaToMia/TmEngine/Base src/us/isidor/gdl/anaToMia/TmEngine/TopicMaps src/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine war
lgiessmann at common-lisp.net
lgiessmann at common-lisp.net
Fri Jun 24 11:07:02 UTC 2011
Author: lgiessmann
Date: Fri Jun 24 04:07:02 2011
New Revision: 500
Log:
gdl-frontend: TmEngine: fixed a bug with type hanlding in the package us.isidor.gdl.anaToMia.TmEngine.TopicMaps
Deleted:
branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/TopicMaps/ConstructStub.java
Modified:
branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/Base/TmEngine.java
branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/TopicMaps/Topic.java
branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/JtmsTmEngine.java
branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/TestClass.java
branches/gdl-frontend/src/anaToMia/GDL_TmEngine/war/GDL_TmEngine.html
Modified: branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/Base/TmEngine.java
==============================================================================
--- branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/Base/TmEngine.java Fri Jun 24 00:14:22 2011 (r499)
+++ branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/Base/TmEngine.java Fri Jun 24 04:07:02 2011 (r500)
@@ -1,11 +1,28 @@
package us.isidor.gdl.anaToMia.TmEngine.Base;
-import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Construct;
+import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Association;
+import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Name;
+import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Occurrence;
+import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Role;
+import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Topic;
import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.TopicMap;
+import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Variant;
public interface TmEngine{
- public Construct importTm(String tmData, TopicMap tm) throws FormatException, MissingReference;
- public String exportTm(Construct construct) throws ExporterException;
+ public TopicMap importTopicMap(String tmData, TopicMap tm) throws FormatException, MissingReference;
+ public Topic importTopic(String tmData, TopicMap tm) throws FormatException, MissingReference;
+ public Name importName(String tmData, TopicMap tm) throws FormatException, MissingReference;
+ public Variant importVariant(String tmData, TopicMap tm) throws FormatException, MissingReference;
+ public Occurrence importOccurrence(String tmData, TopicMap tm) throws FormatException, MissingReference;
+ public Association importAssociation(String tmData, TopicMap tm) throws FormatException, MissingReference;
+ public Role importRole(String tmData, TopicMap tm) throws FormatException, MissingReference;
+ public String exportTm(TopicMap tm) throws ExporterException;
+ public String exportTm(Topic topic) throws ExporterException;
+ public String exportTm(Name name) throws ExporterException;
+ public String exportTm(Variant variant) throws ExporterException;
+ public String exportTm(Occurrence occurrence) throws ExporterException;
+ public String exportTm(Association association) throws ExporterException;
+ public String exportTm(Role role) throws ExporterException;
public TopicMap createTopicMap(String tmLocator);
public TopicMap getTopicMap(String tmLocator);
public TopicMap[] getTopicMaps();
Modified: branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/TopicMaps/Topic.java
==============================================================================
--- branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/TopicMaps/Topic.java Fri Jun 24 00:14:22 2011 (r499)
+++ branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/TopicMaps/Topic.java Fri Jun 24 04:07:02 2011 (r500)
@@ -134,7 +134,7 @@
public final native JsArray<Locator> getItemIdentifiers() /*-{
- return this.getItemIdentifier();
+ return this.getItemIdentifiers();
}-*/;
Modified: branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/JtmsTmEngine.java
==============================================================================
--- branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/JtmsTmEngine.java Fri Jun 24 00:14:22 2011 (r499)
+++ branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/JtmsTmEngine.java Fri Jun 24 04:07:02 2011 (r500)
@@ -14,7 +14,6 @@
import us.isidor.gdl.anaToMia.TmEngine.Base.TmEngine;
import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Association;
import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Construct;
-import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.ConstructStub;
import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Locator;
import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Name;
import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.Occurrence;
@@ -76,47 +75,90 @@
@Override
- public Construct importTm(String tmData, TopicMap tm) throws FormatException, MissingReference {
- return importJTM11(tmData, tm);
+ public TopicMap importTopicMap(String tmData, TopicMap tm) throws FormatException, MissingReference {
+ return importJTM11TopicMap(tmData, tm);
}
-
-
+
+
@Override
- public String exportTm(Construct construct) throws ExporterException {
- String value = "";
- Prefixes prefixes = new Prefixes(construct);
-
- switch(((ConstructStub)construct).classType()) {
- case Association:
- value = exportJTM11((Association)construct, prefixes, true);
- break;
- case Role:
- value = exportJTM11((Role)construct, prefixes, true);
- break;
- case Topic:
- value = exportJTM11((Topic)construct, prefixes, true);
- break;
- case Name:
- value = exportJTM11((Name)construct, prefixes, true);
- break;
- case Variant:
- value = exportJTM11((Variant)construct, prefixes, true);
- break;
- case Occurrence:
- value = exportJTM11((Occurrence)construct, prefixes, true);
- break;
- case TopicMap:
- value = exportJTM11((TopicMap)construct, prefixes, true);
- break;
- default:
- throw new ExporterException("only the constructs Association, Role, Topic, Name, Variant, Occurrence and TopicMap can be exported!");
- }
-
- return value;
+ public Topic importTopic(String tmData, TopicMap tm) throws FormatException, MissingReference {
+ return importJTM11Topic(tmData, tm);
+ }
+
+
+ @Override
+ public Name importName(String tmData, TopicMap tm) throws FormatException, MissingReference {
+ return importJTM11Name(tmData, tm);
+ }
+
+
+ @Override
+ public Variant importVariant(String tmData, TopicMap tm) throws FormatException, MissingReference {
+ return importJTM11Variant(tmData, tm);
+ }
+
+
+ @Override
+ public Occurrence importOccurrence(String tmData, TopicMap tm) throws FormatException, MissingReference {
+ return importJTM11Occurrence(tmData, tm);
+ }
+
+
+ @Override
+ public Association importAssociation(String tmData, TopicMap tm) throws FormatException, MissingReference {
+ return importJTM11Association(tmData, tm);
+ }
+
+
+ @Override
+ public Role importRole(String tmData, TopicMap tm) throws FormatException, MissingReference {
+ return importJTM11Role(tmData, tm);
}
-
- private Construct importJTM11(String jtm, TopicMap tm) throws FormatException, MissingReference{
+
+ @Override
+ public String exportTm(TopicMap tm) throws ExporterException {
+ return exportJTM11(tm, new Prefixes(tm), true);
+ }
+
+
+ @Override
+ public String exportTm(Topic topic) throws ExporterException {
+ return exportJTM11(topic, new Prefixes(topic), true);
+ }
+
+
+ @Override
+ public String exportTm(Name name) throws ExporterException {
+ return exportJTM11(name, new Prefixes(name), true);
+ }
+
+
+ @Override
+ public String exportTm(Variant variant) throws ExporterException {
+ return exportJTM11(variant, new Prefixes(variant), true);
+ }
+
+
+ @Override
+ public String exportTm(Occurrence occurrence) throws ExporterException {
+ return exportJTM11(occurrence, new Prefixes(occurrence), true);
+ }
+
+
+ @Override
+ public String exportTm(Association association) throws ExporterException {
+ return exportJTM11(association, new Prefixes(association), true);
+ }
+
+
+ @Override
+ public String exportTm(Role role) throws ExporterException {
+ return exportJTM11(role, new Prefixes(role), true);
+ }
+
+
+ private JSONObject checkJTMFormat(String jtm) throws FormatException{
// create a JSONObject from the json string
JSONValue jsonValue = JSONParser.parseStrict(jtm);
@@ -134,7 +176,39 @@
if(!version.equals("1.1")){
throw new FormatException("\"version\" field must be set to \"1.1\"");
}
+
+ return jsonObject;
+ }
+
+
+ private TopicMap importJTM11TopicMap(String jtm, TopicMap tm) throws FormatException, MissingReference {
+ JSONObject jsonObject = checkJTMFormat(jtm);
+
+ // get the item_type
+ String item_type = null;
+ if(jsonObject.containsKey("item_type")){
+ item_type = getStringValue(jsonObject, "item_type");
+ }
+ // get all prefixes
+ Prefixes prefixes;
+ if(jsonObject.containsKey("prefixes")){
+ prefixes = new Prefixes(jsonObject.get("prefixes").isObject());
+ } else {
+ prefixes = new Prefixes();
+ }
+
+ if(item_type == null || !item_type.equals("topicmap")){
+ throw new FormatException("expected a topicmap object, but \"item_type\" is not set to \"topicmap\"");
+ }else {
+ return importJTM11TopicMap(jsonObject, tm, prefixes);
+ }
+ }
+
+
+ private Topic importJTM11Topic(String jtm, TopicMap tm) throws FormatException, MissingReference {
+ JSONObject jsonObject = checkJTMFormat(jtm);
+
// get the item_type
String item_type = null;
if(jsonObject.containsKey("item_type")){
@@ -148,31 +222,140 @@
} else {
prefixes = new Prefixes();
}
+
+ if(item_type == null || !item_type.equals("topic")){
+ throw new FormatException("expected a topic object, but \"item_type\" is not set to \"topic\"");
+ }else {
+ return importJTM11Topic(jsonObject, tm, prefixes);
+ }
+ }
- if(item_type == null){
- throw new FormatException("\"item_type\" field must be set to one of \"topicmap\", \"topic\", \"name\", \"variant\", \"occurrence\", \"association\", \"role\"");
- }else if(item_type.equals("topicmap")){
- importJTM11TopicMap(jsonObject, tm, prefixes);
- }else if(item_type.equals("topic")){
- importJTM11Topic(jsonObject, tm, prefixes);
- }else if(item_type.equals("name")){
- importJTM11Name(null, jsonObject, tm, prefixes);
- }else if(item_type.equals("variant")){
- importJTM11Variant(null, jsonObject, tm, prefixes);
- }else if(item_type.equals("occurrence")){
- importJTM11Occurrence(null, jsonObject, tm, prefixes);
- }else if(item_type.equals("association")){
- importJTM11Association(jsonObject, tm, prefixes);
- }else if(item_type.equals("role")){
- importJTM11Role(null, jsonObject, tm, prefixes);
+
+ private Name importJTM11Name(String jtm, TopicMap tm) throws FormatException, MissingReference {
+ JSONObject jsonObject = checkJTMFormat(jtm);
+
+ // get the item_type
+ String item_type = null;
+ if(jsonObject.containsKey("item_type")){
+ item_type = getStringValue(jsonObject, "item_type");
+ }
+
+ // get all prefixes
+ Prefixes prefixes;
+ if(jsonObject.containsKey("prefixes")){
+ prefixes = new Prefixes(jsonObject.get("prefixes").isObject());
+ } else {
+ prefixes = new Prefixes();
+ }
+
+ if(item_type == null || !item_type.equals("name")){
+ throw new FormatException("expected a name object, but \"item_type\" is not set to \"name\"");
+ }else {
+ return importJTM11Name(null, jsonObject, tm, prefixes);
+ }
+ }
+
+
+ private Variant importJTM11Variant(String jtm, TopicMap tm) throws FormatException, MissingReference {
+ JSONObject jsonObject = checkJTMFormat(jtm);
+
+ // get the item_type
+ String item_type = null;
+ if(jsonObject.containsKey("item_type")){
+ item_type = getStringValue(jsonObject, "item_type");
+ }
+
+ // get all prefixes
+ Prefixes prefixes;
+ if(jsonObject.containsKey("prefixes")){
+ prefixes = new Prefixes(jsonObject.get("prefixes").isObject());
+ } else {
+ prefixes = new Prefixes();
+ }
+
+ if(item_type == null || !item_type.equals("variant")){
+ throw new FormatException("expected a variant object, but \"item_type\" is not set to \"variant\"");
+ }else {
+ return importJTM11Variant(null, jsonObject, tm, prefixes);
+ }
+ }
+
+
+ private Occurrence importJTM11Occurrence(String jtm, TopicMap tm) throws FormatException, MissingReference {
+ JSONObject jsonObject = checkJTMFormat(jtm);
+
+ // get the item_type
+ String item_type = null;
+ if(jsonObject.containsKey("item_type")){
+ item_type = getStringValue(jsonObject, "item_type");
+ }
+
+ // get all prefixes
+ Prefixes prefixes;
+ if(jsonObject.containsKey("prefixes")){
+ prefixes = new Prefixes(jsonObject.get("prefixes").isObject());
+ } else {
+ prefixes = new Prefixes();
+ }
+
+ if(item_type == null || !item_type.equals("occurrence")){
+ throw new FormatException("expected an occurrence object, but \"item_type\" is not set to \"occurrence\"");
}else {
- throw new FormatException("\"item_type\" field must be set to one of \"topicmap\", \"topic\", \"name\", \"variant\", \"occurrence\", \"association\", \"role\"");
+ return importJTM11Occurrence(null, jsonObject, tm, prefixes);
+ }
+ }
+
+
+ private Association importJTM11Association(String jtm, TopicMap tm) throws FormatException, MissingReference {
+ JSONObject jsonObject = checkJTMFormat(jtm);
+
+ // get the item_type
+ String item_type = null;
+ if(jsonObject.containsKey("item_type")){
+ item_type = getStringValue(jsonObject, "item_type");
}
- return null;
+ // get all prefixes
+ Prefixes prefixes;
+ if(jsonObject.containsKey("prefixes")){
+ prefixes = new Prefixes(jsonObject.get("prefixes").isObject());
+ } else {
+ prefixes = new Prefixes();
+ }
+
+ if(item_type == null || !item_type.equals("association")){
+ throw new FormatException("expected an association object, but \"item_type\" is not set to \"association\"");
+ }else {
+ return importJTM11Association(jsonObject, tm, prefixes);
+ }
}
+
+
+ private Role importJTM11Role(String jtm, TopicMap tm) throws FormatException, MissingReference {
+ JSONObject jsonObject = checkJTMFormat(jtm);
+
+ // get the item_type
+ String item_type = null;
+ if(jsonObject.containsKey("item_type")){
+ item_type = getStringValue(jsonObject, "item_type");
+ }
+ // get all prefixes
+ Prefixes prefixes;
+ if(jsonObject.containsKey("prefixes")){
+ prefixes = new Prefixes(jsonObject.get("prefixes").isObject());
+ } else {
+ prefixes = new Prefixes();
+ }
+
+ if(item_type == null || !item_type.equals("role")){
+ throw new FormatException("expected a role object, but \"item_type\" is not set to \"role\"");
+ }else {
+ return importJTM11Role(null, jsonObject, tm, prefixes);
+ }
+ }
+
private TopicMap importJTM11TopicMap(JSONObject jtm, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference{
// the received topic map's item identifiers are ignored, the tm's locator is used instead
@@ -776,25 +959,38 @@
}
- private String exportItemType(Construct construct) throws ExporterException{
- switch(((ConstructStub)construct).classType()) {
- case Association:
- return "\"item_type\": \"association\"";
- case Role:
- return "\"item_type\": \"role\"";
- case Topic:
- return "\"item_type\": \"topic\"";
- case Name:
- return "\"item_type\": \"name\"";
- case Variant:
- return "\"item_type\": \"variant\"";
- case Occurrence:
- return "\"item_type\": \"occurrence\"";
- case TopicMap:
- return "\"item_type\": \"topicmap\"";
- default:
- throw new ExporterException("only the constructs Association, Role, Topic, Name, Variant, Occurrence and TopicMap can be exported!");
- }
+ private String exportItemType(TopicMap tm) throws ExporterException{
+ return exportValue("topicmap", "item_type");
+ }
+
+
+ private String exportItemType(Topic topic) throws ExporterException{
+ return exportValue("topic", "item_type");
+ }
+
+
+ private String exportItemType(Name name) throws ExporterException{
+ return exportValue("name", "item_type");
+ }
+
+
+ private String exportItemType(Variant varian) throws ExporterException{
+ return exportValue("variant", "item_type");
+ }
+
+
+ private String exportItemType(Occurrence occurrence) throws ExporterException{
+ return exportValue("occurrence", "item_type");
+ }
+
+
+ private String exportItemType(Association association) throws ExporterException{
+ return exportValue("association", "item_type");
+ }
+
+
+ private String exportItemType(Role role) throws ExporterException{
+ return exportValue("role", "item_type");
}
@@ -803,32 +999,57 @@
}
- private String exportValue(String jsonValue, String key){
- return "\"" + key + "\":" + escapeJSON(jsonValue);
+
+ private String exportValue(String value, String key){
+ return "\"" + key + "\":" + escapeJSON(value);
}
- private String exportArrayValues(ArrayList<String> jsonValues, String key){
+ private String exportJsonValue(String jsonValue, String key){
+ return "\"" + key + "\":" + jsonValue;
+ }
+
+
+
+ private String exportArrayValues(ArrayList<String> values, String key){
+ if(values == null || values.size() == 0){
+ return "\"" + key + "\":" + "null";
+ } else {
+ String localValues = "\"" + key + "\":[";
+ for(int i = 0; i != values.size(); ++i){
+ localValues += escapeJSON(values.get(i));
+ if(i != values.size() - 1){
+ localValues += ",";
+ }
+ }
+ return localValues + "]";
+ }
+ }
+
+
+ private String exportJsonArrayValues(ArrayList<String> jsonValues, String key){
if(jsonValues == null || jsonValues.size() == 0){
return "\"" + key + "\":" + "null";
} else {
- String values = "[";
+ String localValues = "\"" + key + "\":[";
for(int i = 0; i != jsonValues.size(); ++i){
- values += escapeJSON(jsonValues.get(i));
+ localValues += jsonValues.get(i);
if(i != jsonValues.size() - 1){
- values += ",";
+ localValues += ",";
}
}
- return values + "]";
+ return localValues + "]";
}
}
+
private String curie(Locator uri, Prefixes prefixes) throws ExporterException{
return curie(uri.getReference(), prefixes);
}
+
private String curie(String uri, Prefixes prefixes) throws ExporterException {
String qualifier = prefixes.getQualifier(uri);
String suffix = uri.substring(prefixes.getNs(qualifier).length());
@@ -836,16 +1057,19 @@
}
+
private String exportReference(Topic topic, String key, Prefixes prefixes) throws ExporterException {
return "\"" + key + "\":" + exportReference(topic, prefixes);
}
+
private String exportReferences(ArrayList<Topic> topics, String key, Prefixes prefixes) throws ExporterException {
return "\"" + key + "\":" + exportReferences(topics, prefixes);
}
+
private String exportReference(Topic topic, Prefixes prefixes) throws ExporterException{
if(topic != null){
JsArray<Locator> sis = topic.getSubjectIdentifiers();
@@ -866,23 +1090,25 @@
}
+
private String exportReferences(ArrayList<Topic> topics, Prefixes prefixes) throws ExporterException{
- String value = "";
- for(int i = 0; i != topics.size(); ++i){
- value += exportReference(topics.get(i), prefixes);
- if(i != topics.size() - 1){
- value += ",";
- }
- }
-
- if(value.length() == 0){
+ if(topics == null || topics.size() == 0){
return "null";
} else {
- return value;
+ String value = "[";
+ for(int i = 0; i != topics.size(); ++i){
+ value += exportReference(topics.get(i), prefixes);
+ if(i != topics.size() - 1){
+ value += ",";
+ }
+ }
+
+ return value + "]";
}
}
+
private String exportJTM11(TopicMap tm, Prefixes prefixes, boolean standalone) throws ExporterException{
// standalone is ignored, i.e. a TopicMap object mustis always exported as a
// stand alone item.
@@ -893,7 +1119,7 @@
json += exportVersion11() + ",";
// prefixes
- json += prefixes.toJSON() + ",";
+ json += exportJsonValue(prefixes.toJSON(), "prefixes") + ",";
// item_type
json += exportItemType(tm) + ",";
@@ -909,19 +1135,20 @@
for(int i = 0; i != tm.getTopics().length(); ++i){
topicStrings.add(exportJTM11(tm.getTopics().get(i), prefixes, false));
}
- json += exportArrayValues(topicStrings, "topics") + ",";
+ json += exportJsonArrayValues(topicStrings, "topics") + ",";
// associations
ArrayList<String> associationStrings = new ArrayList<String>();
for(int i = 0; i != tm.getAssociations().length(); ++i){
associationStrings.add(exportJTM11(tm.getAssociations().get(i), prefixes, false));
}
- json += exportArrayValues(associationStrings, "topics") + ",";
+ json += exportJsonArrayValues(associationStrings, "associations") + "}";
return json;
}
+
private String exportJTM11(Topic topic, Prefixes prefixes, boolean standalone) throws ExporterException{
String json = "{";
@@ -929,7 +1156,7 @@
// construct is exported as a standalone construct
if(standalone){
json += exportVersion11() + ",";
- json += prefixes.toJSON() + ",";
+ json += exportJsonValue(prefixes.toJSON(), "prefixes") + ",";
json += exportItemType(topic) + ",";
json += exportParent(topic.getParent(), prefixes) + ",";
}
@@ -947,14 +1174,14 @@
for(int i = 0; i != topic.getNames().length(); ++i){
nameStrings.add(exportJTM11(topic.getNames().get(i), prefixes, false));
}
- json += exportArrayValues(nameStrings, "names") + ",";
+ json += exportJsonArrayValues(nameStrings, "names") + ",";
// occurrences
ArrayList<String> occurrenceStrings = new ArrayList<String>();
for(int i = 0; i != topic.getOccurrences().length(); ++i){
occurrenceStrings.add(exportJTM11(topic.getOccurrences().get(i), prefixes, false));
}
- json += exportArrayValues(occurrenceStrings, "names") + ",";
+ json += exportJsonArrayValues(occurrenceStrings, "occurrences") + ",";
// instance_of
ArrayList<Topic> instanceOfs = new ArrayList<Topic>();
@@ -967,6 +1194,7 @@
}
+
private String exportJTM11(Name name, Prefixes prefixes, boolean standalone) throws ExporterException{
String json = "{";
@@ -974,7 +1202,7 @@
// construct is exported as a standalone construct
if(standalone){
json += exportVersion11() + ",";
- json += prefixes.toJSON() + ",";
+ json += exportJsonValue(prefixes.toJSON(), "prefixes") + ",";
json += exportItemType(name) + ",";
json += exportParent(name.getParent(), prefixes) + ",";
}
@@ -997,10 +1225,10 @@
for(int i = 0; i != name.getVariants().length(); ++i){
variantStrings.add(exportJTM11(name.getVariants().get(i), prefixes, false));
}
- json += exportArrayValues(variantStrings, "variants") + ",";
+ json += exportJsonArrayValues(variantStrings, "variants") + ",";
// value
- json += exportValue(name.getValue(), "value");
+ json += exportValue(name.getValue(), "value") + ",";
// type - a name my have null as a type
json += exportReference(name.getType(), "type", prefixes) + "}";
@@ -1009,6 +1237,7 @@
}
+
private String exportJTM11(Variant variant, Prefixes prefixes, boolean standalone) throws ExporterException{
String json = "{";
@@ -1016,7 +1245,7 @@
// construct is exported as a standalone construct
if(standalone){
json += exportVersion11() + ",";
- json += prefixes.toJSON() + ",";
+ json += exportJsonValue(prefixes.toJSON(), "prefixes") + ",";
json += exportItemType(variant) + ",";
json += exportParent(variant.getParent(), prefixes) + ",";
}
@@ -1038,12 +1267,13 @@
json += exportValue(curie(variant.getDatatype(), prefixes), "datatype") + ",";
// value
- json += exportValue(curie(variant.getValue(), prefixes), "value") + "}";
+ json += exportValue(variant.getValue(), "value") + "}";
return json;
}
+
private String exportJTM11(Occurrence occurrence, Prefixes prefixes, boolean standalone) throws ExporterException{
String json = "{";
@@ -1051,7 +1281,7 @@
// construct is exported as a standalone construct
if(standalone){
json += exportVersion11() + ",";
- json += prefixes.toJSON() + ",";
+ json += exportJsonValue(prefixes.toJSON(), "prefixes") + ",";
json += exportItemType(occurrence) + ",";
json += exportParent(occurrence.getParent(), prefixes) + ",";
}
@@ -1079,47 +1309,25 @@
json += exportValue(curie(occurrence.getDatatype(), prefixes), "datatype") + ",";
// value
- json += exportValue(curie(occurrence.getValue(), prefixes), "value") + "}";
+ json += exportValue(occurrence.getValue(), "value") + "}";
return json;
}
- private String exportParent(Construct parent, Prefixes prefixes) throws ExporterException {
- ConstructStub constructStub = (ConstructStub)parent;
-
+
+ private String exportParent(Topic parent, Prefixes prefixes) throws ExporterException {
Locator parentIdentifier = null;
String idType = "ii:";
- switch(constructStub.classType()){
- case TopicMap:
- parentIdentifier = ((TopicMap)parent).getLocator();
- break;
- case Topic:
- Topic topic = (Topic)parent;
- if(topic.getSubjectIdentifiers().length() != 0){
- parentIdentifier = topic.getSubjectIdentifiers().get(0);
- idType = "si:";
- } else if(topic.getSubjectLocators().length() != 0){
- parentIdentifier = topic.getSubjectLocators().get(0);
- idType = "sl:";
- } else if(topic.getItemIdentifiers().length() != 0){
- parentIdentifier = topic.getItemIdentifiers().get(0);
- }
- break;
- case Name:
- Name name = (Name)parent;
- if(name.getItemIdentifiers().length() != 0){
- parentIdentifier = name.getItemIdentifiers().get(0);
- }
- break;
- case Association:
- Association association = (Association)parent;
- if(association.getItemIdentifiers().length() != 0){
- parentIdentifier = association.getItemIdentifiers().get(0);
- }
- break;
- default:
- throw new ExporterException("only TopicMap, Topic, Name and Association can be used as \"parent\" field reference, but found: " + constructStub.classType());
+
+ if(parent.getSubjectIdentifiers().length() != 0){
+ parentIdentifier = parent.getSubjectIdentifiers().get(0);
+ idType = "si:";
+ } else if(parent.getSubjectLocators().length() != 0){
+ parentIdentifier = parent.getSubjectLocators().get(0);
+ idType = "sl:";
+ } else if(parent.getItemIdentifiers().length() != 0){
+ parentIdentifier = parent.getItemIdentifiers().get(0);
}
if(parentIdentifier == null){
@@ -1130,6 +1338,25 @@
}
+
+ private String exportParent(Reifiable parent, Prefixes prefixes) throws ExporterException {
+ Locator parentIdentifier = null;
+
+ ReifiableStub parentStub = (ReifiableStub)parent;
+
+ if(parentStub.getItemIdentifiers().length() != 0){
+ parentIdentifier = parentStub.getItemIdentifiers().get(0);
+ }
+
+ if(parentIdentifier == null){
+ throw new ExporterException("a parent must have at least one identifeir to be serailised");
+ } else {
+ return "\"parent\":[\"ii:" + curie(parentIdentifier, prefixes) + "\"]";
+ }
+ }
+
+
+
private String exportJTM11(Association association, Prefixes prefixes, boolean standalone) throws ExporterException{
String json = "{";
@@ -1137,7 +1364,7 @@
// construct is exported as a standalone construct
if(standalone){
json += exportVersion11() + ",";
- json += prefixes.toJSON() + ",";
+ json += exportJsonValue(prefixes.toJSON(), "prefixes") + ",";
json += exportItemType(association) + ",";
json += exportParent(association.getParent(), prefixes) + ",";
}
@@ -1160,7 +1387,7 @@
for(int i = 0; i != association.getRoles().length(); ++i){
roleStrings.add(exportJTM11(association.getRoles().get(i), prefixes, false));
}
- json += exportArrayValues(roleStrings, "roles") + ",";
+ json += exportJsonArrayValues(roleStrings, "roles") + ",";
// type
if(association.getType() == null){
@@ -1172,16 +1399,18 @@
}
+
private String exportIdentifiers(JsArray<Locator> identifiers, String key, Prefixes prefixes) throws ExporterException{
ArrayList<String> curies = new ArrayList<String>();
for(int i = 0; i != identifiers.length(); ++i){
curies.add(escapeJSON(curie(identifiers.get(i), prefixes)));
}
- return exportArrayValues(curies, key);
+ return exportJsonArrayValues(curies, key);
}
+
private String exportJTM11(Role role, Prefixes prefixes, boolean standalone) throws ExporterException{
String json = "{";
@@ -1189,7 +1418,7 @@
// construct is exported as a standalone construct
if(standalone){
json += exportVersion11() + ",";
- json += prefixes.toJSON() + ",";
+ json += exportJsonValue(prefixes.toJSON(), "prefixes") + ",";
json += exportItemType(role) + ",";
json += exportParent(role.getParent(), prefixes) + ",";
}
@@ -1216,11 +1445,13 @@
}
- private String escapeJSON(String json){
+
+ private static String escapeJSON(String json){
return JsonUtils.escapeValue(json).replaceAll("/", "\\/");
}
+
private class Prefixes {
private ArrayList<String[]> prefixes = new ArrayList<String[]>();
@@ -1252,35 +1483,45 @@
}
- public Prefixes(Construct construct) throws ExporterException {
- ConstructStub constructStub = (ConstructStub)construct;
- this.prefixes = new ArrayList<String[]>();
-
- switch(constructStub.classType()){
- case TopicMap:
- initPrefixes(this.prefixes, (TopicMap)construct);
- break;
- case Topic:
- initPrefixes(this.prefixes, (Topic)construct);
- break;
- case Name:
- initPrefixes(this.prefixes, (Name)construct);
- break;
- case Variant:
- initPrefixes(this.prefixes, (Variant)construct);
- break;
- case Occurrence:
- initPrefixes(this.prefixes, (Occurrence)construct);
- break;
- case Association:
- initPrefixes(this.prefixes, (Association)construct);
- break;
- case Role:
- initPrefixes(this.prefixes, (Role)construct);
- break;
- default:
- throw new ExporterException("a Prefixes object can only be created for TopicMap, Topic, Name, Variant, Occurrence, Association and Role");
- }
+ public Prefixes (Role role) throws ExporterException {
+ this();
+ initPrefixes(prefixes, role);
+ }
+
+
+ public Prefixes (Association association) throws ExporterException {
+ this();
+ initPrefixes(prefixes, association);
+ }
+
+
+ public Prefixes (Occurrence occurrence) throws ExporterException {
+ this();
+ initPrefixes(prefixes, occurrence);
+ }
+
+
+ public Prefixes (Variant variant) throws ExporterException {
+ this();
+ initPrefixes(prefixes, variant);
+ }
+
+
+ public Prefixes (Name name) throws ExporterException {
+ this();
+ initPrefixes(prefixes, name);
+ }
+
+
+ public Prefixes (Topic topic) throws ExporterException {
+ this();
+ initPrefixes(prefixes, topic);
+ }
+
+
+ public Prefixes (TopicMap tm) throws ExporterException {
+ this();
+ initPrefixes(prefixes, tm);
}
@@ -1447,16 +1688,18 @@
private void addPrefixOfReifiableReference(ArrayList<String[]> prefixes, Reifiable reifiable) throws ExporterException {
- if(reifiable != null){
+ // TODO: check
+
+ /*if(reifiable != null){
ReifiableStub reifiableStub = (ReifiableStub)reifiable;
if(reifiableStub.getItemIdentifiers().length() != 0) {
Locator loc = reifiableStub.getItemIdentifiers().get(0);
addPrefixOfIdentifier(prefixes, loc);
} else {
- throw new ExporterException("a reifiable construct must have at least on item identifier set");
+ throw new ExporterException("a reifiable construct must have at least one item identifier set");
}
- }
+ }*/
}
@@ -1565,7 +1808,7 @@
public String toJSON(){
String value = "{";
for(int i = 0; i != prefixes.size(); ++i){
- value += "\"" + prefixes.get(i)[0] + "\":\"" + prefixes.get(i)[1] + "\"";
+ value += "\"" + prefixes.get(i)[0] + "\":" + escapeJSON(prefixes.get(i)[1]);
if(i == prefixes.size() - 1){
value += "}";
} else {
Modified: branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/TestClass.java
==============================================================================
--- branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/TestClass.java Fri Jun 24 00:14:22 2011 (r499)
+++ branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/TestClass.java Fri Jun 24 04:07:02 2011 (r500)
@@ -1,37 +1,151 @@
package us.isidor.gdl.anaToMia.TmEngine.jtmsBasedEngine;
+import us.isidor.gdl.anaToMia.TmEngine.Base.ExporterException;
+import us.isidor.gdl.anaToMia.TmEngine.Base.FormatException;
+import us.isidor.gdl.anaToMia.TmEngine.Base.MissingReference;
import us.isidor.gdl.anaToMia.TmEngine.TopicMaps.TopicMap;
import com.google.gwt.core.client.EntryPoint;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.ui.Button;
+import com.google.gwt.user.client.ui.HorizontalPanel;
+import com.google.gwt.user.client.ui.Label;
+import com.google.gwt.user.client.ui.RootPanel;
+import com.google.gwt.user.client.ui.TextArea;
+import com.google.gwt.user.client.ui.VerticalPanel;
public class TestClass implements EntryPoint{
+ JtmsTmEngine tme = new JtmsTmEngine();
+ private TextArea userInputField = new TextArea();
+ private Button importJsonButton = new Button();
+ private Button exportJsonButton = new Button();
+ private VerticalPanel mainPanel = new VerticalPanel();
+ private Label topicsLabel = new Label();
+ private Label associationsLabel = new Label();
+ private Label exportedJTMLabel = new Label();
+ private Label title = new Label();
+ private HorizontalPanel statusPanel = new HorizontalPanel();
+ private String tmData = "{\"version\":\"1.1\","+
+ "\"item_type\":\"topicmap\"," +
+ "\"item_identifiers\":[\"http://some.where/my-tm\"]," +
+ "\"topics\":[{\"subject_identifiers\":[\"http://some.where/my-top-1\"," +
+ "\"http://some.where/my-top-1-1\"]," +
+ "\"names\":[{\"item_identifiers\":[\"http://some.where/my-name-ii-1\"]," +
+ "\"value\":\"_Test_Topic_\"," +
+ "\"scope\":[\"si:http://some.where/my-scope-1\"," +
+ "\"si:http://some.where/my-scope-2\"]," +
+ "\"type\":\"si:http://some.where/my-name-type-1\"}]}," +
+ "{\"subject_identifiers\":[\"http://some.where/my-name-type-1\"]}," +
+ "{\"subject_identifiers\":[\"http://some.where/my-scope-1\"]}," +
+ "{\"subject_identifiers\":[\"http://some.where/my-scope-2\"]}]}";
@Override
public void onModuleLoad() {
- Window.alert(">> TEST !!!");
-
- String tmData = "{\"version\":\"1.1\","+
- "\"item_type\":\"topicmap\"," +
- "\"topics\":[{\"subject_identifiers\":[\"http://some.where/my-top-1\"," +
- "\"http://some.where/my-top-1-1\"]," +
- "\"names\":[{\"item_identifiers\":[\"http://some.where/my-name-ii-1\"]," +
- "\"value\":\"_Test_Topic_\"," +
- "\"scope\":[\"si:http://some.where/my-scope-1\"," +
- "\"si:http://some.where/my-scope-2\"]," +
- "\"type\":\"si:http://some.where/my-name-type-1\"}]}," +
- "{\"subject_identifiers\":[\"http://some.where/my-name-type-1\"]}," +
- "{\"subject_identifiers\":[\"http://some.where/my-scope-1\"]}," +
- "{\"subject_identifiers\":[\"http://some.where/my-scope-2\"]}]}";
- JtmsTmEngine tme = new JtmsTmEngine();
TopicMap tm = tme.createTopicMap("http://my.topic.map/tm-1");
- try{
- tme.importTm(tmData, tm);
- Window.alert("tops: " + tm.getTopics().length());
- Window.alert("scopes: " +tm.getTopicBySubjectIdentifier(tm.createLocator("http://some.where/my-top-1")).getNames().get(0).getScope().length());
- }catch(Exception e){
- Window.alert("error: " + e.getMessage());
+
+ RootPanel.get("GWT_Content").add(mainPanel);
+
+ mainPanel.add(userInputField);
+ userInputField.setPixelSize(Window.getClientWidth() - 100, Window.getClientHeight() / 6);
+ userInputField.setValue(tmData);
+
+ mainPanel.add(statusPanel);
+ statusPanel.add(importJsonButton);
+ statusPanel.add(exportJsonButton);
+ statusPanel.add(topicsLabel);
+ statusPanel.add(associationsLabel);
+
+ topicsLabel.setText("topics: " + tm.getTopics().length());
+ associationsLabel.setText("associations: " + tm.getAssociations().length());
+
+ importJsonButton.setText("import JSON");
+ importJsonButton.addClickHandler(new ImportJsonButtonClickHandler(tm, tme));
+
+ exportJsonButton.setText("export topic map");
+ exportJsonButton.addClickHandler(new ExportJsonButtonClickHandler(tm, tme));
+
+ mainPanel.add(title);
+ title.setText("Exported TM:");
+ DOM.setStyleAttribute(title.getElement(), "fontSize", "larger");
+ DOM.setStyleAttribute(title.getElement(), "fontWeight", "bold");
+ DOM.setStyleAttribute(title.getElement(), "marginTop", "3%");
+
+ mainPanel.add(exportedJTMLabel);
+ DOM.setStyleAttribute(exportedJTMLabel.getElement(), "backgroundColor", "rgb(100, 255, 100)");
+ DOM.setStyleAttribute(exportedJTMLabel.getElement(), "marginTop", "1em");
+ exportedJTMLabel.setWidth(mainPanel.getOffsetWidth() + "px");
+ DOM.setStyleAttribute(exportedJTMLabel.getElement(), "fontSize", "large");
+
+ setStatusPanel();
+ }
+
+ private void setStatusPanel(){
+ statusPanel.setWidth("30%");
+
+ DOM.setStyleAttribute(topicsLabel.getElement(), "color", "red");
+ DOM.setStyleAttribute(associationsLabel.getElement(), "color", "red");
+ DOM.setStyleAttribute(topicsLabel.getElement(), "fontSize", "large");
+ DOM.setStyleAttribute(associationsLabel.getElement(), "fontSize", "large");
+ }
+
+
+ private class ImportJsonButtonClickHandler implements ClickHandler {
+ private TopicMap tm = null;
+ private JtmsTmEngine tme = null;
+
+ @SuppressWarnings("unused")
+ private ImportJsonButtonClickHandler(){}
+
+
+ public ImportJsonButtonClickHandler(TopicMap tm, JtmsTmEngine tme){
+ this.tm = tm;
+ this.tme = tme;
+ }
+
+ @Override
+ public void onClick(ClickEvent event) {
+ try{
+ tme.importTopicMap(userInputField.getValue(), tm);
+ }catch(FormatException fe){
+ Window.alert("caught error: " + fe.getMessage());
+ }catch(MissingReference me){
+ Window.alert("caught error: " + me.getMessage());
+ }finally {
+ topicsLabel.setText("topics: " + tm.getTopics().length());
+ associationsLabel.setText("associations: " + tm.getAssociations().length());
+ setStatusPanel();
+ }
+ }
+ }
+
+
+ private class ExportJsonButtonClickHandler implements ClickHandler {
+ private TopicMap tm = null;
+ private JtmsTmEngine tme = null;
+
+ @SuppressWarnings("unused")
+ private ExportJsonButtonClickHandler(){}
+
+
+ public ExportJsonButtonClickHandler(TopicMap tm, JtmsTmEngine tme){
+ this.tm = tm;
+ this.tme = tme;
+ }
+
+ @Override
+ public void onClick(ClickEvent event) {
+ String text = "JTM export failed";
+ try{
+ text = tme.exportTm(tm);
+ }catch(ExporterException ee){
+ Window.alert("caught error: " + ee.getMessage());
+ }finally {
+ exportedJTMLabel.setText(text);
+ }
}
}
}
Modified: branches/gdl-frontend/src/anaToMia/GDL_TmEngine/war/GDL_TmEngine.html
==============================================================================
--- branches/gdl-frontend/src/anaToMia/GDL_TmEngine/war/GDL_TmEngine.html Fri Jun 24 00:14:22 2011 (r499)
+++ branches/gdl-frontend/src/anaToMia/GDL_TmEngine/war/GDL_TmEngine.html Fri Jun 24 04:07:02 2011 (r500)
@@ -45,19 +45,10 @@
</div>
</noscript>
- <h1>Web Application Starter Project</h1>
+ <h1>TmEngine Test-App</h1>
- <table align="center">
- <tr>
- <td colspan="2" style="font-weight:bold;">Please enter your name:</td>
- </tr>
- <tr>
- <td id="nameFieldContainer"></td>
- <td id="sendButtonContainer"></td>
- </tr>
- <tr>
- <td colspan="2" style="color:red;" id="errorLabelContainer"></td>
- </tr>
- </table>
+ <center>
+ <div id="GWT_Content"/>
+ </center>
</body>
</html>
More information about the Isidorus-cvs
mailing list