SpringBeanService { public void updateBeanDefinition(File configFile, Project project, String id, Object jaxbElement) { Source xsltSource; Source xmlSource; try { xsltSource = new StreamSource(new ClassPathResource("transform/update-bean.xsl").getInputStream()); xsltSource.setSystemId("update-bean"); List<File> configFiles = new ArrayList<>(); configFiles.add(configFile); configFiles.addAll(getConfigImports(configFile, project)); LSParser parser = XMLUtils.createLSParser(); GetSpringBeanFilter getBeanFilter = new GetSpringBeanFilter(id, jaxbElement.getClass()); parser.setFilter(getBeanFilter); for (File file : configFiles) { parser.parseURI(file.toURI().toString()); if (getBeanFilter.getBeanDefinition() != null) { String source = FileUtils.readToString(new FileInputStream(file)); xmlSource = new StringSource(source); Transformer transformer = transformerFactory.newTransformer(xsltSource); transformer.setParameter("bean_id", id); transformer.setParameter("bean_content", getXmlContent(jaxbElement) .replaceAll("(?m)^(\\s<)", getTabs(1, project.getSettings().getTabSize()) + "$1") .replaceAll("(?m)^(</)", getTabs(1, project.getSettings().getTabSize()) + "$1")); StringResult result = new StringResult(); transformer.transform(xmlSource, result); FileUtils.writeToFile(format(postProcess(source, result.toString()), project.getSettings().getTabSize()), file); return; } } } catch (IOException e) { throw new ApplicationRuntimeException(UNABLE_TO_READ_TRANSFORMATION_SOURCE, e); } catch (TransformerException e) { throw new ApplicationRuntimeException(FAILED_TO_UPDATE_BEAN_DEFINITION, e); } }  List<File> getConfigImports(File configFile, Project project); T getBeanDefinition(File configFile, Project project, String id, Class<T> type); List<T> getBeanDefinitions(File configFile, Project project, Class<T> type); List<T> getBeanDefinitions(File configFile, Project project, Class<T> type, Map<String, String> attributes); List<String> getBeanNames(File configFile, Project project, String beanType); void addBeanDefinition(File configFile, Project project, Object jaxbElement); void removeBeanDefinition(File configFile, Project project, String id); void removeBeanDefinitions(File configFile, Project project, Class<?> type); void removeBeanDefinitions(File configFile, Project project, Class<?> type, String attributeName, String attributeValue); void updateBeanDefinition(File configFile, Project project, String id, Object jaxbElement); void updateBeanDefinitions(File configFile, Project project, Class<?> type, Object jaxbElement); void updateBeanDefinitions(File configFile, Project project, Class<?> type, Object jaxbElement, String attributeName, String attributeValue); void setSpringBeanMarshaller(SpringBeanMarshaller springBeanMarshaller); static final String FAILED_TO_UPDATE_BEAN_DEFINITION; static final String UNABLE_TO_READ_TRANSFORMATION_SOURCE; }
@Test public void testUpdateBeanDefinition() throws Exception { File tempFile = createTempContextFile("citrus-context-update"); SchemaModel helloSchema = new SchemaModelBuilder().withId("helloSchema").withLocation("newLocation").build(); springBeanConfigService.updateBeanDefinition(tempFile, project, "helloSchema", helloSchema); String result = FileUtils.readToString(new FileInputStream(tempFile)); Assert.assertTrue(result.contains("<citrus:schema id=\"helloSchema\" location=\"newLocation\"/>"), "Failed to validate " + result); Assert.assertTrue(result.contains("<property name=\"helloSchema\" value=\"some\"/>"), "Failed to validate " + result); Assert.assertTrue(result.contains("<!-- This is a comment -->"), "Failed to validate " + result); Assert.assertTrue(result.contains("<![CDATA[" + System.lineSeparator() + " some" + System.lineSeparator() + " ]]>" + System.lineSeparator()), "Failed to validate " + result); Assert.assertTrue(result.contains("<![CDATA[" + System.lineSeparator() + " <some>" + System.lineSeparator() + " <text>This is a CDATA text</text>" + System.lineSeparator()), "Failed to validate " + result); }