javaandroidxmleclipsewbxml

StringIndexOutOfBoundsException using SAXParser, InputStream, XMLHandler


I am trying to convert xml into wbxml to pass information from a fake device ( iPhone, Android, Blackberry, etc. ) to a server that knows the request I am sending. I am receiving a StringIndexOutOfBoundsException on this line of code:

xr.parse(new InputSource(in));

I also added this code to my project to try and do the conversion: http://code.google.com/p/k9mail/source/browse/k9mail/branches/activesync/src/com/android/email/mail/internet/WBXML.java?r=403

You can see the comments note that an xml stream needs passed in as a parameter. I do that in the code below.

" /** * Converts an XML input stream to a WBXML output stream * * @param in The XML stream to read from * @param out The WBXML stream to write to */ "

I do not know much about "CodePage" and this could be a problem in how I am using it by putting a '1' when I initialize the array for "CodePage". I do not know what to put there. I do know that if I take the number "1" out, then Eclipse complains that I need to provide a dimensions expression or initialize the array. So, maybe that is where the stringindexoutofboundsexception comes from, but just does not show up until later. Anyways, I also tried 1000 to initialize the CodePage array and that did not help as well.

CodePage[] codePage = new CodePage[ 1000 ];

I know also that this is probably a little more complicated of an issue and I cannot just post my whole project here. I thank anyone in advanced that provides help. Even if it is just in a general sense to help pin this down, it's much appreciated.

====================== Here is the relevant code from that link for this issue ===========

File 1:

    File file19 = new File("data\\test.xml");
    InputStream is19 = new FileInputStream(file19);
    CodePage[] codePage = new CodePage[ 1 ];  // also tried 1000 here and made no difference, didn't help
    WBXMLClass wbxmlObject = new WBXMLClass( codePage );
    ByteArrayOutputStream byteArrayOutputStream19 = new ByteArrayOutputStream( );
    wbxmlObject.convertXmlToWbxml(is19, byteArrayOutputStream19 );

File 2

 public void convertXmlToWbxml(InputStream in, OutputStream out) {

    SAXParserFactory spf = SAXParserFactory.newInstance();
    try {
        SAXParser sp = spf.newSAXParser();

        XMLReader xr = sp.getXMLReader();

        XMLHandler handler = new XMLHandler(out);

        xr.setContentHandler(handler);

        xr.parse(new InputSource(in));  // this is causing string index out of bounds ****************************
    } catch (ParserConfigurationException pce) {
        //Log.e("WBXML", "ParserConfigurationException in convertXmlToWbxml: " + pce);
    } catch (SAXException se) {
        //Log.e("WBXML", "SAXException in convertXmlToWbxml: " + se);
    } catch (IOException ioe) {
        //Log.e("WBXML", "IOException in convertXmlToWbxml: " + ioe);
    }
}

=====================================

Here is the error in Eclipse:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(Unknown Source) at tags.WBXMLClass$XMLHandler.startElement(WBXMLClass.java:447) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source) at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source) at tags.WBXMLClass.convertXmlToWbxml(WBXMLClass.java:370) at Test.main(Test.java:452)


Solution

  • I do not know much about "CodePage"

    This is not about the CodePage. This is about the arrays and array dimensions. But your exception does not rely on this.

    UPDATE1 Ok, I think that problem is here:

    447 line in your WBXML.java

    namespaceURI = qName.substring(0, qName.lastIndexOf(":"));
    

    qName.lastIndexOf(":") may return -1, means there's no ":" symbol in qName.