Mercurial > dive4elements > river
view flys-artifacts/src/main/java/de/intevation/flys/artifacts/datacage/templating/StackFrames.java @ 1072:80aecb01d79a
Let MainValuesArtifact have a state, outputmode and facet.
flys-artifacts/trunk@2556 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Wed, 24 Aug 2011 12:07:33 +0000 |
parents | 5cb84ceee2a0 |
children | 3ca999f507b7 |
line wrap: on
line source
package de.intevation.flys.artifacts.datacage.templating; import java.util.ArrayList; import java.util.Map; import java.util.List; import java.util.HashMap; import java.util.HashSet; import java.util.Set; import javax.xml.xpath.XPathVariableResolver; import javax.xml.namespace.QName; import org.apache.log4j.Logger; public class StackFrames implements XPathVariableResolver { private static Logger log = Logger.getLogger(StackFrames.class); protected List<Map<String, Object>> frames; public StackFrames() { frames = new ArrayList<Map<String, Object>>(); } public StackFrames(Map<String, Object> initialFrame) { this(); if (initialFrame != null) { frames.add(new HashMap<String, Object>(initialFrame)); } } public void enter() { frames.add(new HashMap<String, Object>()); } public void leave() { frames.remove(frames.size()-1); } public void put(String key, Object value) { int N = frames.size(); if (N > 0) { frames.get(N-1).put(key, value); } } public void put(String [] keys, Object [] values) { Map<String, Object> top = frames.get(frames.size()-1); for (int i = 0; i < keys.length; ++i) { top.put(keys[i], values[i]); } } public boolean containsKey(String key) { for (int i = frames.size()-1; i >= 0; --i) { if (frames.get(i).containsKey(key)) { return true; } } return false; } public Object get(String key) { return get(key, null); } public Object get(String key, Object def) { for (int i = frames.size()-1; i >= 0; --i) { Map<String, Object> frame = frames.get(i); if (frame.containsKey(key)) { return frame.get(key); } } return def; } @Override public Object resolveVariable(QName variableName) { if (log.isDebugEnabled()) { log.debug("resolve var: " + variableName); } return get(variableName.getLocalPart()); } public String dump() { StringBuilder sb = new StringBuilder("["); Set<String> already = new HashSet<String>(); boolean first = true; for (int i = frames.size()-1; i >= 0; --i) { Map<String, Object> frame = frames.get(i); for (Map.Entry<String, Object> entry: frame.entrySet()) { if (already.add(entry.getKey())) { if (first) { first = false; } else { sb.append(", "); } sb.append('\'').append(entry.getKey()) .append("'='").append(entry.getValue()).append('\''); } } } return sb.append(']').toString(); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :