/*
 * Java GPX Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * 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.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
 *    Antoine Vianey (https://github.com/avianey)
 */
package io.jenetics.jpx;

import java.util.Iterator;
import java.util.ServiceLoader;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;

/**
 * A {@link ServiceLoader} for managing XML factories used by the library.
 * Custom implementation should be referenced in a META-INF/services/io.jenetics.jpx.XMLProvider file
 */
public abstract class XMLProvider {

	private static volatile XMLProvider INSTANCE;
	private static final Object lock = new Object();

	protected XMLProvider() {
	}

	/**
	 * Returns {@link XMLInputFactory} to be used for reading files.
	 *
	 * @return the xml input factory
	 */
	public XMLInputFactory xmlInputFactory() {
		return XMLInputFactory.newInstance();
	}

	/**
	 * Returns {@link XMLOutputFactory} to be used for writing files.
	 *
	 * @return the xml output factory
	 */
	public XMLOutputFactory xmlOutputFactory() {
		return XMLOutputFactory.newInstance();
	}

	/**
	 * Returns the {@link DocumentBuilderFactory} used for handling extensions documents
	 *
	 * @return the document builder factory
	 */
	public DocumentBuilderFactory documentBuilderFactory() {
		final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setIgnoringElementContentWhitespace(true);
		factory.setNamespaceAware(true);
		return factory;
	}

	public static XMLProvider provider() {
		if (INSTANCE == null) {
			synchronized (lock) {
				if (INSTANCE == null) {
					loadInstance();
				}
			}
		}
		return INSTANCE;
	}

	/**
	 * Clear current spi to allow hot reloading a new one...
	 */
	protected static void clear() {
		if (INSTANCE != null) {
			synchronized (lock) {
				if (INSTANCE != null) {
					INSTANCE = null;
				}
			}
		}
	}

	private static void loadInstance() {
		ServiceLoader<XMLProvider> loader = ServiceLoader.load(XMLProvider.class);
		Iterator<XMLProvider> providers = loader.iterator();
		if (providers.hasNext()) {
			INSTANCE = providers.next();
		} else {
			INSTANCE = new XMLProvider() {};
		}
	}

}

@@ -1121,16 +1121,26 @@
 ced in a
+%0A * %7B@code
  META-IN
@@ -1177,21 +1177,81 @@
 Provider
+%7D
  file
+.%0A *%0A * @see ServiceLoader%0A *%0A * @version 1.7%0A * @since 1.7
 %0A */%0Apub
@@ -2210,16 +2210,148 @@
 ry;%0A%09%7D%0A%0A
+%09/**%0A%09 * Return an instance of the current %7B@code XMLProvider%7D.%0A%09 *%0A%09 * @return an instance of the current %7B@code XMLProvider%7D%0A%09 */%0A
 %09public 
@@ -2781,16 +2781,22 @@
 e() %7B%0A%09%09
+final 
 ServiceL
@@ -2822,17 +2822,20 @@
 loader =
- 
+%0A%09%09%09
 ServiceL
@@ -2865,18 +2865,25 @@
 class);%0A
+%0A
 %09%09
+final 
 Iterator

Improve Javadoc.