Java tutorial
/* * Copyright (c) 2008 TouK.pl * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package pl.touk.ode.scopeEvaluator; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import org.apache.commons.collections.keyvalue.MultiKey; import org.apache.ode.bpel.runtime.BpelRuntimeContext.ValueReferencePair; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; /** * @author mproch * */ public class MockExtVarManager { Map<MultiKey, Element> extVars = new HashMap<MultiKey, Element>(); Map<String, List<String>> extVarConfig = new HashMap<String, List<String>>(); Map<String, List<String>> extVarKeyConfig = new HashMap<String, List<String>>(); Map<String, String> extVarNSConfig = new HashMap<String, String>(); DocumentBuilder db; public MockExtVarManager(DocumentBuilder db) { this.db = db; } public void addExternalVariableType(String name, String type, List<String> keys, List<String> nonKeys) { extVarConfig.put(name, nonKeys); extVarNSConfig.put(name, type); extVarKeyConfig.put(name, keys); } public Element addExternalVariable(String type, Map<String, String> values) { Element el = mapToElement(type, values, false); extVars.put(elementToKey(type, el), el); return mapToElement(type, values, true); } public Element readExternalVariable(String name, String extVarName, Node key) { Element r = extVars.get(elementToKey(extVarName, convertToElement(key))); return r; } public Element readExternalVariable(String name, Map<String, String> values) { return extVars.get(elementToKey(name, mapToElement(name, values, true))); } @SuppressWarnings("unchecked") public ValueReferencePair writeExternalVariable(String name, Node key, Node value) { Element soFar = extVars.remove(elementToKey(name, convertToElement(key))); Map<String, String> vals = new HashMap<String, String>(); vals.putAll((Map<String, String>) elementToKey(name, convertToElement(key)).getKey(1)); if (soFar != null) { vals.putAll(elementToMap(soFar)); } vals.putAll(elementToMap(convertToElement(value))); Element newEl = mapToElement(name, vals, false); MultiKey mk = elementToKey(name, newEl); extVars.put(mk, newEl); ValueReferencePair ret = new ValueReferencePair(); ret.value = newEl; ret.reference = mapToElement(name, (Map<String, String>) mk.getKey(1), true); return ret; } private MultiKey elementToKey(String name, Element key) { HashMap<String, String> ret = new HashMap<String, String>(); for (String k : extVarKeyConfig.get(name)) { ret.put(k, key.getElementsByTagNameNS(extVarNSConfig.get(name), k).item(0).getTextContent()); } return new MultiKey(name, ret); } private Map<String, String> elementToMap(Element el) { HashMap<String, String> ret = new HashMap<String, String>(); for (int i = 0; i < el.getChildNodes().getLength(); i++) { Node n = el.getChildNodes().item(i); if (n instanceof Element) { Element el1 = (Element) el; ret.put(el1.getLocalName(), el.getTextContent()); } } return ret; } private Element convertToElement(Node n) { return n instanceof Document ? ((Document) n).getDocumentElement() : (Element) n; } private Element mapToElement(String name, Map<String, String> value, boolean onlyKey) { Document d = db.newDocument(); Element root = (Element) d.appendChild(d.createElement("whatever")); for (String val : extVarKeyConfig.get(name)) { Element el = d.createElementNS(extVarNSConfig.get(name), val); el.setTextContent(value.get(val)); root.appendChild(el); } if (!onlyKey) { for (String val : extVarConfig.get(name)) { Element el = d.createElementNS(extVarNSConfig.get(name), val); el.setTextContent(value.get(val)); root.appendChild(el); } } return root; } }