<commit_before>/*******************************************************************************
 * Copyright (c) 2014 Salesforce.com, inc..
 * 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:
 *     Salesforce.com, inc. - initial API and implementation
 ******************************************************************************/
package com.salesforce.ide.api.metadata;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sforce.ws.MessageHandler;

/**
 * How hard? This hard.
 *
 * @author beidson
 */
public class MetadataDebuggingInfoHandler extends DefaultHandler implements MessageHandler {

    private static ThreadLocal<String> LOCAL = new ThreadLocal<String>();
    private final ThreadLocal<Boolean> isDebugLog = new ThreadLocal<Boolean>();
    private final ThreadLocal<Boolean> isDebugLogFound = new ThreadLocal<Boolean>();
    private final ThreadLocal<String> log = new ThreadLocal<String>();

    public MetadataDebuggingInfoHandler() {}

    /**
     * Gets the debug log from the last call made on this thread.
     */
    public static String getDebugLog() {
        return LOCAL.get();
    }

    public void handleRequest(URL endpoint, byte[] request) {
        // Don't need to do anything on the request
    }

    public void handleResponse(URL endpoint, byte[] response) {
        try {
            SAXParserFactory.newInstance().newSAXParser().parse(new ByteArrayInputStream(response), this);
        }
        //Silently fail on a parser exception. We only care about well formatted headers with debugLog in it.
        catch (SAXException se) {} catch (ParserConfigurationException pce) {} catch (IOException ie) {}
    }

    @Override
    public void startDocument() throws SAXException {
        //Ensure variables are initialized
        isDebugLog.set(false);
        isDebugLogFound.set(false);
        log.set("");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("DebugLog")) {
            isDebugLog.set(true);
            isDebugLogFound.set(true);
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        if (isDebugLog.get()) {
            log.set(log.get() + new String(ch, start, length));
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("DebugLog")) {
            isDebugLog.set(false);
        }
    }

    @Override
    public void endDocument() throws SAXException {
        if (isDebugLogFound.get()) {
            LOCAL.set(log.get());
        } else {
            LOCAL.set(null);
        }
    }
}<commit_msg>Sped up unit test postprocessing<commit_after>/*******************************************************************************
 * Copyright (c) 2014 Salesforce.com, inc..
 * 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:
 *     Salesforce.com, inc. - initial API and implementation
 ******************************************************************************/
package com.salesforce.ide.api.metadata;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sforce.ws.MessageHandler;

/**
 * How hard? This hard.
 *
 * @author beidson
 */
public class MetadataDebuggingInfoHandler extends DefaultHandler implements MessageHandler {

    private static ThreadLocal<String> LOCAL = new ThreadLocal<String>();
    private final ThreadLocal<Boolean> isDebugLog = new ThreadLocal<Boolean>();
    private final ThreadLocal<Boolean> isDebugLogFound = new ThreadLocal<Boolean>();
    private final ThreadLocal<StringBuilder> log = new ThreadLocal<StringBuilder>();
    private final ThreadLocal<CharacterLogger> characterLogger = new ThreadLocal<CharacterLogger>();
    
    public MetadataDebuggingInfoHandler() {
    	log.set(new StringBuilder());
    }

    /**
     * Gets the debug log from the last call made on this thread.
     */
    public static String getDebugLog() {
        return LOCAL.get();
    }

    public void handleRequest(URL endpoint, byte[] request) {
        // Don't need to do anything on the request
    }

    public void handleResponse(URL endpoint, byte[] response) {
        try {
            SAXParserFactory.newInstance().newSAXParser().parse(new ByteArrayInputStream(response), this);
        }
        //Silently fail on a parser exception. We only care about well formatted headers with debugLog in it.
        catch (SAXException se) {} catch (ParserConfigurationException pce) {} catch (IOException ie) {}
    }

    @Override
    public void startDocument() throws SAXException {
        //Ensure variables are initialized
        isDebugLog.set(false);
        isDebugLogFound.set(false);        
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (qName.equalsIgnoreCase("DebugLog")) {
            isDebugLog.set(true);
            isDebugLogFound.set(true);
            characterLogger.set(new CharacterLogger() { @Override public void appendCharacters(ThreadLocal<StringBuilder> log, char[] ch, int start, int length) {
            	log.get().append(ch, start, length);
            }});
        }
        else
        {
        	 characterLogger.set(new CharacterLogger() { @Override public void appendCharacters(ThreadLocal<StringBuilder> log, char[] ch, int start, int length) {}});
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {	
		characterLogger.get().appendCharacters(log, ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equalsIgnoreCase("DebugLog")) {
            isDebugLog.set(false);
        }
    }

    @Override
    public void endDocument() throws SAXException {
        if (isDebugLogFound.get()) {
            LOCAL.set(log.get().toString());
        } else {
            LOCAL.set(null);
        }
    }
    
    private interface CharacterLogger 
    {
    	void appendCharacters(ThreadLocal<StringBuilder> log, char[] ch, int start, int length);
    }
}
{'fraction_non_alphanumeric': 0.13800306364016154, 'fraction_numerical': 0.0022281019356635567, 'mean_word_length': 3.267379679144385, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, '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': '5400744', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1843, 'n_words': 536}