public boolean write(Type field, Object value, NodeMap<OutputNode> node, Map map) throws Exception { boolean done = strategy.write(field, value, node, map); Node entry = node.remove("class"); if(entry != null) { String className = entry.getValue(); Class type = Class.forName(className); String name = forward.get(type); if(name == null) { throw new PersistenceException("Could not find alias for class %s", className); } node.put("type", name); } return done; }
/** * This is used to resolve and load a class for the given element. * Resolution of the class to used is done by inspecting the * XML element provided. If there is a "class" attribute on the * element then its value is used to resolve the class to use. * If no such attribute exists the specified field is returned, * or if the field type is an array then the component type. * * @param type this is the type of the XML element expected * @param node this is the element used to resolve an override * * @return returns the class that should be used for the object * * @throws Exception thrown if the class cannot be resolved */ private Class readValue(Type type, NodeMap node) throws Exception { Node entry = node.remove(label); Class expect = type.getType(); if(entry != null) { String name = entry.getValue(); Class actual = loader.load(name); // Arrays are annotated with the type of the element. if (expect.isArray()) { if (actual == expect.getComponentType()) actual = expect; else actual = Array.newInstance(actual, 0).getClass(); } expect = actual; } return expect; }
/** * This is used to recover the object references from the document * using the special attributes specified. This allows the element * specified by the <code>NodeMap</code> to be used to discover * exactly which node in the object graph the element represents. * * @param type the type of the field or method in the instance * @param node this is the XML element to be deserialized * * @return this is used to return the type to acquire the value */ public Value read(Type type, NodeMap node) throws Exception { Node entry = node.remove(label); Class expect = type.getType(); if(entry != null) { String name = entry.getValue(); Class actual = loader.load(name); // Arrays are annotated with the type of the element. if (expect.isArray()) { if (actual == expect.getComponentType()) actual = expect; else actual = Array.newInstance(actual, 0).getClass(); } expect = actual; } return readInstance(type, expect, node); }
/** * This is used to recover the object references from the document * using the special attributes specified. This allows the element * specified by the <code>NodeMap</code> to be used to discover * exactly which node in the object graph the element represents. * * @param type the type of the field or method in the instance * @param node this is the XML element to be deserialized * * @return this is used to return the type to acquire the value */ public Value read(Type type, NodeMap node) throws Exception { Node entry = node.remove(label); Class expect = type.getType(); if(expect.isArray()) { expect = expect.getComponentType(); } if(entry != null) { String name = entry.getValue(); expect = loader.load(name); } return readInstance(type, expect, node); }
/** * This is used to recover the object references from the document * using the special attributes specified. This allows the element * specified by the <code>NodeMap</code> to be used to discover * exactly which node in the object graph the element represents. * * @param type the type of the field or method in the instance * @param real this is the overridden type from the XML element * @param node this is the XML element to be deserialized * * @return this is used to return the type to acquire the value */ private Value readInstance(Type type, Class real, NodeMap node) throws Exception { Node entry = node.remove(mark); if(entry == null) { return readReference(type, real, node); } String key = entry.getValue(); if(containsKey(key)) { throw new CycleException("Element '%s' already exists", key); } return readValue(type, real, node, key); }
/** * This is used to recover the object references from the document * using the special attributes specified. This allows the element * specified by the <code>NodeMap</code> to be used to discover * exactly which node in the object graph the element represents. * * @param type the type of the field or method in the instance * @param real this is the overridden type from the XML element * @param node this is the XML element to be deserialized * * @return this is used to return the type to acquire the value */ private Value readReference(Type type, Class real, NodeMap node) throws Exception { Node entry = node.remove(refer); if(entry == null) { return readValue(type, real, node); } String key = entry.getValue(); Object value = get(key); if(!containsKey(key)) { throw new CycleException("Invalid reference '%s' found", key); } return new Reference(value, real); }
public Value read(Type field, NodeMap<InputNode> node, Map map) throws Exception { Node entry = node.remove("type"); if(entry != null) { String value = entry.getValue(); Class type = backward.get(value); if(type == null) { throw new PersistenceException("Could not find class for alias %s", value); } node.put("class", type.getName()); } return strategy.read(field, node, map); }
public Value read(Type field, NodeMap node, Map map) throws Exception { Node value = node.remove(ELEMENT_NAME); if(value == null) { return null; } String name = value.getValue(); Class type = Class.forName(name); return new SimpleType(type); }
/** * This is used to resolve and load a class for the given element. * Resolution of the class to used is done by inspecting the * XML element provided. If there is a "class" attribute on the * element then its value is used to resolve the class to use. * This also expects a "length" attribute for the array length. * * @param type this is the type of the XML element expected * @param node this is the element used to resolve an override * * @return returns the class that should be used for the object * * @throws Exception thrown if the class cannot be resolved */ private Value readArray(Class type, NodeMap node) throws Exception { Node entry = node.remove(length); int size = 0; if(entry != null) { String value = entry.getValue(); size = Integer.parseInt(value); } return new ArrayValue(type, size); }
/** * This is used to resolve and load a class for the given element. * Resolution of the class to used is done by inspecting the * XML element provided. If there is a "class" attribute on the * element then its value is used to resolve the class to use. * If no such attribute exists the specified field is returned, * or if the field type is an array then the component type. * * @param type this is the type of the XML element expected * @param node this is the element used to resolve an override * * @return returns the class that should be used for the object * * @throws Exception thrown if the class cannot be resolved */ private Class readValue(Type type, NodeMap node) throws Exception { Node entry = node.remove(label); Class expect = type.getType(); if(expect.isArray()) { expect = expect.getComponentType(); } if(entry != null) { String name = entry.getValue(); expect = loader.load(name); } return expect; }
/** * This is used to acquire the <code>Value</code> which can be used * to represent the deserialized value. The type create cab be * added to the graph of created instances if the XML element has * an identification attribute, this allows cycles to be completed. * * @param type the type of the field or method in the instance * @param real this is the overridden type from the XML element * @param node this is the XML element to be deserialized * * @return this is used to return the type to acquire the value */ private Value readArray(Type type, Class real, NodeMap node) throws Exception { Node entry = node.remove(length); int size = 0; if(entry != null) { String value = entry.getValue(); size = Integer.parseInt(value); } return new ArrayValue(real, size); }