All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dkschlos.wsdlget.internal.SchemaDownloader Maven / Gradle / Ivy

Go to download

Maven plugin which downloads a wsdl and all referenced schema-filed to a specified target folder (default: src/main/resources/wsdl) and rewrites schema-locations in all files.

The newest version!
package com.github.dkschlos.wsdlget.internal;

import com.ibm.wsdl.util.xml.DOM2Writer;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.wsdl.Definition;
import javax.wsdl.Types;
import javax.wsdl.extensions.schema.Schema;
import javax.wsdl.extensions.schema.SchemaImport;
import javax.wsdl.extensions.schema.SchemaReference;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class SchemaDownloader {

	private static final String IMPORT_TAG = "import";
	private static final String INCLUDE_TAG = "include";
	private static final String SCHEMA_LOCATION = "schemaLocation";

	private Path outputFolder;
	private Definition definition;
	private String serviceName;

	private int schemaCount = 0;
	private Map changedSchemaLocations = new HashMap<>();

	private Set processedSchemas = new HashSet<>();

	public SchemaDownloader(Path outputFolder, Definition definition, String serviceName) {
		this.outputFolder = outputFolder;
		this.definition = definition;
		this.serviceName = serviceName;
	}


	public void download() {
		Types wsdlTypes = definition.getTypes();
		if (wsdlTypes != null) {
			List extensibilityElements = wsdlTypes.getExtensibilityElements();
			for (Iterator iter = extensibilityElements.iterator(); iter.hasNext();) {
				Object currentObject = iter.next();
				if (currentObject instanceof Schema) {
					Schema schema = (Schema) currentObject;
					processSchema(definition, schema, serviceName, null);
				}
			}
		}
	}

	private void processSchema(Definition definition, Schema schema, String serviceName, String fileName) {
		processedSchemas.add(schema.getDocumentBaseURI());

		try {
			if (schema.getIncludes() != null) {
				for (SchemaReference ref : (List) schema.getIncludes()) {
					Schema includedSchema = ref.getReferencedSchema();
					if (includedSchema == null || processedSchemas.contains(includedSchema.getDocumentBaseURI())) {
						continue;
					}

					String fileNameChild = getSchemaFileName(serviceName, ref.getSchemaLocationURI());
					processSchema(definition, includedSchema, serviceName, fileNameChild);

				}
			}
			if (schema.getImports() != null && schema.getImports().values() != null) {
				for (List schemaImportList : (Collection>) schema.getImports().values()) {
					for (SchemaImport imp : schemaImportList) {
						Schema importedSchema = imp.getReferencedSchema();
						if (importedSchema == null || processedSchemas.contains(importedSchema.getDocumentBaseURI())) {
							continue;
						}
						String fileNameChild = getSchemaFileName(serviceName, imp.getSchemaLocationURI());
						processSchema(definition, importedSchema, serviceName, fileNameChild);

					}
				}
			}
			changeLocations(schema.getElement(), changedSchemaLocations);
			if (fileName != null) {
				try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outputFolder.resolve(fileName).toFile()), StandardCharsets.UTF_8)) {
					DOM2Writer.serializeAsXML(schema.getElement(), definition.getNamespaces(), writer);
				}
			}
		} catch (Exception ex) {
			throw new RuntimeException("Unexpected error", ex);
		}

	}

	private String getSchemaFileName(String serviceName, String schemaLocationUrl) {
		String fileNameChild = changedSchemaLocations.get(schemaLocationUrl);
		if (fileNameChild == null) {
			fileNameChild = serviceName + schemaCount++ + ".xsd";
		}
		changedSchemaLocations.put(schemaLocationUrl, fileNameChild);
		return fileNameChild;
	}

	private void changeLocations(Element element, Map changedSchemaLocations) {
		NodeList nodeList = element.getChildNodes();
		for (int i = 0; i < nodeList.getLength(); i++) {
			String tagName = nodeList.item(i).getLocalName();
			if (IMPORT_TAG.equals(tagName) || INCLUDE_TAG.equals(tagName)) {
				processImport(nodeList.item(i), changedSchemaLocations);
			}
		}
	}

	private void processImport(Node importNode, Map changedSchemaLocations) {
		NamedNodeMap nodeMap = importNode.getAttributes();
		for (int i = 0; i < nodeMap.getLength(); i++) {
			Node attribute = nodeMap.item(i);
			if (attribute.getNodeName().equals(SCHEMA_LOCATION)) {
				String attributeValue = attribute.getNodeValue();
				attributeValue = changedSchemaLocations.get(attributeValue);
				if (attributeValue != null) {
					attribute.setNodeValue(attributeValue);
				}
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy