package ch.openech.model;

import java.io.InputStream;
import java.io.StringReader;
import java.util.logging.Logger;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import ch.openech.xml.EchSchemas;
import ch.openech.xml.read.LSInputImpl;

public class EchSchemaValidation {
	public static final Logger logger = Logger.getLogger(EchSchemaValidation.class.getName());
	public static final String OK = "ok";

	//

	static {
	}

	private static Validator validator;

	static Validator getValidator() throws SAXException {
		if (validator == null) {
			SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

			LSResourceResolver resourceResolver = new LSResourceResolver() {
				@Override
				public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
					if (namespaceURI == null) {
						// wenn xml element ohne qualifier (schema Kürzel) angegeben werden wird
						// tatsächlich der Resolver mit null aufgerufen.
						return null;
					}

					String fileName = EchSchemas.getFile(namespaceURI);
					InputStream stream = this.getClass().getClassLoader().getResourceAsStream(fileName);

					return new LSInputImpl(publicId, systemId, baseURI, stream, null);
				}
			};

			schemaFactory.setResourceResolver(resourceResolver);
			Schema schema = schemaFactory.newSchema();
			validator = schema.newValidator();
			validator.setResourceResolver(resourceResolver);
		}
		return validator;
	}

	public static String validate(String string) {
		String message = OK;
		try {
			getValidator().validate(new StreamSource(new StringReader(string)));
		} catch (SAXParseException parseException) {
			message = "XML invalid:\n";
			message += parseException.getLocalizedMessage() + "\n";
			message += "Line: " + parseException.getLineNumber();
			message += " - Column: " + parseException.getColumnNumber();
		} catch (SAXException e) {
			message = e.getLocalizedMessage();
			e.printStackTrace();
		} catch (Exception e) {
			message = e.getLocalizedMessage();
			e.printStackTrace();
		}
		return message;
	}

}

@@ -868,16 +868,19 @@
 ctory = 
+get
 SchemaFa
@@ -888,55 +888,9 @@
 tory
-.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI
+(
 );%0A%0A
@@ -1739,16 +1739,509 @@
 or;%0A%09%7D%0A%0A
+%09private static SchemaFactory getSchemaFactory() %7B%0A%09%09try %7B%0A%09%09%09@SuppressWarnings(%22unchecked%22)%0A%09%09%09// Remote Fetch URL https://github.com/apache/xerces2-j%0A%09%09%09// Tag Xerces-J_2_12_0-xml-schema-1.1%0A%09%09%09Class%3CSchemaFactory%3E xmlSchema11FactoryClass = (Class%3CSchemaFactory%3E) Class.forName(%22org.apache.xerces.jaxp.validation.XMLSchema11Factory%22);%0A%09%09%09return xmlSchema11FactoryClass.newInstance();%0A%09%09%7D catch (Exception x) %7B%0A%09%09%09return SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);%0A%09%09%7D%0A%09%7D%0A%0A
 %09public 

Support für xsd 1.1