Android Open Source - openaccessbutton Navigation Xml Parser






From Project

Back to project page openaccessbutton.

License

The source code is released under:

MIT License

If you think the Android project openaccessbutton listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Open Access Button
 *// w  w  w  .  j a v a  2 s. c  om
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

package org.openaccessbutton.openaccessbutton;


import android.content.Context;
import android.util.Log;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.apache.commons.lang3.StringEscapeUtils;
import org.openaccessbutton.openaccessbutton.blog.Post;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;

/**
 * Parse the custom XML containing the navigation
 */
public class NavigationXmlParser {
    private List<NavigationItem> mItems;
    // Namespaces aren't needed at this point
    private static final String ns = null;

    /**
     * Parse an InputStream containing the navigation items
     * @param in InputStream containing navigation items XML
     * @return List of NavigationItems
     * @throws XmlPullParserException
     * @throws IOException
     */
    public List<NavigationItem> parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            mItems = readFeed(parser);
        } finally {
            in.close();
        }

        return mItems;
    }

    /**
     * Parse the entire XML.
     */
    private List<NavigationItem> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
        // Everything's contained in an <navigation> tag
        parser.require(XmlPullParser.START_TAG, ns, "navigation");

        // To store parsed items in
        List<NavigationItem> items = new ArrayList<NavigationItem>();

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }

            String name = parser.getName();
            // Items are <item> tags
            if (name.equals("item")) {
                items.add(readItem(parser));
            } else {
                skip(parser);
            }
        }
        return items;
    }

    /**
     * Parse an individual navigation item from a <item> tag.
     */
    private NavigationItem readItem(XmlPullParser parser) throws XmlPullParserException, IOException {
        parser.require(XmlPullParser.START_TAG, ns, "item");

        // Item properties
        String title = null;
        String className = null;
        String menu = null;
        String filename = null;

        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            // Read post properties
            if (name.equals("title")) {
                title = readTitle(parser);
            } else if (name.equals("class")) {
                className = readClassName(parser);
            } else if (name.equals("menu")) {
                menu = readMenu(parser);
            } else if (name.equals("filename")) {
                filename = readFilename(parser);
            } else {
                skip(parser);
            }
        }
        return new NavigationItem(title, className, menu, filename);
    }

    private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
                case XmlPullParser.END_TAG:
                    depth--;
                    break;
                case XmlPullParser.START_TAG:
                    depth++;
                    break;
            }
        }
    }

    /**
     * Extract text from a <title> tag
     */
    private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "title");
        String title = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "title");
        return title;
    }

    /**
     * Extract text from a <menu> tag
     */
    private String readMenu(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "menu");
        String menu = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "menu");
        return menu;
    }

    /**
     * Extract text from a <class> tag
     */
    private String readClassName(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "class");
        String className = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "class");
        return className;
    }

    /**
     * Extract text from a <filename> tag
     */
    private String readFilename(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "filename");
        String filename = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "filename");
        return filename;
    }

    private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        // Remove adjacent spaces and newlines
        result = result.trim().replaceAll("[\r\n]+", " ").replaceAll(" +", " ");
        return result;
    }

    /**
     * Obtain an array of just the NavigationItem titles
     */
    public String[] getTitles() {
        String[] titles = new String[mItems.size()];
        for(int i=0; i<mItems.size(); i++) {
            titles[i] = mItems.get(i).title;
        }
        return titles;
    }

    /**
     * Obtain an array of the unique NavigationItem menu titles
     */
    public String[] getMenuTitles() {
        String[] titles = new String[mItems.size()];
        for (int i=0; i<mItems.size(); i++) {
            titles[i] = mItems.get(i).menu;
        }
        // Unique
        return new HashSet<String>(Arrays.asList(titles)).toArray(new String[titles.length]);
    }

    /**
     * Given the position of an item in the XML, return the position of it's menu item
     */
    public int getMenuPositionFromPosition(int position) {
        String menuTitle = mItems.get(position).menu;

        if (menuTitle.equals("Get Informed")) {
            return 1;
        } else if (menuTitle.equals("Do Research")) {
            return 2;
        } else if (menuTitle.equals("Take Action")) {
            return 3;
        } else {
            return 1;
        }
    }

    /**
     * Return a specific NavigationItem
     */
    public NavigationItem get(int position) {
        return mItems.get(position);
    }

    /**
     * Return the number of NavigationItems
     */
    public int size() {
        return mItems.size();
    }
}




Java Source Code List

com.viewpagerindicator.CirclePageIndicator.java
com.viewpagerindicator.IconPageIndicator.java
com.viewpagerindicator.IconPagerAdapter.java
com.viewpagerindicator.IcsLinearLayout.java
com.viewpagerindicator.LinePageIndicator.java
com.viewpagerindicator.PageIndicator.java
com.viewpagerindicator.TabPageIndicator.java
com.viewpagerindicator.TitlePageIndicator.java
com.viewpagerindicator.UnderlinePageIndicator.java
org.openaccessbutton.openaccessbutton.ApplicationTest.java
org.openaccessbutton.openaccessbutton.MainActivity.java
org.openaccessbutton.openaccessbutton.NavigationItem.java
org.openaccessbutton.openaccessbutton.NavigationXmlParser.java
org.openaccessbutton.openaccessbutton.OnFragmentNeededListener.java
org.openaccessbutton.openaccessbutton.OnShareIntentInterface.java
org.openaccessbutton.openaccessbutton.about.AboutActivity.java
org.openaccessbutton.openaccessbutton.advocacy.AdvocacyFragment.java
org.openaccessbutton.openaccessbutton.advocacy.FaqView.java
org.openaccessbutton.openaccessbutton.advocacy.Question.java
org.openaccessbutton.openaccessbutton.advocacy.QuestionsActivity.java
org.openaccessbutton.openaccessbutton.advocacy.XmlParser.java
org.openaccessbutton.openaccessbutton.api.API.java
org.openaccessbutton.openaccessbutton.blog.BlogAdapter.java
org.openaccessbutton.openaccessbutton.blog.BlogDetailsFragment.java
org.openaccessbutton.openaccessbutton.blog.BlogFragment.java
org.openaccessbutton.openaccessbutton.blog.DownloadTask.java
org.openaccessbutton.openaccessbutton.blog.Post.java
org.openaccessbutton.openaccessbutton.blog.RssParser.java
org.openaccessbutton.openaccessbutton.browser.BrowserFragment.java
org.openaccessbutton.openaccessbutton.browser.ScrollingWebView.java
org.openaccessbutton.openaccessbutton.button.ButtonSubmitActivity.java
org.openaccessbutton.openaccessbutton.intro.ForgotPasswordActivity.java
org.openaccessbutton.openaccessbutton.intro.IntroActivity.java
org.openaccessbutton.openaccessbutton.intro.LaunchActivity.java
org.openaccessbutton.openaccessbutton.intro.ScreenSlidePageFragment.java
org.openaccessbutton.openaccessbutton.intro.SigninActivity.java
org.openaccessbutton.openaccessbutton.intro.SigninButtonClickListener.java
org.openaccessbutton.openaccessbutton.intro.SignupActivity.java
org.openaccessbutton.openaccessbutton.intro.SignupEmailButtonClickListener.java
org.openaccessbutton.openaccessbutton.map.Item.java
org.openaccessbutton.openaccessbutton.map.MapFragment.java
org.openaccessbutton.openaccessbutton.menu.MenuActivity.java
org.openaccessbutton.openaccessbutton.preferences.AppPreferencesActivity.java
org.openaccessbutton.openaccessbutton.push.PushReceiver.java
org.openaccessbutton.openaccessbutton.push.Push.java