/*
 * Copyright 2016 Red Hat, Inc, and individual contributors.
 *
 * 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 org.projectodd.vdx.wildfly;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;

import org.jboss.logging.BasicLogger;
import org.projectodd.vdx.core.ErrorPrinter;
import org.projectodd.vdx.core.ErrorType;
import org.projectodd.vdx.core.I18N;
import org.projectodd.vdx.core.Stringifier;
import org.projectodd.vdx.core.ValidationError;
import org.projectodd.vdx.core.XMLStreamValidationException;

public class ErrorReporter {
    public ErrorReporter(final File document, final BasicLogger logger) {
        this.document = document;
        this.logger = logger;
    }

    /**
     * Reports an error to VDX.
     * @param exception
     * @return true if the error was actually printed
     */
    public boolean report(final XMLStreamException exception) {
        boolean printed = false;
        try {
            final List<URL> schemas = findSchemas();

            if (!schemas.isEmpty()) {
                final ValidationError error;
                if (exception instanceof XMLStreamValidationException) {
                    error = ((XMLStreamValidationException) exception).getValidationError();
                } else {
                    final String message = exception.getMessage();

                    // detect duplicate attribute - this message comes from woodstox, and isn't i18n, so we don't have to
                    // worry about other languages
                    final Matcher dupMatcher = Pattern.compile("^Duplicate attribute '(.+?)'\\.").matcher(message);
                    if (dupMatcher.find()) {
                        error = ValidationError.from(exception, ErrorType.DUPLICATE_ATTRIBUTE)
                                .attribute(QName.valueOf(dupMatcher.group(1)));
                    } else {
                        error = ValidationError.from(exception, ErrorType.UNKNOWN_ERROR);
                        // attempt to strip the message code
                        final Matcher m = Pattern.compile("Message: \"?([A-Z]+\\d+: )?(.*?)\"?$").matcher(message);
                        if (m.find()) {
                            error.fallbackMessage(m.group(2));
                        }
                    }
                }

                final List<Stringifier> stringifiers = new ArrayList<>();
                stringifiers.add(new SubsystemStringifier());

                SchemaDocRelationships rel = new SchemaDocRelationships();

                new ErrorPrinter(this.document.toURI().toURL(), schemas)
                        .printer(new LoggingPrinter(this.logger))
                        .stringifiers(stringifiers)
                        .pathGate(rel)
                        .prefixProvider(rel)
                        .print(error);
                printed = true;
            }
        } catch (Exception ex) {
            this.logger.info(I18N.failedToPrintError(ex));
        }

        return printed;
    }

    private List<URL> findSchemas() {
        final ServiceLoader<SchemaProvider> loader = ServiceLoader.load(SchemaProvider.class);
        final SchemaProvider provider;
        if (loader.iterator().hasNext()) {
            provider = loader.iterator().next();
        } else {
            provider = new DefaultWildFlySchemaProvider();
        }

        final List<URL> schemas = provider.schemas();

        if (schemas.isEmpty()) {
            this.logger.info(I18N.noSchemasAvailable(provider.schemaResource()));
        }

        return schemas;
    }

    private final File document;
    private final BasicLogger logger;
}

@@ -750,40 +750,8 @@
 st;%0A
-import java.util.ServiceLoader;%0A
 impo
@@ -3701,21 +3701,23 @@
 %0A%0A    pr
-ivate
+otected
  List%3CUR
@@ -3754,278 +3754,34 @@
 al S
-erviceLoader%3CSchemaProvider%3E loader = ServiceLoader.load(SchemaProvider.class);%0A        final SchemaProvider provider;%0A        if (loader.iterator().hasNext()) %7B%0A            provider = loader.iterator().next();%0A        %7D else %7B%0A            provider = new DefaultWildFlyS
+chemaProvider provider = s
 chem
@@ -3793,27 +3793,16 @@
 ider();%0A
-        %7D%0A%0A
         
@@ -3966,32 +3966,32 @@
 )));%0A        %7D%0A%0A
-
         return s
@@ -4001,24 +4001,130 @@
 mas;%0A    %7D%0A%0A
+    protected SchemaProvider schemaProvider() %7B%0A        return new DefaultWildFlySchemaProvider();%0A    %7D%0A%0A
     private 

Remove ServiceLoader bits, they are overkill