Search in sources :

Example 1 with JAXPParser

use of com.sun.xml.xsom.parser.JAXPParser in project enunciate by stoicflame.

the class TestEnunciateIDLModule method testAgainstFullAPI.

/**
 * Tests the xml artifact generation against the "full" API.
 */
public void testAgainstFullAPI() throws Exception {
    Map<String, String> prefixes = new HashMap<String, String>();
    prefixes.put(FULL_NAMESPACE, "full");
    prefixes.put(DATA_NAMESPACE, "data");
    prefixes.put(CITE_NAMESPACE, "cite");
    Properties testProperties = new Properties();
    testProperties.load(TestEnunciateIDLModule.class.getResourceAsStream("/test.properties"));
    String samplePath = testProperties.getProperty("api.sample.dir");
    assertNotNull(samplePath);
    File sampleDir = new File(samplePath);
    assertTrue(sampleDir.exists());
    Enunciate engine = new Enunciate().addSourceDir(sampleDir).loadConfiguration(TestEnunciateIDLModule.class.getResourceAsStream("test-idl-module-config.xml")).loadDiscoveredModules();
    String cp = System.getProperty("java.class.path");
    String[] path = cp.split(File.pathSeparator);
    List<File> classpath = new ArrayList<File>(path.length);
    for (String element : path) {
        File entry = new File(element);
        if (entry.exists() && !new File(entry, "test.properties").exists()) {
            classpath.add(entry);
        }
    }
    engine.setClasspath(classpath);
    engine.run();
    IDLModule idlModule = null;
    for (EnunciateModule candidate : engine.getModules()) {
        if (candidate instanceof IDLModule) {
            idlModule = (IDLModule) candidate;
            break;
        }
    }
    assertNotNull(idlModule);
    assertNotNull(idlModule.jaxbModule);
    assertNotNull(idlModule.jaxwsModule);
    assertNotNull(idlModule.jaxrsModule);
    final Map<String, String> schemas = new HashMap<String, String>();
    for (SchemaInfo schemaInfo : idlModule.jaxbModule.getJaxbContext().getSchemas().values()) {
        assertNotNull(schemaInfo.getSchemaFile());
        assertTrue(schemaInfo.getSchemaFile() instanceof JaxbSchemaFile);
        String filename = ((JaxbSchemaFile) schemaInfo.getSchemaFile()).filename;
        assertNotNull(filename);
        if (prefixes.containsKey(schemaInfo.getNamespace())) {
            assertEquals(prefixes.get(schemaInfo.getNamespace()) + ".xsd", filename);
        }
        StringWriter schemaOut = new StringWriter();
        ((JaxbSchemaFile) schemaInfo.getSchemaFile()).writeTo(schemaOut);
        schemaOut.flush();
        schemas.put(filename, schemaOut.toString());
    }
    WsdlInfo fullWsdlInfo = idlModule.jaxwsModule.getJaxwsContext().getWsdls().get(FULL_NAMESPACE);
    assertNotNull(fullWsdlInfo);
    assertEquals("full.wsdl", fullWsdlInfo.getFilename());
    assertNotNull(fullWsdlInfo.getWsdlFile());
    assertTrue(fullWsdlInfo.getWsdlFile() instanceof JaxwsWsdlFile);
    JaxwsWsdlFile fullWsdl = (JaxwsWsdlFile) fullWsdlInfo.getWsdlFile();
    assertEquals("full.wsdl", fullWsdl.filename);
    final StringWriter output = new StringWriter();
    fullWsdl.writeTo(output);
    output.flush();
    // make sure the wsdl is built correctly
    WSDLReader wsdlReader = WSDLFactory.newInstance().newWSDLReader();
    Definition definition = wsdlReader.readWSDL(new InMemoryWSDLLocator(output, schemas));
    assertEquals(FULL_NAMESPACE, definition.getTargetNamespace());
    Types types = definition.getTypes();
    List extensibilityElements = types.getExtensibilityElements();
    assertEquals(1, extensibilityElements.size());
    ExtensibilityElement ee = (ExtensibilityElement) extensibilityElements.get(0);
    assertEquals(new QName(W3C_XML_SCHEMA_NS_URI, "schema"), ee.getElementType());
    Schema schema = (Schema) ee;
    Map imports = schema.getImports();
    assertEquals(3, imports.size());
    assertNotNull(imports.get(DATA_NAMESPACE));
    assertNotNull(imports.get(CITE_NAMESPACE));
    assertNotNull(imports.get(null));
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    Element schemaElement = schema.getElement();
    // these namespaces need to be explicitly added because the transformer won't see any references to them...
    schemaElement.setAttribute("xmlns:data", DATA_NAMESPACE);
    schemaElement.setAttribute("xmlns:cite", CITE_NAMESPACE);
    schemaElement.setAttribute("xmlns:full", FULL_NAMESPACE);
    // write out the wsdl schema to its xml form so we can parse it with XSOM...
    DOMSource source = new DOMSource(schemaElement);
    StringWriter fullSchemaOutput = new StringWriter();
    StreamResult result = new StreamResult(fullSchemaOutput);
    transformer.transform(source, result);
    fullSchemaOutput.flush();
    // set up the XSOM Parser.
    final InputSource fullSource = new InputSource(new StringReader(fullSchemaOutput.toString()));
    fullSource.setSystemId("file:/");
    XSOMParser parser = new XSOMParser(new JAXPParser());
    // throw all errors and warnings.
    parser.setErrorHandler(new ThrowEverythingHandler());
    parser.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            String xsd = schemas.get(systemId.substring("file:/".length()));
            if (xsd == null) {
                return null;
            }
            InputSource source = new InputSource(new StringReader(xsd));
            source.setSystemId("file:/");
            return source;
        }
    });
    // make sure the schema included in the wsdl is correct.
    parser.parse(fullSource);
    XSSchemaSet schemaSet = parser.getResult();
    XSSchema wsdlSchema = schemaSet.getSchema(FULL_NAMESPACE);
    assertNotNull(wsdlSchema);
    // make sure the data schema is imported and correct.
    XSSchema dataSchema = schemaSet.getSchema(DATA_NAMESPACE);
    assertNotNull(dataSchema);
    assertDataSchemaStructure(dataSchema);
    // make sure the cite schema is imported and correct.
    XSSchema citeSchema = schemaSet.getSchema(CITE_NAMESPACE);
    assertNotNull(citeSchema);
    assertCiteSchemaStructure(citeSchema);
    // now verify the rest of the WSDL...
    assertWebServiceDefinition(definition);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) Schema(javax.wsdl.extensions.schema.Schema) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Element(org.w3c.dom.Element) ExtensibilityElement(javax.wsdl.extensions.ExtensibilityElement) Enunciate(com.webcohesion.enunciate.Enunciate) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) SchemaInfo(com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo) EnunciateModule(com.webcohesion.enunciate.module.EnunciateModule) TransformerFactory(javax.xml.transform.TransformerFactory) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) StreamResult(javax.xml.transform.stream.StreamResult) QName(javax.xml.namespace.QName) JAXPParser(com.sun.xml.xsom.parser.JAXPParser) IOException(java.io.IOException) File(java.io.File) WsdlInfo(com.webcohesion.enunciate.modules.jaxws.WsdlInfo) WSDLReader(javax.wsdl.xml.WSDLReader)

Example 2 with JAXPParser

use of com.sun.xml.xsom.parser.JAXPParser in project jaxb-ri by eclipse-ee4j.

the class ModelLoader method createXSOMSpeculative.

/**
 * Parses schemas directly into XSOM by assuming that there's
 * no external annotations.
 * <p>
 * When an external annotation is found, a {@link SpeculationFailure} is thrown,
 * and we will do it all over again by using the slow way.
 */
private XSSchemaSet createXSOMSpeculative() throws SAXException, SpeculationFailure {
    // check if the schema contains external binding files. If so, speculation is a failure.
    XMLParser parser = new XMLParser() {

        private final JAXPParser base = new JAXPParser(XmlFactory.createParserFactory(opt.disableXmlSecurity));

        @Override
        public void parse(InputSource source, ContentHandler handler, ErrorHandler errorHandler, EntityResolver entityResolver) throws SAXException, IOException {
            // set up the chain of handlers.
            handler = wrapBy(new SpeculationChecker(), handler);
            handler = wrapBy(new VersionChecker(null, errorReceiver, entityResolver), handler);
            base.parse(source, handler, errorHandler, entityResolver);
        }

        /**
         * Wraps the specified content handler by a filter.
         * It is little awkward to use a helper implementation class like XMLFilterImpl
         * as the method parameter, but this simplifies the code.
         */
        private ContentHandler wrapBy(XMLFilterImpl filter, ContentHandler handler) {
            filter.setContentHandler(handler);
            return filter;
        }
    };
    XSOMParser reader = createXSOMParser(parser);
    // parse source grammars
    for (InputSource value : opt.getGrammars()) reader.parse(value);
    return reader.getResult();
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) InputSource(org.xml.sax.InputSource) VersionChecker(com.sun.tools.xjc.reader.internalizer.VersionChecker) XSOMParser(com.sun.xml.xsom.parser.XSOMParser) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) JAXPParser(com.sun.xml.xsom.parser.JAXPParser) EntityResolver(org.xml.sax.EntityResolver) XMLParser(com.sun.xml.xsom.parser.XMLParser) ContentHandler(org.xml.sax.ContentHandler)

Aggregations

JAXPParser (com.sun.xml.xsom.parser.JAXPParser)2 XSOMParser (com.sun.xml.xsom.parser.XSOMParser)2 VersionChecker (com.sun.tools.xjc.reader.internalizer.VersionChecker)1 XMLParser (com.sun.xml.xsom.parser.XMLParser)1 Enunciate (com.webcohesion.enunciate.Enunciate)1 EnunciateModule (com.webcohesion.enunciate.module.EnunciateModule)1 SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)1 WsdlInfo (com.webcohesion.enunciate.modules.jaxws.WsdlInfo)1 File (java.io.File)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 ExtensibilityElement (javax.wsdl.extensions.ExtensibilityElement)1 Schema (javax.wsdl.extensions.schema.Schema)1 WSDLReader (javax.wsdl.xml.WSDLReader)1 QName (javax.xml.namespace.QName)1 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1