package at.gv.egovernment.moa.spss.server.invoke;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import iaik.server.modules.xml.Base64Transformation;
import iaik.server.modules.xml.Canonicalization;
import iaik.server.modules.xml.EnvelopedSignatureTransformation;
import iaik.server.modules.xml.Transformation;
import iaik.server.modules.xml.XPath2Transformation;
import iaik.server.modules.xml.XPathTransformation;
import iaik.server.modules.xml.XSLTTransformation;
import at.gv.egovernment.moa.spss.MOAApplicationException;
import at.gv.egovernment.moa.spss.api.common.ExclusiveCanonicalizationTransform;
import at.gv.egovernment.moa.spss.api.common.Transform;
import at.gv.egovernment.moa.spss.api.common.XPathFilter;
import at.gv.egovernment.moa.spss.api.common.XPathFilter2Transform;
import at.gv.egovernment.moa.spss.api.common.XPathTransform;
import at.gv.egovernment.moa.spss.api.common.XSLTTransform;
import at.gv.egovernment.moa.spss.server.iaik.xml.Base64TransformationImpl;
import at.gv.egovernment.moa.spss.server.iaik.xml.CanonicalizationImpl;
import at.gv.egovernment.moa.spss.server.iaik.xml.EnvelopedSignatureTransformationImpl;
import at.gv.egovernment.moa.spss.server.iaik.xml.ExclusiveCanonicalizationImpl;
import at.gv.egovernment.moa.spss.server.iaik.xml.XPath2FilterImpl;
import at.gv.egovernment.moa.spss.server.iaik.xml.XPath2TransformationImpl;
import at.gv.egovernment.moa.spss.server.iaik.xml.XPathTransformationImpl;
import at.gv.egovernment.moa.spss.server.iaik.xml.XSLTTransformationImpl;
/**
* A factory to create Transformation
objects from
* Transform
objects.
*
* @author Patrick Peck
* @version $Id$
*/
public class TransformationFactory {
/** The single instance of this class. */
private static TransformationFactory instance = null;
/** Maps XPathFilter
filter types to
* XPath2Transformation
filter types. */
private static Map FILTER_TYPE_MAPPING;
static {
FILTER_TYPE_MAPPING = new HashMap();
FILTER_TYPE_MAPPING.put(
XPathFilter.INTERSECT_TYPE,
XPath2Transformation.XPath2Filter.INTERSECTION);
FILTER_TYPE_MAPPING.put(
XPathFilter.SUBTRACT_TYPE,
XPath2Transformation.XPath2Filter.SUBTRACTION);
FILTER_TYPE_MAPPING.put(
XPathFilter.UNION_TYPE,
XPath2Transformation.XPath2Filter.UNION);
}
/**
* Get the single instance of the factory.
*
* @return TransformationFactory The single instance.
*/
public static synchronized TransformationFactory getInstance() {
if (instance == null) {
instance = new TransformationFactory();
}
return instance;
}
/**
* Create a new TransformationFactory
.
*
* Protected to disallow multiple instances.
*/
protected TransformationFactory() {
}
/**
* Create a Transformation
based on a
* Transform
object.
*
* @param transform The Transform
object to extract
* transformation data from.
* @return The transformation contained in the transform
* object.
* @throws MOAApplicationException An error occured creating the
* Transformation
. See exception message for details.
*/
public Transformation createTransformation(Transform transform)
throws MOAApplicationException {
String algorithmUri = transform.getAlgorithmURI();
if (Canonicalization.CANONICAL_XML.equals(algorithmUri)
|| Canonicalization.CANONICAL_XML_WITH_COMMENTS.equals(algorithmUri)) {
return createC14nTransformation(algorithmUri);
} else if (
Canonicalization.EXCLUSIVE_CANONICAL_XML.equals(algorithmUri)
|| Canonicalization.EXCLUSIVE_CANONICAL_XML_WITH_COMMENTS.equals(
algorithmUri)) {
return createExclusiveC14nTransformation(
(ExclusiveCanonicalizationTransform) transform);
} else if (Base64Transformation.ALL.contains(algorithmUri)) {
return createBase64Transformation();
} else if (EnvelopedSignatureTransformation.ALL.contains(algorithmUri)) {
return createEnvelopedSignatureTransformation();
} else if (XPathTransformation.ALL.contains(algorithmUri)) {
return createXPathTransformation((XPathTransform) transform);
} else if (XPath2Transformation.ALL.contains(algorithmUri)) {
return createXPath2Transformation((XPathFilter2Transform) transform);
} else if (XSLTTransformation.ALL.contains(algorithmUri)) {
return createXSLTTransformation((XSLTTransform) transform);
} else {
throw new MOAApplicationException("1108", new Object[] { algorithmUri });
}
}
/**
* Create a List
of Transformation
s from a
* List
of Transform
s.
*
* @param transforms The List
containing the
* Transform
s.
* @return The List
of Transformation
s corresponding
* to the transforms
.
* @throws MOAApplicationException An error occurred building one of the
* transformations. See exception message for details.
*/
public List createTransformationList(List transforms)
throws MOAApplicationException {
List transformationList = new ArrayList();
Iterator trIter;
for (trIter = transforms.iterator(); trIter.hasNext();) {
Transform transform = (Transform) trIter.next();
transformationList.add(createTransformation(transform));
}
return transformationList;
}
/**
* Create a Canonicalization
.
*
* @param algorithmUri The algorithm URI of the canonicalization.
* @return The Canonicalization
.
*/
private Transformation createC14nTransformation(String algorithmUri) {
return new CanonicalizationImpl(algorithmUri);
}
/**
* Create a ExclusiveCanonicalization
.
*
* @param transform The ExclusiveCanonicalizationTransform
* containing the transformation data.
* @return The ExclusiveCanonicalization
.
*/
private Transformation createExclusiveC14nTransformation(ExclusiveCanonicalizationTransform transform) {
return new ExclusiveCanonicalizationImpl(
transform.getAlgorithmURI(),
transform.getInclusiveNamespacePrefixes());
}
/**
* Create a Base64Transformation
.
*
* @return The
*/
private Transformation createBase64Transformation() {
return new Base64TransformationImpl();
}
/**
* Create an EnvelopedSignatureTransformation
.
*
* @return An EnvelopedSignatureTransformation
.
*/
private Transformation createEnvelopedSignatureTransformation() {
return new EnvelopedSignatureTransformationImpl();
}
/**
* Create an XPathTransformation
.
*
* @param transform The Transform
object containing the
* XPath transformation.
* @return An XPathTransformation
corresponding the
* transformation given in transform
.
* @throws MOAApplicationException An error occurred creating the
* Transformation
.
*/
private Transformation createXPathTransformation(XPathTransform transform)
throws MOAApplicationException {
return new XPathTransformationImpl(
transform.getXPathExpression(),
transform.getNamespaceDeclarations());
}
/**
* Create an XPath2Transformation
.
*
* @param transform The Transform
object containing the
* XPath filter transformation.
* @return An XPath2Transformation
corresponding the
* transformation given in transform
.
* @throws MOAApplicationException An error occurred creating the
* Transformation
.
*/
private Transformation createXPath2Transformation(XPathFilter2Transform transform)
throws MOAApplicationException {
XPath2TransformationImpl xpath2 = new XPath2TransformationImpl();
Iterator iter;
for (iter = transform.getFilters().iterator(); iter.hasNext();) {
XPathFilter filter = (XPathFilter) iter.next();
String mappedFilterType =
(String) FILTER_TYPE_MAPPING.get(filter.getFilterType());
XPath2FilterImpl mappedFilter =
new XPath2FilterImpl(
mappedFilterType,
filter.getXPathExpression(),
filter.getNamespaceDeclarations());
xpath2.addXPathFilter(mappedFilter);
}
if (xpath2.getXPathFilters().size() == 0) {
throw new MOAApplicationException("2216", null);
}
return xpath2;
}
/**
* Create an XSLTTransformation
.
*
* @param transform The Transform
containing the XSLT stylesheet.
* @return An XSLTTransformation
corresponding the transformation
* given in transform
.
* @throws MOAApplicationException An error occurred creating the
* Transformation
.
*/
private Transformation createXSLTTransformation(XSLTTransform transform)
throws MOAApplicationException {
return new XSLTTransformationImpl(transform.getStylesheet());
}
}