@SuppressWarnings("unchecked")
  protected static List runSmooksTransform() throws IOException, SAXException, SmooksException {

    Smooks smooks = new Smooks();

    try {
      // ****
      // And here's the configuration... configuring the CSV reader and
      // the direct
      // binding config to create a List of Person objects
      // (List<Person>)...
      // ****
      smooks.setReaderConfig(
          new CSVReaderConfigurator("firstName,lastName,gender,age,country")
              .setBinding(new CSVBinding("customerList", Customer.class, CSVBindingType.LIST)));

      // Configure the execution context to generate a report...
      ExecutionContext executionContext = smooks.createExecutionContext();
      executionContext.setEventListener(new HtmlReportGenerator("target/report/report.html"));

      JavaResult javaResult = new JavaResult();
      smooks.filterSource(executionContext, new StringSource(messageIn), javaResult);

      return (List) javaResult.getBean("customerList");
    } finally {
      smooks.close();
    }
  }
Exemplo n.º 2
0
  public void visitBefore(SAXElement element, ExecutionContext executionContext)
      throws SmooksException, IOException {
    Result result = FilterResult.getResult(executionContext, JavaResult.class);
    if (result != null) {
      Map beans = new HashMap();

      ((JavaResult) result).setResultMap(beans);
      beans.put("theBean", "Hi there!");
    }
  }
  public Map insertFilter(Source source) {
    JavaResult result = new JavaResult();

    // preserve the previous classloader, While we make Smooks aware of the Drools classloader
    ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread()
        .setContextClassLoader(
            ((InternalRuleBase) this.session.getRuleBase()).getRootClassLoader());

    ExecutionContext executionContext = this.smooks.createExecutionContext();

    // Filter the input message to extract, using the execution context...
    smooks.filter(source, result, executionContext);

    Thread.currentThread().setContextClassLoader(previousClassLoader);

    Map handles = new HashMap<FactHandle, Object>();

    Object object = result.getBean(this.configuration.getRoodId());
    if (object == null) {
      return handles;
    }

    if (this.getterExpr != null) {
      Iterable it = (Iterable) MVEL.executeExpression(this.getterExpr, object);
      if (it != null) {
        for (Object item : it) {
          FactHandle handle = this.session.insert(item);
          handles.put(handle, object);
        }
      }
    } else {
      FactHandle handle = this.session.insert(object);
      handles.put(handle, object);
    }

    return handles;
  }