/*=============================================================================*
* 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 java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.xml.namespace.QName;
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.ws.dm.muws.Match;
import org.apache.muse.ws.resource.WsResource;
/**
*
* ComplexMatch is an abstract class for any pbm:Match that supports
* subexpressions (sub-Matches). It provides all of the collection operations
* needed to store and evaluate the subexpressions but provides no default
* implementation for matches(Resource). Concrete subclasses must define how
* the Match's subexpressions are combined to evaluate another resource.
*
* @author Dan Jemiolo (danj)
*
*/
public abstract class ComplexMatch implements Match
{
//
// Used to lookup all exception messages
//
private static Messages _MESSAGES = MessagesFactory.get(ComplexMatch.class);
//
// All sub-matches (children) of this match
//
private List _matches = new LinkedList();
//
// The local resource that is defining this correlation
//
private WsResource _resource = null;
/**
*
* @param resource
* The resource that is defining the CorrelatableProperties
* capability and thus, this correlation expression.
*
*/
public ComplexMatch(WsResource resource)
{
if (resource == null)
throw new NullPointerException(_MESSAGES.get("NullOwner"));
_resource = resource;
}
/**
*
* @param resource
* The resource that is defining the CorrelatableProperties
* capability and thus, this correlation expression.
*
* @param xml
* The DOM Element representing the simple pbm:Match.
*
*/
public ComplexMatch(WsResource resource, Element xml)
{
if (resource == null)
throw new NullPointerException(_MESSAGES.get("NullOwner"));
if (xml == null)
throw new NullPointerException(_MESSAGES.get("NullMatchElement"));
_resource = resource;
//
// parse all sub-elements as sub-Matches
//
Element[] children = XmlUtils.getAllElements(xml);
if (children.length == 0)
throw new RuntimeException(_MESSAGES.get("NoChildrenFound"));
MatchFactory factory = MatchFactory.getInstance();
for (int n = 0; n < children.length; ++n)
{
Match next = factory.createMatch(_resource, children[n]);
_matches.add(next);
}
}
public void addMatch(Match child)
{
if (child == null)
throw new NullPointerException(_MESSAGES.get("NullMatch"));
_matches.add(child);
}
public List getMatches()
{
return Collections.unmodifiableList(_matches);
}
/**
*
* @return The name of the XML element that defines the concrete match.
*
*/
protected abstract QName getRootQName();
public WsResource getWsResource()
{
return _resource;
}
public void removeMatch(Match child)
{
if (child == null)
throw new NullPointerException(_MESSAGES.get("NullMatch"));
_matches.remove(child);
}
public Element toXML()
{
return toXML(XmlUtils.EMPTY_DOC);
}
public Element toXML(Document doc)
{
if (doc == null)
throw new NullPointerException(_MESSAGES.get("NullDocument"));
Element root = XmlUtils.createElement(doc, getRootQName());
List matches = getMatches();
if (matches.isEmpty())
throw new RuntimeException(_MESSAGES.get("NoChildrenFound"));
Iterator i = matches.iterator();
while (i.hasNext())
{
Match next = (Match)i.next();
root.appendChild(next.toXML(doc));
}
return root;
}
}