PROBLEM:  If the XML to be streamed back has a tag with attributes, but no namespace, a null pointer exception is thrown in the streaming process.  This is because the StreamingOMSerializer.serializeAttributes() method attempts to use the namespace even if there isn’t one.

 

CORRECTION:  I modified the method to perform a null check on the namespace and not use it if it was null.  This allowed the xml to be properly streamed out.  The modified code, in bold, is shown below:

 

    /**

     * @param reader

     * @param writer

     * @throws XMLStreamException

     */

    protected void serializeAttributes(XMLStreamReader reader,

                                       XMLStreamWriter writer)

            throws XMLStreamException {

        int count = reader.getAttributeCount();

        String prefix = null;

        String namespaceName = null;

        String writerPrefix=null;

        for (int i = 0; i < count; i++) {

            prefix = reader.getAttributePrefix(i);

            namespaceName = reader.getAttributeNamespace(i);

 

            if ((namespaceName != null) && !"".equals(namespaceName)){

                writerPrefix =writer.getNamespaceContext().getPrefix(namespaceName);  //moved this line into the if statement

                //prefix has already being declared but this particular attrib has a

                //no prefix attached. So use the prefix provided by the writer

                if (writerPrefix!=null && (prefix==null || prefix.equals(""))){

                    writer.writeAttribute(writerPrefix, namespaceName,

                            reader.getAttributeLocalName(i),

                            reader.getAttributeValue(i));

 

                    //writer prefix is available but different from the current

                    //prefix of the attrib. We should be decalring the new prefix

                    //as a namespace declaration

                }else if (prefix!=null && !"".equals(prefix)&& !prefix.equals(writerPrefix)){

                    writer.writeNamespace(prefix,namespaceName);

                    writer.writeAttribute(prefix, namespaceName,

                            reader.getAttributeLocalName(i),

                            reader.getAttributeValue(i));

 

                    //prefix is null (or empty), but the namespace name is valid! it has not

                    //being written previously also. So we need to generate a prefix

                    //here

                }else{

                    prefix = generateUniquePrefix(writer.getNamespaceContext());

                    writer.writeNamespace(prefix,namespaceName);

                    writer.writeAttribute(prefix, namespaceName,

                            reader.getAttributeLocalName(i),

                            reader.getAttributeValue(i));

                }

            }else{

                //empty namespace is equal to no namespace!

                writer.writeAttribute(reader.getAttributeLocalName(i),

                        reader.getAttributeValue(i));

            }

 

 

        }

    }

 

Recommend this code correction be included in the next Axiom release.

 

Please let me know if this code change will be included or if some other correction is made.

Thanks,

Jim

 

 

Reply via email to