Генерация входного и выходного XSD (схемы) для каждой операции из WSDL с использованием Java.
У меня есть файл WSDL и я хочу сгенерировать отдельный входной и выходной XSD (схема) для каждой операции.Для этого я использовал jar souUI.
Ниже приведен мой WSDL и код для генерации XSD (схемы) для каждой операции.Пожалуйста, найдите WSDL по адресу http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl
Вот код, который я использовал:
public class SOAPInputGenerator {
public static void main(String[] args) throws Exception {
final String INPUT_PARAMETERS = ">?<";
WsdlProject project = new WsdlProject();
WsdlInterface[] wsdls = WsdlImporter.importWsdl(project, "http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl");
WsdlInterface wsdl = wsdls[0];
for (Operation operation : wsdl.getOperationList()) {
WsdlOperation wsdlOperation = (WsdlOperation) operation;
String operationName = wsdlOperation.getName();
System.out.println(wsdlOperation.createRequest(true));
String requestString = wsdlOperation.createRequest(true);
Document doc = getDocumentFromSoapRequestString(requestString);
Element docElement = doc.getDocumentElement();
System.out.println(docElement.getTagName());
NamedNodeMap allAttributes = docElement.getAttributes();
NodeList headerElements =
docElement.getElementsByTagName("soap:Header").item(0) != null ? docElement.getElementsByTagName("soap:Header") : docElement.getElementsByTagName("soapenv:Header");
System.out.println("SOAP header :"+headerElements.item(0).getNodeName());
docElement.removeChild(headerElements.item(0));
if (docElement != null && allAttributes != null) {
while(allAttributes.getLength() != 0)
{
Node attr = allAttributes.item(0);
System.out.println("*************Node : ******"+attr.getNodeName());
docElement.removeAttribute(attr.getNodeName());
}
}
String simpleXmlStr = returnDocumentInString(doc);
simpleXmlStr = simpleXmlStr.replaceAll("soapenv", "Operation").replaceAll("soap", "Operation").replaceAll(":", "");
System.out.println(simpleXmlStr);
if (simpleXmlStr.contains(INPUT_PARAMETERS)) {
generateXsd(operationName, simpleXmlStr);
}
}
}
public static String returnDocumentInString(Document doc) {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = tf.newTransformer();
transformer.transform(domSource, result);
} catch (TransformerConfigurationException transformerConfigurationException) {
System.out.println(transformerConfigurationException.getLocalizedMessage());
}
catch (TransformerException transformerException) {
System.out.println(transformerException.getLocalizedMessage());
}
return writer.toString();
}
public static Document getDocumentFromSoapRequestString(String xmlString) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
return doc;
}
private static void generateXsd(String operationName, String soapRequestXml) throws IOException, ParseException, ParserConfigurationException, SAXException, TransformerException {
FileWriter fw = new FileWriter("exmp.xml");
fw.write(soapRequestXml);
fw.close();
XsdGen gen = new XsdGen();
File tempFile = new File("exmp.xml");
gen.parse(tempFile);
File out = new File(operationName+".xsd");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
gen.write(new FileOutputStream(out));
Document doc = builder.parse(operationName+".xsd");
Element docElement = doc.getDocumentElement();
docElement.setAttribute("targetNamespace", "Default target name space for data mapper");
DOMSource source = new DOMSource(doc);
FileWriter writer = new FileWriter(new File(operationName+".xsd"));
StreamResult result = new StreamResult(writer);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
tempFile.delete();
}
}
Я хотел бы знать, насколько это хороший и безопасный подход - использовать банку soupUI длятак же?Есть ли альтернативный способ добиться того же?