Exemplo n.º 1
0
  private void digestConfigRecursively(Reader stream, String baseURI)
      throws IOException, SAXException, URISyntaxException, SmooksConfigurationException {
    Document configDoc;
    String streamData = StreamUtils.readStream(stream);

    try {
      configDoc =
          XmlUtil.parseStream(
              new StringReader(streamData),
              getDTDEntityResolver(),
              XmlUtil.VALIDATION_TYPE.DTD,
              true);
      logger.debug(
          "Using a deprecated Smooks configuration DTD '"
              + DTD_V10
              + "'.  Update configuration to use XSD '"
              + XSD_V10
              + "'.");
      digestV10DTDValidatedConfig(configDoc);
      logger.debug(
          "Using a deprecated Smooks configuration DTD '"
              + DTD_V10
              + "'.  Update configuration to use XSD '"
              + XSD_V10
              + "'.");
    } catch (Exception e) {
      // Must be an XSD based config...
      try {
        configDoc = XmlUtil.parseStream(new StringReader(streamData));
      } catch (ParserConfigurationException ee) {
        throw new SAXException("Unable to parse Smooks configuration.", ee);
      }

      XsdDOMValidator validator = new XsdDOMValidator(configDoc);
      String defaultNS = validator.getDefaultNamespace().toString();

      validator.validate();

      configStack.peek().defaultNS = defaultNS;
      if (XSD_V10.equals(defaultNS)) {
        if (validator.getNamespaces().size() > 1) {
          throw new SmooksConfigurationException(
              "Unsupported use of multiple configuration namespaces from inside a v1.0 Smooks configuration. Configuration extension not supported from a v1.0 configuration.  Use the v1.1 configuration namespace.");
        }
        digestV10XSDValidatedConfig(baseURI, configDoc);
      } else if (XSD_V11.equals(defaultNS)) {
        digestV11XSDValidatedConfig(baseURI, configDoc);
      } else {
        throw new SAXException(
            "Cannot parse Smooks configuration.  Unsupported default Namespace '"
                + defaultNS
                + "'.");
      }
    }

    if (resourcelist.isEmpty()) {
      throw new SAXException(
          "Invalid Content Delivery Resource archive definition file: 0 Content Delivery Resource definitions.");
    }
  }
Exemplo n.º 2
0
  private void digestV10DTDValidatedConfig(Document configDoc) throws SAXException {
    int cdrIndex = 1;
    Element currentElement;
    String resourceSelector;

    currentElement = (Element) XmlUtil.getNode(configDoc, "/smooks-resource-list");
    String defaultSelector = DomUtils.getAttributeValue(currentElement, "default-selector");
    String defaultNamespace = DomUtils.getAttributeValue(currentElement, "default-namespace");
    String defaultUseragent = DomUtils.getAttributeValue(currentElement, "default-useragent");
    String defaultPath = DomUtils.getAttributeValue(currentElement, "default-path");

    resourceSelector = "/smooks-resource-list/smooks-resource[" + cdrIndex + "]";
    while ((currentElement = (Element) XmlUtil.getNode(configDoc, resourceSelector)) != null) {
      String selector = DomUtils.getAttributeValue(currentElement, "selector");
      String namespace = DomUtils.getAttributeValue(currentElement, "namespace");
      String useragents = DomUtils.getAttributeValue(currentElement, "useragent");
      String path = DomUtils.getAttributeValue(currentElement, "path");
      SmooksResourceConfiguration resourceConfig;

      try {
        resourceConfig =
            new SmooksResourceConfiguration(
                (selector != null ? selector : defaultSelector),
                (namespace != null ? namespace : defaultNamespace),
                (useragents != null ? useragents : defaultUseragent),
                (path != null ? path : defaultPath));
      } catch (IllegalArgumentException e) {
        throw new SAXException("Invalid unit definition.", e);
      }

      // Add the parameters...
      digestParameters(currentElement, resourceConfig);

      resourcelist.add(resourceConfig);
      if (logger.isDebugEnabled()) {
        logger.debug(
            "Adding smooks-resource config from ["
                + resourcelist.getName()
                + "]: "
                + resourceConfig);
      }

      cdrIndex++;
      resourceSelector = "/smooks-resource-list/smooks-resource[" + cdrIndex + "]";
    }
  }
Exemplo n.º 3
0
  public static void write(Edimap edimap, Writer writer) throws IOException {
    try {
      EdimapWriter edimapWriter = new EdimapWriter();

      edimapWriter.write(edimap);

      XmlUtil.serialize(edimapWriter.doc, true, writer);
      writer.flush();
    } catch (ParserConfigurationException e) {
      IOException ioE = new IOException("Error constructing EDI Mapping Model");
      ioE.initCause(e);
      throw ioE;
    }
  }
Exemplo n.º 4
0
  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
              + "'.");
    }
  }
Exemplo n.º 5
0
  private void mapBeanProperties(Object bean, Element target, String... properties) {
    for (String property : properties) {
      String[] propertyTokens = property.split("\\|");
      String propertyName;
      String attributeName;

      if (propertyTokens.length == 2) {
        propertyName = propertyTokens[0];
        attributeName = propertyTokens[1];
      } else {
        propertyName = property;
        attributeName = property;
      }

      Object value = getBeanValue(bean, propertyName);

      if (value != null) {
        target.setAttribute(attributeName, XmlUtil.removeEntities(value.toString()));
      }
    }
  }
Exemplo n.º 6
0
  private void digestImport(Element importElement, URI baseURI)
      throws SAXException, URISyntaxException, SmooksConfigurationException {
    String file = DomUtils.getAttributeValue(importElement, "file");
    URIResourceLocator resourceLocator;
    InputStream resourceStream;

    if (file == null) {
      throw new IllegalStateException(
          "Invalid resource import.  'file' attribute must be specified.");
    }

    resourceLocator = new URIResourceLocator();
    resourceLocator.setBaseURI(baseURI);

    try {
      URI fileURI = resourceLocator.resolveURI(file);

      // Add the resource URI to the list.  Will fail if it was already loaded
      pushConfig(file, fileURI);
      try {
        if (logger.isDebugEnabled()) {
          logger.debug(
              "Importing resource configuration '"
                  + file
                  + "' from inside '"
                  + configStack.peek().configFile
                  + "'.");
        }

        resourceStream = resourceLocator.getResource(file);
        try {
          List<Element> importParams = DomUtils.getElements(importElement, "param", null);
          if (!importParams.isEmpty()) {
            // Inject parameters into import config...
            String importConfig = StreamUtils.readStreamAsString(resourceStream);

            for (Element importParam : importParams) {
              String paramName = DomUtils.getAttributeValue(importParam, "name");
              String paramValue = XmlUtil.serialize(importParam.getChildNodes());

              importConfig = importConfig.replaceAll("@" + paramName + "@", paramValue);
            }

            digestConfigRecursively(
                new StringReader(importConfig),
                URIUtil.getParent(fileURI)
                    .toString()); // the file's parent URI becomes the new base URI.
          } else {
            digestConfigRecursively(
                new InputStreamReader(resourceStream),
                URIUtil.getParent(fileURI)
                    .toString()); // the file's parent URI becomes the new base URI.
          }
        } finally {
          resourceStream.close();
        }
      } finally {
        popConfig();
      }
    } catch (IOException e) {
      throw new SmooksConfigurationException(
          "Failed to load Smooks configuration resource <import> '" + file + "': " + e.getMessage(),
          e);
    }
  }