<commit_before>package org.bimserver.ifc.step.serializer;

/******************************************************************************
 * Copyright (C) 2009-2013  BIMserver.org
 * 
 * This program 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.
 * 
 * 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
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/

import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.geronimo.mail.util.Hex;
import org.bimserver.emf.IdEObject;
import org.bimserver.ifc.IfcSerializer;
import org.bimserver.interfaces.objects.SIfcHeader;
import org.bimserver.models.ifc2x3tc1.Ifc2x3tc1Package;
import org.bimserver.models.ifc2x3tc1.Tristate;
import org.bimserver.plugins.PluginConfiguration;
import org.bimserver.plugins.PluginException;
import org.bimserver.plugins.schema.EntityDefinition;
import org.bimserver.plugins.schema.SchemaDefinition;
import org.bimserver.plugins.serializers.SerializerException;
import org.bimserver.utils.StringUtils;
import org.bimserver.utils.UTF8PrintWriter;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;

public class IfcStepSerializer extends IfcSerializer {
	private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(IfcStepSerializer.class);
	private static final boolean useIso8859_1 = false;
	private static final EcorePackage ECORE_PACKAGE_INSTANCE = EcorePackage.eINSTANCE;
	private static final String NULL = "NULL";
	private static final String OPEN_CLOSE_PAREN = "()";
	private static final String ASTERISK = "*";
	private static final String PAREN_CLOSE_SEMICOLON = ");";
	private static final String DOT_0 = ".0";
	private static final String DASH = "#";
	private static final String IFC_LOGICAL = "IfcLogical";
	private static final String IFC_BOOLEAN = "IfcBoolean";
	private static final String DOT = ".";
	private static final String COMMA = ",";
	private static final String OPEN_PAREN = "(";
	private static final String CLOSE_PAREN = ")";
	private static final String BOOLEAN_UNDEFINED = ".U.";
	private static final String SINGLE_QUOTE = "'";
	private static final String BOOLEAN_FALSE = ".F.";
	private static final String BOOLEAN_TRUE = ".T.";
	private static final String DOLLAR = "$";
	private static final String WRAPPED_VALUE = "wrappedValue";
	
	private Iterator<IdEObject> iterator;
	private UTF8PrintWriter out;

	public IfcStepSerializer(PluginConfiguration pluginConfiguration) {
	}

	@Override
	public void reset() {
		setMode(Mode.HEADER);
		out = null;
	}
	
	public boolean write(OutputStream outputStream) throws SerializerException {
		if (out == null) {
			out = new UTF8PrintWriter(outputStream);
		}
		if (getMode() == Mode.HEADER) {
			writeHeader(out);
			setMode(Mode.BODY);
			iterator = model.getValues().iterator();
			out.flush();
			return true;
		} else if (getMode() == Mode.BODY) {
			if (iterator.hasNext()) {
				write(out, iterator.next());
			} else {
				iterator = null;
				setMode(Mode.FOOTER);
				return write(outputStream);
			}
			return true;
		} else if (getMode() == Mode.FOOTER) {
			writeFooter(out);
			out.flush();
			setMode(Mode.FINISHED);
			return true;
		} else if (getMode() == Mode.FINISHED) {
			return false;
		}
		return false;
	}

	private void writeFooter(UTF8PrintWriter out) {
		out.println("ENDSEC;");
		out.println("END-ISO-10303-21;");
	}

	private void writeHeader(UTF8PrintWriter out) {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'kk:mm:ss");
		out.println("ISO-10303-21;");
		out.println("HEADER;");
		SIfcHeader ifcHeader = getModel().getModelMetaData().getIfcHeader();
		if (ifcHeader == null) {
			Date date = new Date();
			out.println("FILE_DESCRIPTION ((''), '2;1');");
			out.println("FILE_NAME ('', '" + dateFormatter.format(date) + "', (''), (''), '', 'BIMserver', '');");
			out.println("FILE_SCHEMA (('IFC2X3'));");
		} else {
			out.print("FILE_DESCRIPTION ((");
			out.print(StringUtils.concat(ifcHeader.getDescription(), "'", ", "));
			out.println("), '" + ifcHeader.getImplementationLevel() + "');");
			out.println("FILE_NAME ('" + ifcHeader.getFilename() + "', '" + dateFormatter.format(ifcHeader.getTimeStamp()) + "', (" + StringUtils.concat(ifcHeader.getAuthor(), "'", ", ") + "), (" + StringUtils.concat(ifcHeader.getOrganization(), "'", ", ") + "), '" + ifcHeader.getPreProcessorVersion() + "', '" + ifcHeader.getOriginatingSystem() + "', '"	+ ifcHeader.getAuthorization() + "');");

			// TODO For now forcing IFC2x3, maybe make this a setting?
			//	out.println("FILE_SCHEMA (('" + ifcHeader.getIfcSchemaVersion() + "'));");
			out.println("FILE_SCHEMA (('IFC2X3'));");
		}
		out.println("ENDSEC;");
		out.println("DATA;");
		// out.println("//This program comes with ABSOLUTELY NO WARRANTY.");
		// out.println("//This is free software, and you are welcome to redistribute it under certain conditions. See www.bimserver.org <http://www.bimserver.org>");
	}

	private void writePrimitive(PrintWriter out, Object val) throws SerializerException {
		if (val instanceof Tristate) {
			Tristate bool = (Tristate) val;
			if (bool == Tristate.TRUE) {
				out.print(BOOLEAN_TRUE);<commit_msg>Changed HOUR format from 1-24 to 0-23<commit_after>package org.bimserver.ifc.step.serializer;

/******************************************************************************
 * Copyright (C) 2009-2013  BIMserver.org
 * 
 * This program 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.
 * 
 * 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
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/

import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.geronimo.mail.util.Hex;
import org.bimserver.emf.IdEObject;
import org.bimserver.ifc.IfcSerializer;
import org.bimserver.interfaces.objects.SIfcHeader;
import org.bimserver.models.ifc2x3tc1.Ifc2x3tc1Package;
import org.bimserver.models.ifc2x3tc1.Tristate;
import org.bimserver.plugins.PluginConfiguration;
import org.bimserver.plugins.PluginException;
import org.bimserver.plugins.schema.EntityDefinition;
import org.bimserver.plugins.schema.SchemaDefinition;
import org.bimserver.plugins.serializers.SerializerException;
import org.bimserver.utils.StringUtils;
import org.bimserver.utils.UTF8PrintWriter;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.EcorePackage;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;

public class IfcStepSerializer extends IfcSerializer {
	private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(IfcStepSerializer.class);
	private static final boolean useIso8859_1 = false;
	private static final EcorePackage ECORE_PACKAGE_INSTANCE = EcorePackage.eINSTANCE;
	private static final String NULL = "NULL";
	private static final String OPEN_CLOSE_PAREN = "()";
	private static final String ASTERISK = "*";
	private static final String PAREN_CLOSE_SEMICOLON = ");";
	private static final String DOT_0 = ".0";
	private static final String DASH = "#";
	private static final String IFC_LOGICAL = "IfcLogical";
	private static final String IFC_BOOLEAN = "IfcBoolean";
	private static final String DOT = ".";
	private static final String COMMA = ",";
	private static final String OPEN_PAREN = "(";
	private static final String CLOSE_PAREN = ")";
	private static final String BOOLEAN_UNDEFINED = ".U.";
	private static final String SINGLE_QUOTE = "'";
	private static final String BOOLEAN_FALSE = ".F.";
	private static final String BOOLEAN_TRUE = ".T.";
	private static final String DOLLAR = "$";
	private static final String WRAPPED_VALUE = "wrappedValue";
	
	private Iterator<IdEObject> iterator;
	private UTF8PrintWriter out;

	public IfcStepSerializer(PluginConfiguration pluginConfiguration) {
	}

	@Override
	public void reset() {
		setMode(Mode.HEADER);
		out = null;
	}
	
	public boolean write(OutputStream outputStream) throws SerializerException {
		if (out == null) {
			out = new UTF8PrintWriter(outputStream);
		}
		if (getMode() == Mode.HEADER) {
			writeHeader(out);
			setMode(Mode.BODY);
			iterator = model.getValues().iterator();
			out.flush();
			return true;
		} else if (getMode() == Mode.BODY) {
			if (iterator.hasNext()) {
				write(out, iterator.next());
			} else {
				iterator = null;
				setMode(Mode.FOOTER);
				return write(outputStream);
			}
			return true;
		} else if (getMode() == Mode.FOOTER) {
			writeFooter(out);
			out.flush();
			setMode(Mode.FINISHED);
			return true;
		} else if (getMode() == Mode.FINISHED) {
			return false;
		}
		return false;
	}

	private void writeFooter(UTF8PrintWriter out) {
		out.println("ENDSEC;");
		out.println("END-ISO-10303-21;");
	}

	private void writeHeader(UTF8PrintWriter out) {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		out.println("ISO-10303-21;");
		out.println("HEADER;");
		SIfcHeader ifcHeader = getModel().getModelMetaData().getIfcHeader();
		if (ifcHeader == null) {
			Date date = new Date();
			out.println("FILE_DESCRIPTION ((''), '2;1');");
			out.println("FILE_NAME ('', '" + dateFormatter.format(date) + "', (''), (''), '', 'BIMserver', '');");
			out.println("FILE_SCHEMA (('IFC2X3'));");
		} else {
			out.print("FILE_DESCRIPTION ((");
			out.print(StringUtils.concat(ifcHeader.getDescription(), "'", ", "));
			out.println("), '" + ifcHeader.getImplementationLevel() + "');");
			out.println("FILE_NAME ('" + ifcHeader.getFilename() + "', '" + dateFormatter.format(ifcHeader.getTimeStamp()) + "', (" + StringUtils.concat(ifcHeader.getAuthor(), "'", ", ") + "), (" + StringUtils.concat(ifcHeader.getOrganization(), "'", ", ") + "), '" + ifcHeader.getPreProcessorVersion() + "', '" + ifcHeader.getOriginatingSystem() + "', '"	+ ifcHeader.getAuthorization() + "');");

			// TODO For now forcing IFC2x3, maybe make this a setting?
			//	out.println("FILE_SCHEMA (('" + ifcHeader.getIfcSchemaVersion() + "'));");
			out.println("FILE_SCHEMA (('IFC2X3'));");
		}
		out.println("ENDSEC;");
		out.println("DATA;");
		// out.println("//This program comes with ABSOLUTELY NO WARRANTY.");
		// out.println("//This is free software, and you are welcome to redistribute it under certain conditions. See www.bimserver.org <http://www.bimserver.org>");
	}

	private void writePrimitive(PrintWriter out, Object val) throws SerializerException {
		if (val instanceof Tristate) {
			Tristate bool = (Tristate) val;
			if (bool == Tristate.TRUE) {
				out.print(BOOLEAN_TRUE);
{'fraction_non_alphanumeric': 0.155188679245283, 'fraction_numerical': 0.008962264150943396, 'mean_word_length': 4.903016241299304, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, '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': '2894052', 'n_tokens_mistral': 4276, 'n_tokens_neox': 3989, 'n_words': 953}