<commit_before>/*

Copyright (C) 2016 - <NAME> / Rapid Information Systems

<EMAIL>


This file is part of the Rapid Application Platform

Rapid is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version. The terms require you
to include the original copyright, and the license notice in all redistributions.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
in a file named "COPYING".  If not, see <http://www.gnu.org/licenses/>.

*/

package com.rapid.soa;

import java.util.List;

public abstract class SOADataWriter {

	// private instance variables

	protected SOAData _soaData;

	// properties

	public SOAData getSOAData() { return _soaData; }

	// constructor

	public SOADataWriter(SOAData soaData) {
		_soaData = soaData;
	}

	// abstract methods

	public abstract String write();


	// implementing classes

	/*
	 *  SOAXMLWriter
	 *
	 *  Writes SOAData as XML
	 *
	 */

	public static class SOAXMLWriter extends SOADataWriter {

		private static final String NAMESPACE_PREFIX = "soa:";

		StringBuilder _stringBuilder;

		public SOAXMLWriter(SOAData soaData) {
			super(soaData);
		}

		private String xmlEscape(String value) {
			return value.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;").replace("'", "&apos;");
		}

		private void appendElement(SOAElement element) {

			// get the value
			String value = element.getValue();

			// elements require either children or a value to be printed
			if (element.getChildElements() != null || value != null) {

				// check whether array element
				if (element.getIsArray()) {
					// open array
					_stringBuilder.append("<" + NAMESPACE_PREFIX + element.getName() + "Array>");
				} else {
					// open the element
					_stringBuilder.append("<" + NAMESPACE_PREFIX + element.getName() + ">");
				}

				// if we got a value print it into the element
				if (value != null) _stringBuilder.append(xmlEscape(value.trim()));

				// check for any child elements
				List<SOAElement> childElements = element.getChildElements();

				// if we have child elements
				if (childElements != null) {
					// loop and write the child elements
					for (SOAElement childElement : childElements) {
						appendElement(childElement);
					}
				}

				// check whether the array
				if (element.getIsArray()) {
					// close array (if need be)
					_stringBuilder.append("</" + NAMESPACE_PREFIX + element.getName() + "Array>");
				} else {
					// close the element
					_stringBuilder.append("</" + NAMESPACE_PREFIX + element.getName() + ">");
				}
			}
		}

		@Override
		public String write() {
			_stringBuilder = new StringBuilder();
			if (_soaData != null) 	appendElement(_soaData.getRootElement());
			return _stringBuilder.toString();
		}

	}

	/*
	 *  SOAJSONReader
	 *
	 *  Writes SOAData as JSON
	 *
	 */

	public static class SOAJSONWriter extends SOADataWriter {

		StringBuilder _stringBuilder;

		public SOAJSONWriter(SOAData soaData) {
			super(soaData);
		}

		private void append(SOAElement element) {

			if (element.getIsArray()) {

				_stringBuilder.append("{\"" + element.getName() + "\":[");

				List<SOAElement> childElements = element.getChildElements();

				if (childElements != null) {

					for (int i = 0; i < childElements.size(); i ++) {

						append(childElements.get(i));

						if (i < childElements.size() - 1) _stringBuilder.append(",");

					}

				}

				_stringBuilder.append("]}");

			} else {

				if (element.getParentElement() == null) {

					// only the root element has no parent and we have already established that it is not an array

					List<SOAElement> childElements = element.getChildElements();

					if (childElements != null) {

						_stringBuilder.append("{");

						for (int i = 0; i < childElements.size(); i ++) {

							append(childElements.get(i));

							if (i < childElements.size() - 1) _stringBuilder.append(",");

						}

						_stringBuilder.append("}");

					}

				} else {


					if (element.getParentElement().getIsArray()) {

						List<SOAElement> childElements = element.getChildElements();

						if (childElements != null) {

							_stringBuilder.append("{");

							for (int i = 0; i < childElements.size(); i ++) {

								append(childElements.get(i));

								// if we're not on the final element
								if (i < childElements.size() - 1) {
									// only add a separating comma if the next element has a value
									if (element.getChildElements().get(i + 1).getValue() != null) _stringBuilder.append(",");
<commit_msg>Fix to SOADataWriter<commit_after>/*

Copyright (C) 2017 - <NAME> / Rapid Information Systems

<EMAIL>


This file is part of the Rapid Application Platform

Rapid is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version. The terms require you
to include the original copyright, and the license notice in all redistributions.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
in a file named "COPYING".  If not, see <http://www.gnu.org/licenses/>.

*/

package com.rapid.soa;

import java.util.List;

public abstract class SOADataWriter {

	// private instance variables

	protected SOAData _soaData;

	// properties

	public SOAData getSOAData() { return _soaData; }

	// constructor

	public SOADataWriter(SOAData soaData) {
		_soaData = soaData;
	}

	// abstract methods

	public abstract String write();


	// implementing classes

	/*
	 *  SOAXMLWriter
	 *
	 *  Writes SOAData as XML
	 *
	 */

	public static class SOAXMLWriter extends SOADataWriter {

		private static final String NAMESPACE_PREFIX = "soa:";

		StringBuilder _stringBuilder;

		public SOAXMLWriter(SOAData soaData) {
			super(soaData);
		}

		private String xmlEscape(String value) {
			return value.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;").replace("'", "&apos;");
		}

		private void appendElement(SOAElement element) {

			// get the value
			String value = element.getValue();

			// elements require either children or a value to be printed
			if (element.getChildElements() != null || value != null) {

				// check whether array element
				if (element.getIsArray()) {
					// open array
					_stringBuilder.append("<" + NAMESPACE_PREFIX + element.getName() + "Array>");
				} else {
					// open the element
					_stringBuilder.append("<" + NAMESPACE_PREFIX + element.getName() + ">");
				}

				// if we got a value print it into the element
				if (value != null) _stringBuilder.append(xmlEscape(value.trim()));

				// check for any child elements
				List<SOAElement> childElements = element.getChildElements();

				// if we have child elements
				if (childElements != null) {
					// loop and write the child elements
					for (SOAElement childElement : childElements) {
						appendElement(childElement);
					}
				}

				// check whether the array
				if (element.getIsArray()) {
					// close array (if need be)
					_stringBuilder.append("</" + NAMESPACE_PREFIX + element.getName() + "Array>");
				} else {
					// close the element
					_stringBuilder.append("</" + NAMESPACE_PREFIX + element.getName() + ">");
				}
			}
		}

		@Override
		public String write() {
			_stringBuilder = new StringBuilder();
			if (_soaData != null) 	appendElement(_soaData.getRootElement());
			return _stringBuilder.toString();
		}

	}

	/*
	 *  SOAJSONReader
	 *
	 *  Writes SOAData as JSON
	 *
	 */

	public static class SOAJSONWriter extends SOADataWriter {

		StringBuilder _stringBuilder;

		public SOAJSONWriter(SOAData soaData) {
			super(soaData);
		}

		private void append(SOAElement element) {

			if (element.getIsArray()) {

				_stringBuilder.append("{\"" + element.getName() + "\":[");

				List<SOAElement> childElements = element.getChildElements();

				if (childElements != null) {

					for (int i = 0; i < childElements.size(); i ++) {

						append(childElements.get(i));

						if (i < childElements.size() - 1) _stringBuilder.append(",");

					}

				}

				_stringBuilder.append("]}");

			} else {

				// only the root element has no parent and we have already established that it is not an array
				if (element.getParentElement() == null) {

					// open json object
					_stringBuilder.append("{");

					// get any children
					List<SOAElement> childElements = element.getChildElements();

					// if no children just print this value
					if (childElements == null) {

						_stringBuilder.append("\"" + element.getName() + "\":\"" + jsonEscape(element.getValue()) + "\"");

					} else {

						for (int i = 0; i < childElements.size(); i ++) {

							append(childElements.get(i));

							if (i < childElements.size() - 1) _stringBuilder.append(",");

						}

					}

					// close json object
					_stringBuilder.append("}");

				} else {

					if (element.getParentElement().getIsArray()) {

						List<SOAElement> childElements = element.getChildElements();

						if (childElements != null) {

							_stringBuilder.append("{");

							for (int i = 0; i < childElements.size(); i ++) {

								append(childElements.get(i));

								// if we're not on the final element
								if (i < childElements.size() - 1) {
									// only add a separating comma if the next element has a value
									if (element.getChildElements().get(i + 1).getValue() != null) _stringBuilder.append(",");

{'fraction_non_alphanumeric': 0.12246305418719212, 'fraction_numerical': 0.0023645320197044333, 'mean_word_length': 3.070168404170008, 'pattern_counts': {'":': 3, '<': 39, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, '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': '1349759', 'n_tokens_mistral': 3697, 'n_tokens_neox': 2958, 'n_words': 938}