<commit_before>/*
 * ARX: Powerful Data Anonymization
 * Copyright (C) 2012 - 2014 <NAME>, <NAME>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.deidentifier.arx.gui.worker.io;

import org.deidentifier.arx.gui.resources.Resources;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * The default XML handler.
 *
 * @author <NAME>
 * @author <NAME>
 */
public abstract class XMLHandler extends DefaultHandler {

    /**  TODO */
    public String payload;

    /* (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
     */
    @Override
    public void characters(final char[] ch,
                           final int start,
                           final int length) throws SAXException {
        // Directly unescape stuff
        payload = new String(ch, start, length);
    }

    /**
     * 
     *
     * @param uri
     * @param localName
     * @param qName
     * @return
     * @throws SAXException
     */
    protected abstract boolean end(String uri,
                                   String localName,
                                   String qName) throws SAXException;

    /* (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void endElement(final String uri,
                           final String localName,
                           final String qName) throws SAXException {
        if (!end(uri, localName, qName)) { throw new SAXException(Resources.getMessage("WorkerLoad.0") + localName); } //$NON-NLS-1$
    }

    /**
     * 
     *
     * @param uri
     * @param localName
     * @param qName
     * @param attributes
     * @return
     * @throws SAXException
     */
    protected abstract boolean
            start(String uri,
                  String localName,
                  String qName,
                  Attributes attributes) throws SAXException;

    /* (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    @Override
    public void
            startElement(final String uri,
                         final String localName,
                         final String qName,
                         final Attributes attributes) throws SAXException {
        if (!start(uri, localName, qName, attributes)) { throw new SAXException(Resources.getMessage("WorkerLoad.1") + localName); } //$NON-NLS-1$
    }
}<commit_msg>Bugfix: SAX parsers may split contiguous character it into several chunks.<commit_after>/*
 * ARX: Powerful Data Anonymization
 * Copyright (C) 2012 - 2014 <NAME>, <NAME>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.deidentifier.arx.gui.worker.io;

import org.deidentifier.arx.gui.resources.Resources;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.carrotsearch.hppc.CharArrayList;

/**
 * The default XML handler.
 *
 * @author <NAME>
 * @author <NAME>
 */
public abstract class XMLHandler extends DefaultHandler {

    /**  The payload */
    public String payload;
    
    /** The arraylist */
    private CharArrayList sb = new CharArrayList();

    /* (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
     */
    @Override
    public void characters(final char[] ch,
                           final int start,
                           final int length) throws SAXException {
        // Add to chararraylist
        sb.add(ch, start, length);
    }

    /**
     * 
     *
     * @param uri
     * @param localName
     * @param qName
     * @return
     * @throws SAXException
     */
    protected abstract boolean end(String uri,
                                   String localName,
                                   String qName) throws SAXException;

    /* (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void endElement(final String uri,
                           final String localName,
                           final String qName) throws SAXException {
        payload =  new String(sb.buffer, 0, sb.size());
        if (!end(uri, localName, qName)) { throw new SAXException(Resources.getMessage("WorkerLoad.0") + localName); } //$NON-NLS-1$
    }

    /**
     * 
     *
     * @param uri
     * @param localName
     * @param qName
     * @param attributes
     * @return
     * @throws SAXException
     */
    protected abstract boolean
            start(String uri,
                  String localName,
                  String qName,
                  Attributes attributes) throws SAXException;

    /* (non-Javadoc)
     * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    @Override
    public void
            startElement(final String uri,
                         final String localName,
                         final String qName,
                         final Attributes attributes) throws SAXException {
        sb.clear();
        if (!start(uri, localName, qName, attributes)) { throw new SAXException(Resources.getMessage("WorkerLoad.1") + localName); } //$NON-NLS-1$
    }
}
{'fraction_non_alphanumeric': 0.0960613349368611, 'fraction_numerical': 0.004058929645219483, 'mean_word_length': 2.1605700712589075, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3137209', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1791, 'n_words': 564}