private Smooks getExtenededConfigDigester(String configNamespace) {
    Smooks smooks = extendedConfigDigesters.get(configNamespace);

    if (smooks == null) {
      URI namespaceURI;

      try {
        namespaceURI = new URI(configNamespace);
      } catch (URISyntaxException e) {
        throw new SmooksConfigurationException(
            "Unable to parse extended config namespace URI '" + configNamespace + "'.", e);
      }

      String resourcePath = "/META-INF" + namespaceURI.getPath() + "-smooks.xml";
      File resourceFile = new File(resourcePath);
      String baseURI = resourceFile.getParent().replace('\\', '/');

      // Validate the extended config...
      assertExtendedConfigOK(configNamespace, resourcePath);

      // Construct the Smooks instance for processing this config namespace...
      smooks = new Smooks(StandaloneApplicationContext.createNewInstance(false));
      setExtentionDigestOn();
      try {
        SmooksResourceConfigurationStore configStore = smooks.getApplicationContext().getStore();
        SmooksResourceConfigurationList extConfigList =
            new SmooksResourceConfigurationList(baseURI);

        XMLConfigDigester configDigester = new XMLConfigDigester(extConfigList);

        configDigester.extendedConfigDigesters = extendedConfigDigesters;
        configDigester.digestConfigRecursively(
            new InputStreamReader(ClassUtil.getResourceAsStream(resourcePath, classLoader)),
            baseURI);
        configStore.addSmooksResourceConfigurationList(extConfigList);
      } catch (Exception e) {
        throw new SmooksConfigurationException(
            "Failed to construct Smooks instance for processing extended configuration resource '"
                + resourcePath
                + "'.",
            e);
      } finally {
        setExtentionDigestOff();
      }

      // And add it to the Map of extension digesters...
      extendedConfigDigesters.put(configNamespace, smooks);
    }

    if (classLoader != null) {
      smooks.setClassLoader(classLoader);
    }

    return smooks;
  }
Example #2
0
  private Source getNamespaceSource(URI namespace) throws SAXException {
    String resourcePath = "/META-INF" + namespace.getPath();
    InputStream xsdStream = ClassUtil.getResourceAsStream(resourcePath, getClass());

    if (xsdStream == null) {
      throw new SAXException(
          "Failed to locate XSD resource '"
              + resourcePath
              + "' on classpath. Namespace: '"
              + namespace
              + "'.");
    }

    return new StreamSource(xsdStream);
  }
  private void assertExtendedConfigOK(String configNamespace, String resourcePath) {
    InputStream resourceStream = ClassUtil.getResourceAsStream(resourcePath, classLoader);

    if (resourceStream == null) {
      throw new SmooksConfigurationException(
          "Unable to locate Smooks digest configuration '"
              + resourcePath
              + "' for extended resource configuration namespace '"
              + configNamespace
              + "'.  This resource must be available on the classpath.");
    }

    Document configDoc;
    try {
      configDoc = XmlUtil.parseStream(resourceStream);
    } catch (Exception e) {
      throw new SmooksConfigurationException(
          "Unable to parse namespace URI '" + configNamespace + "'.", e);
    }

    XsdDOMValidator validator;
    try {
      validator = new XsdDOMValidator(configDoc);
    } catch (SAXException e) {
      throw new SmooksConfigurationException(
          "Unable to create XsdDOMValidator instance for extended resource config '"
              + resourcePath
              + "'.",
          e);
    }

    String defaultNS = validator.getDefaultNamespace().toString();
    if (!XSD_V10.equals(defaultNS) && !XSD_V11.equals(defaultNS)) {
      throw new SmooksConfigurationException(
          "Extended resource configuration '"
              + resourcePath
              + "' default namespace must be a valid Smooks configuration namespace.");
    }
    if (validator.getNamespaces().size() > 1) {
      throw new SmooksConfigurationException(
          "Extended resource configuration '"
              + resourcePath
              + "' defines configurations from multiple namespaces.  This is not permitted.  Only use configurations from the base Smooks config namespaces e.g. '"
              + XSD_V11
              + "'.");
    }
  }