{"record":{"id":"24d6cf99c08f9ae9","repo":"languagetool-org/languagetool","slug":"cannot-load-or-parse-filename","errorCode":null,"errorMessage":"Cannot load or parse '\" + filename + \"'","messagePattern":"Cannot load or parse '\" \\+ filename \\+ \"'","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"languagetool-core/src/main/java/org/languagetool/rules/patterns/bitext/BitextPatternRuleLoader.java","lineNumber":48,"sourceCode":"/**\n * Loads {@link PatternRule}s from an XML file.\n * \n * @author Marcin Miłkowski\n */\npublic class BitextPatternRuleLoader extends DefaultHandler {\n\n  public final List<BitextPatternRule> getRules(InputStream is, String filename) throws IOException {\n    List<BitextPatternRule> rules;\n    try {\n      BitextPatternRuleHandler handler = new BitextPatternRuleHandler();\n      SAXParserFactory factory = SAXParserFactory.newInstance();\n      SAXParser saxParser = factory.newSAXParser();\n      saxParser.getXMLReader().setFeature(\"http://apache.org/xml/features/nonvalidating/load-external-dtd\", false);\n      saxParser.parse(is, handler);\n      rules = handler.getBitextRules();\n      return rules;\n    } catch (Exception e) {\n      throw new IOException(\"Cannot load or parse '\" + filename + \"'\", e);\n    }\n  }\n\n}\n","sourceCodeStart":30,"sourceCodeEnd":53,"githubUrl":"https://github.com/languagetool-org/languagetool/blob/2e990059ce67d5e2a0f7f7ca5d31160c6709df4b/languagetool-core/src/main/java/org/languagetool/rules/patterns/bitext/BitextPatternRuleLoader.java#L30-L53","documentation":"BitextPatternRuleLoader.getRules wraps any exception from SAX parsing or rule-building of a bitext rule file into an IOException with the message \"Cannot load or parse '<filename>'\" and the original exception as cause. It is a catch-all conversion: the real problem (malformed XML, unsupported element, IO error) is in the cause chain. The loader is called by getBitextRules when loading bilingual pattern rules.","triggerScenarios":"Any failure during factory.newSAXParser(), saxParser.parse(is, handler), or handler.getBitextRules() — e.g. malformed XML, bitext-unsupported elements like triggers_error, or a broken input stream — inside the try block.","commonSituations":"Malformed or hand-edited bitext rule XML; missing file/stream miswired so parse fails; using bitext-unsupported features; XML parser feature misconfiguration.","solutions":["Read the 'Caused by' chain of the IOException to find the real parse error and its line number","Validate the bitext XML file with an XML parser/linter before loading","Check that the filename/path is correct and the InputStream is readable and not already consumed","Remove bitext-unsupported elements (e.g. triggers_error) revealed by the cause"],"exampleFix":"// before\ntry (InputStream is = new FileInputStream(\"rules.xml\")) { ... }\n// after: verify cause and file existence first\nFile f = new File(\"rules.xml\");\nif (!f.isFile()) throw new FileNotFoundException(\"rules.xml\");\ntry (InputStream is = new FileInputStream(f)) { loader.getRules(is, \"rules.xml\"); }","handlingStrategy":"try-catch","validationCode":"File f = new File(filename);\nif (!f.isFile() || !f.canRead()) throw new FileNotFoundException(filename);\ntry (InputStream probe = new FileInputStream(f)) {\n  DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(probe); // fail fast on malformed XML\n}","typeGuard":null,"tryCatchPattern":"try {\n  rules = BitextPatternRuleLoader.getBitextRules(stream, filename, srcLang, trgLang);\n} catch (IOException e) {\n  Throwable cause = e.getCause();\n  log.error(\"Failed to load bitext rules from \" + filename + \": \" + cause, e);\n  if (cause instanceof SAXException) { /* point author at line/column */ }\n}","preventionTips":["Always inspect the IOException's cause chain for the real parse error","Validate bitext XML files with an XML parser before loading","Confirm the file path and that the InputStream is fresh/unconsumed","Avoid bitext-unsupported features (e.g. triggers_error) in bilingual rules"],"tags":["languagetool","bitext","ioexception","xml-parsing"],"backgroundTag":"file-parse-failed","analyzedSha":"2e990059ce67d5e2a0f7f7ca5d31160c6709df4b","analyzedAt":"2026-09-06T09:20:17.015Z","contentChangedAt":"2026-09-06T09:20:17.015Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}