<commit_before>/*
 * SchemaValidation.java
 */

package gov.nist.registry.common2.xml;

import gov.nist.registry.common2.MetadataTypes;
import gov.nist.registry.common2.exception.XdsInternalException;
import gov.nist.registry.common2.registry.Properties;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.io.StringReader;
import org.apache.axiom.om.OMElement;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class SchemaValidation implements MetadataTypes {

	public static String validate(OMElement ele, int metadataType)  throws XdsInternalException {
		return validate_local(ele, metadataType);
	}

	// The only known use case for localhost validation failing is when this is called from
	// xdstest2 in which case it is trying to call home to reference the schema files.
	// What is really needed is a configuration parm that points the reference to the local filesystem
	// and include the schema files in the xdstest2tool environment.

	// port 80 does not exist for requests on-machine (on the server). only requests coming in from
	// off-machine go through the firewall where the port translation happens.

	// even though this says validate_local, it is used by all requests
	public static String validate_local(OMElement ele, int metadataType)  throws XdsInternalException {
		String msg;
		msg = SchemaValidation.run(ele.toString(), metadataType);
		return msg;
	}


	// empty string as result means no errors
	static private String run(String metadata, int metadataType) throws XdsInternalException {


		MyErrorHandler errors = null;
		DOMParser p = null;
		String localSchema =null;
		String SchemaLoc = Properties.loader().getString("XDSSchemaDir");
		//localSchema = CommonProperties.getInstance().getProperty("XDSSchemaDir");
		
		File file =new File(SchemaLoc);
		try{
			localSchema = file.getCanonicalPath();
		}catch (Exception e) {
		    throw new XdsInternalException("I/O exception occured while getting the canonical path");
		}
  		// Decode schema location
		String schemaLocation;
		switch (metadataType) {
		case METADATA_TYPE_Rb:
			schemaLocation = "urn:oasis:names:tc:ebxml-regrep:xsd:lcm:3.0 " + 
			((localSchema == null) ? 
					"/schema/v3/lcm.xsd":
					localSchema + "/schema/v3/lcm.xsd");
			break;
		case METADATA_TYPE_PR:
		case METADATA_TYPE_R:
			schemaLocation = "urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1 " +
			((localSchema == null) ?
			"/schema/v2/rs.xsd" :
			localSchema + "/schema/v2/rs.xsd");
			break;
		case METADATA_TYPE_Q:
			schemaLocation =
			"urn:oasis:names:tc:ebxml-regrep:query:xsd:2.1 " +
			((localSchema == null) ?
			"/schema/v2/query.xsd " :
			localSchema + "/schema/v2/query.xsd "	) + 
			
			"urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1 " +
			((localSchema == null) ?
			"/schema/v2/rs.xsd" :
			localSchema + "/schema/v2/rs.xsd" ) ;
			<commit_msg>Added a System environment property to the XdsSchema  path.<commit_after>/*
 * SchemaValidation.java
 */

package gov.nist.registry.common2.xml;

import gov.nist.registry.common2.MetadataTypes;
import gov.nist.registry.common2.exception.XdsInternalException;
import gov.nist.registry.common2.registry.Properties;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.io.StringReader;
import org.apache.axiom.om.OMElement;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class SchemaValidation implements MetadataTypes {

	public static String validate(OMElement ele, int metadataType)  throws XdsInternalException {
		return validate_local(ele, metadataType);
	}

	// The only known use case for localhost validation failing is when this is called from
	// xdstest2 in which case it is trying to call home to reference the schema files.
	// What is really needed is a configuration parm that points the reference to the local filesystem
	// and include the schema files in the xdstest2tool environment.

	// port 80 does not exist for requests on-machine (on the server). only requests coming in from
	// off-machine go through the firewall where the port translation happens.

	// even though this says validate_local, it is used by all requests
	public static String validate_local(OMElement ele, int metadataType)  throws XdsInternalException {
		String msg;
		msg = SchemaValidation.run(ele.toString(), metadataType);
		return msg;
	}


	// empty string as result means no errors
	static private String run(String metadata, int metadataType) throws XdsInternalException {


		MyErrorHandler errors = null;
		DOMParser p = null;
		//Check System property first which takes a priority
		String localSchema = System.getenv("XDSSchemaDir");

		if (localSchema == null) {
			String SchemaLoc = Properties.loader().getString("XDSSchemaDir");
			File file =new File(SchemaLoc);
			try{
				localSchema = file.getCanonicalPath();
			}catch (Exception e) {
			    throw new XdsInternalException("I/O exception occured while getting the canonical path");
			}
		}
  		// Decode schema location
		String schemaLocation;
		switch (metadataType) {
		case METADATA_TYPE_Rb:
			schemaLocation = "urn:oasis:names:tc:ebxml-regrep:xsd:lcm:3.0 " + 
			((localSchema == null) ? 
					"/schema/v3/lcm.xsd":
					localSchema + "/schema/v3/lcm.xsd");
			break;
		case METADATA_TYPE_PR:
		case METADATA_TYPE_R:
			schemaLocation = "urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1 " +
			((localSchema == null) ?
			"/schema/v2/rs.xsd" :
			localSchema + "/schema/v2/rs.xsd");
			break;
		case METADATA_TYPE_Q:
			schemaLocation =
			"urn:oasis:names:tc:ebxml-regrep:query:xsd:2.1 " +
			((localSchema == null) ?
			"/schema/v2/query.xsd " :
			localSchema + "/schema/v2/query.xsd "	) + 
			
			"urn:oasis:names:tc:ebxml-regrep:registry:xsd:2.1 " +
			((localSchema == null) ?
			"/schema/v2/rs.xsd" :
			localSchema + "/schema/v2/rs.xsd" ) ;
			
{'fraction_non_alphanumeric': 0.10618881699021072, 'fraction_numerical': 0.007964161274265804, 'mean_word_length': 4.095519864750634, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1697649', 'n_tokens_mistral': 2111, 'n_tokens_neox': 1881, 'n_words': 507}