/*=============================================================================*
* 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.notification.topics.impl;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
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.XmlUtils;
import org.apache.muse.util.xml.XsdUtils;
import org.apache.muse.ws.notification.faults.InvalidTopicExpressionFault;
import org.apache.muse.ws.notification.topics.Topic;
import org.apache.muse.ws.notification.topics.TopicNamespace;
import org.apache.muse.ws.notification.topics.WstConstants;
import org.apache.muse.ws.resource.basefaults.BaseFault;
/**
*
* SimpleTopicNamespace is Muse's default implementation of the wsnt:TopicNamespace
* data structure defined in WS-Notification v1.3 and WS-Topics v1.3.
*
* @author Dan Jemiolo (danj)
*
*/
public class SimpleTopicNamespace implements TopicNamespace
{
private static Messages _MESSAGES = MessagesFactory.get(SimpleTopicNamespace.class);
private String _name = null;
private Map _rootTopics = new LinkedHashMap();
private String _targetNamespace = null;
public SimpleTopicNamespace(Element root)
throws BaseFault
{
if (root == null)
throw new NullPointerException(_MESSAGES.get("NullTopicSpaceElement"));
_targetNamespace = root.getAttribute(XmlUtils.TARGET_NS);
if (_targetNamespace == null)
throw new InvalidTopicExpressionFault(_MESSAGES.get("NoTopicSpaceNS"));
_name = root.getAttribute(XsdUtils.NAME);
Element[] children = XmlUtils.getElements(root, WstConstants.TOPIC_QNAME);
for (int n = 0; n < children.length; ++n)
{
Topic topic = new SimpleTopic(children[n], this);
addTopic(topic);
}
}
public SimpleTopicNamespace(String targetNamespace)
{
if (targetNamespace == null)
throw new NullPointerException(_MESSAGES.get("NullTargetNS"));
_targetNamespace = targetNamespace;
}
public synchronized final void addTopic(Topic topic)
throws BaseFault
{
if (topic == null)
throw new NullPointerException(_MESSAGES.get("NullTopic"));
String name = topic.getName();
if (_rootTopics.containsKey(name))
{
Object[] filler = { name, getTargetNamespace() };
throw new InvalidTopicExpressionFault(_MESSAGES.get("TopicExists", filler));
}
TopicNamespace topicSpace = topic.getTopicNamespace();
if (topicSpace != this)
{
Object[] filler = { topicSpace.getTargetNamespace(), getTargetNamespace() };
throw new InvalidTopicExpressionFault(_MESSAGES.get("InvalidTopicNS", filler));
}
_rootTopics.put(name, topic);
}
/**
*
* @return True if the two topic namespaces have the same target namespace.
*
*/
public boolean equals(Object obj)
{
if (obj == null)
return false;
TopicNamespace that = (TopicNamespace)obj;
return getTargetNamespace().equals(that.getTargetNamespace());
}
public synchronized final String getName()
{
return _name;
}
public synchronized final Collection getRootTopics()
{
return Collections.unmodifiableCollection(_rootTopics.values());
}
public synchronized final String getTargetNamespace()
{
return _targetNamespace;
}
public synchronized final Topic getTopic(String topicName)
{
return (Topic)_rootTopics.get(topicName);
}
/**
*
* @return A hash code based on the same values used for testing equality.
*
* @see #equals(Object)
*
*/
public int hashCode()
{
return getTargetNamespace().hashCode();
}
public synchronized final boolean hasTopic(String topicName)
{
return getTopic(topicName) != null;
}
public synchronized final void removeTopic(String topicName)
{
if (topicName == null)
throw new NullPointerException(_MESSAGES.get("NullTopicName"));
_rootTopics.remove(topicName);
}
public synchronized final void setName(String name)
{
_name = name;
}
public synchronized String toString()
{
return XmlUtils.toString(toXML(), false);
}
public synchronized Element toXML()
{
return toXML(XmlUtils.EMPTY_DOC);
}
public synchronized Element toXML(Document doc)
{
if (doc == null)
throw new NullPointerException(_MESSAGES.get("NullDocument"));
Element topicSpace = XmlUtils.createElement(doc, WstConstants.TOPIC_NAMESPACE_QNAME);
topicSpace.setAttribute(XmlUtils.TARGET_NS, getTargetNamespace());
String name = getName();
if (name != null)
topicSpace.setAttribute(XsdUtils.NAME, name);
Iterator i = getRootTopics().iterator();
while (i.hasNext())
{
Topic topic = (Topic)i.next();
Element topicXML = topic.toXML(doc);
topicSpace.appendChild(topicXML);
}
return topicSpace;
}
}