com.wit.and.dialog.xml.XmlDialogParser.java Source code

Java tutorial

Introduction

Here is the source code for com.wit.and.dialog.xml.XmlDialogParser.java

Source

/*
 * =================================================================================
 * Copyright (C) 2013 Martin Albedinsky [Wolf-ITechnologies]
 * =================================================================================
 * Licensed under the Apache License, Version 2.0 or later (further "License" only);
 * ---------------------------------------------------------------------------------
 * You may use this file only in compliance with the License. More details and copy
 * of this License you may obtain at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * You can redistribute, modify or publish any part of the code written in this
 * file but as it is described in the License, the software distributed under the 
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES or CONDITIONS OF
 * ANY KIND.
 * 
 * See the License for the specific language governing permissions and limitations
 * under the License.
 * =================================================================================
 */
package com.wit.and.dialog.xml;

import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.support.v4.app.DialogFragment;

import com.wit.and.dialog.R;
import com.wit.and.dialog.manage.DialogFactory;
import com.wit.and.dialog.manage.DialogOptions;

/**
 * <h4>Class Overview</h4>
 * <p>
 * </p>
 *
 * @param <O>
 * @author Martin Albedinsky
 */
public abstract class XmlDialogParser<O extends DialogOptions> {

    /**
     * Constants =============================
     */

    /**
     * Log TAG.
     */
    private static final String TAG = XmlDialogParser.class.getSimpleName();

    /**
     * Indicates if debug private output trough log-cat is enabled.
     */
    // private static final boolean DEBUG = true;

    /**
     * Indicates if logging for user output trough log-cat is enabled.
     */
    // private static final boolean USER_LOG = true;

    /**
     * Enums =================================
     */

    /**
     * Static members ========================
     */

    /**
     * Members ===============================
     */

    /**
     *
     */
    private Resources mResources;

    /**
     * Listeners -----------------------------
     */

    /**
     * Arrays --------------------------------
     */

    /**
     * Booleans ------------------------------
     */

    /**
     * Constructors ==========================
     */

    /**
     *
     */
    public XmlDialogParser() {
    }

    /**
     * Methods ===============================
     */

    /**
     * Public --------------------------------
     */

    /**
     * @param resources
     */
    public final void dispatchSetUp(Resources resources) {
        this.mResources = resources;
        onSetUp(resources);
    }

    /**
     * <p>
     * </p>
     */
    public void prepare() {
    }

    /**
     * <p>
     * </p>
     *
     * @param parser
     * @return
     */
    public DialogFragment parseXmlDialog(XmlResourceParser parser) {
        O options = onCreateEmptyOptions();

        if (options != null) {
            // Set up default icon.
            options.icon(getDefaultIcon());

            final int attrCount = parser.getAttributeCount();
            // Parse global attributes.
            for (int i = 0; i < attrCount; i++) {
                onParseAttribute(parser, parser.getAttributeNameResource(i), i, options);
            }
        }

        // Create dialog.
        return onCreateDialog(options);
    }

    /**
     * Getters + Setters ---------------------
     */

    /**
     * @return
     */
    public Resources getResources() {
        return mResources;
    }

    /**
     * <p>
     * Returns resource of default icon for XML dialog represented
     * by this parser.
     * </p>
     *
     * @return Icon resource.
     */
    public int getDefaultIcon() {
        return 0;
    }

    /**
     * Protected -----------------------------
     */

    /**
     * <p>
     * Invoked after successful parsing of XML file to create final
     * instance of dialog.
     * </p>
     *
     * @param options Dialog fast options filled during parsing.
     * @return New instance of {@link DialogFragment} or its derived classes.
     */
    protected DialogFragment onCreateDialog(O options) {
        return DialogFactory.dialog(options);
    }

    /**
     * @param resources
     */
    protected void onSetUp(Resources resources) {
    }

    /**
     * <p>
     * Invoked to parse value of specific attribute from currently being parsed XML.
     * </p>
     *
     * @param xmlParser Xml resource parser at position of current attribute which value should be obtained.
     * @param attr      Id of attribute which value is placed at the <var>index</var> position in the <var>xmlParser</var>.
     * @param index     Index of value which should be obtained form the <var>xmlParser</var>.
     * @param options   Dialog options to set up obtained value from parser.
     */
    protected void onParseAttribute(XmlResourceParser xmlParser, int attr, int index, O options) {
        if (attr == android.R.attr.title) {
            options.title(resolveString(xmlParser, index));
        } else if (attr == android.R.attr.icon) {
            options.icon(xmlParser.getAttributeResourceValue(index, 0));
        } else if (attr == android.R.attr.text) {
            options.message(resolveString(xmlParser, index));
        } else if (attr == R.attr.dialogButtonNegative) {
            options.negative(resolveString(xmlParser, index));
        } else if (attr == R.attr.dialogButtonNeutral) {
            options.neutral(resolveString(xmlParser, index));
        } else if (attr == R.attr.dialogButtonPositive) {
            options.positive(resolveString(xmlParser, index));
        } else if (attr == R.attr.dialogTheme) {
            options.dialogTheme(xmlParser.getAttributeResourceValue(index, 0));
        } else if (attr == R.attr.dialogCancelable) {
            options.cancelable(xmlParser.getAttributeBooleanValue(index, true));
        } else if (attr == R.attr.dialogDismissOnRestore) {
            options.dismissOnRestore(xmlParser.getAttributeBooleanValue(index, false));
        }
    }

    /**
     * Resolves situation, if at the current parsed position of Xml parser is string resource or
     * raw string value.
     *
     * @param parser       Parser at position of resolving string.
     * @param currentIndex Index of resolving string in the parser.
     * @return Resolved string.
     */
    protected final String resolveString(XmlResourceParser parser, int currentIndex) {
        String string;
        final int stringRes = parser.getAttributeResourceValue(currentIndex, -1);
        switch (stringRes) {
        case -1:
            string = parser.getAttributeValue(currentIndex);
            break;
        case 0:
            string = null;
            break;
        default:
            string = getResources().getString(stringRes);
            break;
        }
        return string;
    }

    /**
     * Private -------------------------------
     */

    /**
     * Abstract methods ----------------------
     */

    /**
     * @return
     */
    public abstract String getXmlTag();

    /**
     * <p>
     * </p>
     *
     * @return
     */
    protected abstract O onCreateEmptyOptions();

    /**
     * Inner classes =========================
     */

    /**
     * Interface =============================
     */
}