/*
* Copyright 2001-2004 The Apache Software Foundation.
*
* Licensed 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 org.apache.juddi.handler;
import java.util.Vector;
import org.apache.juddi.IRegistry;
import org.apache.juddi.datatype.CategoryBag;
import org.apache.juddi.datatype.Description;
import org.apache.juddi.datatype.KeyedReference;
import org.apache.juddi.datatype.Name;
import org.apache.juddi.datatype.OverviewDoc;
import org.apache.juddi.datatype.RegistryObject;
import org.apache.juddi.datatype.binding.AccessPoint;
import org.apache.juddi.datatype.binding.BindingTemplate;
import org.apache.juddi.datatype.binding.BindingTemplates;
import org.apache.juddi.datatype.binding.HostingRedirector;
import org.apache.juddi.datatype.binding.InstanceDetails;
import org.apache.juddi.datatype.binding.TModelInstanceDetails;
import org.apache.juddi.datatype.binding.TModelInstanceInfo;
import org.apache.juddi.datatype.response.ServiceDetail;
import org.apache.juddi.datatype.service.BusinessService;
import org.apache.juddi.util.xml.XMLUtils;
import org.w3c.dom.Element;
/**
* "Knows about the creation and populating of ServiceDetail objects.
* Returns ServiceDetail."
*
* @author Steve Viens (sviens@apache.org)
*/
public class ServiceDetailHandler extends AbstractHandler
{
public static final String TAG_NAME = "serviceDetail";
private HandlerMaker maker = null;
protected ServiceDetailHandler(HandlerMaker maker)
{
this.maker = maker;
}
public RegistryObject unmarshal(Element element)
{
ServiceDetail obj = new ServiceDetail();
Vector nodeList = null;
AbstractHandler handler = null;
// We could use the generic attribute value to
// determine which version of UDDI was used to
// format the request XML. - Steve
// Attributes
obj.setGeneric(element.getAttribute("generic"));
obj.setOperator(element.getAttribute("operator"));
// We can ignore the xmlns attribute since we
// can always determine it's value using the
// "generic" attribute. - Steve
String truncValue = element.getAttribute("truncated");
if (truncValue != null)
obj.setTruncated(truncValue.equalsIgnoreCase("true"));
// Text Node Value
// {none}
// Child Elements
nodeList = XMLUtils.getChildElementsByTagName(element,BusinessServiceHandler.TAG_NAME);
for (int i=0; i<nodeList.size(); i++)
{
handler = maker.lookup(BusinessServiceHandler.TAG_NAME);
obj.addBusinessService((BusinessService)handler.unmarshal((Element)nodeList.elementAt(i)));
}
return obj;
}
public void marshal(RegistryObject object,Element parent)
{
ServiceDetail detail = (ServiceDetail)object;
Element element = parent.getOwnerDocument().createElementNS(null,TAG_NAME);
AbstractHandler handler = null;
String generic = detail.getGeneric();
if ((generic != null) && (generic.trim().length() > 0))
{
element.setAttribute("generic",generic);
if (generic.equals(IRegistry.UDDI_V1_GENERIC))
element.setAttribute("xmlns",IRegistry.UDDI_V1_NAMESPACE);
else if (generic.equals(IRegistry.UDDI_V2_GENERIC))
element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
else if (generic.equals(IRegistry.UDDI_V3_GENERIC))
element.setAttribute("xmlns",IRegistry.UDDI_V3_NAMESPACE);
}
else // Default to UDDI v2 values
{
element.setAttribute("generic",IRegistry.UDDI_V2_GENERIC);
element.setAttribute("xmlns",IRegistry.UDDI_V2_NAMESPACE);
}
String operator = detail.getOperator();
if (operator != null)
element.setAttribute("operator",operator);
else
element.setAttribute("operator","");
boolean truncated = detail.isTruncated();
if (truncated)
element.setAttribute("truncated","true");
Vector vector = detail.getBusinessServiceVector();
if ((vector!=null) && (vector.size() > 0))
{
handler = maker.lookup(BusinessServiceHandler.TAG_NAME);
for (int i=0; i < vector.size(); i++)
handler.marshal((BusinessService)vector.elementAt(i),element);
}
parent.appendChild(element);
}
/***************************************************************************/
/***************************** TEST DRIVER *********************************/
/***************************************************************************/
public static void main(String args[])
throws Exception
{
HandlerMaker maker = HandlerMaker.getInstance();
AbstractHandler handler = maker.lookup(ServiceDetailHandler.TAG_NAME);
Element parent = XMLUtils.newRootElement();
Element child = null;
OverviewDoc overDoc = new OverviewDoc();
overDoc.setOverviewURL("http://www.sviens.com/service.html");
overDoc.addDescription(new Description("over-doc Descr"));
overDoc.addDescription(new Description("over-doc Descr Two","en"));
InstanceDetails instDetails = new InstanceDetails();
instDetails.addDescription(new Description("body-isa-wunder"));
instDetails.addDescription(new Description("sweetchild-o-mine","it"));
instDetails.setInstanceParms("inst-det parameters");
instDetails.setOverviewDoc(overDoc);
TModelInstanceInfo tModInstInfo = new TModelInstanceInfo();
tModInstInfo.setTModelKey("uuid:ae0f9fd4-287f-40c9-be91-df47a7c72fd9");
tModInstInfo.addDescription(new Description("tMod-Inst-Info"));
tModInstInfo.addDescription(new Description("tMod-Inst-Info too","es"));
tModInstInfo.setInstanceDetails(instDetails);
TModelInstanceDetails tModInstDet = new TModelInstanceDetails();
tModInstDet.addTModelInstanceInfo(tModInstInfo);
BindingTemplate binding = new BindingTemplate();
binding.setBindingKey("c9613c3c-fe55-4f34-a3da-b3167afbca4a");
binding.setServiceKey("997a55bc-563d-4c04-8a94-781604870d31");
binding.addDescription(new Description("whatever"));
binding.addDescription(new Description("whatever too","fr"));
binding.setHostingRedirector(new HostingRedirector("92658289-0bd7-443c-8948-0bb4460b44c0"));
binding.setAccessPoint(new AccessPoint(AccessPoint.HTTP,"http://www.sviens.com/service.wsdl"));
binding.setTModelInstanceDetails(tModInstDet);
BindingTemplates bindings = new BindingTemplates();
bindings.addBindingTemplate(binding);
CategoryBag catBag = new CategoryBag();
catBag.addKeyedReference(new KeyedReference("keyName","keyValue"));
catBag.addKeyedReference(new KeyedReference("uuid:dfddb58c-4853-4a71-865c-f84509017cc7","keyName2","keyValue2"));
BusinessService service = new BusinessService();
service.setServiceKey("fe8af00d-3a2c-4e05-b7ca-95a1259aad4f");
service.setBusinessKey("b8cc7266-9eed-4675-b621-34697f252a77");
service.setBindingTemplates(bindings);
service.setCategoryBag(catBag);
service.addName(new Name("serviceNm"));
service.addName(new Name("serviceNm2","en"));
service.addDescription(new Description("service whatever"));
service.addDescription(new Description("service whatever too","it"));
ServiceDetail detail = new ServiceDetail();
//detail.setGeneric("2.0");
//detail.setOperator("jUDDI.org");
detail.setTruncated(false);
detail.addBusinessService(service);
detail.addBusinessService(service);
System.out.println();
RegistryObject regObject = detail;
handler.marshal(regObject,parent);
child = (Element)parent.getFirstChild();
parent.removeChild(child);
XMLUtils.writeXML(child,System.out);
System.out.println();
regObject = handler.unmarshal(child);
handler.marshal(regObject,parent);
child = (Element)parent.getFirstChild();
parent.removeChild(child);
XMLUtils.writeXML(child,System.out);
System.out.println();
}
}