Package be.selckin.ws.util.java2php

Source Code of be.selckin.ws.util.java2php.JavaToPHP

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package be.selckin.ws.util.java2php;

import be.selckin.ws.util.java2php.php.PhpService;
import be.selckin.ws.util.java2php.php.PhpType;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.common.WSDLConstants;
import org.apache.cxf.jaxb.JAXBDataBinding;
import org.apache.cxf.jaxws.JaxwsServiceBuilder;
import org.apache.cxf.service.ServiceBuilder;
import org.apache.cxf.service.model.SchemaInfo;
import org.apache.cxf.service.model.ServiceInfo;
import org.apache.cxf.tools.common.ToolException;
import org.apache.cxf.tools.util.AnnotationUtil;

import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class JavaToPHP {
    private static final Charset UTF8 = Charset.forName("utf-8");


    public void generate(File target, List<String> serviceClasses) throws ToolException, IOException {

        List<EndPoint> endPoints = new ArrayList<>();
        for (String serviceClass : serviceClasses) {
            endPoints.add(createEndpoint(AnnotationUtil.loadClass(serviceClass, Thread.currentThread().getContextClassLoader())));
        }

        try (FileOutputStream fileOutputStream = new FileOutputStream(target)) {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, UTF8);
            BufferedWriter writer = new BufferedWriter(outputStreamWriter);
            writer.append("<?php\n");
            writer.append("//\n");
            writer.append("// This file is generated - do not modify\n");
            writer.append("//\n");

            Php php = new Php(writer);

            PhpWriter phpWriter = new PhpWriter();
            for (EndPoint endPoint : endPoints) {
                phpWriter.write(php, endPoint);
            }

            Set<PhpType> allTypes = new HashSet<>();
            for (EndPoint endPoint : endPoints) {
                allTypes.addAll(endPoint.getTypes());
            }
            phpWriter.write(php, allTypes);


            writer.append("\n?>");
            writer.close();
        }
    }

    private EndPoint createEndpoint(Class<?> clz) {
        List<PhpService> services;ServiceBuilder builder = getServiceBuilder(clz);
        ServiceInfo serviceInfo = builder.createService();

        NameManager nameManager = NameManager.newNameManager(serviceInfo, null);
        NamespacePrefixAccumulator prefixManager = new NamespacePrefixAccumulator(serviceInfo.getXmlSchemaCollection());
        Collection<SchemaInfo> schemata = serviceInfo.getSchemas();


        TypesPHPBuilder jsBuilder = new TypesPHPBuilder(serviceInfo.getXmlSchemaCollection(), prefixManager, nameManager);

        List<PhpType> phpTypes = new ArrayList<>();
        for (SchemaInfo schema : schemata) {
            phpTypes.addAll(jsBuilder.gatherTypes(schema.getSchema()));
        }

        ServicePHPBuilder serviceBuilder = new ServicePHPBuilder(serviceInfo, prefixManager, nameManager);
        serviceBuilder.walk();

        services = serviceBuilder.getServices();
        return new EndPoint(services, phpTypes);
    }

    @SuppressWarnings("unchecked")
    public ServiceBuilder getServiceBuilder(Class<?> clazz) throws ToolException {
        JaxwsServiceBuilder builder = new JaxwsServiceBuilder();
        JAXBDataBinding dataBinding = new JAXBDataBinding();
        dataBinding.setExtraClass(JaxbUtils.findExtraClasses(clazz));
        builder.setDataBinding(dataBinding);
        builder.setServiceClass(clazz);

        builder.validate();
        String soapNS = getSoapNS(clazz);
        builder.setTransportId(soapNS);
        builder.setBus(getBus());
        builder.setBindingId(soapNS);
        return builder;
    }

    protected String getSoapNS(Class<?> clazz) {
        BindingType bType = clazz.getAnnotation(BindingType.class);
        if (bType != null) {
            if (SOAPBinding.SOAP12HTTP_BINDING.equals(bType.value()))
                return WSDLConstants.NS_SOAP12;
        }
        return WSDLConstants.NS_SOAP11;
    }

    public Bus getBus() {
        return BusFactory.getThreadDefaultBus();
    }

}
TOP

Related Classes of be.selckin.ws.util.java2php.JavaToPHP

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.