/*
 * Copyright 2000-2016 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package consulo.execution.test.sm.runner.history;

import consulo.annotation.component.ComponentScope;
import consulo.annotation.component.ExtensionAPI;
import consulo.component.extension.ExtensionPointName;
import consulo.execution.test.sm.runner.GeneralTestEventsProcessor;
import org.xml.sax.helpers.DefaultHandler;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import java.io.IOException;
import java.io.Reader;
import java.util.function.Supplier;

/**
 * Extension to import test output from xml file, e.g. generated by ant task (https://github.com/windyroad/JUnit-Schema)
 */
@ExtensionAPI(ComponentScope.APPLICATION)
public interface ImportTestOutputExtension {
  ExtensionPointName<ImportTestOutputExtension> EP_NAME = ExtensionPointName.create(ImportTestOutputExtension.class);

  /**
   * When extension can parse xml file under reader, then it should return corresponding handler. Otherwise return null.
   * <p>
   * When no custom extension accepts the xml, xml would be parsed as it was exported by IDEA itself {@link ImportTestOutputExtension#findHandler(Supplier, GeneralTestEventsProcessor)}
   *
   * @return handler if xml contains tests output in recognised format,
   * otherwise null
   */
  @Nullable
  DefaultHandler createHandler(final Reader reader, GeneralTestEventsProcessor processor) throws IOException;

  @Nonnull
  static DefaultHandler findHandler(final Supplier<Reader> readerSupplier, GeneralTestEventsProcessor processor) {
    for (ImportTestOutputExtension extension : EP_NAME.getExtensionList()) {
      Reader reader = readerSupplier.get();
      if (reader == null) continue;
      try {
        DefaultHandler handler = extension.createHandler(reader, processor);
        if (handler != null) return handler;
      }
      catch (IOException ignored) {
      }
    }
    return new ImportedTestContentHandler(processor);
  }
}
