[x3d-public] X3dToJson.xslt error. \n converted to \\ in script

Roy Walmsley roy.walmsley at ntlworld.com
Mon Apr 18 04:05:24 PDT 2016


Hi John,

 

JSON has makes no distinction between different types of text, as can be the case with XML. JSON clearly specifies that escaping is limited to a small character set. These are listed in JSON specifications, e.g. ECMA404 (http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf). So, of relevance here is \” to represent the double quotation mark, \\ to represent the reverse solidus character, and \n to represent the line feed character. Note that the single quotation mark ‘ is never escaped. Therefore the escape sequence \’ is invalid JSON.

 

Now, what about XML.

 

Let’s take as an example line 85 in the example file  CreateX3DFromStringRandomSpheres.x3d, where the line is:

 

    sceneString =    '<X3D version="3.1" profile="Interchange">\n' +

 

Since this is part of a CDATA section, the only escaped character in this line is the \n line feed character.

 

On translating it to JSON, we note that the double quotation marks will both need escaping, as will the line feed character. The single quotation mark must not be escaped. So, how does this line get translated by the stylesheet? In the JSON it is line 278:

 

"    sceneString =    '<X3D version=\"3.1\" profile=\"Interchange\">\\' +",

 

We note that, as expected, both double quotation marks are escaped. The single quotation marks, as expected, are not escaped. However, the line feed is incorrect. Instead, we simply have an escaped reverse solidus character. This is incorrect. The line should be

 

"    sceneString =    '<X3D version=\"3.1\" profile=\"Interchange\">\n' +",

 

So there is a problem with the stylesheet.

 

Now, what about a converter reading this file in and translating it back to XML?

 

The reader should recognise that the whole #sourceText array, starting at line 257, is an array of strings that will be converted back to a single unquoted CDATA section. Each individual string in the JSON is enclosed in double quotation marks. These are being removed on converting back to XML. So the \” escape sequences in JSON are not required to be escaped in XML. Thus the conversion back should, assuming that the corrected JSON was used, would successfully round trip back to the original XML quoted above.

 

Regards,

 

Roy

 

 

From: John Carlson [mailto:yottzumm at gmail.com] 
Sent: 18 April 2016 02:32
To: Don Brutzman; Roy Walmsley
Cc: X3D Graphics public mailing list
Subject: Re: X3dToJson.xslt error. \n converted to \\ in script

 

Also compare quoting in scripts for these two files:

 

/www.web3d.org/x3d/content/examples//Basic/development/CameraPrototypes.json <http://www.web3d.org/x3d/content/examples/Basic/development/CameraPrototypes.json>  





/www.web3d.org/x3d/content/examples//Basic/development/CameraPrototypes.x3d <http://www.web3d.org/x3d/content/examples/Basic/development/CameraPrototypes.x3d>  









\’ gets translated to \\ (left out the ‘)





Probably below, \n should be translated to \\n <smb://n>  or just left as is, I am not sure, something we could try.





there may be others.





Perhaps we need to write down all the initial states (in comment, in script, in shader, in url, etc) and all the transformations we need to do.

 

I am just running X3dToJson.xslt with saxon.jar.  If there’s any more processing I need to do, let me know.





I include Java source code below for debugging.  Source code for roundtripping is here: https://github.com/coderextreme/x3droundtrip  Bug reports/Issues are welcome.

 

 

class RunSaxon {

protected static class ExitException extends SecurityException 

    {

        public final int status;

        public ExitException(int status) 

        {

            super("There is no escape!");

            this.status = status;

        }

    }

 

    private static class NoExitSecurityManager extends SecurityManager 

    {

        @Override

        public void checkPermission(java.security.Permission perm) 

        {

            // allow anything.

        }

        @Override

        public void checkPermission(java.security.Permission perm, Object context) 

        {

            // allow anything.

        }

        @Override

        public void checkExit(int status) 

        {

            super.checkExit(status);

            throw new ExitException(status);

        }

    }

 

                public static void main(String args[]) {

                                try {

                                                System.setSecurityManager(new NoExitSecurityManager());

                                                for (int a = 0; a < args.length; a++) {

                                                                try {

                                                                                System.err.println("BEGIN "+args[a]);

                                                                                net.sf.saxon.Transform.main(new String[] {

                                                                                                                                "-warnings:recover",

                                                                                                                                "-o",

                                                                                                                                args[a].substring(0, args[a].lastIndexOf("."))+".json",

                                                                                                                                args[a],

                                                                                                                                "X3dToJson.xslt" });

                                                                                // -t  #timing -c # compiled

                                                                                System.err.println("END "+args[a]);

                                                                } catch (Throwable e) {

                                                                                System.err.println("FATAL "+args[a]);

                                                                                System.err.println(e.getMessage());

                                                                }

                                                }

                                                System.setSecurityManager(null); // or save and restore original

                                } catch (ExitException ee) {

                                                ee.printStackTrace();

                                }

                }

}





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20160418/25af0709/attachment-0001.html>


More information about the x3d-public mailing list