/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.ws.notification.Filter;
import org.apache.muse.ws.notification.NotificationMessage;
import org.apache.muse.ws.notification.WsnConstants;
/**
*
* FilterCollection is a set of WSN subscription filters that a notification
* producer must evaluate when determining whether it should send a message
* to a consumer or not. It implements the {@linkplain Filter Filter} interface
* so that it can be treated as a single filter by filter-evaluating code; this
* allows us to use FilterCollection objects in places where a Filter is
* specified even though FilterCollection was not part of the original 2.x API.
*
* @author Dan Jemiolo (danj)
*
*/
public class FilterCollection implements Filter
{
private Collection _filters = new ArrayList();
public boolean accepts(NotificationMessage message)
{
Iterator i = getFilters().iterator();
while (i.hasNext())
{
Filter next = (Filter)i.next();
//
// only one filter has to fail for the whole thing to fail
//
if (!next.accepts(message))
return false;
}
return true;
}
public void addFilter(Filter filter)
{
_filters.add(filter);
}
public Collection getFilters()
{
return Collections.unmodifiableCollection(_filters);
}
public Element toXML()
{
return toXML(XmlUtils.EMPTY_DOC);
}
public Element toXML(Document doc)
{
Element filterXML = XmlUtils.createElement(doc, WsnConstants.FILTER_QNAME);
Iterator i = getFilters().iterator();
while (i.hasNext())
{
Filter next = (Filter)i.next();
Element nextXML = next.toXML(doc);
//
// we have to 'move' instead of 'appendChild' because the other
// Filter types already add a <Filter/> element as part of their
// toXML() implementations, and we can't change this for reasons
// of backwards compatibility. therefore, we just take the element
// under the <Filter/> and move it under our new <Filter/>
//
XmlUtils.moveSubTree(nextXML, filterXML);
}
return filterXML;
}
}