<commit_before>/*******************************************************************************
 * Copyright (c) 2010 <NAME>
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     <NAME> (JKM Software) - initial API and implementation
 *******************************************************************************/
package org.openhealthtools.mdht.uml.cda.internal.validate;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.openhealthtools.mdht.uml.cda.ClinicalDocument;
import org.openhealthtools.mdht.uml.cda.DocumentRoot;
import org.openhealthtools.mdht.uml.cda.util.BasicValidationHandler;
import org.openhealthtools.mdht.uml.cda.util.CDAUtil;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * Simple main validate routine used by the workspace xml instance validation in
 * the cda xml ui plugin
 * 
 * Step 1 Index XML Instance Step 2 Validation Instance Step 3 write results to
 * temp file
 * 
 */
public class Validate {

	public static String getPath(EObject eObject) {
		String path = "";
		while (eObject != null && !(eObject instanceof DocumentRoot)) {
			EStructuralFeature feature = eObject.eContainingFeature();
			EObject container = eObject.eContainer();
			Object value = container.eGet(feature);
			if (feature.isMany()) {
				List<?> list = (List<?>) value;
				int index = list.indexOf(eObject) + 1;
				path = "/" + feature.getName() + "[" + index + "]" + path;
			} else {
				path = "/" + feature.getName() + "[1]" + path;
			}
			eObject = eObject.eContainer();
		}
		return path;
	}

	public static void main(String[] args) {

		final String DELIMITER = "~";

		ClinicalDocument clinicalDocument;

		CDAUtil.loadPackages();

		try {

			URI cdaURI = URI.createURI(args[0]);

			if (cdaURI.isFile()) {

				InputSource inputSource = new InputSource(cdaURI.path());

				inputSource.setEncoding("UTF-8");

				OutputStream fout = new FileOutputStream(args[1]);

				OutputStream bout = new BufferedOutputStream(fout);

				final OutputStreamWriter out = new OutputStreamWriter(bout);

				XMLReader parser = XMLReaderFactory.createXMLReader();

				// Install the Content Handler
				final XPathIndexer xpathIndexer = new XPathIndexer();

				parser.setContentHandler(xpathIndexer);

				try {

					parser.parse(cdaURI.path());

					clinicalDocument = CDAUtil.load(inputSource);

					CDAUtil.validate(clinicalDocument, new BasicValidationHandler() {

						@Override
						public void handleError(Diagnostic diagnostic) {

							String path = "";
							if (diagnostic.getData().size() > 0 && diagnostic.getData().get(0) instanceof EObject) {
								path = getPath((EObject) diagnostic.getData().get(0));
							}

							XPathIndexer.ElementLocationData eld = xpathIndexer.getElementLocationByPath(path.toUpperCase());

							try {
								if (eld != null) {
									out.write("error" + DELIMITER + eld.line + DELIMITER + eld.column + DELIMITER +
											diagnostic.getMessage() + "\n");
								} else {
									out.write("error" + DELIMITER + 0 + DELIMITER + 0 + DELIMITER +
											diagnostic.getMessage() + "(" + path + ")" + "\n");<commit_msg>Support validating for specified document type EClass.<commit_after>/*******************************************************************************
 * Copyright (c) 2010 <NAME>
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     <NAME> (JKM Software) - initial API and implementation
 *******************************************************************************/
package org.openhealthtools.mdht.uml.cda.internal.validate;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.openhealthtools.mdht.uml.cda.ClinicalDocument;
import org.openhealthtools.mdht.uml.cda.DocumentRoot;
import org.openhealthtools.mdht.uml.cda.util.BasicValidationHandler;
import org.openhealthtools.mdht.uml.cda.util.CDAUtil;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * Simple main validate routine used by the workspace xml instance validation in
 * the cda xml ui plugin
 * 
 * Step 1 Index XML Instance Step 2 Validation Instance Step 3 write results to
 * temp file
 * 
 */
public class Validate {

	public static String getPath(EObject eObject) {
		String path = "";
		while (eObject != null && !(eObject instanceof DocumentRoot)) {
			EStructuralFeature feature = eObject.eContainingFeature();
			EObject container = eObject.eContainer();
			Object value = container.eGet(feature);
			if (feature.isMany()) {
				List<?> list = (List<?>) value;
				int index = list.indexOf(eObject) + 1;
				path = "/" + feature.getName() + "[" + index + "]" + path;
			} else {
				path = "/" + feature.getName() + "[1]" + path;
			}
			eObject = eObject.eContainer();
		}
		return path;
	}

	/**
	 * arg0 = CDA input URI
	 * arg1 = output file for validation results
	 * arg3 = model qualified name for document type class, or null
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		final String DELIMITER = "~";

		ClinicalDocument clinicalDocument;

		CDAUtil.loadPackages();

		try {

			URI cdaURI = URI.createURI(args[0]);

			if (cdaURI.isFile()) {

				InputSource inputSource = new InputSource(cdaURI.path());

				inputSource.setEncoding("UTF-8");

				OutputStream fout = new FileOutputStream(args[1]);

				OutputStream bout = new BufferedOutputStream(fout);

				final OutputStreamWriter out = new OutputStreamWriter(bout);

				XMLReader parser = XMLReaderFactory.createXMLReader();

				// Install the Content Handler
				final XPathIndexer xpathIndexer = new XPathIndexer();

				parser.setContentHandler(xpathIndexer);

				// set the document type class, or null to discover from templateId
				if (args.length >= 3) {
					String documentClassQName = args[2];
					CDAUtil.setDocumentClassQName(documentClassQName);
				}

				try {

					parser.parse(cdaURI.path());

					clinicalDocument = CDAUtil.load(inputSource);

					CDAUtil.validate(clinicalDocument, new BasicValidationHandler() {

						@Override
						public void handleError(Diagnostic diagnostic) {

							String path = "";
							if (diagnostic.getData().size() > 0 && diagnostic.getData().get(0) instanceof EObject) {
								path = getPath((EObject) diagnostic.getData().get(0));
							}

							XPathIndexer.ElementLocationData eld = xpathIndexer.getElementLocationByPath(path.toUpperCase());

							try {
								if (eld != null) {
									out.write("error" + DELIMITER + eld.line + DELIMITER + eld.column + DELIMITER +
											diagnostic.getMessage() + "\n");
								} else {
									out.write("error" + DELIMITER + 0 + DELIMITER + 0 + DELIMITER +
											diagnostic.getMessage() + "(" + path + ")" + "\n");
{'fraction_non_alphanumeric': 0.15371046995086304, 'fraction_numerical': 0.005921632858762757, 'mean_word_length': 3.8461538461538463, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 12}, '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': '6547093', 'n_tokens_mistral': 2788, 'n_tokens_neox': 2321, 'n_words': 565}