scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

xml null namespace #1626

Closed scabug closed 13 years ago

scabug commented 15 years ago

I would expect the following to return true

 val n = <a xmlns=""/>
 n.namespace == null

Instead I get :

 type mismatch;
 found   : object Nil
 required: String
       <a xmlns=""/>
scabug commented 15 years ago

Imported From: https://issues.scala-lang.org/browse/SI-1626?orig=1 Reporter: Bruno Bieth (mustaghattack) Attachments:

scabug commented 15 years ago

Geoffrey Alan Washburn (washburn) said: The problem has nothing to do with projecting the namespace. Just writing

val n = <a xmlns=""/>

will cause the failure.

I can't seem to find the XML maintainer in the users list. Does anyone if he even has an account?

scabug commented 15 years ago

@cunei said: The XML maintainer can be contacted from this page: [http://www.scala-lang.org/node/292]. Just click on the maintainer's name and you will reach the corresponding contact form.

scabug commented 15 years ago

@michelou said: From the file header of class scala.xml.NamespaceBinding one can read:

/** The class <code>NamespaceBinding</code> represents namespace bindings
 *  and scopes. The binding for the default namespace is treated as a null
 *  prefix. the absent namespace is represented with the null uri. Neither
 *  prefix nor uri may be empty, which is not checked.
 *
 *  @author  Burak Emir
 *  @version 1.0
 */

That means that with the current implementation of NamespaceBinding the uri may not be empty and is not checked.

scabug commented 15 years ago

Geoffrey Alan Washburn (washburn) said: Replying to [comment:3 michelou]:

From the file header of class scala.xml.NamespaceBinding one can read: {code} /** The class NamespaceBinding represents namespace bindings

  • and scopes. The binding for the default namespace is treated as a null
  • prefix. the absent namespace is represented with the null uri. Neither
  • prefix nor uri may be empty, which is not checked.
  • @author Burak Emir
  • @version 1.0 */ }} That means that with the current implementation of NamespaceBinding the uri may not be empty and is not checked.

Okay, but to my knowledge this is valid XML, so it probably should work by default.

scabug commented 15 years ago

Bruno Bieth (mustaghattack) said: Replying to [comment:1 washburn]:

The problem has nothing to do with projecting the namespace. Just writing {code} val n = }} will cause the failure.

I can't seem to find the XML maintainer in the users list. Does anyone if he even has an account?

Where did I write that projecting the namespace would cause the failure ?

I'm just saying that the XML parser should understand standard XML, so I can express this in scala :

<root xmlns="root-ns">
    <noNsElement xmlns=""/>
</root>
scabug commented 15 years ago

Geoffrey Alan Washburn (washburn) said: Replying to [comment:5 mustaghattack]:

Where did I write that projecting the namespace would cause the failure ?

Then why did you bother including it in your example?

scabug commented 15 years ago

Bruno Bieth (mustaghattack) said: To show my expectation. As xmlns="" can't be used at the moment, I would expect it to be parsed as a null namespace.

Maybe this wasn't clear enough.

Bruno

scabug commented 15 years ago

Bruno Bieth (mustaghattack) said: More over I would expect the following :

   val n = <a xmlns=""/>
   n.toString == """<a xmlns=""></a>"""

Cause at the moment I find this quite ugly :

  val n = xml.XML.loadString("""<a xmlns=""/>""")
  n.toString == """<a xmlns="null"></a>"""

returns true ...

scabug commented 15 years ago

Bruno Bieth (mustaghattack) said: This is also interesting :

xml.XML.loadString("""<a xmlns="cool"><b xmlns=""/></a>""").child.first.toString

returns

<b xmlns="null" xmlns="cool"></b>
scabug commented 15 years ago

Bruno Bieth (mustaghattack) said: A printer that works for empty namespace

scabug commented 15 years ago

Bruno Bieth (mustaghattack) said: As a workaround for those who need empty namespaces to be converted correctly to string, i've created this printer. It's far from good. It doesn't handle comments and stuff, just element and atom nodes. Here are some unit tests :

import org.testng.annotations.Test
import org.testng.Assert._

class EmptyNullNamespacePrinterTest { 
    val printer = new EmptyNullNamespacePrinter

    @Test
    def shouldPrintTextNodeAsString {
        testXmlStringConversion( """<aNode xmlns="a-ns">Hello<b>Inside B</b> from scala</aNode>""" )
    }   

    @Test
    def shouldPrintEmptyNamespaceEmpty {
        testXmlStringConversion( """<aNode xmlns="a-ns">Hello<b xmlns="">Inside B<c></c></b> from scala</aNode>""" )
    }

    @Test
    def shouldPrintPCDataNodeAsString {
        val s = "a-string"
        val r = printer.print( <a>{ s }</a> )
        assertEquals( r, "<a>a-string</a>" )
    }

    protected def testXmlStringConversion( xmlString : String ) {
        val r = printer.print( scala.xml.XML.loadString( xmlString ) )
        assertEquals( r, xmlString )
    }
}
scabug commented 15 years ago

@paulp said: The original report and first bug in comments are fixed in r17604 and r17605. Not sure what's going on with the last bug in comments; please open a new ticket if you think it needs addressing.