Author: lgiessmann Date: Mon Jun 27 05:20:08 2011 New Revision: 512
Log: gdl-frontend: Topic Maps Engine: added a unit-test for the class Name; modified the referenced file: GDL_TopicMaps_Model.jar
Modified: branches/gdl-frontend/src/anaToMia/GDL_TmEngine/lib/GDL_TopicMaps_Model.jar branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/JtmsTmEngine.java branches/gdl-frontend/src/anaToMia/GDL_TmEngine/test/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/JtmsTmEngineTest.java
Modified: branches/gdl-frontend/src/anaToMia/GDL_TmEngine/lib/GDL_TopicMaps_Model.jar ============================================================================== Binary file (source and/or target). No diff available.
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 Mon Jun 27 04:47:25 2011 (r511) +++ branches/gdl-frontend/src/anaToMia/GDL_TmEngine/src/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/JtmsTmEngine.java Mon Jun 27 05:20:08 2011 (r512) @@ -28,9 +28,16 @@ public class JtmsTmEngine implements TmEngine{ private final String xsd = "http://www.w3.org/2001/XMLSchema#"; private final String xsdString = "http://www.w3.org/2001/XMLSchema#string"; + private final String defaultNameTypePsi = "http://psi.topicmaps.org/iso13250/model/topic-name"; private ArrayList<TopicMap> tms = new ArrayList<TopicMap>();
+ public Topic createDefaultNameType(TopicMap tm){ + Locator psi = tm.createLocator(defaultNameTypePsi); + return tm.createTopicBySubjectIdentifier(psi); + } + + private final native TopicMap jsCreateTopicMap(String tmLocator) /*-{ var factory, sys, tmid, tm; factory = $wnd.TopicMapSystemFactory.newInstance(); @@ -50,7 +57,7 @@ return tm; } } - + TopicMap tm = jsCreateTopicMap(tmLocator); tms.add(tm); return tm; @@ -84,86 +91,86 @@ public TopicMap importTopicMap(String tmData, TopicMap tm) throws FormatException, MissingReference { return importJTM11TopicMap(tmData, tm); } - - + + @Override 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); }
- + @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); @@ -182,14 +189,14 @@ 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")){ @@ -203,18 +210,18 @@ } 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")){ @@ -228,7 +235,7 @@ } 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 { @@ -239,7 +246,7 @@
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")){ @@ -253,18 +260,18 @@ } 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")){ @@ -278,18 +285,18 @@ } 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")){ @@ -303,7 +310,7 @@ } 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 { @@ -311,10 +318,10 @@ } }
- + 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")){ @@ -328,18 +335,18 @@ } 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")){ @@ -353,7 +360,7 @@ } 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 { @@ -361,7 +368,7 @@ } }
- + 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
@@ -379,7 +386,7 @@ topics.add(importTopicStubFromTm(topicObject, tm, prefixes)); } } - + // get all instance_of topics of all topics for(int i = 0; i != topics.size(); ++i){ importTopicCharacteristicsFromTm(topics.get(i), jsonTopics.get(i).isObject(), tm, prefixes); @@ -403,10 +410,10 @@ Topic reifier = getReferenced(jtm.get("reifier"), tm, prefixes); tm.setReifier(reifier); } - + return tm; } - +
private Topic getReferenced(JSONValue reference, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference{ JSONString stringValue = reference.isString(); @@ -443,12 +450,12 @@
private ArrayList<Topic> getReferenceds(JSONValue references, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference { ArrayList<String> stringReferences = new ArrayList<String>(); - + JSONArray stringValues = references.isArray(); if(stringValues == null){ throw new FormatException("expected an array of strings as topic references, but found "" + references.toString() + """); } - + for(int i = 0; i != stringValues.size(); ++i){ JSONValue stringValue = stringValues.get(i); JSONString string = stringValue.isString(); @@ -458,22 +465,22 @@ stringReferences.add(string.stringValue()); } } - + return getReferenceds(stringReferences, tm, prefixes); } - - + + private ArrayList<Topic> getReferenceds(ArrayList<String> references, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference { ArrayList<Topic> values = new ArrayList<Topic>(); - + for(String reference : references){ values.add(getReferenced(reference, tm, prefixes)); } - + return values; } - - + + private String unCurie(String curie, Prefixes prefixes) throws FormatException{ if(curie.startsWith("[") && curie.endsWith("]")){ String[] strs = curie.substring(1).split(":"); @@ -513,7 +520,7 @@ for(int i = 1; i != subjectIdentifiers.size(); ++i){ top.addSubjectIdentifier(tm.createLocator(subjectIdentifiers.get(i))); } - + for (String itemIdentifier : itemIdentifiers) { top.addItemIdentifier(tm.createLocator(itemIdentifier)); } @@ -536,7 +543,7 @@ } else { throw new FormatException("a topic must have at least one identifier"); } - + return top; }
@@ -568,7 +575,7 @@
private ArrayList<JSONObject> getObjectsOfJSONArray(JSONObject object, String key) throws FormatException { ArrayList<JSONObject> values = new ArrayList<JSONObject>(); - + if(object.containsKey(key)){ JSONValue jsonValues = object.get(key); JSONArray jsonArray = jsonValues.isArray(); @@ -586,22 +593,22 @@ } } } - + return values; } - - + + private void importTopicCharacteristicsFromTm(Topic topic, JSONObject jsonTopic, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference{ // set instance_of of this topic for (String instanceOf : getStringsOfJSONArray(jsonTopic, "instance_of")) { topic.addType(getReferenced(instanceOf, tm, prefixes)); } - + // set all names of this topic for (JSONObject name : getObjectsOfJSONArray(jsonTopic, "names")) { importJTM11Name(topic, name, tm, prefixes); } - + // set all occurrences of this topic for (JSONObject occurrence : getObjectsOfJSONArray(jsonTopic, "occurrences")) { importJTM11Name(topic, occurrence, tm, prefixes); @@ -611,24 +618,24 @@
private Topic importJTM11Topic(JSONObject jtm, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference{ // the parent item is ignored, the passed tm is set as parent item - + Topic topic = importTopicStubFromTm(jtm, tm, prefixes); importTopicCharacteristicsFromTm(topic, jtm, tm, prefixes); return topic; } - - + + private static JsArray<Topic> toJsArray(ArrayList<Topic> topics){ JsArray<Topic> tops = null; - + for (Topic topic : topics) { tops = appendToJsArray(tops, topic); } - + return tops; } - - + + private static final native JsArray<Topic> appendToJsArray(JsArray<Topic> topics, Topic topic) /*-{ var array; if(topics === null){ @@ -639,11 +646,11 @@ array.push(topic); return array; }-*/; - - + + private ArrayList<Construct> getParents(JSONValue parentValue, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference{ ArrayList<Construct> values = new ArrayList<Construct>(); - + JSONArray parents = parentValue.isArray(); if(parents == null){ throw new FormatException("the field "parent" must be an array of item identifiers"); @@ -664,10 +671,10 @@ } } } - + return values; } - +
private Name importJTM11Name(Topic parent, JSONObject jtm, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference{ Topic topic = null; @@ -685,7 +692,7 @@ throw new FormatException("a name must contain the field "parent" with an array of string values"); } } - + // get name value String value = ""; if(jtm.containsKey("value")){ @@ -693,47 +700,47 @@ } else { throw new FormatException("a name must contain the field "value" with a string value"); } - + // get type Topic type = null; if(jtm.containsKey("type")){ type = getReferenced(jtm.get("type"), tm, prefixes); } - + // get scope ArrayList<Topic> scope = new ArrayList<Topic>(); if(jtm.containsKey("scope")){ scope = getReferenceds(jtm.get("scope"), tm, prefixes); } JsArray<Topic> jsScope = toJsArray(scope); - + Name name = null; if(type != null){ name = topic.createName(value, type, jsScope); } else { name = topic.createName(value, jsScope); } - + // get item_identifiers for (String ii : getStringsOfJSONArray(jtm, "item_identifiers")) { name.addItemIdentifier(tm.createLocator(unCurie(ii, prefixes))); } - + // set reifier if(jtm.containsKey("reifier")){ Topic reifier = getReferenced(jtm.get("reifier"), tm, prefixes); name.setReifier(reifier); } - + // get variants for (JSONObject variant : getObjectsOfJSONArray(jtm, "variants")) { importJTM11Variant(name, variant, tm, prefixes); } - + return name; } - - + + private String getStringValue(JSONObject jtm, String key) throws FormatException{ if(jtm.containsKey(key)){ JSONValue stringValue = jtm.get(key); @@ -747,7 +754,7 @@ return null; } } - +
private Variant importJTM11Variant(Name parent, JSONObject jtm, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference{ // set the variant parent @@ -766,7 +773,7 @@ throw new FormatException("a name must contain the field "parent" with an array of string values"); } } - + // get variant value String value = null; if(jtm.containsKey("value")){ @@ -774,13 +781,13 @@ } else { throw new FormatException("a name must contain the field "value" with a string value"); } - + // get variant datatype String datatype = xsdString; if(jtm.containsKey("value")){ datatype = unCurie(getStringValue(jtm, "datatype"), prefixes); } - + // get scope ArrayList<Topic> scope = new ArrayList<Topic>(); if(jtm.containsKey("scope")){ @@ -790,24 +797,24 @@ throw new FormatException("a variant must have at least on scope topic set"); } JsArray<Topic> jsScope = toJsArray(scope); - + Variant variant = name.createVariant(value, tm.createLocator(datatype), jsScope); - + // get item_identifiers for (String ii : getStringsOfJSONArray(jtm, "item_identifiers")) { variant.addItemIdentifier(tm.createLocator(unCurie(ii, prefixes))); } - + // set reifier if(jtm.containsKey("reifier")){ Topic reifier = getReferenced(jtm.get("reifier"), tm, prefixes); variant.setReifier(reifier); } - + return variant; }
- + private Occurrence importJTM11Occurrence(Topic parent, JSONObject jtm, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference{ // set occurrence parent Topic topic = null; @@ -825,7 +832,7 @@ throw new FormatException("a name must contain the field "parent" with an array of string values"); } } - + // get occurrence value String value = ""; if(jtm.containsKey("value")){ @@ -833,91 +840,91 @@ } else { throw new FormatException("a name must contain the field "value" with a string value"); } - + // get occurrence datatype String datatype = xsdString; if(jtm.containsKey("value")){ datatype = unCurie(getStringValue(jtm, "datatype"), prefixes); } - + // get occurrence type Topic type = null; if(jtm.containsKey("type")){ type = getReferenced(jtm.get("type"), tm, prefixes); } - + // get occurrence scope ArrayList<Topic> scope = new ArrayList<Topic>(); if(jtm.containsKey("scope")){ scope = getReferenceds(jtm.get("scope"), tm, prefixes); } JsArray<Topic> jsScope = toJsArray(scope); - + Occurrence occurrence = null; if(type == null){ throw new FormatException("an occurrence must contain the field "type" with a topic reference"); } else { occurrence = topic.createOccurrence(type, value, tm.createLocator(datatype), jsScope); } - + // get item_identifiers for (String ii : getStringsOfJSONArray(jtm, "item_identifiers")) { occurrence.addItemIdentifier(tm.createLocator(unCurie(ii, prefixes))); } - + // set reifier if(jtm.containsKey("reifier")){ Topic reifier = getReferenced(jtm.get("reifier"), tm, prefixes); occurrence.setReifier(reifier); } - + return occurrence; }
- + private Association importJTM11Association(JSONObject jtm, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference{ // the parent is ignored, instead the passed tm is used as parent item - + // get association type Topic type = null; if(jtm.containsKey("type")){ type = getReferenced(jtm.get("type"), tm, prefixes); } - + // get association scope ArrayList<Topic> scope = new ArrayList<Topic>(); if(jtm.containsKey("scope")){ scope = getReferenceds(jtm.get("scope"), tm, prefixes); } JsArray<Topic> jsScope = toJsArray(scope); - + Association association = null; if(type == null){ throw new FormatException("an occurrence must contain the field "type" with a topic reference"); } else { association = tm.createAssociation(type, jsScope); } - + // get association roles for (JSONObject role : getObjectsOfJSONArray(jtm, "roles")) { importJTM11Role(association, role, tm, prefixes); } - + // get item_identifiers for (String ii : getStringsOfJSONArray(jtm, "item_identifiers")) { association.addItemIdentifier(tm.createLocator(unCurie(ii, prefixes))); } - + // set reifier if(jtm.containsKey("reifier")){ Topic reifier = getReferenced(jtm.get("reifier"), tm, prefixes); association.setReifier(reifier); } - + return association; } - - + + private Role importJTM11Role(Association parent, JSONObject jtm, TopicMap tm, Prefixes prefixes) throws FormatException, MissingReference{ // set the variant parent Association association = null; @@ -935,86 +942,86 @@ throw new FormatException("a role must contain the field "parent" with an array of string values"); } } - + // get role player Topic player = null; if(jtm.containsKey("player")){ player = getReferenced(jtm.get("player"), tm, prefixes); } - + // get role type Topic type = null; if(jtm.containsKey("type")){ type = getReferenced(jtm.get("type"), tm, prefixes); } - + Role role = association.createRole(type, player); - + // get item_identifiers for (String ii : getStringsOfJSONArray(jtm, "item_identifiers")) { role.addItemIdentifier(tm.createLocator(unCurie(ii, prefixes))); } - + // set reifier if(jtm.containsKey("reifier")){ Topic reifier = getReferenced(jtm.get("reifier"), tm, prefixes); role.setReifier(reifier); } - + return role; }
- + 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"); } - - + + private String exportVersion11(){ return ""version":"1.1""; } - - + + private String exportValue(String value, String key){ return """ + key + "":" + escapeJSON(value); } - - + + 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"; @@ -1029,8 +1036,8 @@ return localValues + "]"; } } - - + + private String exportJsonArrayValues(ArrayList<String> jsonValues, String key){ if(jsonValues == null || jsonValues.size() == 0){ return """ + key + "":" + "null"; @@ -1045,30 +1052,30 @@ 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()); return "[" + qualifier + ":" + suffix + "]"; } - - + + 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(); @@ -1087,8 +1094,8 @@ return "null"; } } - - + + private String exportReferences(ArrayList<Topic> topics, Prefixes prefixes) throws ExporterException{ if(topics == null || topics.size() == 0){ return "null"; @@ -1105,49 +1112,49 @@ } }
- + 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. - + String json = "{";
// version json += exportVersion11() + ","; - + // prefixes json += exportJsonValue(prefixes.toJSON(), "prefixes") + ","; - + // item_type json += exportItemType(tm) + ","; - + // item_identifiers json += exportIdentifiers(tm.getItemIdentifiers(), "item_identifiers", prefixes) + ","; - + // reifier json += exportReference(tm.getReifier(), "reifier", prefixes) + ","; - + // topics ArrayList<String> topicStrings = new ArrayList<String>(); for(int i = 0; i != tm.getTopics().length(); ++i){ topicStrings.add(exportJTM11(tm.getTopics().get(i), prefixes, false)); } 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 += exportJsonArrayValues(associationStrings, "associations") + "}"; - + return json; } - - + + private String exportJTM11(Topic topic, Prefixes prefixes, boolean standalone) throws ExporterException{ String json = "{"; - + // the fields version, item_type, parent and prefixes are exported only if this // construct is exported as a standalone construct if(standalone){ @@ -1156,43 +1163,43 @@ json += exportItemType(topic) + ","; json += exportParent(topic.getParent(), prefixes) + ","; } - + // the fields subject_identifiers, subject_locators, item_identifiers, names, occurrences // and instance_of are always exported - + // identifiers json += exportIdentifiers(topic.getSubjectIdentifiers(), "subject_identifiers", prefixes) + ","; json += exportIdentifiers(topic.getSubjectLocators(), "subject_locators", prefixes) + ","; json += exportIdentifiers(topic.getItemIdentifiers(), "item_identifiers", prefixes) + ","; - + // names ArrayList<String> nameStrings = new ArrayList<String>(); for(int i = 0; i != topic.getNames().length(); ++i){ nameStrings.add(exportJTM11(topic.getNames().get(i), prefixes, false)); } 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 += exportJsonArrayValues(occurrenceStrings, "occurrences") + ","; - + // instance_of ArrayList<Topic> instanceOfs = new ArrayList<Topic>(); for(int i = 0; i != topic.getTypes().length(); ++i){ instanceOfs.add(topic.getTypes().get(i)); } json += exportReferences(instanceOfs, "instance_of", prefixes) + "}"; - + return json; }
- + private String exportJTM11(Name name, Prefixes prefixes, boolean standalone) throws ExporterException{ String json = "{"; - + // the fields version, item_type, parent and prefixes are exported only if this // construct is exported as a standalone construct if(standalone){ @@ -1201,40 +1208,40 @@ json += exportItemType(name) + ","; json += exportParent(name.getParent(), prefixes) + ","; } - + // the fields item_identifiers, reifier, scope, variants, value and type are always exported json += exportIdentifiers(name.getItemIdentifiers(), "item_identifiers", prefixes) + ","; - + // reifier json += exportReference(name.getReifier(), "reifier", prefixes) + ","; - + // scope ArrayList<Topic> scopeTopics = new ArrayList<Topic>(); for(int i = 0; i != name.getScope().length(); ++i){ scopeTopics.add(name.getScope().get(i)); } json += exportReferences(scopeTopics, "scope", prefixes) + ","; - + // variants ArrayList<String> variantStrings = new ArrayList<String>(); for(int i = 0; i != name.getVariants().length(); ++i){ variantStrings.add(exportJTM11(name.getVariants().get(i), prefixes, false)); } json += exportJsonArrayValues(variantStrings, "variants") + ","; - + // value json += exportValue(name.getValue(), "value") + ","; - + // type - a name my have null as a type json += exportReference(name.getType(), "type", prefixes) + "}"; - + return json; } - - + + private String exportJTM11(Variant variant, Prefixes prefixes, boolean standalone) throws ExporterException{ String json = "{"; - + // the fields version, item_type, parent and prefixes are exported only if this // construct is exported as a standalone construct if(standalone){ @@ -1243,33 +1250,33 @@ json += exportItemType(variant) + ","; json += exportParent(variant.getParent(), prefixes) + ","; } - + // the fields item_identifiers, reifier, scope, datatype, value are always exported json += exportIdentifiers(variant.getItemIdentifiers(), "item_identifiers", prefixes) + ","; - + // reifier json += exportReference(variant.getReifier(), "reifier", prefixes) + ","; - + // scope ArrayList<Topic> scopeTopics = new ArrayList<Topic>(); for(int i = 0; i != variant.getScope().length(); ++i){ scopeTopics.add(variant.getScope().get(i)); } json += exportReferences(scopeTopics, "scope", prefixes) + ","; - + // datatype json += exportValue(curie(variant.getDatatype(), prefixes), "datatype") + ","; - + // value json += exportValue(variant.getValue(), "value") + "}"; - + return json; } - - + + private String exportJTM11(Occurrence occurrence, Prefixes prefixes, boolean standalone) throws ExporterException{ String json = "{"; - + // the fields version, item_type, parent and prefixes are exported only if this // construct is exported as a standalone construct if(standalone){ @@ -1278,40 +1285,40 @@ json += exportItemType(occurrence) + ","; json += exportParent(occurrence.getParent(), prefixes) + ","; } - + // the fields item_identifiers, reifier, type, scope, datatype, value are always exported json += exportIdentifiers(occurrence.getItemIdentifiers(), "item_identifiers", prefixes) + ","; - + // reifier json += exportReference(occurrence.getReifier(), "reifier", prefixes) + ","; - + // type if(occurrence.getType() == null){ throw new ExporterException("a role must have a player set"); } json += exportReference(occurrence.getType(), "type", prefixes) + ","; - + // scope ArrayList<Topic> scopeTopics = new ArrayList<Topic>(); for(int i = 0; i != occurrence.getScope().length(); ++i){ scopeTopics.add(occurrence.getScope().get(i)); } json += exportReferences(scopeTopics, "scope", prefixes) + ","; - + // datatype json += exportValue(curie(occurrence.getDatatype(), prefixes), "datatype") + ","; - + // value json += exportValue(occurrence.getValue(), "value") + "}"; - + return json; } - - + + private String exportParent(Topic parent, Prefixes prefixes) throws ExporterException { Locator parentIdentifier = null; String idType = "ii:"; - + if(parent.getSubjectIdentifiers().length() != 0){ parentIdentifier = parent.getSubjectIdentifiers().get(0); idType = "si:"; @@ -1321,35 +1328,35 @@ } else if(parent.getItemIdentifiers().length() != 0){ parentIdentifier = parent.getItemIdentifiers().get(0); } - + if(parentIdentifier == null){ throw new ExporterException("a parent must have at least one identifeir to be serailised"); } else { return ""parent":["" + idType + curie(parentIdentifier, prefixes) + ""]"; } } - - + + 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 = "{"; - + // the fields version, item_type, parent and prefixes are exported only if this // construct is exported as a standalone construct if(standalone){ @@ -1358,50 +1365,50 @@ json += exportItemType(association) + ","; json += exportParent(association.getParent(), prefixes) + ","; } - + // the fields item_identifiers, reifier, scope, roles and type are always exported json += exportIdentifiers(association.getItemIdentifiers(), "item_identifiers", prefixes) + ","; - + // reifier json += exportReference(association.getReifier(), "reifier", prefixes) + ","; - + // scope ArrayList<Topic> scopeTopics = new ArrayList<Topic>(); for(int i = 0; i != association.getScope().length(); ++i){ scopeTopics.add(association.getScope().get(i)); } json += exportReferences(scopeTopics, "scope", prefixes) + ","; - + // roles ArrayList<String> roleStrings = new ArrayList<String>(); for(int i = 0; i != association.getRoles().length(); ++i){ roleStrings.add(exportJTM11(association.getRoles().get(i), prefixes, false)); } json += exportJsonArrayValues(roleStrings, "roles") + ","; - + // type if(association.getType() == null){ throw new ExporterException("an association must have a player set"); } json += exportReference(association.getType(), "type", prefixes) + "}"; - + return json; } - - + + 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 exportJsonArrayValues(curies, key); } - - + + private String exportJTM11(Role role, Prefixes prefixes, boolean standalone) throws ExporterException{ String json = "{"; - + // the fields version, item_type, parent and prefixes are exported only if this // construct is exported as a standalone construct if(standalone){ @@ -1410,34 +1417,34 @@ json += exportItemType(role) + ","; json += exportParent(role.getParent(), prefixes) + ","; } - + // the fields item_identifiers, reifier, player and type are always exported json += exportIdentifiers(role.getItemIdentifiers(), "item_identifiers", prefixes) + ","; - + // reifier json += exportReference(role.getReifier(), "reifier", prefixes) + ","; - + // player if(role.getPlayer() == null){ throw new ExporterException("a role must have a player set"); } json += exportReference(role.getPlayer(), "player", prefixes) + ","; - + // type if(role.getType() == null){ throw new ExporterException("a role must have a player set"); } json += exportReference(role.getType(), "type", prefixes) + "}"; - + return json; } - - + + private static String escapeJSON(String json){ return JsonUtils.escapeValue(json).replaceAll("/", "\/"); }
- + private class Prefixes { private ArrayList<String[]> prefixes = new ArrayList<String[]>();
@@ -1468,211 +1475,211 @@ } }
- + 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); } - - + + private void initPrefixes(ArrayList<String[]> prefixes, TopicMap tm) throws ExporterException{ // item_identifiers addPrefixesOfIdentifiers(prefixes, tm.getItemIdentifiers()); - + // reifier addPrefixOfTopicReference(prefixes, tm.getReifier()); - + // topics for(int i = 0; i != tm.getTopics().length(); ++i){ initPrefixes(prefixes, tm.getTopics().get(i)); } - + // associations for(int i = 0; i != tm.getAssociations().length(); ++i){ initPrefixes(prefixes, tm.getAssociations().get(i)); } } - - + + private void initPrefixes(ArrayList<String[]> prefixes, Topic topic) throws ExporterException{ // parent addPrefixOfReifiableReference(prefixes, topic.getTopicMap()); - + // subject_identifiers addPrefixesOfIdentifiers(prefixes, topic.getSubjectIdentifiers()); - + // subject_locators addPrefixesOfIdentifiers(prefixes, topic.getSubjectLocators()); - + // item_identifeirs addPrefixesOfIdentifiers(prefixes, topic.getItemIdentifiers()); - + // names for(int i = 0; i != topic.getNames().length(); ++i){ initPrefixes(prefixes, topic.getNames().get(i)); } - + // occurrences for(int i = 0; i != topic.getOccurrences().length(); ++i){ initPrefixes(prefixes, topic.getOccurrences().get(i)); } } - - + + private void initPrefixes(ArrayList<String[]> prefixes, Name name) throws ExporterException{ if(null == name.getParent()){ throw new ExporterException("a name must have a parent set"); } - + // parent addPrefixOfTopicReference(prefixes, name.getParent()); - + // item_idenifiers addPrefixesOfIdentifiers(prefixes, name.getItemIdentifiers()); - + // reifier addPrefixOfTopicReference(prefixes, name.getReifier()); - + // scope addPrefixesOfTopicReferences(prefixes, name.getScope()); - + // type addPrefixOfTopicReference(prefixes, name.getType()); - + // variants for(int i = 0; i != name.getVariants().length(); ++i){ initPrefixes(prefixes, name.getVariants().get(i)); } } - - + + private void initPrefixes(ArrayList<String[]> prefixes, Variant variant) throws ExporterException{ if(null == variant.getParent() || null == variant.getScope() || variant.getScope().length() == 0){ throw new ExporterException("a variant must have a parent and at least one scope set"); } - + // parent addPrefixOfReifiableReference(prefixes, variant.getParent()); - + // item_idenifiers addPrefixesOfIdentifiers(prefixes, variant.getItemIdentifiers()); - + // reifier addPrefixOfTopicReference(prefixes, variant.getReifier()); - + // scope addPrefixesOfTopicReferences(prefixes, variant.getScope()); } - - + + private void initPrefixes(ArrayList<String[]> prefixes, Occurrence occurrence) throws ExporterException{ if(null == occurrence.getParent() || null == occurrence.getType()){ throw new ExporterException("an occurrence must have a parent and type set"); } - + // parent addPrefixOfTopicReference(prefixes, occurrence.getParent()); - + // item_idenifiers addPrefixesOfIdentifiers(prefixes, occurrence.getItemIdentifiers()); - + // reifier addPrefixOfTopicReference(prefixes, occurrence.getReifier()); - + // scope addPrefixesOfTopicReferences(prefixes, occurrence.getScope()); - + // type addPrefixOfTopicReference(prefixes, occurrence.getType()); } - - + + private void initPrefixes(ArrayList<String[]> prefixes, Association association) throws ExporterException{ if(null == association.getParent() || null == association.getType() || null == association.getRoles() || association.getRoles().length() == 0){ throw new ExporterException("an association must have a parent, type and role set"); } - + // parent addPrefixOfReifiableReference(prefixes, association.getParent()); - + // item_idenifiers addPrefixesOfIdentifiers(prefixes, association.getItemIdentifiers()); - + // reifier addPrefixOfTopicReference(prefixes, association.getReifier()); - + // scope addPrefixesOfTopicReferences(prefixes, association.getScope()); - + // type addPrefixOfTopicReference(prefixes, association.getType()); - + // roles for(int i = 0; i != association.getRoles().length(); ++i){ initPrefixes(prefixes, association.getRoles().get(i)); } } - - + + private void initPrefixes(ArrayList<String[]> prefixes, Role role) throws ExporterException{ if(null == role.getParent() || null == role.getType() || null == role.getPlayer()){ throw new ExporterException("a role must have a parent, type and player set"); } - + // item_identifiers addPrefixesOfIdentifiers(prefixes, role.getItemIdentifiers()); - + // reifier addPrefixOfTopicReference(prefixes, role.getReifier()); - + // player addPrefixOfTopicReference(prefixes, role.getPlayer()); - + // parent addPrefixOfReifiableReference(prefixes, role.getParent()); - + // type addPrefixOfTopicReference(prefixes, role.getType()); } - - + + private void addPrefixOfReifiableReference(ArrayList<String[]> prefixes, Reifiable reifiable){ if(reifiable != null){ ReifiableStub reifiableStub = (ReifiableStub)reifiable; @@ -1684,7 +1691,7 @@ } }
- + private void addPrefixOfTopicReference(ArrayList<String[]> prefixes, Topic topic) throws ExporterException{ if(topic != null){ Locator loc = null; @@ -1702,8 +1709,8 @@ addPrefixOfIdentifier(prefixes, loc); } } - - + + private void addPrefixesOfTopicReferences(ArrayList<String[]> prefixes, JsArray<Topic> topics) throws ExporterException { if(topics != null) { for(int i = 0; i != topics.length(); ++i){ @@ -1711,8 +1718,8 @@ } } } - - + + private void addIfNew(ArrayList<String[]> prefixes, String uriPrefix){ if(prefixes != null){ for (String[] item : prefixes) { @@ -1723,14 +1730,14 @@ prefixes.add(new String[]{"pref_" + prefixes.size(), uriPrefix}); } } - - + + private void addPrefixOfIdentifier(ArrayList<String[]> prefixes, Locator loc){ String uriPrefix = splitUriByLastFragment(loc.getReference())[0]; addIfNew(prefixes, uriPrefix); } - - + + private void addPrefixesOfIdentifiers(ArrayList<String[]> prefixes, JsArray<Locator> locs){ if(locs != null){ for(int i = 0; i != locs.length(); ++i) { @@ -1750,7 +1757,7 @@ return null; }
- + private String[] splitUriByLastFragment(String uri) { int idxSlash = uri.lastIndexOf("/"); int idxSharp = uri.lastIndexOf("#"); @@ -1758,25 +1765,25 @@ String prefix = uri.substring(0, lastPos); String suffix = uri.substring(lastPos); suffix = suffix == null ? "" : suffix; - + return new String[]{prefix, suffix}; } - - + + public String getQualifier(String uri) throws ExporterException{ String prefix = splitUriByLastFragment(uri)[0]; - + for (String[] item : prefixes) { if(item[1].equals(prefix)){ return item[0]; } } - + // no match => throw an exception throw new ExporterException("could not found a qualifier for the name space "" + uri + """); } - - + + public String getFullUri(String qualifier, String suffix){ String ns = getNs(qualifier); if(ns != null){ @@ -1785,8 +1792,8 @@ return null; } } - - + + public String toJSON(){ String value = "{"; for(int i = 0; i != prefixes.size(); ++i){ @@ -1797,7 +1804,7 @@ value += ","; } } - + return value; } }
Modified: branches/gdl-frontend/src/anaToMia/GDL_TmEngine/test/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/JtmsTmEngineTest.java ============================================================================== --- branches/gdl-frontend/src/anaToMia/GDL_TmEngine/test/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/JtmsTmEngineTest.java Mon Jun 27 04:47:25 2011 (r511) +++ branches/gdl-frontend/src/anaToMia/GDL_TmEngine/test/us/isidor/gdl/anaToMia/TmEngine/jtmsBasedEngine/JtmsTmEngineTest.java Mon Jun 27 05:20:08 2011 (r512) @@ -3,6 +3,7 @@ import java.util.ArrayList;
import us.isidor.gdl.anaToMia.TopicMaps.TopicMapsModel.Association; +import us.isidor.gdl.anaToMia.TopicMaps.TopicMapsModel.Name; import us.isidor.gdl.anaToMia.TopicMaps.TopicMapsModel.Topic; import us.isidor.gdl.anaToMia.TopicMaps.TopicMapsModel.TopicMap;
@@ -114,6 +115,50 @@ assertEquals(0, assoc1.getScope().length()); assertEquals(2, assoc2.getScope().length()); } + + + public void testName(){ + jtme = new JtmsTmEngine(); + + TopicMap tm = jtme.createTopicMap(tmLocator1); + + Topic top1 = tm.createTopicBySubjectLocator(tm.createLocator(slLocator1)); + Topic theme1 = tm.createTopic(); + Topic theme2 = tm.createTopic(); + Topic theme3 = tm.createTopic(); + Topic type = tm.createTopic(); + ArrayList<Topic> scope = new ArrayList<Topic>(); + scope.add(theme1); + scope.add(theme2); + + Topic defaultNameType = jtme.createDefaultNameType(tm); + assertNotNull(defaultNameType); + Name name1 = top1.createName("name 1", appendToJsArray(null, null));//appendToJsArray(null, null)); + Name name2 = top1.createName("name 2", createJsArray(scope)); + Name name3 = top1.createName("name 3", type, appendToJsArray(null, null)); + Name name4 = top1.createName("name 4", type, createJsArray(scope)); + name4.addItemIdentifier(tm.createLocator(iiLocator1)); + name4.addTheme(theme3); + + assertEquals(4, top1.getNames().length()); + assertEquals(2, top1.getNames(type).length()); + assertEquals(top1, name1.getParent()); + assertEquals(top1, name4.getParent()); + assertEquals(tm, name2.getTopicMap()); + assertEquals(tm, name3.getTopicMap()); + assertEquals("name 1", name1.getValue()); + assertEquals(1, name4.getItemIdentifiers().length()); + assertEquals(iiLocator1, name4.getItemIdentifiers().get(0).getReference()); + assertEquals(3, name4.getScope().length()); + } + + // TODO: Name + // TODO: Variant + // TODO: Occurrence + // TODO: Association + // TODO: Role + // TODO: JTM 1.1 import + // TODO: JTM 1.1 export
private JsArray<Topic> createJsArray(ArrayList<Topic> topics){