/*
* ChainBuilder ESB
* Visual Enterprise Integration
*
* Copyright (C) 2006 Bostech Corporation
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* $Id$
*/
package com.bostechcorp.cbesb.common.util.generators;
import java.util.Enumeration;
import java.util.Properties;
import org.jdom.Attribute;
import org.jdom.DefaultJDOMFactory;
import org.jdom.Element;
import org.jdom.JDOMFactory;
import org.jdom.Namespace;
/**
* @author LPS
*
*/
public abstract class AbstractComponentDOMWsdlXMLGenerator extends AbstractDOMGenerator {
protected static String DEFINITIONS_T="definitions";
protected static String DEFINITIONS_XMLNS_A="xmlns";
protected static String DEFINITIONS_TNS_NS="tns";
protected static String DEFINITIONS_FILE_NS="file";
protected static String DEFINITIONS_HTTP_NS="http";
protected static String DEFINITIONS_SOAP_NS="soap";
protected static String DEFINITIONS_JBI_NS="jbi";
protected static String DEFINITIONS_JMS_NS="jms";
protected static String DEFINITIONS_FTP_NS="ftp";
protected static String DEFINITIONS_TCPIP_NS="tcpip";
protected static String DEFINITIONS_EMAIL_NS="email";
protected static String DEFINITIONS_TRANSFORMER_NS="transformer";
protected static String DEFINITIONS_SEQUENCER_NS="sequencer";
protected static String DEFINITIONS_SCRIPT_NS="script";
protected static String DEFINITIONS_CBR_NS="cbr";
protected static String DEFINITIONS_JDBC_NS="jdbc";
protected static String DEFINITIONS_PARSER_NS="parser";
protected static String DEFINITIONS_XSLT_NS="xslt";
protected static String DEFINITIONS_SCHEDULER_NS="sched";
protected static String DEFINITIONS_CBHTTP_NS="cbhttp"; //defines custom CBESB http extensibulity elements
protected static String DEFINITIONS_NAME_A="name";
protected static String DEFINITIONS_TARGET_NAMESPACE_A="targetNamespace";
protected static String PORT_TYPE_T="portType";
protected static String BINDING_T="binding";
protected static String BINDING_TYPE_A="type";
protected static String SERVICE_T="service";
protected static String PORT_T="port";
protected static String PORT_BINDING_A="binding";
protected static String TYPES_T = "types";
protected static String MESSAGE_T = "message";
protected static String OPERATION_T="operation";
protected static String INPUT_T="input";
protected static String OUTPUT_T="output";
protected static String NAME_A="name";
protected static String MESSAGE_A="message";
//Schema elements
protected static Namespace xsdNamespace=Namespace.getNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
protected static String SCHEMA_T="schema";
protected static String ELEMENT_T="element";
protected static String IMPORT_T="import";
protected static String COMPLEX_TYPE_T="complexType";
protected static String CHOICE_T="choice";
protected static String NAMESPACE_A="namespace";
protected static Namespace ref=Namespace.getNamespace("ref", "http://ws-i.org/profiles/basic/1.1/xsd");
protected static Namespace de=Namespace.getNamespace("de", "http://cbesb.bostechcorp.com/dataenvelope/1.0");
protected static String MAX_OCCURS_A="maxOccurs";
protected static String TYPE_A="type";
//
//other standard elements
protected static String PART_T="part";
protected static String ELEMENT_A="element";
// basic tags that always appear in the wsdl Description
protected JDOMFactory jdomFactory = new DefaultJDOMFactory();
protected Element portTypeElement;
protected Element bindingNameElement;
protected Element serviceElement;
protected Element portElement;
protected Element typesElement;
protected Element messageElement;
Namespace xmlns ;
Namespace tns ;
Namespace componentxmlns ;
Namespace httpxmlns;
Namespace soapxmlns;
Namespace jbixmlns;
Attribute SUname;
Attribute targetNamespace;
// file name
protected String fileName="";
//protected String componentName="";
public AbstractComponentDOMWsdlXMLGenerator(String s)
{super(s);}
/* (non-Javadoc)
* @see com.bostechcorp.cbesb.common.util.AbstractDOMGenerator#buildDocumentStructure()
*/
@Override
protected void buildDocumentStructure()
{
jdomFactory = new DefaultJDOMFactory();
rootElement=jdomFactory.element(DEFINITIONS_T);
portTypeElement=jdomFactory.element(PORT_TYPE_T,rootElement.getNamespace());
bindingNameElement=jdomFactory.element(BINDING_T,rootElement.getNamespace());
serviceElement=jdomFactory.element(SERVICE_T,rootElement.getNamespace());
portElement=jdomFactory.element(PORT_T,rootElement.getNamespace());
typesElement=jdomFactory.element(TYPES_T,rootElement.getNamespace());
messageElement= jdomFactory.element(MESSAGE_T,rootElement.getNamespace());
rootElement.addContent(typesElement);
rootElement.addContent(messageElement);
rootElement.addContent(portTypeElement);
rootElement.addContent(bindingNameElement);
rootElement.addContent(serviceElement);
serviceElement.addContent(portElement);
addXSDSchema();
addMessage();
}
private void addXSDSchema()
{
Element schema= jdomFactory.element(SCHEMA_T);
schema.setNamespace(xsdNamespace);
schema.addNamespaceDeclaration(ref);
schema.addNamespaceDeclaration(de);
schema.setAttribute("targetNamespace", de.getURI());
rootElement.addNamespaceDeclaration(de);
Element importElement = jdomFactory.element(IMPORT_T,xsdNamespace);
importElement.setAttribute(NAMESPACE_A, ref.getURI());
Element elementDataEnvelope= jdomFactory.element(ELEMENT_T,xsdNamespace);
elementDataEnvelope.setAttribute(NAME_A, "DataEnvelope");
Element complexType = jdomFactory.element(COMPLEX_TYPE_T, xsdNamespace);
Element choice = jdomFactory.element(CHOICE_T, xsdNamespace);
choice.setAttribute(MAX_OCCURS_A,"unbounded");
Element elementXmlRecord= jdomFactory.element(ELEMENT_T, xsdNamespace);
elementXmlRecord.setAttribute(NAME_A, "XMLRecord");
elementXmlRecord.setAttribute(TYPE_A, xsdNamespace.getPrefix()+":anyType");
Element elementStringRecord= jdomFactory.element(ELEMENT_T, xsdNamespace);
elementStringRecord.setAttribute(NAME_A, "StringRecord");
elementStringRecord.setAttribute(TYPE_A, xsdNamespace.getPrefix()+":swaRef");
Element elementBinaryRecord= jdomFactory.element(ELEMENT_T, xsdNamespace);
elementBinaryRecord.setAttribute(NAME_A, "BinaryRecord");
elementBinaryRecord.setAttribute(TYPE_A, xsdNamespace.getPrefix()+":swaRef");
choice.addContent(elementXmlRecord);
choice.addContent(elementStringRecord);
choice.addContent(elementBinaryRecord);
complexType.addContent(choice);
elementDataEnvelope.addContent(complexType);
schema.addContent(importElement);
schema.addContent(elementDataEnvelope);
typesElement.addContent(schema);
}
private void addMessage()
{
Element message=messageElement;
message.setAttribute(NAME_A,"DataEnvelopeMessage");
Element part= jdomFactory.element(PART_T);
part.setAttribute(NAME_A,"body");
part.setAttribute(ELEMENT_A,"de:DataEnvelope");
message.addContent(part);
//rootElement.addContent(message); // content already has a parent "definitions"
}
public void setDefinitionNamespace(String componentType,String saName, String suName,
String endPointName)throws Exception{
String serviceUnitName = saName + "_" + suName;
xmlns = Namespace.getNamespace("http://schemas.xmlsoap.org/wsdl/");
tns= Namespace.getNamespace("tns","http://bostechcorp.com/SU/"+serviceUnitName);
componentxmlns = Namespace.getNamespace(componentType, "http://cbesb.bostechcorp.com/wsdl/"+componentType+"/1.0");
targetNamespace = new Attribute("targetNamespace","http://bostechcorp.com/SU/"+serviceUnitName);
SUname = new Attribute("name",serviceUnitName);
rootElement.setAttribute(SUname);
rootElement.setAttribute(targetNamespace);
rootElement.setNamespace(xmlns);
rootElement.addNamespaceDeclaration(tns);
rootElement.addNamespaceDeclaration(componentxmlns);
//optional namespace for Scheduleable components
//xmlns:xsd="http://www.w3.org/2001/XMLSchema"
//xmlns:sm="http://cbesb.bostechcorp.com/soap/sendmessage/1.0"
Namespace xsdNs= Namespace.getNamespace("xsd","http://www.w3.org/2001/XMLSchema");
Namespace smNs= Namespace.getNamespace("sm","http://cbesb.bostechcorp.com/soap/sendmessage/1.0");
rootElement.addNamespaceDeclaration(smNs);
rootElement.addNamespaceDeclaration(xsdNs);
}
public void setDefinitionAttributes(String name, String TargetNamespace)
{
rootElement.setAttribute(jdomFactory.attribute(DEFINITIONS_NAME_A, name));
rootElement.setAttribute(jdomFactory.attribute(DEFINITIONS_TARGET_NAMESPACE_A, TargetNamespace));
}
public void setPortTypeAttributes(String name)
{
portTypeElement.setAttribute(jdomFactory.attribute(NAME_A, name));
portTypeElement.setNamespace(Namespace.NO_NAMESPACE);
}
/**
* sets the operation
* @param operationName
* @param isInOut
*/
public void setOperation(String operationName,boolean isInOut )
{
//setting the operation
if (!operationName.endsWith("_Operation"))
{
operationName=operationName.replaceAll("_Interface", "");
operationName+="_Operation";
}
Element operationElement = jdomFactory.element(OPERATION_T);
operationElement.setAttribute(NAME_A,operationName);
Element inputElement= jdomFactory.element(INPUT_T);
inputElement.setAttribute(MESSAGE_A,"tns:DataEnvelopeMessage");
Element outputElement= jdomFactory.element(OUTPUT_T);
outputElement.setAttribute(MESSAGE_A,"tns:DataEnvelopeMessage");
operationElement.addContent(inputElement);
if (isInOut)
{operationElement.addContent(outputElement);}
portTypeElement.addContent(operationElement);
}
// public void setProviderAdditionalTags()
// {
// //
// //addXSDSchema();
// //addMessage();
// }
public void setBindingAttributes(String name, String type)
{
bindingNameElement.setAttribute(jdomFactory.attribute(NAME_A, name));
bindingNameElement.setAttribute(jdomFactory.attribute(BINDING_TYPE_A,
type));
bindingNameElement.setNamespace(Namespace.NO_NAMESPACE);
//Namespace ns = Namespace.getNamespace(componentType,componentType);
// adding jbi inner binding
/*
Namespace jbiBindingNS = Namespace.getNamespace("jbi",
"http://java.sun.com/xml/ns/jbi/binding/service+engine" );
Element innerBinding = jdomFactory.element(BINDING_T, jbiBindingNS);
innerBinding.addNamespaceDeclaration(jbiBindingNS);
innerBinding.setNamespace(jbiBindingNS);
rootElement.addNamespaceDeclaration(jbiBindingNS);
bindingNameElement.addContent(innerBinding);
*/
// adding component inner binding
Element specificBinding = jdomFactory.element(BINDING_T, rootElement
.getNamespace());
specificBinding.setNamespace(componentxmlns);
bindingNameElement.addContent(specificBinding);
}
public void setServiceElementAtributes(String serviceName)
{
serviceElement.setAttribute(jdomFactory.attribute(NAME_A, serviceName));
}
public void setPortElementAttributes(String portName)
{
String bindingName=bindingNameElement.getAttribute(NAME_A).getValue();
portElement.setAttribute(jdomFactory.attribute(NAME_A, portName));
portElement.setAttribute(jdomFactory.attribute(PORT_BINDING_A, "tns:"+bindingName));
}
protected void setTagAttributes(Element tag,String root, Properties settings)
{
boolean bMatch=false;
if(root!=null && !root.equals("")){
bMatch=true;
}
String rootMatch = root + ".";
Enumeration e = settings.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
if(!bMatch){
String value = settings.getProperty(key);
if (value.contains("{"))
{
tag.setAttribute(key, value);
}
else {
if ("SYSTEM_DEFAULT".equals(value))
{
tag.setAttribute(key,"");
} else
{
tag.setAttribute(key.trim(), value);
}
}
}
else if (key.indexOf(rootMatch) == 0) {
String subKey = key.substring(rootMatch.length());
String value = settings.getProperty(key);
if (value.contains("{"))
{
//writer.println(subKey + "=\"" + value + "\"");
tag.setAttribute(subKey, value);
}
else {
if ("SYSTEM_DEFAULT".equals(value))
{
//writer.println(subKey + "=\"\"");
tag.setAttribute(subKey,"");
} else
{
//writer.println(subKey + "=\"" + value + "\"");
tag.setAttribute(subKey, value);
}
}
}
}
}
abstract protected String getFileOut();
/**
* @deprecated
* @param properties
* @param role
* @param componentType
* @param endPointName
* @param saName
* @param componentName
*/
abstract public void setComponentProperty(Properties properties,
String role,
String componentType,String endPointName,String saName, String componentName) ;
/*{return filePath+fileName;}*/
/*public static void main(String[] args)
{
AbstractComponentDOMWsdlXMLGenerator a = new AbstractComponentDOMWsdlXMLGenerator("c:/");
a.setDefinitionAttributes("sdefinitionName", "stargetNamespace");
a.setServiceElementAtributes("s Service name");
a.setPortTypeAttributes("s port type");
a.setPortElementAttributes("ds", "binding");
a.setBindingAttributes("name", "type", "someComponent");
//for each type of component need to specify the Component definition and service/port content
//
a.write();
}*/
}
|