google / cap-library

Common Alerting Protocol Library
Apache License 2.0
80 stars 30 forks source link

CapXmlBuilder should not treat xmlns as an ordinary attribute #37

Open sschiavoni opened 9 years ago

sschiavoni commented 9 years ago

Original issue 38 created by ceharris414 on 2013-02-25T12:12:44.000Z:

The org.w3c.dom.Docment does not properly set the namespace URI on document elements when "xmlns" is treated as an ordinary attribute. Consequently, the Document returned by CapXmlBuilder.toXmlDocument(AlertOrBuilder) cannot be validated against its schema definition (using, for example, javax.xml.validation.Validator). The Document.createElementNS(String, String) method must be used to create elements in a particular namespace.

What steps will reproduce the problem?

  1. Construct an Alert or Alert.Builder, using Alert.Builder.setXmlns(String) to set the namespace; e.g. to the value "urn:oasis:names:tc:emergency:cap:1.2"
  2. Build a Document using CapXmlBuilder.toXmlDocument(AlertOrBuilder).
  3. Examine the namespace URI of any element in the Document; e.g. document.getDocumentElement().getNamespaceURI()

What is the expected output? What do you see instead?

The namespaceURI should be the value set by Alert.Builder.setXmlns; e.g. "urn:oasis:names:tc:emergency:cap:1.2"

Instead, the returned namespaceURI is null.

What version of the product are you using? On what operating system?

r3 on linux

Please provide any additional information below.

The CapXmlBuilder should use Document.createElementNS(String, String) instead of Document.createElement(String) when creating each element of the document. The first argument to createElementNS should be the namespace returned by AlertOrBuilder.getXmlns().

See the attached patch for an approach to solving this issue.