From 9c5b6686313cdaffa018f8048f0d751f11fc8c26 Mon Sep 17 00:00:00 2001 From: "harald.bratko" Date: Fri, 12 Aug 2005 13:00:53 +0000 Subject: Added methods for retrieving SecurityLayer prefixes. git-svn-id: https://joinup.ec.europa.eu/svn/moa-idspss/trunk@440 d688527b-c9ab-4aba-bd8d-4036d912da1d --- .../src/at/gv/egovernment/moa/util/XPathUtils.java | 105 +++++++++++++++++++++ 1 file changed, 105 insertions(+) (limited to 'common/src/at') diff --git a/common/src/at/gv/egovernment/moa/util/XPathUtils.java b/common/src/at/gv/egovernment/moa/util/XPathUtils.java index 4ee160476..aa5a4fec5 100644 --- a/common/src/at/gv/egovernment/moa/util/XPathUtils.java +++ b/common/src/at/gv/egovernment/moa/util/XPathUtils.java @@ -412,5 +412,110 @@ public class XPathUtils { Attr attr = (Attr) XPathUtils.selectSingleNode(root, xpath); return attr != null ? attr.getValue() : def; } + + /** + * Return the SecurityLayer namespace prefix of the context element. + * If the context element has no prefix explicitely set (i.e. is specified + * within the default namespace), the method sets the prefix + * according to the value of the xmlns attribute of the context + * element. + * The returned prefix is needed for evaluating XPATH expressions. + * + * @param contextElement The element to get a prefix from. + * + * @return The string sl10, sl11 or sl, + * depending on the SecurityLayer namespace of the contextElement. + * + * throws XpathException If the element has no prefix or no valid SecurityLayer + * namespace is used as default namespace. + */ + public static String getSlPrefix (Element contextElement) throws XPathException { + String slPrefix = contextElement.getPrefix(); + if (slPrefix != null) { + return slPrefix; + } else { + String nameSpace = contextElement.getAttribute("xmlns"); + + if (nameSpace.equals(Constants.SL10_NS_URI)) { + slPrefix = Constants.SL10_PREFIX; + } else if (nameSpace.equals(Constants.SL12_NS_URI)) { + slPrefix = Constants.SL12_PREFIX; + } else if (nameSpace.equals(Constants.SL11_NS_URI)) { + slPrefix = Constants.SL11_PREFIX; + } else { + MessageProvider msg = MessageProvider.getInstance(); + String message = msg.getMessage("xpath.00", new Object[] { "Ungültiger SecurityLayer Namespace: \"" + nameSpace + "\"."}); + throw new XPathException(message, null); + + } + + return slPrefix; + } + } + + + /** + * Return the SecurityLayer namespace prefix of the context element. + * If the context element is not the element that lies within the + * SecurityLayer namespace. The Securitylayer namespace is derived from + * the xmlns:sl10, sl11 or sl + * attribute of the context element. + * + * The returned prefix is needed for evaluating XPATH expressions. + * + * @param contextElement The element to get a prefix for the Securitylayer namespace, + * that is used within the corresponding document. + * + * @return The string sl10, sl11 or sl, + * depending on the SecurityLayer namespace of the contextElement. + * + * throws XPathException If no (vlalid) SecurityLayer namespace prefix or namespace + * is defined. + */ + public static String getSlPrefixFromNoRoot (Element contextElement) throws XPathException { + + String slPrefix = checkSLnsDeclaration(contextElement, Constants.SL10_PREFIX, Constants.SL10_NS_URI); + if (slPrefix == null) { + slPrefix = checkSLnsDeclaration(contextElement, Constants.SL11_PREFIX, Constants.SL11_NS_URI); + } + if (slPrefix == null) { + slPrefix = checkSLnsDeclaration(contextElement, Constants.SL12_PREFIX, Constants.SL12_NS_URI); + } + + return slPrefix; + + } + + /** + * Checks if the context element has an attribute xmlns:slPrefix and + * if the prefix of that attribute corresponds with a valid SecurityLayer namespace. + * + * @param contextElement The element to be checked. + * @param slPrefix The prefix which should be checked. Must be a valid SecurityLayer + * namespace prefix. + * @param slNameSpace The SecurityLayer namespace that corresponds to the specified prefix. + * + * @return The valid SecurityLayer prefix or null if this prefix is + * not used. + * @throws XPathException + */ + private static String checkSLnsDeclaration(Element contextElement, String slPrefix, String slNameSpace) + throws XPathException + { + String nsAtt = "xmlns:" + slPrefix; + String nameSpace = contextElement.getAttribute(nsAtt); + if (nameSpace == "") { + return null; + } else { + // check if namespace is correct + if (nameSpace.equals(slNameSpace)) { + return slPrefix; + } else { + MessageProvider msg = MessageProvider.getInstance(); + String message = msg.getMessage("xpath.00", new Object[] { "Ungültiger SecurityLayer Namespace: \"" + nameSpace + "\"."}); + throw new XPathException(message, null); + } + } + } } -- cgit v1.2.3