0

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)

Archer
  • 5,073
  • 8
  • 50
  • 96
toolmania1
  • 309
  • 7
  • 15
  • Can you post the stacktrace? – Don Roby Sep 17 '13 at 13:25
  • Test [Java Application] Test at localhost:28099 Thread [main] (Suspended (exception StringIndexOutOfBoundsException)) XIncludeAwareParserConfiguration(XML11Configuration).parse(XMLInputSource) line: not available SAXParserImpl$JAXPSAXParser(XMLParser).parse(XMLInputSource) line: not available SAXParserImpl$JAXPSAXParser(AbstractSAXParser).parse(InputSource) line: not available SAXParserImpl$JAXPSAXParser.parse(InputSource) line: not available WBXMLClass.convertXmlToWbxml(InputStream, OutputStream) line: 371 Test.main(String[]) line: 459 – toolmania1 Sep 17 '13 at 15:07

1 Answers1

0

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.

Archer
  • 5,073
  • 8
  • 50
  • 96
  • If I print it out using System.out.println(), I get this: is19 = java.io.FileInputStream@799ec4b6. But, if I add this: InputStream is19 = new FileInputStream(file19); InputStreamReader isr19 = new InputStreamReader( is19 ); StringBuilder sb=new StringBuilder(); BufferedReader br = new BufferedReader(isr19); String read = br.readLine(); while(read != null) { sb.append(read); read =br.readLine(); } System.out.println( "sb = " + sb ); Then, I can see the xml. It looks ok. There are spaces in between the tags. But, I thought that did not matter in xml. – toolmania1 Sep 17 '13 at 13:52
  • You should print a content, not the variable reference. Check this: http://codereview.stackexchange.com/questions/8835/java-most-compact-way-to-print-inputstream-to-system-out – Archer Sep 17 '13 at 13:53
  • Show us the XML wich looks OK. – Archer Sep 17 '13 at 14:14
  • I just saw that post, here was the xml: 1 – toolmania1 Sep 17 '13 at 14:57
  • Your answer is 100% right. I debugged further and got to that line and there is an error. qName = "someCommand" and localName = "" at this point. There is no ":" present. Therefore, namespaceURI cannot be assigned a substring of qName because the length is not known since there is no ":" to mark the end of the substring. – toolmania1 Sep 17 '13 at 17:40
  • Nice to hear this helped you ;) – Archer Sep 17 '13 at 20:02