XsltTransformer implements RawDataTransformer { @Override public String transform(String input) throws IOException, URISyntaxException, TransformerException { StreamSource xslt = new StreamSource(new ByteArrayInputStream(xsltContent.getBytes())); Transformer transformer = factory.newTransformer(xslt); Source text = new StreamSource(new ByteArrayInputStream(input.getBytes())); ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length()); transformer.transform(text, new StreamResult(baos)); return new String(baos.toByteArray()); } XsltTransformer(String xsltContent); @Override String transform(String input);  }
@Test public void testRecursiveCopyExtraRules() throws Exception { String xsltTemplate = ResourceUtils.create(XsltTransformerTest.class).getResourceAsString(SAMPLE_TRANSFORMER_RECURSIVE_COPY); String xslt = TemplateProcessor.processTemplateContents(xsltTemplate, ImmutableMap.of( "extra_rules", "<xsl:template match=\"nested\"><empty_nest/></xsl:template>")); String input = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype</type>"+NEWLINE+ " <nested>"+NEWLINE+ " <type myattrib3=\"myval3\">foo</type>"+NEWLINE+ " bar"+NEWLINE+ " </nested>"+NEWLINE+ " <id>myid</id>"+NEWLINE+ "</entity>"; String expected = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype</type>"+NEWLINE+ " <empty_nest/>"+NEWLINE+ " <id>myid</id>"+NEWLINE+ "</entity>"; XsltTransformer transformer = new XsltTransformer(xslt); String result = transformer.transform(input); assertEquals(result, expected); }
@Test public void testRenameType() throws Exception { String xsltTemplate = ResourceUtils.create(XsltTransformerTest.class).getResourceAsString("classpath: String xslt = TemplateProcessor.processTemplateContents(xsltTemplate, ImmutableMap.of("old_val", "mytype.Before", "new_val", "mytype.After")); String input = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.Before</type>"+NEWLINE+ " <nested>"+NEWLINE+ " <type myattrib3=\"myval3\">doesNotMatch</type>"+NEWLINE+ " <type myattrib4=\"myval4\">partial.mytype.Before</type>"+NEWLINE+ " <type myattrib5=\"myval5\">mytype.Before</type>"+NEWLINE+ " </nested>"+NEWLINE+ " <id>myid</id>"+NEWLINE+ "</entity>"; String expected = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.After</type>"+NEWLINE+ " <nested>"+NEWLINE+ " <type myattrib3=\"myval3\">doesNotMatch</type>"+NEWLINE+ " <type myattrib4=\"myval4\">partial.mytype.Before</type>"+NEWLINE+ " <type myattrib5=\"myval5\">mytype.After</type>"+NEWLINE+ " </nested>"+NEWLINE+ " <id>myid</id>"+NEWLINE+ "</entity>"; XsltTransformer transformer = new XsltTransformer(xslt); String result = transformer.transform(input); assertEquals(result, expected); }
@Test public void testRenameField() throws Exception { String xsltTemplate = ResourceUtils.create(XsltTransformerTest.class).getResourceAsString("classpath: String xslt = TemplateProcessor.processTemplateContents(xsltTemplate, ImmutableMap.of("class_name", "MyClass", "old_val", "myFieldBefore", "new_val", "myFieldAfter")); String input = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.Before</type>"+NEWLINE+ " <config>"+NEWLINE+ " <test.conf1>"+NEWLINE+ " <MyClass>"+NEWLINE+ " <myFieldBefore class=\"string\">myfieldval</myFieldBefore>"+NEWLINE+ " </MyClass>"+NEWLINE+ " </test.conf1>"+NEWLINE+ " <test.conf2>"+NEWLINE+ " <MyOtherClass>"+NEWLINE+ " <myFieldBefore class=\"string\">myfieldval</myFieldBefore>"+NEWLINE+ " </MyOtherClass>"+NEWLINE+ " </test.conf2>"+NEWLINE+ " </config>"+NEWLINE+ "</entity>"; String expected = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.Before</type>"+NEWLINE+ " <config>"+NEWLINE+ " <test.conf1>"+NEWLINE+ " <MyClass>"+NEWLINE+ " <myFieldAfter class=\"string\">myfieldval</myFieldAfter>"+NEWLINE+ " </MyClass>"+NEWLINE+ " </test.conf1>"+NEWLINE+ " <test.conf2>"+NEWLINE+ " <MyOtherClass>"+NEWLINE+ " <myFieldBefore class=\"string\">myfieldval</myFieldBefore>"+NEWLINE+ " </MyOtherClass>"+NEWLINE+ " </test.conf2>"+NEWLINE+ " </config>"+NEWLINE+ "</entity>"; XsltTransformer transformer = new XsltTransformer(xslt); String result = transformer.transform(input); assertEquals(result, expected); }
@Test public void testRenameClass() throws Exception { String xsltTemplate = ResourceUtils.create(XsltTransformerTest.class).getResourceAsString("classpath: String xslt = TemplateProcessor.processTemplateContents(xsltTemplate, ImmutableMap.of("old_val", "MyClassBefore", "new_val", "MyClassAfter")); String input = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.Before</type>"+NEWLINE+ " <config>"+NEWLINE+ " <test.conf1>"+NEWLINE+ " <MyClassBefore>"+NEWLINE+ " </MyClassBefore>"+NEWLINE+ " </test.conf1>"+NEWLINE+ " </config>"+NEWLINE+ "</entity>"; String expected = "<entity myattrib=\"myval\">"+NEWLINE+ " <type myattrib2=\"myval2\">mytype.Before</type>"+NEWLINE+ " <config>"+NEWLINE+ " <test.conf1>"+NEWLINE+ " <MyClassAfter>"+NEWLINE+ " </MyClassAfter>"+NEWLINE+ " </test.conf1>"+NEWLINE+ " </config>"+NEWLINE+ "</entity>"; XsltTransformer transformer = new XsltTransformer(xslt); String result = transformer.transform(input); assertEquals(result, expected); }