From 0872d2d8a64fd701776b272f49222428d8def07f Mon Sep 17 00:00:00 2001 From: Andreas Fitzek Date: Tue, 3 Nov 2015 14:38:34 +0100 Subject: initial commit --- .../spss/server/invoke/TransformationFactory.java | 282 +++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/TransformationFactory.java (limited to 'moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/TransformationFactory.java') diff --git a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/TransformationFactory.java b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/TransformationFactory.java new file mode 100644 index 0000000..7842f14 --- /dev/null +++ b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/server/invoke/TransformationFactory.java @@ -0,0 +1,282 @@ +/* + * Copyright 2003 Federal Chancellery Austria + * MOA-SPSS has been developed in a cooperation between BRZ, the Federal + * Chancellery Austria - ICT staff unit, and Graz University of Technology. + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by + * the European Commission - subsequent versions of the EUPL (the "Licence"); + * You may not use this work except in compliance with the Licence. + * You may obtain a copy of the Licence at: + * http://www.osor.eu/eupl/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the Licence is distributed on an "AS IS" basis, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the Licence for the specific language governing permissions and + * limitations under the Licence. + * + * This product combines work with different licenses. See the "NOTICE" text + * file for details on the various modules and licenses. + * The "NOTICE" text file is part of the distribution. Any derivative works + * that you distribute must include a readable copy of the "NOTICE" text file. + */ + + +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 Transformations from a + * List of Transforms. + * + * @param transforms The List containing the + * Transforms. + * @return The List of Transformations 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()); + } + +} -- cgit v1.2.3