package
org.springframework.schema.beans;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.OutputStream;
import
java.net.URISyntaxException;
import
java.util.ArrayList;
import
javax.xml.bind.JAXBContext;
import
javax.xml.bind.JAXBException;
import
javax.xml.bind.Marshaller;
import
javax.xml.namespace.QName;
import
org.springframework.util.CollectionUtils;
import
com.hrb.leap.bean.SpringConfigGenerator;
public
class
Test
{
public
static
void
main(String[] args)
throws
JAXBException
{
ObjectFactory factory =
new
ObjectFactory();
JAXBContext context = JAXBContext.newInstance("org.springframework.schema.beans");
Beans rootElement = factory.createBeans();
rootElement.getOtherAttributes().put(
new
QName
("xmlns:xsi"), "http:
rootElement.getOtherAttributes().put(
new
QName
("xsi:schemaLocation"),
"http:
java.util.List<String> resources =
new
ArrayList<>();
resources.add("ResourceOne");
resources.add("ResourceTwo");
resources.add("ResourceThree");
resources.forEach(resourceName ->
{
Import importResource =
new
Import();
importResource.setResource(resourceName+".xml");
rootElement.getImportOrAliasOrBean().add(importResource);
});
Bean bean = factory.createBean();
bean.setId("id");
bean.setClazz("java.util.ArrayList");
if
(!CollectionUtils.isEmpty(resources))
{
ConstructorArg constructorArgs = factory.createConstructorArg();
org.springframework.schema.beans.List listOfResources =
new
org.springframework.schema.beans.List();
resources.forEach(referenceFormName ->
{
Ref refBean =
new
Ref();
refBean.setBean(referenceFormName);
listOfResources.getBeanOrRefOrIdref().add(refBean);
});
constructorArgs.setList(listOfResources);
bean.getMetaOrConstructorArgOrProperty().add(constructorArgs);
}
rootElement.getImportOrAliasOrBean().add(bean);
Bean simpleBean = factory.createBean();
simpleBean.setId("SimpleBean");
simpleBean.setClazz("com.packagename.ClassName");
PropertyType firstProperty= factory.createPropertyType();
firstProperty.setName("listOfValuesDemo");
java.util.List<String> listOfValues =
new
ArrayList<>();
listOfValues.add("ValueOne");
listOfValues.add("ValueTwo");
listOfValues.add("ValueThree");
org.springframework.schema.beans.List listToStoreValues
=
new
org.springframework.schema.beans.List();
listOfValues.forEach(name ->
{
Value value = factory.createValue();
value.getContent().add(name);
listToStoreValues.getBeanOrRefOrIdref().add(value);
});
firstProperty.setList(listToStoreValues);
simpleBean.getMetaOrConstructorArgOrProperty().add
(factory.createProperty(firstProperty));
rootElement.getImportOrAliasOrBean().add(simpleBean);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty("jaxb.formatted.output",Boolean.TRUE);
createXmlConfiguration(marshaller , "output", rootElement);
}
public
static
void
createXmlConfiguration (Marshaller marshaller ,
String fileName, Beans rootElement)
{
try
{
java.net.URL url = SpringConfigGenerator.
class
.getResource("/");
File fullPathToSubfolder =
new
File(url.toURI()).getAbsoluteFile();
String projectFolder =
fullPathToSubfolder.getAbsolutePath().split("target")[
0
];
File outputFile =
new
File(projectFolder
+ "src/main/resources/" + "/"+fileName+".xml");
if
(!outputFile.exists())
{
outputFile.createNewFile();
}
OutputStream os =
new
FileOutputStream(outputFile);
marshaller.marshal(rootElement, os);
}
catch
(URISyntaxException uriException)
{
}
catch
(IOException ioException)
{
}
catch
(JAXBException jaxbException)
{
}
}
}