@Test @SuppressWarnings("unchecked") public void getParameters_withTwoAttributes_expectMapWithTwoProperties() throws Exception { NodeMap<InputNode> emptyNodeMap = Mockito.mock(NodeMap.class); when(inputNode.getAttributes()).thenReturn(emptyNodeMap); when(emptyNodeMap.iterator()) .thenReturn(Arrays.asList("attribute1_key", "attribute2_key").iterator()); InputNode inputValue1 = Mockito.mock(InputNode.class); when(inputValue1.getValue()).thenReturn("attribute1_value"); InputNode inputValue2 = Mockito.mock(InputNode.class); when(inputValue2.getValue()).thenReturn("attribute2_value"); when(inputNode.getAttribute("attribute1_key")).thenReturn(inputValue1); when(inputNode.getAttribute("attribute2_key")).thenReturn(inputValue2); Map<String, String> parameters = BasicPhaseConverter.getParameters(inputNode); assertThat(parameters.size(), is(2)); assertThat(parameters.get("attribute1_key"), is("attribute1_value")); assertThat(parameters.get("attribute2_key"), is("attribute2_value")); }
/** * This is used to read the <code>Value</code> which will be used * to represent the deserialized object. If there is an binding * present then the value will contain an object instance. If it * does not then it is up to the internal strategy to determine * what the returned value contains. * * @param type this is the type that represents a method or field * @param node this is the node representing the XML element * @param value this is the value from the internal strategy * * @return the value representing the deserialized value */ private Value read(Type type, NodeMap<InputNode> node, Value value) throws Exception { Converter converter = lookup(type, value); InputNode source = node.getNode(); if(converter != null) { Object data = converter.read(source); Class actual = type.getType(); if(value != null) { value.setValue(data); } return new Reference(value, data, actual); } return value; }
/** * This is used to read the <code>Value</code> which will be used * to represent the deserialized object. If there is an annotation * present then the value will contain an object instance. If it * does not then it is up to the internal strategy to determine * what the returned value contains. * * @param type this is the type that represents a method or field * @param node this is the node representing the XML element * @param value this is the value from the internal strategy * * @return the value representing the deserialized value */ private Value read(Type type, NodeMap<InputNode> node, Value value) throws Exception { Converter converter = scanner.getConverter(type, value); InputNode parent = node.getNode(); if(converter != null) { Object data = converter.read(parent); Class actual = type.getType(); if(value != null) { value.setValue(data); } return new Reference(value, data, actual); } return value; }
/** * This is used to write the XML element attributes representing * the serialized object instance. If the object has already been * serialized to the XML document then a reference attribute is * inserted and this returns true, if not, then this will write * a unique identity marker attribute and return false. * * @param value this is the instance that is to be serialized * @param node this is the node that contains the attributes * * @return returns true if the element has been fully written */ private boolean writeReference(Object value, NodeMap node) { String name = get(value); int size = size(); if(name != null) { node.put(refer, name); return true; } String unique = String.valueOf(size); node.put(mark, unique); put(value, unique); return false; }
/** * This method is used to read the version from the provided input * node. Once the version has been read it is used to determine how * to deserialize the object. If the version is not the initial * version then it is read in a manner that ignores excessive XML * elements and attributes. Also none of the annotated fields or * methods are required if the version is not the initial version. * * @param node the XML element contact values are deserialized from * @param source this object whose contacts are to be deserialized * @param schema this object visits the objects contacts */ private void readVersion(InputNode node, Object source, Schema schema) throws Exception { Label label = schema.getVersion(); Class expect = type.getType(); if(label != null) { String name = label.getName(); NodeMap<InputNode> map = node.getAttributes(); InputNode value = map.remove(name); if(value != null) { readVersion(value, source, label); } else { Version version = context.getVersion(expect); Double start = revision.getDefault(); Double expected = version.revision(); criteria.set(label, start); revision.compare(expected, start); } } }
public Value read(Type type, NodeMap<InputNode> node, Map map) throws Exception { Value value = strategy.read(type, node, map); Convert convert = type.getAnnotation(Convert.class); InputNode parent = node.getNode(); if(convert != null) { Class<? extends Converter> converterClass = convert.value(); Constructor<? extends Converter> converterConstructor = converterClass.getDeclaredConstructor(); if(!converterConstructor.isAccessible()) { converterConstructor.setAccessible(true); } Converter converter = converterConstructor.newInstance(); Object result = converter.read(parent); return new Wrapper(result); } return value; }
public boolean write(Type type, Object value, NodeMap<OutputNode> node, Map map) throws Exception { Convert convert = type.getAnnotation(Convert.class); OutputNode parent = node.getNode(); if(convert != null) { Class<? extends Converter> converterClass = convert.value(); Constructor<? extends Converter> converterConstructor = converterClass.getDeclaredConstructor(); if(!converterConstructor.isAccessible()) { converterConstructor.setAccessible(true); } Converter converter = converterConstructor.newInstance(); converter.write(parent, value); return true; } return strategy.write(type, value, node, map); }
public void testReference() throws Exception { StringReader reader = new StringReader(REFERENCE); Contract contract = new Contract("id", "reference", "class", "length"); ReadGraph graph = new ReadGraph(contract, new Loader()); InputNode event = NodeBuilder.read(reader); NodeMap attributes = event.getAttributes(); graph.put("1", "first text"); graph.put("2", "second text"); graph.put("3", "third text"); Value value = graph.read(new Entry(String.class), attributes); assertEquals(0, value.getLength()); assertEquals("first text", value.getValue()); assertEquals(String.class, value.getType()); assertEquals(true, value.isReference()); }
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); }
@Override public void write(final Type type, final NodeMap<OutputNode> node) throws Exception { if (type.getType().equals(Command[].class)) { OutputNode element = node.getNode(); StringBuilder builder = new StringBuilder("A configuration must have 9 commands, one for each button.\n Possible icons are:"); for (Command.Icon icon : Command.Icon.values()) builder.append("\n - ").append(icon.toString()); element.setComment(builder.toString()); } }
@Override public void write(Type type, NodeMap<OutputNode> node) throws Exception { OutputNode outputNode = node.getNode(); if ("DdsRequestObject".equals(outputNode.getName())) { OutputNode operationNameAtribute = outputNode.getParent().getAttributes().get("DdsOperationName"); outputNode.setName(operationNameAtribute.getValue()); } }
@Test @SuppressWarnings("unchecked") public void getParameters_withNoAttributes_expectEmptyMap() throws Exception { NodeMap<InputNode> emptyNodeMap = Mockito.mock(NodeMap.class); when(emptyNodeMap.iterator()).thenReturn(Iterators.<String>emptyIterator()); when(inputNode.getAttributes()).thenReturn(emptyNodeMap); Map<String, String> parameters = BasicPhaseConverter.getParameters(inputNode); assertTrue(parameters.isEmpty()); }
/** * This is used to write the XML element attributes representing * the serialized object instance. If the object has already been * serialized to the XML document then a reference attribute is * inserted and this returns true, if not, then this will write * a unique identity marker attribute and return false. * * @param type this is the type of the object to be serialized * @param value this is the instance that is to be serialized * @param node this is the node that contains the attributes * * @return returns true if the element has been fully written */ public boolean write(Type type, Object value, NodeMap node){ Class actual = value.getClass(); Class expect = type.getType(); Class real = actual; if(actual.isArray()) { real = writeArray(actual, value, node); } if(actual != expect) { node.put(label, real.getName()); } return writeReference(value, 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 void testSmallSource() throws Exception { InputNode event = NodeBuilder.read(new StringReader(SMALL_SOURCE)); assertTrue(event.isRoot()); assertEquals("override", event.getName()); assertEquals("12", event.getAttribute("id").getValue()); assertEquals("true", event.getAttribute("flag").getValue()); NodeMap list = event.getAttributes(); assertEquals("12", list.get("id").getValue()); assertEquals("true", list.get("flag").getValue()); InputNode text = event.getNext(); assertFalse(text.isRoot()); assertTrue(event.isRoot()); assertEquals("text", text.getName()); assertEquals("entry text", text.getValue()); assertEquals(null, text.getNext()); InputNode name = event.getNext(); assertFalse(name.isRoot()); assertEquals("name", name.getName()); assertEquals("some name", name.getValue()); assertEquals(null, name.getNext()); assertEquals(null, text.getNext()); InputNode third = event.getNext(); assertTrue(event.isRoot()); assertFalse(third.isRoot()); assertEquals("third", third.getName()); assertEquals("text", text.getName()); assertEquals(null, text.getNext()); assertEquals("added to schema", third.getValue()); assertEquals(null, event.getNext()); }
public void read(Type type, NodeMap<InputNode> node){ InputNode element = node.getNode(); if(element.isRoot()) { Object source = element.getSource(); Class sourceType = source.getClass(); Class itemType = type.getType(); System.out.printf(">>>>> ELEMENT=[%s]%n>>>>> TYPE=[%s]%n>>>>> SOURCE=[%s]%n", element, itemType, sourceType); } }
public Car read(InputNode node) throws Exception { Car car = persister.read(Car.class, node, false); NodeMap<InputNode> attributes = node.getAttributes(); for(String name : attributes) { InputNode attribute = attributes.get(name); String value = attribute.getValue(); car.furtherAttributes.put(name, value); } return car; }
public void write(Type type, NodeMap<OutputNode> node) throws Exception { if(!node.getNode().isRoot()) { Comment comment = type.getAnnotation(Comment.class); if(comment != null) { node.getNode().setComment(comment.value()); } } }
public Value read(Type type, NodeMap<InputNode> node, Map map) throws Exception { Component component = type.getAnnotation(Component.class); if(component != null) { String name = component.name(); InputNode value = node.get(KEY); if(!value.getValue().equals(name)) { throw new IllegalStateException("Component name incorrect, expected '"+name+"' but was '"+value.getValue()+"'"); } } return strategy.read(type, node, map); }
public boolean write(Type type, Object value, NodeMap<OutputNode> node, Map map) throws Exception { Component component = type.getAnnotation(Component.class); if(component != null) { String name = component.name(); if(name != null) { node.put(KEY, name); } } return strategy.write(type, value, node, map); }
public void read(Type field, NodeMap<InputNode> node) throws Exception { String namespace = node.getNode().getReference(); if(namespace != null && namespace.length() > 0) { String type = new PackageParser().revert(namespace).getName(); if(type == null) { throw new PersistenceException("Could not match name %s", namespace); } node.put("class", type); } }
public void write(Type field, NodeMap<OutputNode> node) throws Exception { OutputNode value = node.remove("class"); if(value != null) { String type = value.getValue(); String name = new PackageParser().parse(type); if(name == null) { throw new PersistenceException("Could not match class %s", type); } if(comment) { node.getNode().setComment(type); } node.getNode().getNamespaces().setReference(name, "class"); node.getNode().setReference(name); } }
public void testArray() throws Exception { Map map = new HashMap(); StringReader reader = new StringReader(ARRAY); CycleStrategy strategy = new CycleStrategy(); InputNode event = NodeBuilder.read(reader); NodeMap attributes = event.getAttributes(); Value value = strategy.read(new Entry(String[].class), attributes, map); assertEquals(12, value.getLength()); assertEquals(null, value.getValue()); assertEquals(String.class, value.getType()); assertEquals(false, value.isReference()); }
public void testObject() throws Exception { Map map = new HashMap(); StringReader reader = new StringReader(OBJECT); CycleStrategy strategy = new CycleStrategy(); InputNode event = NodeBuilder.read(reader); NodeMap attributes = event.getAttributes(); Value value = strategy.read(new Entry(String.class), attributes, map); assertEquals(0, value.getLength()); assertEquals(null, value.getValue()); assertEquals(String.class, value.getType()); assertEquals(false, value.isReference()); }
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); }
public void read(Class field, NodeMap<InputNode> node) throws Exception{ InputNode value = node.remove(replace); if(value != null) { String name = value.getValue(); String type = read.get(name); if(type == null) { throw new PersistenceException("Could not match name %s", name); } node.put(label, type); } }
public void write(Class field, NodeMap<OutputNode> node) throws Exception { OutputNode value = node.remove(label); if(value != null) { String type = value.getValue(); String name = write.get(type); if(name == null) { throw new PersistenceException("Could not match class %s", type); } node.put(replace, name); } }