(CommonTree t)
| 515 | } |
| 516 | |
| 517 | private void macroImport(CommonTree t) throws ParserException { |
| 518 | // remove quote |
| 519 | String fname = t.getChild(0).getText(); |
| 520 | fname = QueryParserUtils.removeQuotes(fname); |
| 521 | if (!importSeen.add(fname)) { |
| 522 | // we've already imported this file, so just skip this import statement |
| 523 | LOG.debug("Ignoring duplicated import " + fname); |
| 524 | t.getParent().deleteChild(t.getChildIndex()); |
| 525 | return; |
| 526 | } |
| 527 | |
| 528 | Tree macroAST = null; |
| 529 | if (pigContext.macros.containsKey(fname)) { |
| 530 | macroAST = pigContext.macros.get(fname); |
| 531 | } else { |
| 532 | FetchFileRet localFileRet = getMacroFile(fname); |
| 533 | |
| 534 | BufferedReader in = null; |
| 535 | try { |
| 536 | in = new BufferedReader(new FileReader(localFileRet.file)); |
| 537 | } catch (FileNotFoundException e) { |
| 538 | String msg = getErrorMessage(fname, t, |
| 539 | "Failed to import file '" + fname + "'", e.getMessage()); |
| 540 | throw new ParserException(msg); |
| 541 | } |
| 542 | |
| 543 | StringBuilder sb = new StringBuilder(); |
| 544 | String line = null; |
| 545 | try { |
| 546 | line = in.readLine(); |
| 547 | while (line != null) { |
| 548 | sb.append(line).append("\n"); |
| 549 | line = in.readLine(); |
| 550 | } |
| 551 | } catch (IOException e) { |
| 552 | String msg = getErrorMessage(fname, t, |
| 553 | "Failed to read file '" + fname + "'", e.getMessage()); |
| 554 | throw new ParserException(msg); |
| 555 | } |
| 556 | |
| 557 | String macroText = null; |
| 558 | try { |
| 559 | in.close(); |
| 560 | in = new BufferedReader(new StringReader(sb.toString())); |
| 561 | macroText = pigContext.doParamSubstitution(in); |
| 562 | } catch (IOException e) { |
| 563 | String msg = getErrorMessage(fname, t, |
| 564 | "Parameter sustitution failed for macro.", e.getMessage()); |
| 565 | throw new ParserException(msg); |
| 566 | } |
| 567 | |
| 568 | // parse |
| 569 | CommonTokenStream tokenStream = tokenize(macroText, fname); |
| 570 | |
| 571 | try { |
| 572 | macroAST = parse( tokenStream ); |
| 573 | pigContext.macros.put(fname, macroAST); |
| 574 | } catch(RuntimeException ex) { |
no test coverage detected