001package net.gdface.codegen.webclient;
002
003import java.io.File;
004import java.util.Map;
005import java.util.Set;
006
007import javax.xml.namespace.QName;
008
009import org.apache.commons.cli.Options;
010
011import net.gdface.cli.Context;
012import net.gdface.codegen.AbstractSchema;
013import net.gdface.codegen.generator.Generator;
014import net.gdface.codegen.generator.GeneratorConfiguration;
015
016public class KSoapStubGenerator extends Generator implements KSoapConstants {
017        private KSoapStubConfiguration config;
018        private final KSoapStubOptions options;
019        private AbstractSchema schema=null;
020        private AbstractSchema parent=null;
021        private Map<String, Map<QName, ComplexType>> schemaMap=null;
022        private boolean inlineSource=false;
023        private String templateFolder="";
024        public KSoapStubGenerator() {
025                config=new KSoapStubConfiguration();
026                options=KSoapStubOptions.getInstance();
027        }
028
029        public static void main(String[] args) {
030                KSoapStubGenerator g = (KSoapStubGenerator) new KSoapStubGenerator().parseCommandLine(args).initEngine().generate();//先生成共用代码
031                g.parent=g.schema;
032                g.generateSchemas();//生成独立的数据类型代码
033        }
034        public void generateSchemas(){
035                if(null==schemaMap)
036                        throw new IllegalArgumentException("schemaMap not initialized");
037                Map<QName, ComplexType> complexTypeMap;           
038                Set<String> independentSchemas = config.getIndependentSchemas();
039                if(independentSchemas.size()==0)return;
040                //检查参数合法性
041                for (String schema : independentSchemas) {
042                        try {
043                                TypePurpose.valueOf(schema);
044                        } catch (Exception e) {
045                                throw new IllegalArgumentException(String.format("UNKNOW schema name%s", schema));
046                        }
047                }
048                inlineSource=false;
049                //按TypePurpose定义的顺序来生成
050                for(TypePurpose value:TypePurpose.values()){
051                        if(independentSchemas.contains(value.name())){                          
052                                complexTypeMap=schemaMap.get(value.name());
053                                config.setTemplateFolder(new File(new File(templateFolder,SCHEMA_FOLDER),value.name().toLowerCase()+".vm").getPath());
054                                for(ComplexType complexType:complexTypeMap.values()){
055                                        //if(complexType.isUsed()){
056                                                this.schema=complexType;
057                                                initEngine().generate();
058                                        //}
059                                }
060                        }
061                }
062
063        }
064        @SuppressWarnings({ "unchecked", "rawtypes" })
065        @Override
066        protected Context createEngineContext() {
067                Context context = super.createEngineContext();
068                if(null==schema){
069                        schema = new KSoapStub(config.getServiceClass(), config.getWsdlLocatoin().toURI(),
070                                        config.getServiceName());                       
071                        schemaMap=((KSoapStub)schema).getSchemaMap();
072                        templateFolder=config.getTemplateFolder();
073                }else if(schema instanceof KSoapStub){//KSoapClient执行两次没意义
074                        throw new IllegalArgumentException(String.format("error type %s",schema.getClass().getName())); 
075                }
076                if (!schema.compile())
077                        System.exit(1);
078                context.setProperty(SOURCE_INFO, schema);
079                context.setProperty(PARENT_SCHEMA, parent);
080                context.setProperty(INLINE_SOURCE, inlineSource);
081                return context;
082        }
083
084
085        @Override
086        protected GeneratorConfiguration getGeneratorConfiguration() {
087                return config;
088        }
089
090        @Override
091        protected Options getOptions() {
092                return options.getOptions();
093        }
094
095
096}