samuelowino / mobile-translate

Automate Translation Workflows for Android and iOS Projects
5 stars 5 forks source link

xml parsing error #9

Closed mwangidennis1 closed 2 months ago

mwangidennis1 commented 2 months ago

Description

The fromXml method in XmlParserReader class is throwing an error when trying to deserialize an XML file using JAXB.

Steps to Reproduce

  1. put some strings you want to translate in the output.xml file
  2. run the ./mobile-translate.sh
  3. Enter the L (translation) then A (android) then the path to the output.xml path
  4. observe the error

    Code

    INFO: No strings found in xml Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because the return value of "com.owino.mobiletranslate.android.model.Resources.getStrings()" is null at com.owino.mobiletranslate.android.translate.AndroidTranslateFactory.translateResources(AndroidTranslateFactory.java:26) at com.owino.mobiletranslate.android.translate.AndroidTranslateFactory.getTranslatedResources(AndroidTranslateFactory.java:20) at com.owino.mobiletranslate.android.xml.XmlParserReader.executeXmlAndTranslationParser(XmlParserReader.java:31) at com.owino.mobiletranslate.android.AndroidTranslationProcessor.runTranslation(AndroidTranslationProcessor.java:10) at com.owino.mobiletranslate.TranslateApplication.main(TranslateApplication.java:28)

expected behavior

The project will generate localised content and place them in the project/target directory, in the format of [locale].xml e.g de.xml or zh.xml.

Actual behavior

The method 'executeXmlAndTranslationParser` in the class XmlParserReader expects a type of File as it inputs to its parameters but in the method main of TranslateApplication we pass a type string which is the path to the xml file hence a type mismatch leading to the xml file not being parsed well

environment

ran it on windows 10 and arch both have openjdk17

possible solution

if allowed will fix it since my main goal was to take care of previous issues on the issues page

mwangidennis1 commented 2 months ago

saw reason for error

samuelowino commented 1 month ago

The project expected your input xml to have tags in the form

<resources>
<strings>Content</strings>
</resources>
samuelowino commented 1 month ago

Can you improve the error handling to make this a bit clearer?

mwangidennis1 commented 1 month ago

I realized the conversion to File type happens if i do an instanceof and so the error was in the format of my xml mine: hello world accepted format was hello world

On Wed, Oct 2, 2024 at 10:00 AM Samuel Owino @.***> wrote:

Can you improve the error handling to make this a bit clearer?

— Reply to this email directly, view it on GitHub https://github.com/samuelowino/mobile-translate/issues/9#issuecomment-2387758298, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXT32Z7CIYXXGNHNQPNUNC3ZZOKXPAVCNFSM6AAAAABO7TMZI2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOBXG42TQMRZHA . You are receiving this because you modified the open/close state.Message ID: @.***>

samuelowino commented 1 month ago

You could add a try-catch block around the section where the failure occurs due to an incorrect format. Instead of allowing the error to propagate, log a more detailed error message with a descriptive stack trace. An IOException with a custom message should be sufficient in this case.

try {
    // Code where the failure occurs
} catch (NullPointerException e) {
    log.error("Invalid XML format detected in the pre-translate input. Expected format: <strings></strings>. Please refer to the documentation for correct usage.", e);
}

This approach will provide more meaningful error information and help users resolve format-related issues more easily.

mwangidennis1 commented 1 month ago

Fixed

On Wed, Oct 2, 2024 at 9:15 PM Samuel Owino @.***> wrote:

You could add a try-catch block around the section where the failure occurs due to an incorrect format. Instead of allowing the error to propagate, log a more detailed error message with a descriptive stack trace. An IOException with a custom message should be sufficient in this case.

try { // Code where the failure occurs } catch (IOException e) { log.error("Invalid XML format detected in the pre-translate input. Expected format: . Please refer to the documentation for correct usage.", e); }

This approach will provide more meaningful error information and help users resolve format-related issues more easily.

— Reply to this email directly, view it on GitHub https://github.com/samuelowino/mobile-translate/issues/9#issuecomment-2389367357, or unsubscribe https://github.com/notifications/unsubscribe-auth/AXT32Z3HAUZCF7E2GVKWGHDZZQZ57AVCNFSM6AAAAABO7TMZI2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOBZGM3DOMZVG4 . You are receiving this because you modified the open/close state.Message ID: @.***>