public void addValue(String key, Object value) { ObjectTree tree = new ObjectTree(); tree.put(key, checkToCopy(value)); add(tree); }
/** * Add a data object with id and tag name for xpath usage. * * @param id * the id of the object * @param rootTagName * the root tag name for xpath usage. * @param c * the data object * @param checkConflict * true to prevent id conflicts, false to overwrite the existing * value with the same id. * */ public void addConst(String id, String rootTagName, Object c, boolean checkConflict) { if (checkConflict && idConstsMap.get(id) != null) { throw new PaxmlRuntimeException("Const with id '" + id + "' already exists!"); } idConstsMap.addValue(id, c); if (null != rootTagName) { idToTagName.put(id, rootTagName); } }
/** * Construct from an existing map. * * @param map * the map * @param checkConflict * true to check id conflicts, false to convert conflicting id * values into a list and put the list as the value with that id. */ public ObjectTree(final Map<?, ?> map) { this(); addValues(map); }
/** * {@inheritDoc} */ public ObjectTree copy() { ObjectTree newTree = emptyCopy(); for (Map.Entry<String, Object> entry : entrySet()) { Object item = entry.getValue(); if (item instanceof IObjectContainer) { item = ((IObjectContainer) item).copy(); } newTree.put(entry.getKey(), item); } return newTree; } protected ObjectTree emptyCopy() {
/** * Get a data object with id. * * @param id * the id * @param searchParent * true to search in parent contexts, false not to. * @return null if not found, otherwise the data object. */ public Object getConst(String id, boolean searchParent) { if (idConstsMap.containsKey(id)) { return idConstsMap.get(id); } if (searchParent && parent != null) { return parent.getConst(id, true); } return null; }
/** * Get a copy of the map keyed with tag names. * * @param mergeParents * true to merge with the map from parent context, false not to. * @param includesRoot * true to include the map from the root context, false not to. * If the current is already root context, set to false will * result in empty map being returned. * @return the map copy. */ public ObjectTree getNameMap(boolean mergeParents, boolean includesRoot) { ObjectTree tree = new ObjectTree(); if (includesRoot || !isRoot()) { for (Map.Entry<String, Object> entry : idConstsMap.entrySet()) { Object value = entry.getValue(); String id = entry.getKey(); String tagName = idToTagName.get(id); if (tagName != null) { tree.addValue(tagName, value); } } } if (mergeParents && parent != null) { tree.addValues(parent.getNameMap(mergeParents, includesRoot)); } return tree; }
@Override protected Object doInvoke(Context context) throws Exception { Object val = getValue(); if (val == null) { return null; } Map map = (Map) XmlUtils.fromXml(val.toString(), true); String rootName = map.keySet().iterator().next().toString(); Object obj = XmlUtils.extractSingleMapRoot(map); if(!ignoreRootTag){ ObjectTree tree=new ObjectTree(null); tree.addValue(rootName, obj); return tree; } if (obj instanceof Map) { ObjectTree tree = new ObjectTree(rootName); tree.addValues((Map) obj); return tree; } else if (obj instanceof Collection) { ObjectList list = new ObjectList(rootName, true); list.addAll((Collection) obj); return list; } else { return obj; } }
ObjectTree tree = new ObjectTree(valueName); tree.addXmlAttributes(attributes); for (Object childResult : childrenResults) { ConstNode node = (ConstNode) childResult; if (node.getValue() != null) { tree.addValue(node.getName(), node.getValue()); myValue = tree.shrink(); } else if (hasSubconsts) { ObjectTree tree = new ObjectTree(valueName); tree.addXmlAttributes(attributes); for (Object childResult : childrenResults) { if (childResult instanceof ConstNode) { ConstNode node = (ConstNode) childResult; if (node.getValue() != null) { tree.addValue(node.getName(), node.getValue()); tree.addValue("value", childResult.toString()); myValue = tree.shrink();
ObjectTree tree = new ObjectTree(); for (Object childResult : childrenResults) { ConstNode node = (ConstNode) childResult; if (node.getValue() != null) { tree.addValue(node.getName(), node.getValue()); myValue = tree.shrink(); } else if (hasSubconsts) { throw new PaxmlRuntimeException("Cannot mix const tag with value tag under the same parent");
private void populateTree(ObjectTree tree, OMElement ele) { if (ele.getFirstElement() == null) { tree.addValue(ele.getLocalName(), ele.getText()); } else { ObjectTree subTree = new ObjectTree(); tree.addValue(ele.getLocalName(), subTree); for (OMElement child : new Elements(ele)) { populateTree(subTree, child); } } }
/** * Set a bunch of consts into the current context. * * @param map * the map containing the consts * @param rootTags * the root tags for the map parameter. * @param checkConflict * true to assert no ids conflicts before setting, false not to * assert. */ public void setConsts(Map<String, Object> map, Map<String, String> rootTags, boolean checkConflict) { if (checkConflict) { Collection<String> overlap = CollectionUtils.intersection(map.keySet(), idConstsMap.keySet()); if (overlap.size() > 0) { throw new PaxmlRuntimeException("The followng id conflicts detected: " + overlap); } } idConstsMap.putAll(map); idToTagName.putAll(rootTags); }
@Override public void addValue(String key, Object value) { Object existing = get(key); if (existing == null) { put(key, value); Object v = get(key); if (v instanceof ObjectList) { ((ObjectList) v)._dynamic = false; } } else if (existing instanceof ObjectList) { ObjectList existingList = ((ObjectList) existing); if (existingList._dynamic) { ObjectList list = (ObjectList) existing; list.add(value); } else { ObjectList list = new ObjectList(true, existing, value); put(key, list); } } else { ObjectList list = new ObjectList(true, existing, value); put(key, list); } }
vars.declareVariable(XPATH_NAME_LOCAL_VAR, nameLocal); ObjectTree nameAuto = new ObjectTree(null, nameGlobal); nameAuto.addValues(nameLocal);
@Override public Object put(String key, Object value) { return super.put(key, checkToCopy(value)); }
protected ObjectTree emptyCopy() { return new ObjectTree(name); } public boolean isXmlAttribute(String propName){
final Set<String> ex = new HashSet<String>(Arrays.asList(excludes)); if (strict) { for (Map.Entry<String, Object> entry : idConstsMap.entrySet()) { if (entry.getValue() == obj && !ex.contains(entry.getKey())) { return entry.getKey(); for (Map.Entry<String, Object> entry : idConstsMap.entrySet()) { Object v = entry.getValue(); final boolean exclude = ex.contains(entry.getKey());
/** * Get the ids of the consts. * * @return a read only id set, never null */ public Set<String> getConstIds() { return Collections.unmodifiableSet(idConstsMap.keySet()); }
public void addValues(Map<?, ?> map) { for (Map.Entry<?, ?> entry : map.entrySet()) { addValue(entry.getKey().toString(), entry.getValue()); } }
/** * Check the existence of an data object. * * @param id * the id * @param searchParent * true to search in parent contexts, false not to. * @return true if exists, false if not */ public boolean hasConstId(String id, boolean searchParent) { if (idConstsMap.containsKey(id)) { return true; } if (searchParent && parent != null) { return parent.hasConstId(id, searchParent); } return false; }
/** * Set a data object in context, overwriting existing const by id. * * @param id * the id of the const to set and overwrite if exists. * @param rootTagName * the root tag name of the new const to set. * @param c * the new const * @param checkConflict * true to check id conflicts if there is conflict, exception * will be thrown, false not to. * @return the existing data, or null if there isn't existing data with the * id. */ public Object setConst(String id, String rootTagName, Object c, boolean checkConflict) { final Object existing = idConstsMap.put(id, c); if (existing != null && checkConflict) { throw new PaxmlRuntimeException("Id conflict: " + id); } if (null != rootTagName) { idToTagName.put(id, rootTagName); } return existing; }