com.tinspx.util.io.charset.AbstractHeaderCharDet.java Source code

Java tutorial

Introduction

Here is the source code for com.tinspx.util.io.charset.AbstractHeaderCharDet.java

Source

/* Copyright (C) 2013-2014 Ian Teune <ian.teune@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.tinspx.util.io.charset;

import com.google.common.collect.Multimap;
import com.tinspx.util.base.BasicError;
import java.util.Map;
import lombok.ToString;

/**
 * Abstract {@link HeaderCharDet}. Subclasses need only implement
 * {@link #onHeader(String, String)}, which will be called for every header
 * value. When the {@code Charset} is successfully detected, call
 * {@link #onCharset(String)}; header processing will stop as soon as a
 * {@code Charset} is detected. Report any errors encountered through
 * {@link #onError(BasicError)}.
 * 
 * @see DefaultHeaderCharDet
 * @author Ian
 */
@ToString(callSuper = true)
public abstract class AbstractHeaderCharDet extends AbstractCharDet implements HeaderCharDet {

    protected AbstractHeaderCharDet() {
    }

    @Override
    public void onHeaders(String name, Iterable<String> values) {
        if (complete) {
            return;
        }
        for (String value : values) {
            onHeader(name, value);
            if (complete) {
                break;
            }
        }
    }

    @Override
    public void onHeaders(Map<String, ? extends Iterable<String>> headers) {
        if (complete) {
            return;
        }
        for (Map.Entry<String, ? extends Iterable<String>> e : headers.entrySet()) {
            onHeaders(e.getKey(), e.getValue());
            if (complete) {
                break;
            }
        }
    }

    @Override
    public void onHeaders(Multimap<String, String> headers) {
        if (complete) {
            return;
        }
        for (Map.Entry<String, String> e : headers.entries()) {
            onHeader(e.getKey(), e.getValue());
            if (complete) {
                break;
            }
        }
    }

    @Override
    public void onHeadersComplete() {
        failIfNotDetected();
    }
}