Menu

Exception while decoding using SAXTransmogrifier

Help
2014-04-15
2014-04-24
  • Martin Bobak

    Martin Bobak - 2014-04-15

    We are using javax.xml.transform.Transformer for encoding and decoding xml. Encoding is done without any errors, but when decoding exi we got exception :

    ERROR: ''
    javax.xml.transform.TransformerException: java.lang.NullPointerException
    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:755)

    When there is GrammarOptions.addNS(go) option used, exception is :

    org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

    this code snippet shows how encoding is done :

        final Transmogrifier transmogrifier = new Transmogrifier();
    
        transmogrifier.setAlignmentType(AlignmentType.bitPacked);
    
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
    
        transmogrifier.setGrammarCache(getGrammarCache());
    
        transmogrifier.setOutputStream(out);
    
        transmogrifier.encode(new InputSource(new ByteArrayInputStream(toString(xml, false).getBytes())));
    
        out.flush();
    
        return out.toByteArray();
    

    code snippet for decoding :

        try(ByteArrayInputStream in = new ByteArrayInputStream(input)) {
    
            final EXIReader reader = new EXIReader();
    
            reader.setAlignmentType(AlignmentType.bitPacked);
            grammarCache = getGrammarCache();
    
            reader.setGrammarCache(grammarCache);
    
            final SAXTransformerFactory transformerFactory
                    = (SAXTransformerFactory) TransformerFactory.newInstance();
            SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
            saxParserFactory.setNamespaceAware(true);
            final TransformerHandler handler = transformerFactory.newTransformerHandler();
            handler.setResult(domResult);
    
            reader.setContentHandler(handler);
    
            reader.parse(new InputSource(in));
        }
    

    code snippet grammar options used :

       private static GrammarCache getGrammarCache() {
            short go = GrammarOptions.DEFAULT_OPTIONS;
    
            // This option on or off, nagasena still fails
            go = GrammarOptions.addNS(go);
    
            return new GrammarCache(null, go);
        }
    
     
  • Takuki Kamiya

    Takuki Kamiya - 2014-04-15

    Hi,

    Can you send me the XML and schema files that were used?

    taki

     
    • Martin Bobak

      Martin Bobak - 2014-04-16

      This is xml used for testing :

      <abc:rpc message-id="id" xmlns:abc="a.b.c">
      <abc:inner/>
      </abc:rpc>
      

      it is generated from Element object constructed by method getDom :

      public static Element getDom() {
          final Element dom;
          final Document d = newDocument();
          dom = d.createElementNS("a.b.c", "abc:rpc");
          dom.setAttribute("message-id", "id");
          Element innerElement = d.createElement("abc:inner");
          dom.appendChild(innerElement);
          return dom;
      }
      
       
  • Martin Bobak

    Martin Bobak - 2014-04-16

    I discovered so far that javax.xml.transform.TransformerException: java.lang.NullPointerException is caused by Transmogrifier$SAXEventHandler#verifyPrefix in which new Locator is created using copy constructor with m_locator as argument which is null. There seems to be issue related to uri extracted from prefix.

     

    Last edit: Martin Bobak 2014-04-16
  • Takuki Kamiya

    Takuki Kamiya - 2014-04-16

    I added a test case "testToDOM_01" in XmlSerializationTest.java.
    The test case basically does what you described.

    Can you retrieve the source and tests, and run the unit tests?

    You can export nagasena by:
    $ svn export https://svn.code.sf.net/p/openexi/code/trunk/nagasena

    Unit tests can be run by:
    $ ant clean; ant test

     
    • Martin Bobak

      Martin Bobak - 2014-04-23

      Test fails when Transformer javax.xml.transform.Transformer is used for encoding exi. Here is code snippet how we are using it :

          final Transmogrifier transmogrifier = new Transmogrifier();
          transmogrifier.setAlignmentType(AlignmentType.bitPacked);
          final ByteArrayOutputStream out = new ByteArrayOutputStream();
          transmogrifier.setGrammarCache(getGrammarCache());
          transmogrifier.setOutputStream(out);
          final Transformer transformer = saxTransformerFactory.newTransformer();
          transformer.transform(new DOMSource(xml), new SAXResult(transmogrifier.getSAXTransmogrifier()));
          byte[] bts = out.toByteArray();
      
       

      Last edit: Martin Bobak 2014-04-23
  • Takuki Kamiya

    Takuki Kamiya - 2014-04-23

    In your initial report, you said "Encoding is done without any errors, but when decoding exi we got exception". That's what I investigated. Also, the test I implemented was about the decoding, which I verified works. Was that statement bogus?

     
  • Takuki Kamiya

    Takuki Kamiya - 2014-04-23

    Please give precise description to your report next time.
    I believe I fixed this issue in the latest Nagasena.

     
    • Martin Bobak

      Martin Bobak - 2014-04-24

      I'm sorry for misleading you. My bad. But error happend during decoding only when exi was encoded using Transformer.

       

      Last edit: Martin Bobak 2014-04-24
  • Takuki Kamiya

    Takuki Kamiya - 2014-04-24

    Thanks for finding the issue.

     

Log in to post a comment.