view flys-client/src/main/java/de/intevation/flys/client/server/features/XMLFileFeatures.java @ 3482:c64cad8dc772

Add missing import of ArrayList flys-client/trunk@5177 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Wed, 08 Aug 2012 13:41:58 +0000
parents e59588ea27bd
children 83845aa322ea
line wrap: on
line source
package de.intevation.flys.client.server.features;

import java.io.FileInputStream;
import java.io.IOException;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.LinkedList;

import javax.xml.xpath.XPathConstants;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import de.intevation.artifacts.common.utils.XMLUtils;

public class XMLFileFeatures implements Features {

    private static final Logger logger = Logger.getLogger(XMLFileFeatures.class);

    private Hashtable<String, List<String>> featuremap = new Hashtable<String, List<String>>();

    private final static String XPATH_FEATURES = "ftr:feature/child::text()";
    private final static String XPATH_ROLES    = "/ftr:features/ftr:role";

    public XMLFileFeatures(String filename) throws IOException {
        FileInputStream finput = new FileInputStream(filename);

        try {
            Document doc = XMLUtils.parseDocument(finput);

            NodeList roles = (NodeList) XMLUtils.xpath(
                doc,
                XPATH_ROLES,
                XPathConstants.NODESET,
                FeaturesNamespaceContext.INSTANCE);

            for(int i=0, m = roles.getLength(); i < m; i++) {
                Element rolenode = (Element)roles.item(i);

                String name = rolenode.getAttributeNS(
                        FeaturesNamespaceContext.NAMESPACE_URI, "name");

                logger.debug("Found role: " + name);

                NodeList features = (NodeList) XMLUtils.xpath(
                    rolenode,
                    XPATH_FEATURES,
                    XPathConstants.NODESET,
                    FeaturesNamespaceContext.INSTANCE);

                if (features.getLength() > 0) {
                    List<String> allowed = new ArrayList<String>(features.getLength());
                    for (int j=0, l = features.getLength(); j < l; j++) {
                        Node featurenode = features.item(j);
                        String featurename = featurenode.getNodeValue();

                        logger.debug("Found feature: " + featurename);

                        allowed.add(featurename);
                    }
                    featuremap.put(name, allowed);
                }
            }
            logger.debug("Loaded all features");
        }
        finally {
            finput.close();
        }
    }

    @Override
    public List<String> getFeatures(List<String> roles) {
        List<String> features = new LinkedList<String>();

        for (String role: roles) {
            List<String> allowed = this.featuremap.get(role);
            if (!allowed.isEmpty()) {
                features.addAll(allowed);
            }
        }
        return features;
    }
}

http://dive4elements.wald.intevation.org