public void test_filtering() throws IOException, SAXException {
    Smooks smooks = new Smooks();
    BasicExecutionEventListener eventListener = new BasicExecutionEventListener();

    smooks.addConfigurations("config2.xml", getClass().getResourceAsStream("config2.xml"));
    // Create an exec context - no profiles....
    ExecutionContext executionContext = smooks.createExecutionContext();
    CharArrayWriter outputWriter = new CharArrayWriter();

    // Filter the input message to the outputWriter, using the execution context...
    executionContext.setEventListener(eventListener);
    smooks.filterSource(
        executionContext,
        new StreamSource(getClass().getResourceAsStream("testxml1.xml")),
        new StreamResult(outputWriter));

    log.debug(outputWriter.toString());
    byte[] expected =
        StreamUtils.readStream(getClass().getResourceAsStream("testxml1-expected.xml"));
    assertTrue(
        StreamUtils.compareCharStreams(
            new ByteArrayInputStream(expected),
            new ByteArrayInputStream(outputWriter.toString().getBytes())));
    assertEquals(32, eventListener.getEvents().size());
  }
  public void test_phase_selection() throws IOException, SAXException {
    Smooks smooks = new Smooks();
    ExecutionContext execContext;
    DOMContentDeliveryConfig config;

    smooks.addConfigurations("config1.xml", getClass().getResourceAsStream("config1.xml"));
    execContext = smooks.createExecutionContext();
    config = (DOMContentDeliveryConfig) execContext.getDeliveryConfig();

    // Check the assembly units...
    List<ContentHandlerConfigMap<DOMVisitBefore>> assemblyVBs =
        config.getAssemblyVisitBefores().getMappings("a");
    List<ContentHandlerConfigMap<DOMVisitAfter>> assemblyVAs =
        config.getAssemblyVisitAfters().getMappings("a");
    assertEquals(2, assemblyVBs.size());
    assertTrue(assemblyVBs.get(0).getContentHandler() instanceof AssemblyVisitor1);
    assertTrue(assemblyVBs.get(1).getContentHandler() instanceof ConfigurableVisitor);
    assertEquals(2, assemblyVAs.size());
    assertTrue(assemblyVAs.get(0).getContentHandler() instanceof ConfigurableVisitor);
    assertTrue(assemblyVAs.get(1).getContentHandler() instanceof AssemblyVisitor1);

    List<ContentHandlerConfigMap<DOMVisitBefore>> processingVBs =
        config.getProcessingVisitBefores().getMappings("a");
    List<ContentHandlerConfigMap<DOMVisitAfter>> processingVAs =
        config.getProcessingVisitAfters().getMappings("a");
    assertEquals(2, processingVBs.size());
    assertTrue(processingVBs.get(0).getContentHandler() instanceof ProcessorVisitor1);
    assertTrue(processingVBs.get(1).getContentHandler() instanceof ConfigurableVisitor);
    assertEquals(2, processingVAs.size());
    assertTrue(processingVAs.get(0).getContentHandler() instanceof ConfigurableVisitor);
    assertTrue(processingVAs.get(1).getContentHandler() instanceof ProcessorVisitor1);
  }
  @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();
    }
  }
  public void test_SAX() throws IOException, SAXException {
    Smooks smooks;
    ExecutionContext execContext;

    SAXAndDOMVisitor.visited = false;
    smooks = new Smooks(getClass().getResourceAsStream("test-config-SAX-01.xml"));
    execContext = smooks.createExecutionContext();
    smooks.filterSource(execContext, new StreamSource(new StringReader("<a/>")), null);
    assertEquals(execContext, TestExecutionContextExpressionEvaluator.context);
    assertTrue(SAXAndDOMVisitor.visited);

    SAXAndDOMVisitor.visited = false;
    smooks = new Smooks(getClass().getResourceAsStream("test-config-SAX-02.xml"));
    execContext = smooks.createExecutionContext();
    smooks.filterSource(execContext, new StreamSource(new StringReader("<a/>")), null);
    assertEquals(execContext, TestExecutionContextExpressionEvaluator.context);
    assertFalse(SAXAndDOMVisitor.visited);
  }
  private void test_config_file(String testName, Smooks smooks) throws IOException {
    ExecutionContext context = smooks.createExecutionContext();
    String result =
        SmooksUtil.filterAndSerialize(
            context,
            getClass().getResourceAsStream("/test/" + testName + "/input-message.jsn"),
            smooks);

    if (logger.isDebugEnabled()) {
      logger.debug("Result: " + result);
    }

    assertEquals("/test/" + testName + "/expected.xml", result.getBytes());
  }
  private void digestExtendedResourceConfig(
      Element configElement,
      String defaultSelector,
      String defaultNamespace,
      String defaultProfile,
      String defaultConditionRef) {
    String configNamespace = configElement.getNamespaceURI();
    Smooks configDigester = getExtenededConfigDigester(configNamespace);
    ExecutionContext executionContext = configDigester.createExecutionContext();
    ExtensionContext extentionContext;
    Element conditionElement = DomUtils.getElement(configElement, "condition", 1);

    // Create the ExtenstionContext and set it on the ExecutionContext...
    if (conditionElement != null
        && (conditionElement.getNamespaceURI().equals(XSD_V10)
            || conditionElement.getNamespaceURI().equals(XSD_V11))) {
      extentionContext =
          new ExtensionContext(
              this,
              defaultSelector,
              defaultNamespace,
              defaultProfile,
              digestCondition(conditionElement));
    } else if (defaultConditionRef != null) {
      extentionContext =
          new ExtensionContext(
              this,
              defaultSelector,
              defaultNamespace,
              defaultProfile,
              getConditionEvaluator(defaultConditionRef));
    } else {
      extentionContext =
          new ExtensionContext(this, defaultSelector, defaultNamespace, defaultProfile, null);
    }
    ExtensionContext.setExtensionContext(extentionContext, executionContext);

    // Filter the extension element through Smooks...
    configDigester.filterSource(executionContext, new DOMSource(configElement), null);

    // Copy the created resources from the ExtensionContext and onto the
    // SmooksResourceConfigurationList...
    List<SmooksResourceConfiguration> resources = extentionContext.getResources();
    for (SmooksResourceConfiguration resource : resources) {
      resourcelist.add(resource);
    }
  }
Esempio n. 7
0
  /**
   * Run the transform for the request or response.
   *
   * @param inputJavaObject The input Java Object.
   * @return The transformed Java Object XML.
   */
  public String runSmooksTransform(Object inputJavaObject, String configFile)
      throws IOException, SAXException {
    Smooks smooks = new Smooks(configFile);
    String resultString = new String();
    try {
      ExecutionContext executionContext = smooks.createExecutionContext();
      StringWriter writer = new StringWriter();

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

      // Filter the message to the result writer, using the execution context...
      smooks.filterSource(
          executionContext, new JavaSource(inputJavaObject), new StreamResult(writer));
      resultString = writer.toString();
      /*            resultString = this.replace(resultString, "list", "ResultSet");
                  resultString = this.replace(resultString, "<id>", "<GeneID type='entrez'>");
                  resultString = this.replace(resultString, "</id>", "</GeneID>");
                  resultString = this.replace(resultString, "<symbol>", "<GeneSymbol>");
                  resultString = this.replace(resultString, "</symbol>", "</GeneSymbol>");
                  resultString = this.replace(resultString, "org.ncibi.mimiweb.data.ResultGeneMolecule", "Result");
                  resultString = this.replace(resultString, "<taxid>", "<TaxonomyID>");
                  resultString = this.replace(resultString, "</taxid>", "</TaxonomyID>");
                  resultString = this.replace(resultString, "<description>", "<GeneDescription>");
                  resultString = this.replace(resultString, "</description>", "</GeneDescription>");
                  resultString = this.replace(resultString, "interactionGeneIds", "InteractingGeneIDSet");
                  resultString = this.replace(resultString, "<int>", "<GeneID type='entrez'>");
                  resultString = this.replace(resultString, "</int>", "</GeneID>");
                  resultString = this.replace(resultString, "interactionGeneSymbols", "InteractingGeneSymbolSet");
      */
      return resultString;

    } finally {
      smooks.close();
    }
  }