/** * Construct an instance based on the provided xpath expression, namespaces and * {@link TransformerFactory}. * @param expression the xpath expression for splitting. * @param namespaces the XML namespace for parsing. * @param transformerFactory the {@link TransformerFactory} * for parsing and building documents. * @since 4.3.19 */ public XPathMessageSplitter(String expression, Map<String, String> namespaces, TransformerFactory transformerFactory) { this(XPathExpressionFactory.createXPathExpression(expression, namespaces), transformerFactory); XPath xpath = XPathFactory.newInstance().newXPath(); SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); namespaceContext.setBindings(namespaces); xpath.setNamespaceContext(namespaceContext); try { this.jaxpExpression = xpath.compile(expression); } catch (XPathExpressionException e) { throw new org.springframework.xml.xpath.XPathParseException( "Could not compile [" + expression + "] to a XPathExpression: " + e.getMessage(), e); } }
/** * Binds the given prefix to the given namespaces. * * @see SimpleNamespaceContext#bindNamespaceUri(String,String) */ public final void startPrefixMapping(String prefix, String uri) { namespaceContext.bindNamespaceUri(prefix, uri); }
public String getPrefix(String namespaceUri) { List<String> prefixes = getPrefixesInternal(namespaceUri); return prefixes.isEmpty() ? null : prefixes.get(0); }
SimpleNamespaceContext simpleNamespaceContext = new SimpleNamespaceContext(); simpleNamespaceContext.setBindings(namespaceMappings); simpleNamespaceContext.bindNamespaceUri(binding.getKey(), binding.getValue()); simpleNamespaceContext.setBindings(namespaces); } else { simpleNamespaceContext.setBindings(dynamicBindings);
/** * Creates a {@code NamespaceContext} for the specified method, based on {@link Namespaces @Namespaces} and {@link * Namespace @Namespace} annotations. * * <p>This method will search for {@link Namespaces @Namespaces} and {@link Namespace @Namespace} annotation in the * given method, its class, and its package, in reverse order. That is: package-level annotations are overridden by * class-level annotations, which again are overridden by method-level annotations. * * @param method the method to create the namespace context for * @return the namespace context */ public static NamespaceContext getNamespaceContext(Method method) { Assert.notNull(method, "'method' must not be null"); SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); Class<?> endpointClass = method.getDeclaringClass(); Package endpointPackage = endpointClass.getPackage(); if (endpointPackage != null) { addNamespaceAnnotations(endpointPackage, namespaceContext); } addNamespaceAnnotations(endpointClass, namespaceContext); addNamespaceAnnotations(method, namespaceContext); return namespaceContext; }
private void startPrefixMapping(String prefix, String namespace) throws SAXException { if (!namespaceContext.getNamespaceURI(prefix).equals(namespace)) { getContentHandler().startPrefixMapping(prefix, namespace); namespaceContext.bindNamespaceUri(prefix, namespace); } }
/** Creates and returns a list of <code>NameSpace</code> objects from the <code>NamespaceContext</code>. */ private List<Namespace> createNamespaces(SimpleNamespaceContext namespaceContext) { List<Namespace> namespaces = new ArrayList<Namespace>(); String defaultNamespaceUri = namespaceContext.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX); if (StringUtils.hasLength(defaultNamespaceUri)) { namespaces.add(eventFactory.createNamespace(defaultNamespaceUri)); } for (Iterator<String> iterator = namespaceContext.getBoundPrefixes(); iterator.hasNext();) { String prefix = iterator.next(); String namespaceUri = namespaceContext.getNamespaceURI(prefix); namespaces.add(eventFactory.createNamespace(prefix, namespaceUri)); } return namespaces; }
@Override public String getPrefix(String namespaceUri) { Iterator<String> iterator = getPrefixes(namespaceUri); return iterator.hasNext() ? iterator.next() : null; }
@SuppressWarnings("unchecked") public boolean isSoap(Document document) { XPathFactory factory = XPathFactory.newInstance(); XPath xpath = factory.newXPath(); xpath.setNamespaceContext(SOAP_NAMESPACE_CONTEXT); try { for (Iterator iterator = SOAP_NAMESPACE_CONTEXT.getBoundPrefixes(); iterator.hasNext();) { String prefix = (String) iterator.next(); if ((Boolean)xpath.evaluate("/"+prefix+":Envelope", document, XPathConstants.BOOLEAN)) { return true; } } } catch (XPathExpressionException e) { logger.warn(e); } return false; } }
/** * Creates a {@code NamespaceContext} for the specified method, based on {@link Namespaces @Namespaces} and {@link * Namespace @Namespace} annotations. * <p/> * This method will search for {@link Namespaces @Namespaces} and {@link Namespace @Namespace} annotation in the * given method, its class, and its package, in reverse order. That is: package-level annotations are overridden by * class-level annotations, which again are overridden by method-level annotations. * * @param method the method to create the namespace context for * @return the namespace context */ public static NamespaceContext getNamespaceContext(Method method) { Assert.notNull(method, "'method' must not be null"); SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); Class<?> endpointClass = method.getDeclaringClass(); Package endpointPackage = endpointClass.getPackage(); if (endpointPackage != null) { addNamespaceAnnotations(endpointPackage, namespaceContext); } addNamespaceAnnotations(endpointClass, namespaceContext); addNamespaceAnnotations(method, namespaceContext); return namespaceContext; }
@Override protected void startElementInternal(QName name, Attributes attributes, SimpleNamespaceContext namespaceContext) throws XMLStreamException { streamWriter.writeStartElement(QNameUtils.getPrefix(name), name.getLocalPart(), name.getNamespaceURI()); String defaultNamespaceUri = namespaceContext.getNamespaceURI(""); if (StringUtils.hasLength(defaultNamespaceUri)) { streamWriter.writeNamespace("", defaultNamespaceUri); streamWriter.setDefaultNamespace(defaultNamespaceUri); } for (Iterator<String> iterator = namespaceContext.getBoundPrefixes(); iterator.hasNext();) { String prefix = iterator.next(); streamWriter.writeNamespace(prefix, namespaceContext.getNamespaceURI(prefix)); streamWriter.setPrefix(prefix, namespaceContext.getNamespaceURI(prefix)); } for (int i = 0; i < attributes.getLength(); i++) { QName attrName = QNameUtils.toQName(attributes.getURI(i), attributes.getQName(i)); String attrPrefix = QNameUtils.getPrefix(attrName); if (!("xmlns".equals(attrName.getLocalPart()) || "xmlns".equals(attrPrefix))) { streamWriter.writeAttribute(attrPrefix, attrName.getNamespaceURI(), attrName.getLocalPart(), attributes.getValue(i)); } } } }
@Override public String getPrefix(String namespaceUri) { Iterator<String> iterator = getPrefixes(namespaceUri); return iterator.hasNext() ? iterator.next() : null; }
/** * Sets namespace mapping for all XPath validations like {@link #assertXPath(String)} * @param namespaceMapping * @return */ public MessageValidator useNamespaceMapping(Map<String, String> namespaceMapping) { namespaceContext = new SimpleNamespaceContext(); namespaceContext.setBindings(namespaceMapping); return this; }
/** * Binds the given namespace as default namespace. * * @param namespaceUri the namespace uri */ public void bindDefaultNamespaceUri(String namespaceUri) { bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri); }
/** * Creates a {@code NamespaceContext} for the specified method, based on {@link Namespaces @Namespaces} and {@link * Namespace @Namespace} annotations. * * <p>This method will search for {@link Namespaces @Namespaces} and {@link Namespace @Namespace} annotation in the * given method, its class, and its package, in reverse order. That is: package-level annotations are overridden by * class-level annotations, which again are overridden by method-level annotations. * * @param method the method to create the namespace context for * @return the namespace context */ public static NamespaceContext getNamespaceContext(Method method) { Assert.notNull(method, "'method' must not be null"); SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); Class<?> endpointClass = method.getDeclaringClass(); Package endpointPackage = endpointClass.getPackage(); if (endpointPackage != null) { addNamespaceAnnotations(endpointPackage, namespaceContext); } addNamespaceAnnotations(endpointClass, namespaceContext); addNamespaceAnnotations(method, namespaceContext); return namespaceContext; }
public Iterator<String> getPrefixes(String namespaceUri) { return getPrefixesInternal(namespaceUri).iterator(); }
private synchronized XPath createXPath() { XPath xpath = xpathFactory.newXPath(); if (namespaces != null) { SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); namespaceContext.setBindings(namespaces); xpath.setNamespaceContext(namespaceContext); } return xpath; }
/** * Binds the given namespace as default namespace. * * @param namespaceUri the namespace uri */ public void bindDefaultNamespaceUri(String namespaceUri) { bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, namespaceUri); }
/** * Creates a {@code NamespaceContext} for the specified method, based on {@link Namespaces @Namespaces} and {@link * Namespace @Namespace} annotations. * * <p>This method will search for {@link Namespaces @Namespaces} and {@link Namespace @Namespace} annotation in the * given method, its class, and its package, in reverse order. That is: package-level annotations are overridden by * class-level annotations, which again are overridden by method-level annotations. * * @param method the method to create the namespace context for * @return the namespace context */ public static NamespaceContext getNamespaceContext(Method method) { Assert.notNull(method, "'method' must not be null"); SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext(); Class<?> endpointClass = method.getDeclaringClass(); Package endpointPackage = endpointClass.getPackage(); if (endpointPackage != null) { addNamespaceAnnotations(endpointPackage, namespaceContext); } addNamespaceAnnotations(endpointClass, namespaceContext); addNamespaceAnnotations(method, namespaceContext); return namespaceContext; }
@Override public Iterator<String> getPrefixes(String namespaceUri) { Set<String> prefixes = getPrefixesInternal(namespaceUri); prefixes = Collections.unmodifiableSet(prefixes); return prefixes.iterator(); }