Navigation.java

/*
 * Copyright 2007 Roberto Fasciolo
 * 
 * This file is part of vsitegen.
 * 
 * vsitegen 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 2 of the License, or
 * (at your option) any later version.
 * 
 * vsitegen 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 vsitegen; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package net.robyf.vsitegen.model;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.xml.sax.SAXException;

public final class Navigation {

    private final List<Section> sections = new ArrayList<>();

    public static Navigation build(final InputStream inputFile) {
        Navigation navigation = new Navigation();
        Digester digester = 
            DigesterLoader.createDigester(
                Navigation.class.getResource("/config/navigation-digesterRules.xml"));
        digester.push(navigation);
        try {
            digester.parse(inputFile);
        } catch (IOException e) {
            throw new ParserException("Error reading a navigation file", e);
        } catch (SAXException e) {
            throw new ParserException("Error parsing a navigation file", e);
        }
        return navigation;
    }

    public void addSection(final Section section) {
        this.sections.add(section);
    }

    public List<Section> getSections() {
        return Collections.unmodifiableList(this.sections);
    }
}