referencebyxpathunmarshaller.java
来自「xstream是一个把java object序列化成xml文件的开源库,轻便好用」· Java 代码 · 共 47 行
JAVA
47 行
package com.thoughtworks.xstream.core;import com.thoughtworks.xstream.alias.ClassMapper;import com.thoughtworks.xstream.converters.ConverterLookup;import com.thoughtworks.xstream.core.util.FastStack;import com.thoughtworks.xstream.io.HierarchicalStreamReader;import com.thoughtworks.xstream.io.path.Path;import com.thoughtworks.xstream.io.path.PathTracker;import com.thoughtworks.xstream.io.path.PathTrackingReader;import java.util.HashMap;import java.util.Map;public class ReferenceByXPathUnmarshaller extends TreeUnmarshaller { private Map values = new HashMap(); private FastStack parentPathStack = new FastStack(16); private PathTracker pathTracker = new PathTracker(); public ReferenceByXPathUnmarshaller(Object root, HierarchicalStreamReader reader, ConverterLookup converterLookup, ClassMapper classMapper) { super(root, reader, converterLookup, classMapper); this.reader = new PathTrackingReader(reader, pathTracker); } public Object convertAnother(Object parent, Class type) { if (parentPathStack.size() > 0) { // handles circular references Object parentPath = parentPathStack.peek(); if (!values.containsKey(parentPath)) { // see AbstractCircularReferenceTest.testWeirdCircularReference() values.put(parentPath, parent); } } String relativePathOfReference = reader.getAttribute("reference"); Path currentPath = pathTracker.getPath(); if (relativePathOfReference != null) { return values.get(currentPath.apply(new Path(relativePathOfReference))); } else { parentPathStack.push(currentPath); Object result = super.convertAnother(parent, type); values.put(currentPath, result); parentPathStack.popSilently(); return result; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?