EntityReferenceReplacer { public String replaceEntityReferences(final Connection connection, final String data) { String newData = data; for (final String container : CONTAINERS) { Pattern pattern = Pattern.compile("<" + container + ">\\s*<type>.*?</type>.*?</" + container + ">", Pattern.DOTALL); Matcher matcher = pattern.matcher(data); while (matcher.find()) { try { final String ref = data.substring(matcher.start(), matcher.end()); final String type = getString(ref, "type"); final String id = getString(ref, "id"); final String name = getString(ref, "name"); final String path = getString(ref, "path"); if (type != null) { final StringBuilder sb = new StringBuilder(); switch (type) { case "TextConverter": appendReference(connection, container, "TXT_CONV", id, type, name, path, sb); break; case "XSLT": appendReference(connection, container, "XSLT", id, type, name, path, sb); break; case "Feed": appendReference(connection, container, "FD", id, type, name, path, sb); break; case "StreamType": if (container.equals("streamType")) { sb.append("<"); sb.append(container); sb.append(">"); if (name != null) { sb.append(name); } else { sb.append(path); } sb.append("</"); sb.append(container); sb.append(">"); } else { sb.append("<string>"); if (name != null) { sb.append(name); } else { sb.append(path); } sb.append("</string>"); } break; case "Pipeline": appendReference(connection, container, "PIPE", id, type, name, path, sb); break; case "Index": appendReference(connection, container, "IDX", id, type, name, path, sb); break; case "Visualisation": appendReference(connection, container, "VIS", id, type, name, path, sb); break; case "StatisticStore": appendReference(connection, container, "STAT_DAT_SRC", id, "StatisticStore", name, path, sb); break; case "StatisticsDataSource": appendReference(connection, container, "STAT_DAT_SRC", id, "StatisticStore", name, path, sb); break; default: LOGGER.error("Unable to perform entity replacement for unknown type '" + type + "'"); } newData = newData.replace(ref, sb.toString()); } } catch (final RuntimeException e) { LOGGER.error(e.getMessage(), e); } } } return newData; }  String replaceEntityReferences(final Connection connection, final String data);  }
@Test void testEntityStreamType() { final String original = "<property><element>test</element><name>streamType</name><value><entity><type>StreamType</type><path>Events</path></entity></value></property>"; final String expected = "<property><element>test</element><name>streamType</name><value><string>Events</string></value></property>"; final String actual = new Replacer().replaceEntityReferences(null, original); assertThat(actual).isEqualTo(expected); }
@Test void testReferenceStreamType() { final String original = "<reference><streamType><type>StreamType</type><path>Reference</path></streamType></reference>"; final String expected = "<reference><streamType>Reference</streamType></reference>"; final String actual = new Replacer().replaceEntityReferences(null, original); assertThat(actual).isEqualTo(expected); }
@Test void testEntity1() { final String original = "<property><element>test</element><name>xslt</name><value><entity><type>XSLT</type><id>1234</id><path>/Test/Test XSLT</path></entity></value></property>"; final String expected = "<property><element>test</element><name>xslt</name><value><entity><type>XSLT</type><uuid>uuid</uuid><name>Test XSLT</name></entity></value></property>"; final String actual = new Replacer().replaceEntityReferences(null, original); assertThat(actual).isEqualTo(expected); }
@Test void testEntity2() { final String original = "<property><element>test</element><name>xslt</name><value><entity><type>XSLT</type><id>1234</id><name>Test XSLT</name></entity></value></property>"; final String expected = "<property><element>test</element><name>xslt</name><value><entity><type>XSLT</type><uuid>uuid</uuid><name>Test XSLT</name></entity></value></property>"; final String actual = new Replacer().replaceEntityReferences(null, original); assertThat(actual).isEqualTo(expected); }
@Test void fullTest() { final InputStream originalIS = getClass().getResourceAsStream("Sample.Pipeline.data.xml"); final String original = StreamUtil.streamToString(originalIS); final InputStream updatedIS = getClass().getResourceAsStream("Updated.Pipeline.data.xml"); final String updated = StreamUtil.streamToString(updatedIS); final String actual = new Replacer().replaceEntityReferences(null, original); assertThat(actual).isEqualTo(updated); }