From 578ad0d6bc408edf9e6c875156054374f5fd8337 Mon Sep 17 00:00:00 2001 From: Thomas <> Date: Mon, 22 Mar 2021 18:40:26 +0100 Subject: change to EGIZ codestyle --- .../moa/spss/api/xmlbind/RequestParserUtils.java | 310 ++++++++++----------- 1 file changed, 154 insertions(+), 156 deletions(-) (limited to 'moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/RequestParserUtils.java') diff --git a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/RequestParserUtils.java b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/RequestParserUtils.java index 99a64d9..173ecbf 100644 --- a/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/RequestParserUtils.java +++ b/moaSig/moa-sig-lib/src/main/java/at/gv/egovernment/moa/spss/api/xmlbind/RequestParserUtils.java @@ -42,165 +42,163 @@ import at.gv.egovernment.moaspss.util.XPathUtils; /** * Utility methods for parsing XML requests definied in the MOA XML schema. - * + * * @author Patrick Peck * @version $Id$ */ public class RequestParserUtils { - // - // XPath expressions for parsing parts of a request - // - private static final String MOA = Constants.MOA_PREFIX + ":"; - private static final String REFERENCE_ATTR_NAME = "Reference"; - private static final String MIME_TYPE_XPATH = MOA + "MimeType"; - private static final String DESCRIPTION_XPATH = MOA + "Description"; - private static final String TYPE_XPATH = MOA + "Type"; - private static final String XML_ASSOC_CONTENT_XPATH = MOA + "Content"; - private static final String CONTENT_XPATH = MOA + "Base64Content | " + MOA + "XMLContent |" + MOA + "LocRefContent"; - private static final String ANY_OTHER_XPATH = "*[namespace-uri() != \"" + Constants.MOA_NS_URI + "\"]"; - - /** - * Parse a XMLDataObjectAssociationType kind of DOM element. - * - * @param assocElem - * The XMLDataObjectAssociationType kind of DOM - * elmeent to parse. - * @return The XMLDataObjectAssociation API object containing - * the data from the XMLDataObjectAssociationType DOM - * element. - */ - public static XMLDataObjectAssociation parseXMLDataObjectAssociation(Element assocElem) { - SPSSFactory factory = SPSSFactory.getInstance(); - MetaInfo metaInfo = parseMetaInfo(assocElem); - Element contentElem = (Element) XPathUtils.selectSingleNode(assocElem, XML_ASSOC_CONTENT_XPATH); - Content content = parseContent(contentElem); - - return factory.createXMLDataObjectAssociation(metaInfo, content); - } - - /** - * Parse a MetaInfoType kind of DOM element. - * - * @param metaInfoElem - * The MetaInfoType kind of DOM element. - * @return The MetaInfo API object containing the data from the - * metaInfoElem. - */ - public static MetaInfo parseMetaInfo(Element metaInfoElem) { - SPSSFactory factory = SPSSFactory.getInstance(); - String mimeType = XPathUtils.getElementValue(metaInfoElem, MIME_TYPE_XPATH, null); - String description = XPathUtils.getElementValue(metaInfoElem, DESCRIPTION_XPATH, null); - NodeList anyOther = XPathUtils.selectNodeList(metaInfoElem, ANY_OTHER_XPATH); - String type = XPathUtils.getElementValue(metaInfoElem, TYPE_XPATH, null); - - return factory.createMetaInfo(mimeType, description, anyOther, type); - } - - /** - * Parse a ContentOptionalRefType or - * ContentRequiredRefType kind of DOM element. - * - * @param contentParentElem - * The DOM element being the parent of the content element. - * @return The Content API object containing the data from the - * given DOM element. - */ - public static Content parseContent(Element contentParentElem) { - SPSSFactory factory = SPSSFactory.getInstance(); - String referenceURI = contentParentElem.hasAttribute(REFERENCE_ATTR_NAME) - ? contentParentElem.getAttribute(REFERENCE_ATTR_NAME) : null; - Element contentElem = (Element) XPathUtils.selectSingleNode(contentParentElem, CONTENT_XPATH); - - if (contentElem == null) { - return factory.createContent(referenceURI); - } - - if ("Base64Content".equals(contentElem.getLocalName())) { - String base64String = DOMUtils.getText(contentElem); - return factory.createContent(Base64Utils.decodeToStream(base64String, true), referenceURI); - } else if ("LocRefContent".equals(contentElem.getLocalName())) { - String locationReferenceURI = DOMUtils.getText(contentElem); - return factory.createContent(locationReferenceURI, referenceURI); - } else { // "XMLContent".equals(contentElem.getLocalName()) - return factory.createContent(contentElem.getChildNodes(), referenceURI); - } - } - - /** - * Get the signing time from a Verfiy(CMS|XML)SignatureRequest. - * - * @param requestElem - * A Verify(CMS|XML)SignatureRequest DOM element. - * @param dateTimeXPath - * The XPath to lookup the DateTime element within - * the request. - * @return Date The date and time corresponding to the DateTime - * element in the request. If no DateTime element - * exists in the request, null is returned. - * @throws MOAApplicationException - * An error occurred during a parsing the DateTime - * element or creating the return value. - */ - public static Date parseDateTime(Element requestElem, String dateTimeXPath) throws MOAApplicationException { - - Element dateTimeElem; - String dateTimeText; - - // select the DateTime element - dateTimeElem = (Element) XPathUtils.selectSingleNode(requestElem, dateTimeXPath); - - // parse a date from the element value - if (dateTimeElem != null) { - dateTimeText = DOMUtils.getText(dateTimeElem); - try { - return DateTimeUtils.parseDateTime(dateTimeText); - } catch (ParseException e) { - throw new MOAApplicationException("1104", new Object[] { dateTimeText }); - } - } else { - return null; - } - } - - /** - * Get the signing time from a Verfiy(CMS|XML)SignatureRequest. - * - * @param requestElem - * A Verify(CMS|XML)SignatureRequest DOM element. - * @param dateTimeXPath - * The XPath to lookup the DateTime element within - * the request. - * @return Date The date and time corresponding to the DateTime - * element in the request. If no DateTime element - * exists in the request, null is returned. - * @throws MOAApplicationException - * An error occurred during a parsing the DateTime - * element or creating the return value. - */ - public static boolean parseExtendedValidation(Element requestElem, String extendedValidationXPath, - boolean defaultValue) throws MOAApplicationException { - - Element dateTimeElem; - String dateTimeText; - - // select the DateTime element - dateTimeElem = (Element) XPathUtils.selectSingleNode(requestElem, extendedValidationXPath); - - // parse a date from the element value - if (dateTimeElem != null) { - dateTimeText = DOMUtils.getText(dateTimeElem); - String xsdBoolean = dateTimeText.trim(); - if ("".equals(xsdBoolean)) - return defaultValue; - if("true".equalsIgnoreCase(xsdBoolean) || - "1".equalsIgnoreCase(xsdBoolean)) { - return true; - } else { - return false; - } - } else { - return defaultValue; - } - } + // + // XPath expressions for parsing parts of a request + // + private static final String MOA = Constants.MOA_PREFIX + ":"; + private static final String REFERENCE_ATTR_NAME = "Reference"; + private static final String MIME_TYPE_XPATH = MOA + "MimeType"; + private static final String DESCRIPTION_XPATH = MOA + "Description"; + private static final String TYPE_XPATH = MOA + "Type"; + private static final String XML_ASSOC_CONTENT_XPATH = MOA + "Content"; + private static final String CONTENT_XPATH = MOA + "Base64Content | " + MOA + "XMLContent |" + MOA + + "LocRefContent"; + private static final String ANY_OTHER_XPATH = "*[namespace-uri() != \"" + Constants.MOA_NS_URI + "\"]"; + + /** + * Parse a XMLDataObjectAssociationType kind of DOM element. + * + * @param assocElem The XMLDataObjectAssociationType kind of DOM + * elmeent to parse. + * @return The XMLDataObjectAssociation API object containing the + * data from the XMLDataObjectAssociationType DOM element. + */ + public static XMLDataObjectAssociation parseXMLDataObjectAssociation(Element assocElem) { + final SPSSFactory factory = SPSSFactory.getInstance(); + final MetaInfo metaInfo = parseMetaInfo(assocElem); + final Element contentElem = (Element) XPathUtils.selectSingleNode(assocElem, XML_ASSOC_CONTENT_XPATH); + final Content content = parseContent(contentElem); + + return factory.createXMLDataObjectAssociation(metaInfo, content); + } + + /** + * Parse a MetaInfoType kind of DOM element. + * + * @param metaInfoElem The MetaInfoType kind of DOM element. + * @return The MetaInfo API object containing the data from the + * metaInfoElem. + */ + public static MetaInfo parseMetaInfo(Element metaInfoElem) { + final SPSSFactory factory = SPSSFactory.getInstance(); + final String mimeType = XPathUtils.getElementValue(metaInfoElem, MIME_TYPE_XPATH, null); + final String description = XPathUtils.getElementValue(metaInfoElem, DESCRIPTION_XPATH, null); + final NodeList anyOther = XPathUtils.selectNodeList(metaInfoElem, ANY_OTHER_XPATH); + final String type = XPathUtils.getElementValue(metaInfoElem, TYPE_XPATH, null); + + return factory.createMetaInfo(mimeType, description, anyOther, type); + } + + /** + * Parse a ContentOptionalRefType or + * ContentRequiredRefType kind of DOM element. + * + * @param contentParentElem The DOM element being the parent of the content + * element. + * @return The Content API object containing the data from the + * given DOM element. + */ + public static Content parseContent(Element contentParentElem) { + final SPSSFactory factory = SPSSFactory.getInstance(); + final String referenceURI = contentParentElem.hasAttribute(REFERENCE_ATTR_NAME) + ? contentParentElem.getAttribute(REFERENCE_ATTR_NAME) + : null; + final Element contentElem = (Element) XPathUtils.selectSingleNode(contentParentElem, CONTENT_XPATH); + + if (contentElem == null) { + return factory.createContent(referenceURI); + } + + if ("Base64Content".equals(contentElem.getLocalName())) { + final String base64String = DOMUtils.getText(contentElem); + return factory.createContent(Base64Utils.decodeToStream(base64String, true), referenceURI); + } else if ("LocRefContent".equals(contentElem.getLocalName())) { + final String locationReferenceURI = DOMUtils.getText(contentElem); + return factory.createContent(locationReferenceURI, referenceURI); + } else { // "XMLContent".equals(contentElem.getLocalName()) + return factory.createContent(contentElem.getChildNodes(), referenceURI); + } + } + + /** + * Get the signing time from a Verfiy(CMS|XML)SignatureRequest. + * + * @param requestElem A Verify(CMS|XML)SignatureRequest DOM + * element. + * @param dateTimeXPath The XPath to lookup the DateTime element + * within the request. + * @return Date The date and time corresponding to the DateTime + * element in the request. If no DateTime element exists in + * the request, null is returned. + * @throws MOAApplicationException An error occurred during a parsing the + * DateTime element or creating the + * return value. + */ + public static Date parseDateTime(Element requestElem, String dateTimeXPath) throws MOAApplicationException { + + Element dateTimeElem; + String dateTimeText; + + // select the DateTime element + dateTimeElem = (Element) XPathUtils.selectSingleNode(requestElem, dateTimeXPath); + + // parse a date from the element value + if (dateTimeElem != null) { + dateTimeText = DOMUtils.getText(dateTimeElem); + try { + return DateTimeUtils.parseDateTime(dateTimeText); + } catch (final ParseException e) { + throw new MOAApplicationException("1104", new Object[] { dateTimeText }); + } + } else { + return null; + } + } + + /** + * Get the signing time from a Verfiy(CMS|XML)SignatureRequest. + * + * @param requestElem A Verify(CMS|XML)SignatureRequest DOM + * element. + * @param dateTimeXPath The XPath to lookup the DateTime element + * within the request. + * @return Date The date and time corresponding to the DateTime + * element in the request. If no DateTime element exists in + * the request, null is returned. + * @throws MOAApplicationException An error occurred during a parsing the + * DateTime element or creating the + * return value. + */ + public static boolean parseExtendedValidation(Element requestElem, String extendedValidationXPath, + boolean defaultValue) throws MOAApplicationException { + + Element dateTimeElem; + String dateTimeText; + + // select the DateTime element + dateTimeElem = (Element) XPathUtils.selectSingleNode(requestElem, extendedValidationXPath); + + // parse a date from the element value + if (dateTimeElem != null) { + dateTimeText = DOMUtils.getText(dateTimeElem); + final String xsdBoolean = dateTimeText.trim(); + if ("".equals(xsdBoolean)) { + return defaultValue; + } + if ("true".equalsIgnoreCase(xsdBoolean) || + "1".equalsIgnoreCase(xsdBoolean)) { + return true; + } else { + return false; + } + } else { + return defaultValue; + } + } } -- cgit v1.2.3