/*=============================================================================*
* Copyright 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.ws.notification.topics;
import org.apache.commons.lang.StringUtils;
import org.apache.ws.UnsupportedNamespaceException;
import org.apache.ws.notification.topics.expression.TopicExpression;
import org.apache.ws.notification.topics.v2004_06.TopicsConstants;
import org.apache.ws.notification.topics.v2004_06.TopicsTypeWriterImpl;
import org.apache.ws.util.XmlBeanUtils;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import javax.xml.namespace.QName;
/**
* A writer that can serialize various WS-Topics types to strongly-typed {@link XmlObject}s.
* It is capable of supporting multiple versions of the WS-Topics specification.
*/
public abstract class TopicsTypeWriter
{
/**
* DOCUMENT_ME
*
* @param schemaNamespace DOCUMENT_ME
*
* @return DOCUMENT_ME
*
* @throws UnsupportedNamespaceException DOCUMENT_ME
*/
public static TopicsTypeWriter newInstance( String schemaNamespace )
throws UnsupportedNamespaceException
{
if ( schemaNamespace.equals( TopicsConstants.NSURI_WSTOP_SCHEMA ) )
{
return new TopicsTypeWriterImpl( );
}
else
{
throw new UnsupportedNamespaceException( schemaNamespace
+ " is not the namespace of a supported version of the WS-Topics schema." );
}
}
/**
* DOCUMENT_ME
*
* @param topicExpr DOCUMENT_ME
* @param elemName DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public abstract XmlObject toXmlObject( TopicExpression topicExpr,
QName elemName );
/**
* DOCUMENT_ME
*
* @param topicNs DOCUMENT_ME
*
* @return DOCUMENT_ME
*/
public abstract XmlObject toXmlObject( TopicNamespace topicNs );
/**
* DOCUMENT_ME
*
* @param topicExpr DOCUMENT_ME
* @param topicExprXBean DOCUMENT_ME
*/
protected void copyContent( TopicExpression topicExpr,
XmlObject topicExprXBean )
{
Object content = topicExpr.getContent( );
if ( content instanceof QName )
{
QName topicPath = (QName) content;
XmlBeanUtils.setValue( topicExprXBean,
toString( topicPath, topicExprXBean ) );
}
else if ( content instanceof QName[] )
{
QName[] topicPaths = (QName[]) content;
XmlBeanUtils.setValue( topicExprXBean,
toString( topicPaths, topicExprXBean ) );
}
}
private String toString( QName topicPath,
XmlObject targetXBean )
{
StringBuffer strBuf = new StringBuffer( );
if ( StringUtils.isNotEmpty( topicPath.getNamespaceURI( ) ) )
{
XmlCursor xCursor = targetXBean.newCursor( );
String prefix = xCursor.prefixForNamespace( topicPath.getNamespaceURI( ) );
xCursor.dispose( );
strBuf.append( prefix );
strBuf.append( ':' );
}
strBuf.append( topicPath.getLocalPart( ) );
return strBuf.toString( );
}
private String toString( QName[] topicPaths,
XmlObject targetXBean )
{
StringBuffer strBuf = new StringBuffer( );
for ( int i = 0; i < ( topicPaths.length - 1 ); i++ )
{
strBuf.append( toString( topicPaths[i], targetXBean ) );
strBuf.append( '|' );
}
strBuf.append( toString( topicPaths[topicPaths.length - 1], targetXBean ) );
return strBuf.toString( );
}
}