LTMTopicMapWriter implements TopicMapWriterIF { @Override public void write(TopicMapIF tm) throws IOException { LocatorIF baseLocator = tm.getStore().getBaseAddress(); base = (baseLocator == null) ? null : baseLocator.getExternalForm(); idManager = new IdManager(); ClassInstanceIndexIF classIndex = (ClassInstanceIndexIF)tm.getIndex( "net.ontopia.topicmaps.core.index.ClassInstanceIndexIF"); boolean existsUntyped = (hasUnfiltered(classIndex.getAssociationRoles(null)) || hasUnfiltered(classIndex.getAssociations(null)) || hasUnfiltered(classIndex .getOccurrences(null))); Collection<TopicIF> topics = tm.getTopics(); recordIds(topics); topics = sort(filterCollection(topics), topicComparator); TopicIF reifier = tm.getReifier(); Collection<TopicIF> tmReifiers = (reifier == null ? Collections.<TopicIF>emptySet() : Collections.singleton(reifier)); tmReifiers = filterCollection(tmReifiers); topics.removeAll(tmReifiers); Collection<AssociationIF> allAssociations = filterCollection(tm.getAssociations()); boolean existsUnspecified = existsUnspecifiedRolePlayer(allAssociations); Collection<TopicIF> topicInstances = sort(topics, topicComparator); Collection<TopicIF> topicTypes = topicTypes(topics, topicInstances); Collection<TopicIF> roleTypes = roleTypes(allAssociations, topicInstances); Collection<TopicIF> associationTypes = associationTypes(allAssociations, topicInstances); Collection<TopicIF> occurrenceTypes = occurrenceTypes(topics, topicInstances); countRoles(allAssociations); allAssociations = sort(allAssociations, associationComparator); TopicIF supersubtype = tm.getTopicBySubjectIdentifier(PSI .getXTMSuperclassSubclass()); Collection<AssociationIF> supersubAssociations = classIndex.getAssociations(supersubtype); supersubAssociations = filterCollection(supersubAssociations); allAssociations.removeAll(supersubAssociations); Collection<AssociationIF> tmReifierAssociations = playerAssociations(tmReifiers, allAssociations); if (encoding != null) out.write("@\"" + encoding + "\"\n"); out.write("#VERSION \"1.3\"\n"); out.write("\n"); for (String key : prefixes.keySet()) { out.write("#PREFIX " + key + " @\"" + prefixes.get(key) + "\"\n"); } if (existsUntyped) out.write("\n#PREFIX untyped @\"http: if (existsUnspecified) { out.write("\n#PREFIX unspecified"); out.write(" @\"http: } Iterator<TopicIF> tmReifiersIt = tmReifiers.iterator(); if (!tmReifiers.isEmpty()) { out.write("\n\n"); out.write("\n#TOPICMAP"); writeReifiers(tm, out); out.write("\n"); groupString1 = ""; tmReifiersIt = tmReifiers.iterator(); while (tmReifiersIt.hasNext()) writeTopic(tmReifiersIt.next(), out, false); } groupString1 = ""; Iterator<AssociationIF> tmReifierAssociationsIt = tmReifierAssociations.iterator(); while (tmReifierAssociationsIt.hasNext()) writeAssociation(tmReifierAssociationsIt.next(), out, false); out.write("\n\n"); out.write("\n\n"); writeTopics(topicTypes); out.write("\n\n"); groupString1 = ""; Iterator<AssociationIF> hierarchyIt = sort(supersubAssociations, supersubComparator) .iterator(); if (hierarchyIt.hasNext()) out.write("\n"); while (hierarchyIt.hasNext()) { AssociationIF currentAssociation = hierarchyIt.next(); writeSupersub(currentAssociation, out); } out.write("\n\n"); writeTopics(roleTypes); out.write("\n\n"); writeTopics(associationTypes); out.write("\n\n"); writeTopics(occurrenceTypes); out.write("\n\n"); out.write("\n\n"); writeTopics(topicInstances); out.write("\n\n"); groupString1 = ""; Iterator<AssociationIF> associationsIt = allAssociations.iterator(); while (associationsIt.hasNext()) writeAssociation(associationsIt.next(), out); out.flush(); if (closeWriter) { out.close(); } } LTMTopicMapWriter(File file);  LTMTopicMapWriter(File file, String encoding);  LTMTopicMapWriter(OutputStream stream);  LTMTopicMapWriter(OutputStream stream, String encoding);  LTMTopicMapWriter(Writer out, String encoding); void setPreserveIds(boolean preserveIds); void setFilter(DeciderIF<Object> filter); boolean addPrefix(String key, String prefix); @Override void write(TopicMapIF tm); @SuppressWarnings("unchecked") @Override void setAdditionalProperties(Map<String, Object> properties); static final String PROPERTY_PREFIXES; static final String PROPERTY_FILTER; static final String PROPERTY_PRESERVE_IDS; }
@Test public void testBadId() throws IOException { LocatorIF base = new URILocator("http: TopicIF topic = builder.makeTopic(); topic.addItemIdentifier(base.resolveAbsolute("#22")); String root = TestFileUtils.getTestdataOutputDirectory(); TestFileUtils.verifyDirectory(root, testdataDirectory); String thebase = root + File.separator + testdataDirectory + File.separator; TestFileUtils.verifyDirectory(thebase, "out"); File file = new File(thebase + File.separator + "out" + File.separator + "testBadId.ltm"); new LTMTopicMapWriter(file).write(tm); tm = new LTMTopicMapReader(file).read(); topic = (TopicIF) tm.getTopics().iterator().next(); LocatorIF itemid = (LocatorIF) topic.getItemIdentifiers().iterator().next(); Assert.assertTrue("Bad item ID was not filtered out", itemid.getAddress().endsWith("testBadId.ltm#id1")); }
@Test public void testWriteToFile() throws IOException { builder.makeTopic(); File file = TestFileUtils.getTestOutputFile("ltm", "io-f.ltm"); new LTMTopicMapWriter(file).write(tm); Assert.assertTrue(Files.size(file.toPath()) > 0); }
@Test public void testWriteToOutputStream() throws IOException { builder.makeTopic(); File file = TestFileUtils.getTestOutputFile("ltm", "io-o.ltm"); new LTMTopicMapWriter(new FileOutputStream(file)).write(tm); Assert.assertTrue(Files.size(file.toPath()) > 0); }
@Test public void testWriteToWriter() throws IOException { builder.makeTopic(); File file = TestFileUtils.getTestOutputFile("ltm", "io-w.ltm"); new LTMTopicMapWriter(new FileWriter(file), "utf-8").write(tm); Assert.assertTrue(Files.size(file.toPath()) > 0); }