Skip to content

Commit

Permalink
[bugfix] Node#getNamespaceURI and Node#getPrefix must return null unl…
Browse files Browse the repository at this point in the history
…ess a namespace or prefix is explicitly set at creation time
  • Loading branch information
adamretter committed Aug 12, 2017
1 parent 4e2df64 commit a9c76a0
Show file tree
Hide file tree
Showing 26 changed files with 274 additions and 182 deletions.
Expand Up @@ -37,6 +37,7 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.XMLConstants;
import javax.xml.namespace.QName;

/**
Expand Down Expand Up @@ -143,7 +144,7 @@ public boolean hasNoNsChild() {
final NodeList children = element.getNode().getChildNodes();
for(int i = 0; i < children.getLength(); i++) {
final Node child = children.item(i);
if(child.getNamespaceURI() == null && child.getPrefix() == null || child.getNamespaceURI().equalsIgnoreCase("")) {
if(child.getNamespaceURI() == null && child.getPrefix() == null || child.getNamespaceURI().equals(XMLConstants.NULL_NS_URI)) {
return true;
}
}
Expand Down Expand Up @@ -173,7 +174,7 @@ public void noOtherNCNameAttribute(final String[] names, String[] forbidden_ns)
final String ns = attr.getNamespaceURI();

if( Arrays.binarySearch(sorted_ns, ns) >= 0 ) {
if(attr.getNamespaceURI().equals(HttpConstants.HTTP_CLIENT_NS_URI)) {
if(ns != null && ns.equals(HttpConstants.HTTP_CLIENT_NS_URI)) {
throw new ToolsException("@" + attr_name + " in namespace " + ns + " not allowed on " + getDisplayName());
}
} else if ( ! "".equals(ns) ) {
Expand Down Expand Up @@ -275,7 +276,8 @@ private List<org.w3c.dom.Element> getElements() {
final Node child = children.item(i);
if(child.getNodeType() == Node.ELEMENT_NODE) {
if(inNamespaceURI != null) {
if(inNamespaceURI.equals(child.getNamespaceURI())){
final String ns = child.getNamespaceURI();
if(ns != null && inNamespaceURI.equals(ns)){
elements.add((org.w3c.dom.Element)child);
}
} else {
Expand Down
Expand Up @@ -279,8 +279,10 @@ private void compressFile(OutputStream os, File file, boolean useHierarchy, Stri
private void compressElement(OutputStream os, Element element, boolean useHierarchy, String stripOffset) throws XPathException
{

if(!(element.getNodeName().equals("entry") || element.getNamespaceURI().length() > 0))
final String ns = element.getNamespaceURI();
if(!(element.getNodeName().equals("entry") || (ns != null && ns.length() > 0))) {
throw new XPathException(this, "Item must be type of xs:anyURI or element entry.");
}

if(element.getChildNodes().getLength() > 1)
throw new XPathException(this, "Entry content is not valid XML fragment.");
Expand Down
Expand Up @@ -163,7 +163,8 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
post.setRequestEntity(entity);
} else if (isCalledAs("post-form")) {
final Node nPayload = ((NodeValue) payload).getNode();
if ((nPayload instanceof Element) && nPayload.getNamespaceURI().equals(HTTPClientModule.NAMESPACE_URI) && nPayload.getLocalName().equals("fields")) {
final String ns = nPayload.getNamespaceURI();
if ((nPayload instanceof Element) && ns != null && ns.equals(HTTPClientModule.NAMESPACE_URI) && nPayload.getLocalName().equals("fields")) {
final Part[] parts = parseFields((Element) nPayload);
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
} else {
Expand Down
Expand Up @@ -366,7 +366,8 @@ public ExecuteFunction( XQueryContext context, FunctionSignature signature )
if( stmt instanceof PreparedStatement ) {
Element parametersElement = (Element)args[2].itemAt( 0 );

if( parametersElement.getNamespaceURI().equals( SQLModule.NAMESPACE_URI ) && parametersElement.getLocalName().equals( PARAMETERS_ELEMENT_NAME ) ) {
final String ns = parametersElement.getNamespaceURI();
if(ns != null && ns.equals( SQLModule.NAMESPACE_URI ) && parametersElement.getLocalName().equals( PARAMETERS_ELEMENT_NAME ) ) {
NodeList paramElements = parametersElement.getElementsByTagNameNS( SQLModule.NAMESPACE_URI, PARAM_ELEMENT_NAME );

builder.startElement( new QName( PARAMETERS_ELEMENT_NAME, SQLModule.NAMESPACE_URI, SQLModule.PREFIX ), null );
Expand Down Expand Up @@ -428,7 +429,8 @@ public ExecuteFunction( XQueryContext context, FunctionSignature signature )

private void setParametersOnPreparedStatement( Statement stmt, Element parametersElement ) throws SQLException, XPathException
{
if (parametersElement.getNamespaceURI().equals(SQLModule.NAMESPACE_URI) && parametersElement.getLocalName().equals(PARAMETERS_ELEMENT_NAME)) {
final String ns = parametersElement.getNamespaceURI();
if (ns != null && ns.equals(SQLModule.NAMESPACE_URI) && parametersElement.getLocalName().equals(PARAMETERS_ELEMENT_NAME)) {
NodeList paramElements = parametersElement.getElementsByTagNameNS(SQLModule.NAMESPACE_URI, PARAM_ELEMENT_NAME);

for (int i = 0; i < paramElements.getLength(); i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/org/exist/collections/CollectionConfiguration.java
Expand Up @@ -130,7 +130,7 @@ protected void read(final DBBroker broker, final Document doc, final boolean che
"' in configuration document. Got element '" + root.getLocalName() + "'", checkOnly);
return;
}
if (!NAMESPACE.equals(root.getNamespaceURI())) {
if (root.getNamespaceURI() == null || !NAMESPACE.equals(root.getNamespaceURI())) {
throwOrLog("Expected namespace '" + NAMESPACE +
"' for element '" + PARAMETER_ELEMENT +
"' in configuration document. Got '" + root.getNamespaceURI() + "'", checkOnly);
Expand Down
15 changes: 9 additions & 6 deletions src/org/exist/config/ConfigurationImpl.java
Expand Up @@ -92,8 +92,9 @@ public List<Configuration> getConfigurations(String name) {
if (child.getNodeType() == Node.ELEMENT_NODE) {

final Element el = (Element)child;

if (name.equals( el.getLocalName() ) && NS.equals( el.getNamespaceURI() )) {

final String ns = el.getNamespaceURI();
if (name.equals( el.getLocalName() ) && ns != null && NS.equals( ns )) {

final Configuration config = new ConfigurationImpl(el);
list.add(config);
Expand All @@ -118,8 +119,9 @@ private void cache() {
while (child != null) {

if (child.getNodeType() == Node.ELEMENT_NODE) {

if (NS.equals(child.getNamespaceURI())) {

final String ns = child.getNamespaceURI();
if (ns != null && NS.equals(ns)) {

String name = child.getLocalName();

Expand Down Expand Up @@ -193,8 +195,9 @@ public Map<String, String> getPropertyMap(String name) {
if (child.getNodeType() == Node.ELEMENT_NODE) {

final Element el = (Element) child;

if (name.equals( el.getLocalName() ) && NS.equals( el.getNamespaceURI() )) {

final String ns = el.getNamespaceURI();
if (name.equals( el.getLocalName() ) && ns != null && NS.equals( ns )) {

if(!el.hasAttributes()){
continue;
Expand Down
10 changes: 8 additions & 2 deletions src/org/exist/dom/memtree/NodeImpl.java
Expand Up @@ -196,7 +196,12 @@ public String getNamespaceURI() {
switch(getNodeType()) {
case Node.ELEMENT_NODE:
case Node.ATTRIBUTE_NODE:
return getQName().getNamespaceURI();
final String nsUri = getQName().getNamespaceURI();
if(nsUri.equals(XMLConstants.NULL_NS_URI)) {
return null;
} else {
return nsUri;
}

case NodeImpl.NAMESPACE_NODE:
return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
Expand All @@ -212,7 +217,8 @@ public final String getPrefix() {
case Node.ELEMENT_NODE:
case Node.ATTRIBUTE_NODE:
case NodeImpl.NAMESPACE_NODE:
return getQName().getPrefix();
final String prefix = getQName().getPrefix();
return prefix == null ? null : prefix;

default:
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/org/exist/dom/persistent/DocumentImpl.java
Expand Up @@ -171,7 +171,7 @@ public String getLocalName() {
*/
@Override
public String getNamespaceURI() {
return XMLConstants.NULL_NS_URI;
return null;
}

/************************************************
Expand Down
19 changes: 15 additions & 4 deletions src/org/exist/dom/persistent/NodeImpl.java
Expand Up @@ -256,9 +256,15 @@ public Object setUserData(final String key, final Object data, final UserDataHan

@Override
public String getPrefix() {
final QName nodeName = getQName();
final String prefix = nodeName.getPrefix();
return prefix == null ? XMLConstants.DEFAULT_NS_PREFIX : prefix;
switch(getNodeType()) {
case Node.ELEMENT_NODE:
case Node.ATTRIBUTE_NODE:
final String prefix = getQName().getPrefix();
return prefix;

default:
return null;
}
}

@Override
Expand All @@ -271,7 +277,12 @@ public void setPrefix(final String prefix) throws DOMException {

@Override
public String getNamespaceURI() {
return getQName().getNamespaceURI();
final String nsUri = getQName().getNamespaceURI();
if(nsUri.equals(XMLConstants.NULL_NS_URI)) {
return null;
} else {
return nsUri;
}
}

@Override
Expand Down
6 changes: 4 additions & 2 deletions src/org/exist/http/servlets/RedirectorServlet.java
Expand Up @@ -200,7 +200,8 @@ protected void service(HttpServletRequest request, HttpServletResponse response)
return;
}
Element elem = (Element) node;
if (!(Namespaces.EXIST_NS.equals(elem.getNamespaceURI()) && "dispatch".equals(elem.getLocalName())))
final String ns = elem.getNamespaceURI();
if (ns == null || ((!Namespaces.EXIST_NS.equals(ns)) && "dispatch".equals(elem.getLocalName())))
{
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Redirect XQuery should return an element <exist:dispatch>. Received: " + resource.getContent());
Expand All @@ -223,7 +224,8 @@ else if (elem.hasAttribute("redirect"))
if (elem.hasChildNodes()) {
node = elem.getFirstChild();
while (node != null) {
if (node.getNodeType() == Node.ELEMENT_NODE && Namespaces.EXIST_NS.equals(node.getNamespaceURI())) {
final String nsUri = node.getNamespaceURI();
if (node.getNodeType() == Node.ELEMENT_NODE && nsUri != null && Namespaces.EXIST_NS.equals(nsUri)) {
elem = (Element) node;
if ("add-parameter".equals(elem.getLocalName())) {
if (modifiedRequest == null)
Expand Down
5 changes: 3 additions & 2 deletions src/org/exist/http/urlrewrite/RewriteConfig.java
Expand Up @@ -234,8 +234,9 @@ private void parse(Document doc) throws ServletException {
final Element root = doc.getDocumentElement();
Node child = root.getFirstChild();
while (child != null) {
if (child.getNodeType() == Node.ELEMENT_NODE &&
child.getNamespaceURI().equals(Namespaces.EXIST_NS)) {
final String ns = child.getNamespaceURI();
if (child.getNodeType() == Node.ELEMENT_NODE && ns!= null &&
ns.equals(Namespaces.EXIST_NS)) {
final Element elem = (Element) child;
final String pattern = elem.getAttribute(PATTERN_ATTRIBUTE);
if (pattern == null) {
Expand Down
3 changes: 2 additions & 1 deletion src/org/exist/http/urlrewrite/URLRewrite.java
Expand Up @@ -67,7 +67,8 @@ protected URLRewrite(Element config, String uri) {
if (config != null && config.hasChildNodes()) {
Node node = config.getFirstChild();
while (node != null) {
if (node.getNodeType() == Node.ELEMENT_NODE && Namespaces.EXIST_NS.equals(node.getNamespaceURI())) {
final String ns = node.getNamespaceURI();
if (node.getNodeType() == Node.ELEMENT_NODE && ns != null && Namespaces.EXIST_NS.equals(ns)) {
final Element elem = (Element) node;
if ("add-parameter".equals(elem.getLocalName())) {
addParameter(elem.getAttribute("name"), elem.getAttribute("value"));
Expand Down
19 changes: 12 additions & 7 deletions src/org/exist/http/urlrewrite/XQueryURLRewrite.java
Expand Up @@ -275,16 +275,19 @@ protected void service(HttpServletRequest servletRequest, HttpServletResponse se
return;
}
Element elem = (Element) node;
if (!(Namespaces.EXIST_NS.equals(elem.getNamespaceURI()))) {
final String ns = elem.getNamespaceURI();
if (ns == null || !(Namespaces.EXIST_NS.equals(ns))) {
response(broker, response, outputProperties, result);
return;
// throw new ServletException("Redirect XQuery should return an element in namespace " + Namespaces.EXIST_NS);
}

if (Namespaces.EXIST_NS.equals(elem.getNamespaceURI()) && "dispatch".equals(elem.getLocalName())) {

final String nsUri = elem.getNamespaceURI();
if (nsUri != null && Namespaces.EXIST_NS.equals(nsUri) && "dispatch".equals(elem.getLocalName())) {
node = elem.getFirstChild();
while (node != null) {
if (node.getNodeType() == Node.ELEMENT_NODE && Namespaces.EXIST_NS.equals(node.getNamespaceURI())) {
final String nodeNs = node.getNamespaceURI();
if (node.getNodeType() == Node.ELEMENT_NODE && nodeNs != null && Namespaces.EXIST_NS.equals(nodeNs)) {
final Element action = (Element) node;
if ("view".equals(action.getLocalName())) {
parseViews(modifiedRequest, action, modelView);
Expand All @@ -303,7 +306,7 @@ protected void service(HttpServletRequest servletRequest, HttpServletResponse se
}
if (modelView.getModel() == null)
{modelView.setModel(new PassThrough(config, elem, modifiedRequest));}
} else if (Namespaces.EXIST_NS.equals(elem.getNamespaceURI()) && "ignore".equals(elem.getLocalName())) {
} else if (nsUri != null && Namespaces.EXIST_NS.equals(elem.getNamespaceURI()) && "ignore".equals(elem.getLocalName())) {
modelView.setModel(new PassThrough(config, elem, modifiedRequest));
final NodeList nl = elem.getElementsByTagNameNS(Namespaces.EXIST_NS, "cache-control");
if (nl.getLength() > 0) {
Expand Down Expand Up @@ -564,7 +567,8 @@ private URLRewrite parseAction(HttpServletRequest request, Element action) throw
private void parseViews(HttpServletRequest request, Element view, ModelAndView modelView) throws ServletException {
Node node = view.getFirstChild();
while (node != null) {
if (node.getNodeType() == Node.ELEMENT_NODE && Namespaces.EXIST_NS.equals(node.getNamespaceURI())) {
final String ns = node.getNamespaceURI();
if (node.getNodeType() == Node.ELEMENT_NODE && ns != null && Namespaces.EXIST_NS.equals(ns)) {
final URLRewrite urw = parseAction(request, (Element) node);
if (urw != null)
{modelView.addView(urw);}
Expand All @@ -576,7 +580,8 @@ private void parseViews(HttpServletRequest request, Element view, ModelAndView m
private void parseErrorHandlers(HttpServletRequest request, Element view, ModelAndView modelView) throws ServletException {
Node node = view.getFirstChild();
while (node != null) {
if (node.getNodeType() == Node.ELEMENT_NODE && Namespaces.EXIST_NS.equals(node.getNamespaceURI())) {
final String ns = node.getNamespaceURI();
if (node.getNodeType() == Node.ELEMENT_NODE && ns != null && Namespaces.EXIST_NS.equals(ns)) {
final URLRewrite urw = parseAction(request, (Element) node);
if (urw != null)
{modelView.addErrorHandler(urw);}
Expand Down
27 changes: 19 additions & 8 deletions src/org/exist/storage/serializers/NativeSerializer.java
Expand Up @@ -53,6 +53,8 @@
import java.util.regex.Pattern;
import org.exist.storage.dom.INodeIterator;

import javax.xml.XMLConstants;

/**
* Serializer implementation for the native database backend.
*
Expand Down Expand Up @@ -160,9 +162,9 @@ protected void serializeToReceiver(IStoredNode node, INodeIterator iter,
String prefix, uri;
for (final Iterator<String> i = ((ElementImpl) node).getPrefixes(); i.hasNext();) {
prefix = i.next();
if (prefix.length() == 0) {
if (prefix.isEmpty()) {
defaultNS = ((ElementImpl) node).getNamespaceForPrefix(prefix);
receiver.startPrefixMapping("", defaultNS);
receiver.startPrefixMapping(XMLConstants.DEFAULT_NS_PREFIX, defaultNS);
namespaces.add(defaultNS);
} else {
uri = ((ElementImpl) node).getNamespaceForPrefix(prefix);
Expand All @@ -172,8 +174,13 @@ protected void serializeToReceiver(IStoredNode node, INodeIterator iter,
}
}
final String ns = defaultNS == null ? node.getNamespaceURI() : defaultNS;
if (ns.length() > 0 && (!namespaces.contains(ns)))
{receiver.startPrefixMapping(node.getPrefix(), ns);}
if (ns != null && ns.length() > 0 && (!namespaces.contains(ns))) {
String prefix = node.getPrefix();
if(prefix == null) {
prefix = XMLConstants.DEFAULT_NS_PREFIX;
}
receiver.startPrefixMapping(prefix, ns);
}
final AttrList attribs = new AttrList();
if ((first && showId == EXIST_ID_ELEMENT) || showId == EXIST_ID_ALL) {
attribs.addAttribute(ID_ATTRIB, node.getNodeId().toString());
Expand Down Expand Up @@ -272,14 +279,18 @@ protected void serializeToReceiver(IStoredNode node, INodeIterator iter,
receiver.setCurrentNode(node);
receiver.endElement(node.getQName());
if (((ElementImpl) node).declaresNamespacePrefixes()) {
String prefix;
for (final Iterator<String> i = ((ElementImpl) node).getPrefixes(); i.hasNext();) {
prefix = i.next();
final String prefix = i.next();
receiver.endPrefixMapping(prefix);
}
}
if (ns.length() > 0 && (!namespaces.contains(ns)))
{receiver.endPrefixMapping(node.getPrefix());}
if (ns != null && ns.length() > 0 && (!namespaces.contains(ns))) {
String prefix = node.getPrefix();
if(prefix == null) {
prefix = XMLConstants.DEFAULT_NS_PREFIX;
}
receiver.endPrefixMapping(prefix);
}
node.release();
break;
case Node.TEXT_NODE:
Expand Down

0 comments on commit a9c76a0

Please sign in to comment.