@Override public Compare read(InputNode node) throws Exception { Set<ComparatorStep> comparatorSteps = Sets.newHashSet(); InputNode inputNode; while ((inputNode = node.getNext()) != null) { Map<String, String> parameters = getParameters(inputNode); String comparator = parameters.get("comparator"); String name = parameters.get("name"); String collectorName = parameters.get("collectorName"); List<DataModifierStep> dataModifierSteps = Lists.newArrayList(); InputNode childNode; while ((childNode = inputNode.getNext()) != null) { Map<String, String> dataModifierParameters = getParameters(childNode); dataModifierSteps.add(new DataModifierStep(childNode.getName(), dataModifierParameters)); } ComparatorStep comparatorStep = new ComparatorStep(comparator, inputNode.getName(), name, collectorName, parameters, dataModifierSteps); comparatorSteps.add(comparatorStep); } return new Compare(comparatorSteps); }
@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 method checks to see if there are any <code>Label</code> * objects remaining in the provided map that are required. This is * used when deserialization is performed to ensure the the XML * element deserialized contains sufficient details to satisfy the * XML schema class annotations. If there is a required label that * remains it is reported within the exception thrown. * * @param node this is the node that contains the contact value * @param map this is the map to check for remaining labels * @param source this is the object that has been deserialized */ private void validate(InputNode node, LabelMap map, Object source) throws Exception { Class expect = context.getType(type, source); Position line = node.getPosition(); for(Label label : map) { if(label.isRequired() && revision.isEqual()) { throw new ValueRequiredException("Unable to satisfy %s for %s at %s", label, expect, line); } Object value = label.getEmpty(context); if(value != null) { criteria.set(label, value); } } }
/** * Creates the map object to use. The <code>Strategy</code> object * is consulted for the map object class, if one is not resolved * by the strategy implementation or if the collection resolved is * abstract then the Java Collections framework is consulted. * * @param node this is the input node representing the list * * @return this is the map object instantiated for the field */ public Instance getInstance(InputNode node) throws Exception { Value value = getOverride(node); Class expect = getType(); if(value != null) { return getInstance(value); } if(!isInstantiable(expect)) { expect = getConversion(expect); } if(!isMap(expect)) { throw new InstantiationException("Invalid map %s for %s", expect, type); } return context.getInstance(expect); }
/** * This <code>validateAttribute</code> method performs a validation * of the provided node object using a delegate converter. This is * typically another <code>Composite</code> converter, or if the * node is an attribute a <code>Primitive</code> converter. If this * fails validation then an exception is thrown to report the issue. * * @param node this is the node that contains the contact value * @param section this is the section to validate this attribute in * @param map this is the map that contains the label objects */ private void validateAttribute(InputNode node, Section section, LabelMap map) throws Exception { Position line = node.getPosition(); String name = node.getName(); String path = section.getAttribute(name); Label label = map.getLabel(path); if(label == null) { Class expect = type.getType(); if(map.isStrict(context) && revision.isEqual()) { throw new AttributeException("Attribute '%s' does not exist for %s at %s", path, expect, line); } } else { validate(node, label); } }
/** * This <code>read</code> method will read the XML element list from * the provided node and deserialize its children as entry types. * This will deserialize each entry type as a primitive value. In * order to do this the parent string provided forms the element. * * @param node this is the XML element that is to be deserialized * @param list this is the array to read the array values in to * * @return this returns the item to attach to the object contact */ public Object read(InputNode node, Object list) throws Exception{ int length = Array.getLength(list); for(int pos = 0; true; pos++) { Position line = node.getPosition(); InputNode next = node.getNext(); if(next == null) { return list; } if(pos >= length){ throw new ElementException("Array length missing or incorrect for %s at %s", type, line); } Array.set(list, pos, root.read(next)); } }
/** * This <code>read</code> method is used to perform deserialization * of the provided node object using a delegate converter. This is * typically another <code>Composite</code> converter, or if the * node is an attribute a <code>Primitive</code> converter. When * the delegate converter has completed the deserialized value is * assigned to the contact. * * @param node this is the node that contains the contact value * @param source the type of the object that is being deserialized * @param label this is the label used to create the converter */ private Object readInstance(InputNode node, Object source, Label label) throws Exception { Object object = readVariable(node, source, label); if(object == null) { Position line = node.getPosition(); Class expect = context.getType(type, source); if(label.isRequired() && revision.isEqual()) { throw new ValueRequiredException("Empty value for %s in %s at %s", label, expect, line); } } else { if(object != label.getEmpty(context)) { criteria.set(label, object); } } return object; }
/** * This <code>readElement</code> method is used for deserialization * of the provided node object using a delegate converter. This is * typically another <code>Composite</code> converter, or if the * node is an attribute a <code>Primitive</code> converter. When * the delegate converter has completed the deserialized value is * assigned to the contact. * * @param node this is the node that contains the contact value * @param source the type of the object that is being deserialized * @param section this is the section to read the element from * @param map this is the map that contains the label objects */ private void readElement(InputNode node, Object source, Section section, LabelMap map) throws Exception { String name = node.getName(); String path = section.getPath(name); Label label = map.getLabel(path); if(label == null) { label = criteria.resolve(path); } if(label == null) { Position line = node.getPosition(); Class expect = context.getType(type, source); if(map.isStrict(context) && revision.isEqual()) { throw new ElementException("Element '%s' does not have a match in %s at %s", path, expect, line); } else { node.skip(); } } else { readUnion(node, source, map, label); } }
/** * This <code>validateElements</code> method validates the elements * from the provided XML element. This will iterate over all elements * within the element and validate those elements as primitives or * composite objects depending on the contact annotation. * <p> * Once all elements within the XML element have been evaluated * the <code>Schema</code> is checked to ensure that there are no * required contacts annotated with the <code>Element</code> that * remain. If any required element remains an exception is thrown. * * @param node this is the XML element to be evaluated * @param section this is the section that defines the XML structure */ private void validateElements(InputNode node, Section section) throws Exception { LabelMap map = section.getElements(); InputNode next = node.getNext(); while(next != null) { String name = next.getName(); Section child = section.getSection(name); if(child != null) { validateSection(next, child); } else { validateElement(next, section, map); } next = node.getNext(); } validate(node, map); }
/** * This method is used to read the key value from the node. The * value read from the node is resolved using the template filter. * If the key value can not be found according to the annotation * attributes then null is assumed and returned. * * @param node this is the node to read the key value from * @param key this is the name of the key wrapper XML element * * @return this returns the value deserialized from the node */ private Object read(InputNode node, String key) throws Exception { String name = style.getElement(key); Class expect = type.getType(); if(name != null) { node = node.getNext(name); } if(node == null) { return null; } if(node.isEmpty()) { return null; } return root.read(node, expect); }
/** * This <code>readElements</code> method reads the elements from * the provided XML element. This will iterate over all elements * within the element and convert those elements to primitives or * composite objects depending on the contact annotation. * <p> * Once all elements within the XML element have been evaluated * the <code>Schema</code> is checked to ensure that there are no * required contacts annotated with the <code>Element</code> that * remain. If any required element remains an exception is thrown. * * @param node this is the XML element to be evaluated * @param source the type of the object that is being deserialized * @param section the XML section that contains the structure */ private void readElements(InputNode node, Object source, Section section) throws Exception { LabelMap map = section.getElements(); InputNode child = node.getNext(); while(child != null) { String name = child.getName(); Section block = section.getSection(name); if(block != null) { readSection(child, source, block); } else { readElement(child, source, section, map); } child = node.getNext(); } validate(node, map, source); }
/** * This <code>read</code> method wll read the XML element list from * the provided node and deserialize its children as entry types. * This will each entry type is deserialized as a root type, that * is, its <code>Root</code> annotation must be present and the * name of the entry element must match that root element name. * * @param node this is the XML element that is to be deserialized * @param list this is the collection that is to be populated * * @return this returns the item to attach to the object contact */ private Object read(InputNode node, Collection list) throws Exception { InputNode from = node.getParent(); String name = node.getName(); while(node != null) { Class type = entry.getType(); Object item = read(node, type); if(item != null) { list.add(item); } node = from.getNext(name); } return list; }
/** * This <code>populate</code> method will read the XML element map * from the provided node and deserialize its children as entry types. * Each entry type must contain a key and value so that the entry * can be inserted in to the map as a pair. If either the key or * value is composite it is read as a root object, which means its * <code>Root</code> annotation must be present and the name of the * object element must match that root element name. * * @param node this is the XML element that is to be deserialized * @param result this is the map object that is to be populated * * @return this returns the item to attach to the object contact */ private Object populate(InputNode node, Object result) throws Exception { Map map = (Map) result; while(true) { InputNode next = node.getNext(); if(next == null) { return map; } Object index = key.read(next); Object item = value.read(next); map.put(index, item); } }
public void testTwo() throws Exception { Context context = new Source(new TreeStrategy(), new Support(), new Session()); PrimitiveArray primitive = new PrimitiveArray(context, new ClassType(String[].class), new ClassType(String.class), "entry"); InputNode node = NodeBuilder.read(new StringReader(TWO)); Object value = primitive.read(node); assertEquals(value.getClass(), String[].class); String[] list = (String[]) value; assertEquals(list.length, 2); assertEquals(list[0], "one"); assertEquals(list[1], "two"); InputNode newNode = NodeBuilder.read(new StringReader(TWO)); assertTrue(primitive.validate(newNode)); }
/** * This <code>read</code> method will read the XML element list from * the provided node and deserialize its children as entry types. * This ensures each entry type is deserialized as a root type, that * is, its <code>Root</code> annotation must be present and the * name of the entry element must match that root element name. * * @param node this is the XML element that is to be deserialized * @param list this is the array that is to be deserialized * * @return this returns the item to attach to the object contact */ public Object read(InputNode node, Object list) throws Exception{ int length = Array.getLength(list); for(int pos = 0; true; pos++) { Position line = node.getPosition(); InputNode next = node.getNext(); if(next == null) { return list; } if(pos >= length){ throw new ElementException("Array length missing or incorrect for %s at %s", type, line); } read(next, list, pos); } }
private static Value getValue(InputNode node) throws Exception { switch (node.getName()) { case IntegerValue.CODE: return IntegerValue.parse(node.getValue()); case IntegerValue.CODE_ALTERNTAIVE: return IntegerValue.parse(node.getValue()); case LongValue.CODE: return LongValue.parse(node.getValue()); case DoubleValue.CODE: return DoubleValue.parse(node.getValue()); case BooleanValue.CODE: return BooleanValue.parse(node.getValue()); case StringValue.CODE: return StringValue.parse(node.getValue()); case DateValue.CODE: return DateValue.parse(node.getValue()); case Base64Value.CODE: return Base64Value.parse(node.getValue()); case ArrayValue.CODE: ArrayList<Value> data = new ArrayList<>(); InputNode dataNode = node.getNext().getNext(); while (dataNode != null && dataNode.getName().equals(Value.CODE)) { data.add(getValue(dataNode.getNext())); dataNode = node.getNext(); } return new ArrayValue(data); case StructValue.CODE: List<Member> members = new ArrayList<>(); InputNode member = node.getNext(); do { String name = member.getNext().getValue(); Value value = getValue(member.getNext().getNext()); members.add(Member.create(name, value)); member = node.getNext(); } while (member != null && member.getName().equals(Member.CODE)); return new StructValue(members); } throw new PersistenceException(node.getName() + " is an unsupported type in XML-RPC"); }
@Override public String read(InputNode node) throws Exception { String value = node.getValue(); if(value == null) { value = ""; } return value; }
@Override public Integer read(InputNode node) throws Exception { try { String value = node.getValue(); return Integer.valueOf(node.getValue()); }catch (Exception e){ return 0; } }
static Map<String, String> getParameters(InputNode inputNode) throws ParseException { Map<String, String> parameters = Maps.newHashMap(); try { for (String name : inputNode.getAttributes()) { String value = inputNode.getAttribute(name).getValue(); parameters.put(name, value); } } catch (Exception e) { throw new ParseException(e.getMessage(), e); } return parameters; }
@Override public Collect read(InputNode node) throws Exception { List<CollectorStep> collectorSteps = Lists.newArrayList(); InputNode inputNode; while ((inputNode = node.getNext()) != null) { Map<String, String> parameters = getParameters(inputNode); String name = StringUtils.defaultString(parameters.get("name"), inputNode.getName()); CollectorStep collectorStep = new CollectorStep(inputNode.getName(), name, parameters); collectorSteps.add(collectorStep); } ValidationUtils.validateSleep(collectorSteps); return new Collect(collectorSteps); }
@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()); }
@Override public UUID read(InputNode inputNode) throws Exception { String value = inputNode.getValue(); if (value == null || value.equals("")) { return UUID.randomUUID(); } return Helpers.uuidFromBase64(value); }
@Override public Recording.Status read(InputNode node) throws Exception { Recording.Status result = null; String val = node.getValue(); try { result = Recording.Status.valueOf(val); } catch (Exception e) { log.warn("Failed to read recording status out of {}", val); result = PROCESSING.equals(val) ? Status.CONVERTING : Status.NONE; } return result; }
@Override public BaseFileItem read(InputNode node) throws Exception { long oldId = toLong(node.getValue()); long newId = idMap.containsKey(oldId) ? idMap.get(oldId) : oldId; BaseFileItem r = fileDao.get(newId); return r == null ? new FileItem() : r; }
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()); }
@Override public User read(InputNode node) throws Exception { long oldId = toLong(node.getValue()); Long newId = idMap.containsKey(oldId) ? idMap.get(oldId) : oldId; User u = userDao.get(newId); return u == null ? new User() : u; }
@Override public Group read(InputNode node) throws Exception { long oldId = toLong(node.getValue()); long newId = idMap.containsKey(oldId) ? idMap.get(oldId) : oldId; Group o = groupDao.get(newId); return o == null ? new Group() : o; }
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 void testPrimitive() throws Exception { Context context = new Source(new TreeStrategy(), new Support(), new Session()); Primitive primitive = new Primitive(context, new ClassType(String.class)); InputNode node = NodeBuilder.read(new StringReader(SOURCE)); Object value = primitive.read(node); assertEquals("some text", value); InputNode newNode = NodeBuilder.read(new StringReader(SOURCE)); assertTrue(primitive.validate(newNode)); }
@Override public InetSocketAddress read(InputNode node) throws Exception { String addrtext = getAttribute(node, ADDRESS); String porttext = getAttribute(node, PORT); InetAddress addr = InetAddress.getByName(addrtext); int port = Integer.parseInt(porttext); return new InetSocketAddress(addr, port); }
/** * The <code>validateUnion</code> method is determine the unions * for a particular label and set the value of that union to * the same value as the label. This helps the deserialization * process by ensuring once a union is validated it is not * replaced. This is also required when validating inline lists. * * @param node this is the XML element to read the elements from * @param map this is the label map associated with the label * @param label this is the label used to define the XML element */ private void validateUnion(InputNode node, LabelMap map, Label label) throws Exception { String[] list = label.getPaths(); for(String key : list) { map.getLabel(key); } if(label.isInline()) { criteria.set(label, null); } validate(node, label); }
/** * This <code>read</code> method wll read the XML element list from * the provided node and deserialize its children as entry types. * This will deserialize each entry type as a primitive value. In * order to do this the parent string provided forms the element. * * @param node this is the XML element that is to be deserialized * @param list this is the collection that is to be populated * * @return this returns the item to attach to the object contact */ private Object read(InputNode node, Collection list) throws Exception { InputNode from = node.getParent(); String name = node.getName(); while(node != null) { Object item = root.read(node); if(item != null) { list.add(item); } node = from.getNext(name); } return list; }
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); }
@Nonnull protected String getValue(InputNode node) throws Exception { Preconditions.checkNotNull(node, "InputNode was null."); String nodevalue = node.getValue(); if (nodevalue == null) throw new NullPointerException("InputNode has no value: " + node); return nodevalue; }
@Nonnull protected String getAttributeOrValue(InputNode node, String attrname) throws Exception { Preconditions.checkNotNull(node, "InputNode was null."); InputNode attrnode = node.getAttribute(attrname); if (attrnode == null) return getValue(node); String attrvalue = attrnode.getValue(); if (attrvalue == null) return getValue(node); return attrvalue; }
public void testPrimitiveCycle() throws Exception { Context context = new Source(new CycleStrategy(), new Support(), new Session()); Primitive primitive = new Primitive(context, new ClassType(String.class)); InputNode node = NodeBuilder.read(new StringReader(CYCLE_1)); Object value = primitive.read(node); assertEquals("some text", value); // Need to use a different id for validate as reading has created the object // and an exception is thrown that the value already exists if id=1 is used InputNode newNode = NodeBuilder.read(new StringReader(CYCLE_2)); assertTrue(primitive.validate(newNode)); }
public Value read(Type field, NodeMap<InputNode> node, Map map) throws Exception { Value value = strategy.read(field, node, map); Class type = value == null ? field.getType() : value.getType(); Converter converter = registry.resolve(type); if(converter != null) { InputNode source = node.getNode(); Object data = converter.read(source); return new Wrapper(value, data); } return value; }
/** * This method is used to read the value value from the node. The * value read from the node is resolved using the template filter. * If the value value can not be found according to the annotation * attributes then an exception is thrown. * * @param node this is the node to read the value object from * * @return this returns the value deserialized from the node */ public Object read(InputNode node) throws Exception { Class expect = type.getType(); String name = entry.getValue(); if(!entry.isInline()) { if(name == null) { name = context.getName(expect); } return readElement(node, name); } return readAttribute(node, name); }
/** * This <code>validate</code> method wll validate the XML element list * from the provided node and validate its children as entry types. * This will validate each entry type as a primitive value. In order * to do this the parent string provided forms the element. * * @param node this is the XML element that is to be deserialized * * @return true if the element matches the XML schema class given */ public boolean validate(InputNode node) throws Exception{ Instance value = factory.getInstance(node); if(!value.isReference()) { Object result = value.setInstance(null); Class expect = value.getType(); return validate(node, expect); } return true; }
/** * This <code>validate</code> method will validate the XML element list * from the provided node and validate its children as entry types. * This will validate each entry type as a primitive value. In order * to do this the parent string provided forms the element. * * @param node this is the XML element that is to be validated * * @return true if the element matches the XML schema class given */ public boolean validate(InputNode node) throws Exception{ Instance value = factory.getInstance(node); if(!value.isReference()) { Object result = value.setInstance(null); Class expect = value.getType(); return validate(node, expect); } return true; }