/*=============================================================================*
* Copyright 2006 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.muse.ws.dm.muws.impl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.muse.util.messages.Messages;
import org.apache.muse.util.messages.MessagesFactory;
import org.apache.muse.util.xml.XmlSerializable;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.ws.addressing.EndpointReference;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.ws.dm.muws.MuwsConstants;
import org.apache.muse.ws.dm.muws.RelationshipResource;
import org.apache.muse.ws.resource.WsResource;
import org.apache.muse.ws.resource.remote.WsResourceClient;
/**
*
* QueryRelationshipsResponse is a serializer/deserializer for the
* MUWS (Part 2) QueryRelationshipsByType operation's response content.
*
* @author Dan Jemiolo (danj)
*
*/
public class QueryRelationshipsResponse implements XmlSerializable
{
private static Messages _MESSAGES = MessagesFactory.get(QueryRelationshipsResponse.class);
private Element[] _relationshipsXML = null;
public QueryRelationshipsResponse(Element xml)
{
_relationshipsXML = XmlUtils.getElements(xml, MuwsConstants.RELATIONSHIP_QNAME);
}
public QueryRelationshipsResponse(WsResource[] matches)
{
if (matches == null)
throw new NullPointerException(_MESSAGES.get("NullRelationshipArray"));
_relationshipsXML = new Element[matches.length];
for (int n = 0; n < matches.length; ++n)
{
RelationshipResource relationship =
(RelationshipResource)matches[n].getCapability(MuwsConstants.RELATIONSHIP_RESOURCE_URI);
_relationshipsXML[n] = relationship.toXML();
}
}
public WsResourceClient[] getRelationshipClients()
throws SoapFault
{
Element[] xml = getRelationshipXML();
WsResourceClient[] clients = new WsResourceClient[xml.length];
for (int n = 0; n < xml.length; ++n)
{
Element eprXML = XmlUtils.getElement(xml[n], MuwsConstants.ACCESS_EPR_QNAME);
EndpointReference epr = new EndpointReference(eprXML);
clients[n] = new WsResourceClient(epr);
}
return clients;
}
public Element[] getRelationshipXML()
{
return _relationshipsXML;
}
public Element toXML()
{
return toXML(org.apache.muse.util.xml.XmlUtils.EMPTY_DOC);
}
public Element toXML(Document factory)
{
if (factory == null)
throw new NullPointerException(_MESSAGES.get("NullDocument"));
Element root = org.apache.muse.util.xml.XmlUtils.createElement(factory, MuwsConstants.QUERY_RESPONSE_QNAME);
Element[] relationships = getRelationshipXML();
for (int n = 0; n < relationships.length; ++n)
{
relationships[n] = (Element)factory.importNode(relationships[n], true);
root.appendChild(relationships[n]);
}
return root;
}
}