org.jvnet.jax_ws_commons.json.SchemaInfo Maven / Gradle / Ivy
package org.jvnet.jax_ws_commons.json;
import com.sun.istack.NotNull;
import com.sun.istack.XMLStreamException2;
import com.sun.xml.bind.unmarshaller.DOMScanner;
import com.sun.xml.stream.buffer.MutableXMLStreamBuffer;
import com.sun.xml.stream.buffer.stax.StreamWriterBufferCreator;
import com.sun.xml.ws.api.model.wsdl.WSDLBoundOperation;
import com.sun.xml.ws.api.model.wsdl.WSDLPort;
import com.sun.xml.ws.api.server.DocumentAddressResolver;
import com.sun.xml.ws.api.server.SDDocument;
import com.sun.xml.ws.api.server.ServiceDefinition;
import com.sun.xml.ws.api.server.WSEndpoint;
import com.sun.xml.ws.model.wsdl.WSDLBoundPortTypeImpl;
import com.sun.xml.xsom.XSElementDecl;
import com.sun.xml.xsom.XSSchema;
import com.sun.xml.xsom.XSSchemaSet;
import com.sun.xml.xsom.parser.JAXPParser;
import com.sun.xml.xsom.parser.XMLParser;
import com.sun.xml.xsom.parser.XSOMParser;
import com.sun.xml.xsom.visitor.XSVisitor;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.jvnet.jax_ws_commons.json.schema.CompositeJsonType;
import org.jvnet.jax_ws_commons.json.schema.JsonOperation;
import org.jvnet.jax_ws_commons.json.schema.JsonTypeBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.ContentHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.jws.soap.SOAPBinding.Style;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.ws.WebServiceException;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Captures the information parsed from XML Schema.
* Used to guide the JSON/XML conversion.
*
* @author Kohsuke Kawaguchi
*/
public final class SchemaInfo {
/**
* Endpoint for which this schema info applies.
*/
final @NotNull WSEndpoint endpoint;
/**
* Parent tag name to possible child tag names.
*/
final Set tagNames = new HashSet();
final List operations = new ArrayList();
final SchemaConvention convention;
/**
* @throws WebServiceException
* If failed to parse schema portion inside WSDL.
*/
public SchemaInfo(WSEndpoint endpoint) {
this.endpoint = endpoint;
final ServiceDefinition sd = endpoint.getServiceDefinition();
final Map byURL = new HashMap();
for (SDDocument doc : sd)
byURL.put(doc.getURL().toExternalForm(),doc);
// set up XSOMParser to read from SDDocuments
XSOMParser p = new XSOMParser(new XMLParser() {
private final XMLParser jaxp = new JAXPParser();
public void parse(InputSource source, ContentHandler handler, ErrorHandler errorHandler, EntityResolver entityResolver) throws SAXException, IOException {
SDDocument doc = byURL.get(source.getSystemId());
if(doc!=null) {
try {
readToBuffer(doc).writeTo(handler,errorHandler,false);
} catch (XMLStreamException e) {
throw new SAXException(e);
}
} else {
// default behavior
jaxp.parse(source,handler,errorHandler,entityResolver);
}
}
});
try {
// parse the primary WSDL, and it should recursively parse all referenced schemas
// TODO: this is super slow
TransformerHandler h = ((SAXTransformerFactory) TransformerFactory.newInstance()).newTransformerHandler();
DOMResult r = new DOMResult();
h.setResult(r);
readToBuffer(sd.getPrimary()).writeTo(h,false);
Document dom = (Document)r.getNode();
NodeList schemas = dom.getElementsByTagNameNS(XMLConstants.W3C_XML_SCHEMA_NS_URI, "schema");
for( int i=0; i getOperations() {
return operations;
}
public Set getTypes() {
Set r = new LinkedHashSet();
for (JsonOperation op : operations) {
op.input.listCompositeTypes(r);
op.output.listCompositeTypes(r);
}
return r;
}
//private static final String WSDL_NSURI = "http://schemas.xmlsoap.org/wsdl/";
}