<commit_before>/*
 * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, v. 2.1. This program is distributed in the
 * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details. You should have received a
 * copy of the GNU Lesser General Public License, v.2.1 along with this
 * distribution; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Red Hat Author(s): <NAME>
 */
package org.jboss.jgettext.catalog.write;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;

import org.jboss.jgettext.Catalog;
import org.jboss.jgettext.Message;
import org.jboss.jgettext.Occurence;

/**
 * MessageProcessor implementation
 *
 * @author <NAME>
 */
public class MessageProcessor implements Catalog.MessageProcessor {
	protected final Writer writer;
	protected final Message header; // header handled specially...

	public MessageProcessor(Message header, Writer writer) {
		this.writer = writer;
		this.header = header;
	}

	public MessageProcessor(Writer writer) {
		this( null, writer );
	}

	public MessageProcessor() {
		this( new OutputStreamWriter( System.out ) );
	}

	public void processMessage(Message message) {
		if ( message == header ) {
			return;
		}
		writeMessage(message);
	}

	public void writeMessage(Message message) {
	    try {
	    	messageStart( message );

	    	for ( String comment : message.getComments() ) {
	    		writer.write( "#" + comment ); // no space on purpose!!!
	    		writer.write( '\n' );
	    	}

	    	for ( String comment : message.getExtractedComments() ) {
	    		writer.write( "#. " + comment );
	    		writer.write( '\n' );
	    	}

	    	for ( Occurence occurence : message.getOccurences() ) {
	    		writer.write( "#: " + occurence.toString() );
	    		writer.write( '\n' );
	    	}

	    	if ( message.isFuzzy() ) {
	    		writer.write( "#, fuzzy" );
	    		writer.write( '\n' );
	    	}

	    	for ( String format : message.getFormats() ) {
	    		writer.write( "#, " + format );
	    		writer.write( '\n' );
	    	}

	    	if ( message.getPrevMsgctx() != null ) {
	    		writeMsgctxt( "#| ", message.getPrevMsgctx() );
	    	}

	    	if ( message.getPrevMsgid() != null ) {
	    		writeMsgid( "#| ", message.getPrevMsgid() );
	    	}

	    	if ( message.getPrevMsgidPlural() != null ) {
	    		writeMsgidPlural( "#| ", message.getPrevMsgidPlural() );
	    	}

	    	String prefix = message.isObsolete() ? "#~ " : "";
	    	if ( message.getMsgctxt() != null ) {
	    		writeMsgctxt( prefix, message.getMsgctxt() );
	    	}

	    	writeMsgid( prefix, message.getMsgid() );

	    	if ( message.getMsgidPlural() != null ) {
	    		writeMsgidPlural( prefix, message.getMsgidPlural() );
	    	}

	    	writeMsgstr( prefix, message.getMsgstr() );

	    	writeMsgstrPlurals( prefix, message.getMsgstrPlural() );

	    	messageEnd( message );

	    	writer.flush();
	    }
	    catch ( IOException e ) {
	    	throw new RuntimeException( "Problem writing message : " + e.getMessage(), e );
	    }
	}

	protected void messageStart(Message message) throws IOException {
	}

	protected void messageEnd(Message message) throws IOException {
		writer.write( '\n' );
	}
	
	private void writeString(String s) throws IOException {
	    String split = s.replace("\\n", "\\n\"\n\"");
	    if (split.contains("\n"))
		writer.write("\"\"\n");
	    writer.write("\""+split+"\"\n");<commit_msg>Fixed writing of comments containing newlines.<commit_after>/*
 * Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, v. 2.1. This program is distributed in the
 * hope that it will be useful, but WITHOUT A WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details. You should have received a
 * copy of the GNU Lesser General Public License, v.2.1 along with this
 * distribution; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Red Hat Author(s): <NAME>
 */
package org.jboss.jgettext.catalog.write;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;

import org.jboss.jgettext.Catalog;
import org.jboss.jgettext.Message;
import org.jboss.jgettext.Occurence;
import org.jboss.jgettext.catalog.util.StringUtil;

/**
 * MessageProcessor implementation
 *
 * @author <NAME>
 */
public class MessageProcessor implements Catalog.MessageProcessor {
	protected final Writer writer;
	protected final Message header; // header handled specially...

	public MessageProcessor(Message header, Writer writer) {
		this.writer = writer;
		this.header = header;
	}

	public MessageProcessor(Writer writer) {
		this( null, writer );
	}

	public MessageProcessor() {
		this( new OutputStreamWriter( System.out ) );
	}

	public void processMessage(Message message) {
		if ( message == header ) {
			return;
		}
		writeMessage(message);
	}
	
	private void writeComment(String prefix, String comment) throws IOException {
	    String[] lines = comment.split("\n");
	    for (String line : lines) {
		writer.write(prefix);
		writer.write(line);
		writer.write('\n');
	    }
	    
	}

	public void writeMessage(Message message) {
	    try {
	    	messageStart( message );

	    	for ( String comment : message.getComments() ) {
	    	    writeComment("#", comment); // no space on purpose!!!
	    	}

	    	for ( String comment : message.getExtractedComments() ) {
	    	    writeComment("#. ", comment);
	    	}

	    	for ( Occurence occurence : message.getOccurences() ) {
	    	    writeComment("#: ", occurence.toString());
	    	}

	    	if ( message.isFuzzy() ) {
	    		writer.write( "#, fuzzy" );
	    		writer.write( '\n' );
	    	}

	    	for ( String format : message.getFormats() ) {
	    		writer.write( "#, " + format );
	    		writer.write( '\n' );
	    	}

	    	if ( message.getPrevMsgctx() != null ) {
	    		writeMsgctxt( "#| ", message.getPrevMsgctx() );
	    	}

	    	if ( message.getPrevMsgid() != null ) {
	    		writeMsgid( "#| ", message.getPrevMsgid() );
	    	}

	    	if ( message.getPrevMsgidPlural() != null ) {
	    		writeMsgidPlural( "#| ", message.getPrevMsgidPlural() );
	    	}

	    	String prefix = message.isObsolete() ? "#~ " : "";
	    	if ( message.getMsgctxt() != null ) {
	    		writeMsgctxt( prefix, message.getMsgctxt() );
	    	}

	    	writeMsgid( prefix, message.getMsgid() );

	    	if ( message.getMsgidPlural() != null ) {
	    		writeMsgidPlural( prefix, message.getMsgidPlural() );
	    	}

	    	writeMsgstr( prefix, message.getMsgstr() );

	    	writeMsgstrPlurals( prefix, message.getMsgstrPlural() );

	    	messageEnd( message );

	    	writer.flush();
	    }
	    catch ( IOException e ) {
	    	throw new RuntimeException( "Problem writing message : " + e.getMessage(), e );
	    }
	}

	protected void messageStart(Message message) throws IOException {
	}

	protected void messageEnd(Message message) throws IOException {
		writer.write( '\n' );
	}
	
	private void writeString(String s) throws IOException {
	    s = StringUtil.addEscapes(s);
	    String split = s.replace("\\n", "\\n\"\n\"");
	    // multiline strings are preceded by a blank line for neatness:
	    if (split.contains("\n"))
		writer.write("\"\"\n");
	    writer.write("\""+split+"\"\n");
{'fraction_non_alphanumeric': 0.12927565392354123, 'fraction_numerical': 0.004778672032193159, 'mean_word_length': 2.990466633216257, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1893478', 'n_tokens_mistral': 2618, 'n_tokens_neox': 2390, 'n_words': 664}