<commit_before>/*
 * This file is part of the DITA Open Toolkit project hosted on
 * Sourceforge.net. See the accompanying license.txt file for 
 * applicable licenses.
 */

/*
 * (c) Copyright IBM Corp. 2005, 2006 All Rights Reserved.
 */
package org.dita.dost.platform;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.StringTokenizer;

import org.dita.dost.log.DITAOTJavaLogger;
import org.dita.dost.util.Constants;
import org.dita.dost.util.StringUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * InsertAction implements IAction and insert the resource 
 * provided by plug-ins into the xsl files, ant scripts and xml catalog.
 * @author Zhang, <NAME>
 */
public class InsertAction extends DefaultHandler implements IAction, LexicalHandler {

	protected XMLReader reader;
	protected DITAOTJavaLogger logger;
	protected Set<String> fileNameSet = null;
	protected StringBuffer retBuf;
	protected Hashtable<String,String> paramTable = null;
	protected int elemLevel = 0;
	
	/**
	 * Default Constructor.
	 */
	public InsertAction() {
		fileNameSet = new LinkedHashSet<String>(Constants.INT_16);
		logger = new DITAOTJavaLogger();
		retBuf = new StringBuffer(Constants.INT_4096);
		paramTable = new Hashtable<String,String>();
		try {
            if (System.getProperty(Constants.SAX_DRIVER_PROPERTY) == null){
                //The default sax driver is set to xerces's sax driver
            	StringUtils.initSaxDriver();
            }
            reader = XMLReaderFactory.createXMLReader();
            reader.setContentHandler(this);
            reader.setFeature(Constants.FEATURE_NAMESPACE_PREFIX, true);
            //added by Alan for bug: #2893316 on Date: 2009-11-09 begin
            reader.setProperty(Constants.LEXICAL_HANDLER_PROPERTY, this);
            //added by Alan for bug: #2893316 on Date: 2009-11-09 end
            
            //Edited by william on 2009-11-8 for ampbug:2893664 start
			reader.setFeature("http://apache.org/xml/features/scanner/notify-char-refs", true);
			reader.setFeature("http://apache.org/xml/features/scanner/notify-builtin-refs", true);
			//Edited by william on 2009-11-8 for ampbug:2893664 end
			
        } catch (Exception e) {
        	logger.logException(e);
        }
	}

	/**
	 * @see org.dita.dost.platform.IAction#setInput(java.lang.String)
	 */
	public void setInput(String input) {
		StringTokenizer inputTokenizer = new StringTokenizer(input,",");
		while(inputTokenizer.hasMoreElements()){
			fileNameSet.add((String) inputTokenizer.nextElement());
		}
	}

	/**
	 * @see org.dita.dost.platform.IAction#setParam(java.lang.String)
	 */
	public void setParam(String param) {
		StringTokenizer paramTokenizer = new StringTokenizer(param,";");
		String paramExpression = null;
		int index;
		while(paramTokenizer.hasMoreElements()){
			paramExpression = (String) paramTokenizer.nextElement();
			index = paramExpression.indexOf("=");
			if(index > 0){
				paramTable.put(paramExpression.substring(0,index),
						paramExpression.substring(index+1));
			}
		}		
	}

	/**
	 * @see org.dita.dost.platform.IAction#getResult()
	 */
	public String getResult() {
		Iterator<String> iter;
		iter = fileNameSet.iterator();
		try{
			while(iter.hasNext()){
				reader.parse(iter.next());
			}
		} catch (Exception e) {
	       	logger.logException(e);
		}
		return retBuf.toString();
	}

	/**
	 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if(elemLevel != 0){
			int attLen = attributes.getLength();
			retBuf.append(Constants.LINE_SEPARATOR);
			retBuf.append("<"+qName);
			for (int i = 0; i < attLen; i++){
				retBuf.append(" ").append(attributes.getQName(i)).append("=\"");
				retBuf.append(attributes.getValue(i)).append("\"");
			}
			//Added by Jason on 2010-05-09 for bug:2974667 start
			if(("public".equals(localName) ||
					"system".equals(localName) ||
					"uri".equals(localName))){
				retBuf.append("/>");
			}
			//Added by Jason on 2010-05-09 for bug:2974667 end
			else{
				retBuf.append(">");
			}
		}
		elemLevel ++;
	}

	/**
	 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
	 */
	public void characters(char[] ch, int start, int length) throws SAXException {
		retBuf.append(ch, start, length);
	}

	/**
	 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void endElement(String uri, String localName, String qName) throws SAXException {
		elemLevel --;
		//edited by william on 2010-03-23 for bug:2974667 start
		if(elemLevel != 0 && 
				(!"public".equals(localName) &&
				 !"system".equals(localName) &&
				 !"uri".equals(localName))
		){
		//edited by william on 2010-03-23 for bug:2974667 start
			retBuf.append(Constants.LINE_SEPARATOR);<commit_msg>Messages extension damages custom message formatting - ID: 3062912<commit_after>/*
 * This file is part of the DITA Open Toolkit project hosted on
 * Sourceforge.net. See the accompanying license.txt file for 
 * applicable licenses.
 */

/*
 * (c) Copyright IBM Corp. 2005, 2006 All Rights Reserved.
 */
package org.dita.dost.platform;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.StringTokenizer;

import org.dita.dost.log.DITAOTJavaLogger;
import org.dita.dost.util.Constants;
import org.dita.dost.util.StringUtils;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

/**
 * InsertAction implements IAction and insert the resource 
 * provided by plug-ins into the xsl files, ant scripts and xml catalog.
 * @author Zhang, <NAME>
 */
public class InsertAction extends DefaultHandler implements IAction, LexicalHandler {

	protected XMLReader reader;
	protected DITAOTJavaLogger logger;
	protected Set<String> fileNameSet = null;
	protected StringBuffer retBuf;
	protected Hashtable<String,String> paramTable = null;
	protected int elemLevel = 0;
	
	/**
	 * Default Constructor.
	 */
	public InsertAction() {
		fileNameSet = new LinkedHashSet<String>(Constants.INT_16);
		logger = new DITAOTJavaLogger();
		retBuf = new StringBuffer(Constants.INT_4096);
		paramTable = new Hashtable<String,String>();
		try {
            if (System.getProperty(Constants.SAX_DRIVER_PROPERTY) == null){
                //The default sax driver is set to xerces's sax driver
            	StringUtils.initSaxDriver();
            }
            reader = XMLReaderFactory.createXMLReader();
            reader.setContentHandler(this);
            reader.setFeature(Constants.FEATURE_NAMESPACE_PREFIX, true);
            //added by Alan for bug: #2893316 on Date: 2009-11-09 begin
            reader.setProperty(Constants.LEXICAL_HANDLER_PROPERTY, this);
            //added by Alan for bug: #2893316 on Date: 2009-11-09 end
            
            //Edited by william on 2009-11-8 for ampbug:2893664 start
			reader.setFeature("http://apache.org/xml/features/scanner/notify-char-refs", true);
			reader.setFeature("http://apache.org/xml/features/scanner/notify-builtin-refs", true);
			//Edited by william on 2009-11-8 for ampbug:2893664 end
			
        } catch (Exception e) {
        	logger.logException(e);
        }
	}

	/**
	 * @see org.dita.dost.platform.IAction#setInput(java.lang.String)
	 */
	public void setInput(String input) {
		StringTokenizer inputTokenizer = new StringTokenizer(input,",");
		while(inputTokenizer.hasMoreElements()){
			fileNameSet.add((String) inputTokenizer.nextElement());
		}
	}

	/**
	 * @see org.dita.dost.platform.IAction#setParam(java.lang.String)
	 */
	public void setParam(String param) {
		StringTokenizer paramTokenizer = new StringTokenizer(param,";");
		String paramExpression = null;
		int index;
		while(paramTokenizer.hasMoreElements()){
			paramExpression = (String) paramTokenizer.nextElement();
			index = paramExpression.indexOf("=");
			if(index > 0){
				paramTable.put(paramExpression.substring(0,index),
						paramExpression.substring(index+1));
			}
		}		
	}

	/**
	 * @see org.dita.dost.platform.IAction#getResult()
	 */
	public String getResult() {
		Iterator<String> iter;
		iter = fileNameSet.iterator();
		try{
			while(iter.hasNext()){
				reader.parse(iter.next());
			}
		} catch (Exception e) {
	       	logger.logException(e);
		}
		return retBuf.toString();
	}

	/**
	 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		if(elemLevel != 0){
			int attLen = attributes.getLength();
			retBuf.append(Constants.LINE_SEPARATOR);
			retBuf.append("<"+qName);
			for (int i = 0; i < attLen; i++){
				retBuf.append(" ").append(attributes.getQName(i)).append("=\"");
				retBuf.append(attributes.getValue(i)).append("\"");
			}
			//Added by Jason on 2010-05-09 for bug:2974667 start
			if(("public".equals(localName) ||
					"system".equals(localName) ||
					"uri".equals(localName))){
				retBuf.append("/>");
			}
			//Added by Jason on 2010-05-09 for bug:2974667 end
			else{
				retBuf.append(">");
			}
		}
		elemLevel ++;
	}

	/**
	 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
	 */
	public void characters(char[] ch, int start, int length) throws SAXException {
		retBuf.append(ch, start, length);
	}

	/**
	 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void endElement(String uri, String localName, String qName) throws SAXException {
		elemLevel --;
		//edited by william on 2010-03-23 for bug:2974667 start
		if(elemLevel != 0 && 
				(!"public".equals(localName) &&
				 !"system".equals(localName) &&
				 !"uri".equals(localName))
		){
		//edited by william on 2010-03-23 for bug:2974667 end
			//remove line break bug:3062912
			//retBuf.append(Constants.LINE_SEPARATOR);
{'fraction_non_alphanumeric': 0.13208812260536398, 'fraction_numerical': 0.027969348659003832, 'mean_word_length': 3.6137870083959345, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 26}, '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': '6878868', 'n_tokens_mistral': 3814, 'n_tokens_neox': 3309, 'n_words': 732}