jibx2wsdl.java
来自「对xml很好的java处理引擎,编译中绑定xml」· Java 代码 · 共 665 行 · 第 1/2 页
JAVA
665 行
if (msg == null) { // first time for this throwable, create the message FaultCustom fault = m_wsdlCustom.forceFaultCustomization(type); qname = new QName(sns, fault.getElementName()); part = new MessagePart("fault", qname); msg = new Message(fault.getFaultName(), part); def.addMessage(msg); // make sure the corresponding mapping exists BindingGenerator.MappingDetail detail = m_bindingGenerator.getMappingDetail(fault.getDataType()); if (detail == null) { throw new IllegalStateException("No mapping found for type " + type); } // record that the fault has been defined fltmap.put(type, msg); } // add fault to operation definition op.addFaultMessage(msg); } // add operation to list of definitions def.addOperation(op); } holder.fixReferences(); return def; } /** * Accumulate data type(s) from value to be included in binding. * * @param value * @param dataset set of types for binding */ private void accumulateData(ValueCustom value, Set dataset) { String type = value.getBoundType(); if (!dataset.contains(type) && !Types.isSimpleValue(type)) { String itype = value.getItemType(); if (itype == null) { dataset.add(type); } else { dataset.add(itype); } } } /** * Add the <mapping> definition for a typed collection to a binding. This * always creates an abstract mapping with the type name based on both the * item type and the collection type. * * @param value collection value * @param typemap map from parameterized type to abstract mapping name * @param bind target binding * @return qualified name for collection */ public QName addCollectionBinding(ValueCustom value, Map typemap, BindingHolder bind) { // check for existing mapping String ptype = value.getBoundType(); QName qname = (QName)typemap.get(ptype); if (qname == null) { // create abstract mapping for collection class type MappingElement mapping = new MappingElement(); mapping.setClassName(value.getType()); mapping.setAbstract(true); mapping.setCreateType(value.getCreateType()); mapping.setFactoryName(value.getFactoryMethod()); // generate the mapping type name from item class name and suffix String suffix; String type = value.getType(); IClass clas = m_global.getClassInfo(type); if (clas.isImplements("Ljava/util/List;")) { suffix = "List"; } else if (clas.isImplements("Ljava/util/Set;")) { suffix = "Set"; } else { suffix = "Collection"; } String itype = value.getItemType(); ClassCustom cust = m_global.forceClassCustomization(itype); // register the type name for mapping String name = cust.getSimpleName() + suffix; qname = new QName(bind.getNamespace(), CustomBase.convertName(name, CustomBase.CAMEL_CASE_NAMES)); mapping.setTypeQName(qname); typemap.put(ptype, qname); // add collection definition details CollectionElement coll = new CollectionElement(); m_bindingGenerator.defineCollection(itype, value.getItemElementName(), coll, bind); mapping.addChild(coll); // add mapping to binding bind.getBinding().addTopChild(mapping); } return qname; } /** * Generate based on list of service classes. * * @param classes service class list * @param extras list of extra classes for binding * @param name singleton or root binding definition file name * @return list of WSDLs * @throws JiBXException * @throws IOException */ private ArrayList generate(List classes, List extras, String name) throws JiBXException, IOException { // add any service classes not already present in customizations for (int i = 0; i < classes.size(); i++) { String sclas = (String)classes.get(i); if (m_wsdlCustom.getServiceCustomization(sclas) == null) { m_wsdlCustom.addServiceCustomization(sclas); } } // accumulate the data classes used by all service operations // TODO: throws class handling, with multiple services per WSDL InsertionOrderedSet abstrs = new InsertionOrderedSet(); InsertionOrderedSet concrs = new InsertionOrderedSet(); ArrayList qnames = new ArrayList(); List services = m_wsdlCustom.getServices(); for (Iterator iter = services.iterator(); iter.hasNext();) { ServiceCustom service = (ServiceCustom)iter.next(); List ops = service.getOperations(); for (Iterator iter1 = ops.iterator(); iter1.hasNext();) { OperationCustom op = (OperationCustom)iter1.next(); List parms = op.getParameters(); for (Iterator iter2 = parms.iterator(); iter2.hasNext();) { accumulateData((ValueCustom)iter2.next(), abstrs); } accumulateData(op.getReturn(), abstrs); ArrayList thrws = op.getThrows(); for (int i = 0; i < thrws.size(); i++) { // add concrete mapping for data type, if used ThrowsCustom thrw = (ThrowsCustom)thrws.get(i); FaultCustom fault = m_wsdlCustom.forceFaultCustomization(thrw.getType()); if (!concrs.contains(fault.getDataType())) { concrs.add(fault.getDataType()); qnames.add(new QName(service.getNamespace(), fault.getElementName())); } } } } // include extra classes as needing concrete mappings for (int i = 0; i < extras.size(); i++) { String type = (String)extras.get(i); if (!concrs.contains(type)) { concrs.add(type); ClassCustom clas = m_global.forceClassCustomization(type); qnames.add(new QName(clas.getNamespace(), clas.getElementName())); } } // generate bindings for all data classes used m_bindingGenerator.generateSpecified(qnames, concrs.asList(), abstrs.asList()); // add binding definitions for collections passed or returned Map typemap = new HashMap(); for (Iterator iter = services.iterator(); iter.hasNext();) { ServiceCustom service = (ServiceCustom)iter.next(); List ops = service.getOperations(); BindingHolder hold = null; String uri = service.getNamespace(); for (Iterator iter1 = ops.iterator(); iter1.hasNext();) { OperationCustom op = (OperationCustom)iter1.next(); List parms = op.getParameters(); for (Iterator iter2 = parms.iterator(); iter2.hasNext();) { ValueCustom parm = (ValueCustom)iter2.next(); if (parm.getItemType() != null) { if (hold == null) { hold = m_bindingGenerator.findBinding(uri); } addCollectionBinding(parm, typemap, hold); } } ValueCustom ret = op.getReturn(); if (ret.getItemType() != null) { if (hold == null) { hold = m_bindingGenerator.findBinding(uri); } addCollectionBinding(ret, typemap, hold); } } if (hold != null) { hold.fixReferences(); } } // get singleton or root binding with fake URL for locating includes BindingHolder rhold = m_bindingGenerator.configureBindings(name); BindingElement binding = rhold.getBinding(); binding.setBaseUrl(new URL("http://localhost/")); // add all other binding definitions for access from root ArrayList uris = m_bindingGenerator.getNamespaces(); ArrayList holders = new ArrayList(); for (int i = 0; i < uris.size(); i++) { String uri = (String)uris.get(i); BindingHolder hold = m_bindingGenerator.findBinding(uri); holders.add(hold); if (hold != rhold) { String path = "http://localhost/" + hold.getFileName(); binding.addIncludeBinding(path, hold.getBinding()); } } // validate the binding definition ValidationContext vctx = new ValidationContext(m_locator); vctx.setBindingRoot(binding); binding.runValidation(vctx); // report any validation errors ArrayList probs = vctx.getProblems(); if (vctx.getErrorCount() > 0 || vctx.getFatalCount() > 0) { System.out.println("Errors in generated binding:"); for (int j = 0; j < probs.size(); j++) { ValidationProblem prob = (ValidationProblem)probs.get(j); System.out.print(prob.getSeverity() >= ValidationProblem.ERROR_LEVEL ? "Error: " : "Warning: "); System.out.println(prob.getDescription()); } // print all the bindings containing the errors IBindingFactory fact = BindingDirectory.getFactory(BindingElement.class); IMarshallingContext ictx = fact.createMarshallingContext(); ictx.setOutput(System.out, null); ictx.setIndent(2); for (int i = 0; i < uris.size(); i++) { String uri = (String)uris.get(i); BindingHolder hold = m_bindingGenerator.findBinding(uri); ictx.reset(); ((IMarshallable)hold.getBinding()).marshal(ictx); ictx.getXmlWriter().flush(); } return null; } else { // build and record the schemas ArrayList schemas = m_schemaGenerator.generate(holders); // TODO: fix this for (int i = 0; i < schemas.size(); i++) { SchemaHolder holder = (SchemaHolder)schemas.get(i); m_uriSchemaMap.put(holder.getNamespace(), holder); } // build the WSDL for each service ArrayList wsdls = new ArrayList(); for (Iterator iter = services.iterator(); iter.hasNext();) { wsdls.add(buildWSDL((ServiceCustom)iter.next(), typemap)); } return wsdls; } } /** * Run the WSDL generation using command line parameters. * * @param args * @throws JiBXException * @throws IOException */ public static void main(String[] args) throws JiBXException, IOException { WsdlGeneratorCommandLine parms = new WsdlGeneratorCommandLine(); if (args.length > 0 && parms.processArgs(args)) { // generate services, bindings, and WSDLs Jibx2Wsdl inst = new Jibx2Wsdl(parms.getLocator(), parms.getGlobal(), parms.getWsdlCustom()); ArrayList wsdls = inst.generate(parms.getExtraArgs(), parms.getExtraTypes(), "binding.xml"); if (wsdls != null) { // write the bindings inst.m_bindingGenerator.writeBindings(parms.getGeneratePath()); // write the corresponding schemas IBindingFactory fact = BindingDirectory.getFactory(SchemaElement.class); for (Iterator iter = inst.m_uriSchemaMap.values().iterator(); iter.hasNext();) { SchemaHolder holder = (SchemaHolder)iter.next(); IMarshallingContext ictx = fact.createMarshallingContext(); File file = new File(parms.getGeneratePath(), holder.getFileName()); ictx.setOutput(new FileOutputStream(file), null); ictx.setIndent(2); ((IMarshallable)holder.getSchema()).marshal(ictx); ictx.getXmlWriter().flush(); } // output the generated WSDLs WsdlWriter writer = new WsdlWriter(); for (int i = 0; i < wsdls.size(); i++) { Definitions def = (Definitions)wsdls.get(i); File file = new File(parms.getGeneratePath(), def.getServiceName() + ".wsdl"); writer.writeWSDL(def, new FileOutputStream(file)); } } } else { if (args.length > 0) { System.err.println("Terminating due to command line errors"); } else { parms.printUsage(); } System.exit(1); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?