public void testAttributeNoKeyString() throws Exception { Source source = new Source(new TreeStrategy(), new Support(), new Session()); MockElementMap map = new MockElementMap(true, // attribute false, // data "entry", // entry true, // inline "", // key String.class, // keyType "name", // name true, // required "value", // value String.class); // valueType PrimitiveType type = new PrimitiveType(map); Contact string = type.getString(); Entry entry = new Entry(string, map); PrimitiveKey value = new PrimitiveKey(source, entry, new ClassType(String.class)); OutputNode node = NodeBuilder.write(new PrintWriter(System.out)); value.write(node.getChild("attributeNoKeyString"), "example"); node.commit(); }
public void testNoAttributeString() throws Exception { Source source = new Source(new TreeStrategy(), new Support(), new Session()); MockElementMap map = new MockElementMap(false, // attribute false, // data "entry", // entry true, // inline "", // key String.class, // keyType "name", // name true, // required "value", // value String.class); // valueType PrimitiveType type = new PrimitiveType(map); Contact string = type.getString(); Entry entry = new Entry(string, map); CompositeInlineMap value = new CompositeInlineMap(source, entry, new ClassType(Map.class)); OutputNode node = NodeBuilder.write(new PrintWriter(System.out)); Map exampleMap = new HashMap(); exampleMap.put("a", "1"); exampleMap.put("b", "2"); value.write(node.getChild("noAttributeString").getChild("map"), exampleMap); node.commit(); }
public void testAttributeNoKeyString() throws Exception { Source source = new Source(new TreeStrategy(), new Support(), new Session()); MockElementMap map = new MockElementMap(true, // attribute false, // data "entry", // entry true, // inline "", // key String.class, // keyType "name", // name true, // required "value", // value String.class); // valueType PrimitiveType type = new PrimitiveType(map); Contact string = type.getString(); Entry entry = new Entry(string, map); PrimitiveValue value = new PrimitiveValue(source, entry, new ClassType(String.class)); OutputNode node = NodeBuilder.write(new PrintWriter(System.out)); value.write(node.getChild("attributeNoKeyString"), "example"); node.commit(); }
public void testNotInlineString() throws Exception { Source source = new Source(new TreeStrategy(), new Support(), new Session()); MockElementMap map = new MockElementMap(false, // attribute false, // data "entry", // entry true, // inline "key", // key String.class, // keyType "name", // name true, // required "value", // value String.class); // valueType PrimitiveType type = new PrimitiveType(map); Contact string = type.getString(); Entry entry = new Entry(string, map); PrimitiveValue value = new PrimitiveValue(source, entry, new ClassType(String.class)); OutputNode node = NodeBuilder.write(new PrintWriter(System.out)); value.write(node.getChild("notInlineString"), "example"); node.commit(); }
public void testNotInlineString() throws Exception { Source source = new Source(new TreeStrategy(), new Support(), new Session()); MockElementMap map = new MockElementMap(false, // attribute false, // data "entry", // entry true, // inline "key", // key String.class, // keyType "name", // name true, // required "value", // value String.class); // valueType PrimitiveType type = new PrimitiveType(map); Contact string = type.getString(); Entry entry = new Entry(string, map); CompositeMap value = new CompositeMap(source, entry, new ClassType(Map.class)); OutputNode node = NodeBuilder.write(new PrintWriter(System.out)); Map exampleMap = new HashMap(); exampleMap.put("a", "1"); exampleMap.put("b", "2"); value.write(node.getChild("notInlineString"), exampleMap); node.commit(); }
public void testInlineString() throws Exception { Source source = new Source(new TreeStrategy(), new Support(), new Session()); MockElementMap map = new MockElementMap(true, // attribute false, // data "entry", // entry true, // inline "key", // key String.class, // keyType "name", // name true, // required "value", // value String.class); // valueType PrimitiveType type = new PrimitiveType(map); Contact string = type.getString(); Entry entry = new Entry(string, map); PrimitiveKey value = new PrimitiveKey(source, entry, new ClassType(String.class)); OutputNode node = NodeBuilder.write(new PrintWriter(System.out)); value.write(node.getChild("inlineString"), "example"); node.commit(); }
public void testNotInlineString() throws Exception { Source source = new Source(new TreeStrategy(), new Support(), new Session()); MockElementMap map = new MockElementMap(false, // attribute false, // data "entry", // entry true, // inline "key", // key String.class, // keyType "name", // name true, // required "value", // value String.class); // valueType PrimitiveType type = new PrimitiveType(map); Contact string = type.getString(); Entry entry = new Entry(string, map); CompositeInlineMap value = new CompositeInlineMap(source, entry, new ClassType(Map.class)); OutputNode node = NodeBuilder.write(new PrintWriter(System.out)); Map exampleMap = new HashMap(); exampleMap.put("a", "1"); exampleMap.put("b", "2"); value.write(node.getChild("notInlineString").getChild("map"), exampleMap); node.commit(); }
/** * This <code>write</code> method is used to convert the provided * object to an XML element. This creates a child node from the * given <code>OutputNode</code> object. Once this child element * is created it is populated with the fields of the source object * in accordance with the XML schema class. * * @param source this is the object to be serialized to XML * @param expect this is the class that is expected to be written * @param name this is the name of the root annotation used * * @throws Exception thrown if there is a problem serializing */ public void write(OutputNode node, Object source, Class expect, String name) throws Exception { OutputNode child = node.getChild(name); Type type = getType(expect); if(source != null) { Class actual = source.getClass(); Decorator decorator = getDecorator(actual); if(decorator != null) { decorator.decorate(child); } if(!context.setOverride(type, source, child)) { getComposite(actual).write(child, source); } } child.commit(); }
public void testAttributeNoKeyString() throws Exception { Source source = new Source(new TreeStrategy(), new Support(), new Session()); MockElementMap map = new MockElementMap(true, // attribute false, // data "entry", // entry true, // inline "", // key String.class, // keyType "name", // name true, // required "value", // value String.class); // valueType PrimitiveType type = new PrimitiveType(map); Contact string = type.getString(); Entry entry = new Entry(string, map); CompositeInlineMap value = new CompositeInlineMap(source, entry, new ClassType(Map.class)); OutputNode node = NodeBuilder.write(new PrintWriter(System.out)); Map exampleMap = new HashMap(); exampleMap.put("a", "1"); exampleMap.put("b", "2"); value.write(node.getChild("attributeNoKeyString").getChild("map"), exampleMap); node.commit(); }
/** * This <code>write</code> method is used to perform serialization of * the given source object. Serialization is performed by appending * elements and attributes from the source object to the provided XML * element object. How the objects contacts are serialized is * determined by the XML schema class that the source object is an * instance of. If a required contact is null an exception is thrown. * * @param source this is the source object to be serialized * @param node the XML element the object is to be serialized to */ public void write(OutputNode node, Object source) throws Exception { Class type = source.getClass(); Schema schema = context.getSchema(type); Caller caller = schema.getCaller(); try { if(schema.isPrimitive()) { primitive.write(node, source); } else { caller.persist(source); write(node, source, schema); } } finally { caller.complete(source); } }
/** * This write method is used to write all the element contacts from * the provided source object to the XML element. This visits all * the contacts marked with the <code>Element</code> annotation in * the source object. All annotated contacts are written as children * to the XML element. This will throw an exception if a required * contact within the source object is null. * * @param source this is the source object to be serialized * @param node this is the XML element to write elements to * @param section this is the section that defines the XML structure */ private void writeElements(OutputNode node, Object source, Section section) throws Exception { for(String name : section) { Section child = section.getSection(name); if(child != null) { OutputNode next = node.getChild(name); writeSection(next, source, child); } else { String path = section.getPath(name); Label label = section.getElement(path); Class expect = context.getType(type, source); Object value = criteria.get(label); if(value == null) { if(label == null) { throw new ElementException("Element '%s' not defined in %s", name, expect); } writeUnion(node, source, section, label); } } } }
/** * The <code>writeUnion</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 serialization * process by ensuring once a union is written it is not * replaced. This is also required when writing inline lists. * * @param node this is the XML element to write elements to * @param source this is the source object to be serialized * @param section this is the section associated with the label * @param label this is the label used to define the XML element */ private void writeUnion(OutputNode node, Object source, Section section, Label label) throws Exception { Contact contact = label.getContact(); Object value = contact.get(source); Class expect = context.getType(type, source); if(value == null && label.isRequired()) { throw new ElementException("Value for %s is null in %s", label, expect); } Object replace = writeReplace(value); if(replace != null) { writeElement(node, replace, label); } criteria.set(label, replace); }
/** * This write method is used to append the provided object as an * element to the given XML element object. This will recursively * write the contacts from the provided object as elements. This is * done using the <code>Converter</code> acquired from the contact * label. If the type of the contact value is not of the same * type as the XML schema class a "class" attribute is appended. * <p> * If the element being written is inline, then this will not * check to see if there is a "class" attribute specifying the * name of the class. This is because inline elements do not have * an outer class and thus could never have an override. * * @param value this is the value to be set as an element * @param node this is the XML element to write the element to * @param label the label that contains the contact details */ private void writeElement(OutputNode node, Object value, Label label) throws Exception { if(value != null) { Class real = value.getClass(); Label match = label.getLabel(real); String name = match.getName(); Type type = label.getType(real); OutputNode next = node.getChild(name); if(!match.isInline()) { writeNamespaces(next, type, match); } if(match.isInline() || !isOverridden(next, value, type)) { Converter convert = match.getConverter(context); boolean data = match.isData(); next.setData(data); writeElement(next, value, convert); } } }
public void testNoAttributeString() throws Exception { Source source = new Source(new TreeStrategy(), new Support(), new Session()); MockElementMap map = new MockElementMap(false, // attribute false, // data "entry", // entry true, // inline "", // key String.class, // keyType "name", // name true, // required "value", // value String.class); // valueType PrimitiveType type = new PrimitiveType(map); Contact string = type.getString(); Entry entry = new Entry(string, map); PrimitiveKey value = new PrimitiveKey(source, entry, new ClassType(String.class)); OutputNode node = NodeBuilder.write(new PrintWriter(System.out)); value.write(node.getChild("noAttributeString"), "example"); node.commit(); }
public void write(OutputNode node) throws Exception { OutputNode member = node.getChild(CODE); OutputNode nameNode = member.getChild(Member.NAME); nameNode.setValue(name); OutputNode valueNode = member.getChild(Value.CODE); value.write(valueNode); }
@Override public void write(OutputNode node) throws Exception { OutputNode child = node.getChild(CODE); OutputNode dataNode = child.getChild(ArrayValue.DATA); for (Value datum : data) { datum.write(dataNode.getChild(Value.CODE)); } }
@Override public void write(OutputNode node) throws Exception { OutputNode struct = node.getChild(CODE); for (Member member : members) { member.write(struct); } }
@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()); } }
@Override public void write(OutputNode outputNode, String s) throws Exception { if(s==null || s.equals("")) { outputNode.setValue(null); } else { outputNode.setValue(s); } }
/** * This method is used to write the value to the specified node. * The value written to the node can be an attribute or an element * depending on the annotation attribute values. This method will * maintain references for serialized elements. * * @param node this is the node that the value is written to * @param item this is the item that is to be written */ public void write(OutputNode node, Object item) throws Exception { Class expect = type.getType(); String name = entry.getValue(); if(!entry.isInline()) { if(name == null) { name = context.getName(expect); } writeElement(node, item, name); } else { writeAttribute(node, item, name); } }
/** * This method is used to write the value to the specified node. * The value written to the node can be an attribute or an element * depending on the annotation attribute values. This method will * maintain references for serialized elements. * * @param node this is the node that the value is written to * @param item this is the item that is to be written * @param key this is the name of the element to be created */ private void writeElement(OutputNode node, Object item, String key) throws Exception { String name = style.getAttribute(key); OutputNode child = node.getChild(name); if(item != null) { if(!isOverridden(child, item)) { root.write(child, item); } } }
@Override public void write(OutputNode node, InetSocketAddress value) throws Exception { InetAddress addr = value.getAddress(); String addrtext = (addr == null) ? value.getHostName() : addr.getHostAddress(); node.setAttribute(ADDRESS, addrtext); node.setAttribute(PORT, String.valueOf(value.getPort())); }
public void write(OutputNode node, Map map) throws Exception { Set keys = map.keySet(); for(Object key : keys) { OutputNode next = node.getChild("entry"); next.setAttribute("key", key.toString()); OutputNode value = next.getChild("value"); value.setValue(map.get(key).toString()); } }
/** * This method is used to write the value to the specified node. * The value written to the node must be a composite object and if * the element map annotation is configured to have a key attribute * then this method will throw an exception. * * @param node this is the node that the value is written to * @param item this is the item that is to be written */ public void write(OutputNode node, Object item) throws Exception { Class expect = type.getType(); String key = entry.getKey(); if(entry.isAttribute()) { throw new ElementException("Can not have %s as an attribute for %s", expect, entry); } if(key == null) { key = context.getName(expect); } String name = style.getElement(key); root.write(node, item, expect, name); }
/** * This method is used to write the value to the specified node. * This will write the item as an attribute to the provided node, * the name of the attribute is taken from the annotation. * * @param node this is the node that the value is written to * @param item this is the item that is to be written */ private void writeAttribute(OutputNode node, Object item) throws Exception { Class expect = type.getType(); String text = factory.getText(item); String key = entry.getKey(); if(key == null) { key = context.getName(expect); } String name = style.getAttribute(key); if(text != null) { node.setAttribute(name, text); } }
public void write(OutputNode node, Car car) throws Exception { Set<String> keys = car.furtherAttributes.keySet(); for(String name : keys) { String value = car.furtherAttributes.get(name); node.setAttribute(name, value); } }
/** * The <code>write</code> method uses the name of the XML element to * select a converter to be used to write the instance. Selection of * the converter is done by looking up the associated label from * the union group using the instance type. Once the converter has * been selected it is used to write the instance. * * @param source this is the source collection to be serialized * @param node this is the XML element container to be populated */ public void write(OutputNode node, Object source) throws Exception { Collection list = (Collection) source; if(group.isInline()) { if(!list.isEmpty()) { write(node, list); } else if(!node.isCommitted()){ node.remove(); } } else { write(node, list); } }
/** * The <code>write</code> method uses the name of the XML element to * select a converter to be used to write the instance. Selection of * the converter is done by looking up the associated label from * the union group using the instance type. Once the converter has * been selected it is used to write the instance. * * @param node this is the XML element used to write the instance * @param list this is the value that is to be written */ private void write(OutputNode node, Collection list) throws Exception { for(Object item : list) { if(item != null) { Class real = item.getClass(); Label label = group.getLabel(real); if(label == null) { throw new UnionException("Entry of %s not declared in %s with annotation %s", real, type, group); } write(node, item, label); } } }
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); }
/** * The <code>write</code> method uses the name of the XML element to * select a converter to be used to write the instance. Selection of * the converter is done by looking up the associated label from * the union group using the instance type. Once the converter has * been selected it is used to write the instance. * * @param node this is the XML element used to write the instance * @param source this is the value that is to be written */ public void write(OutputNode node, Object source) throws Exception { Map map = (Map) source; if(group.isInline()) { if(!map.isEmpty()) { write(node, map); } else if(!node.isCommitted()){ node.remove(); } } else { write(node, map); } }
public void write(Type type, NodeMap<OutputNode> node) throws Exception { Class key = type.getType(); String name = binding.get(key); if(name != null) { node.put("type", name); } }
/** * This <code>write</code> method will write the specified object * to the given XML element as as array entries. Each entry within * the given array must be assignable to the array component type. * Each array entry is serialized as a root element, that is, its * <code>Root</code> annotation is used to extract the name. * * @param source this is the source object array to be serialized * @param node this is the XML element container to be populated */ public void write(OutputNode node, Object source) throws Exception { int size = Array.getLength(source); for(int i = 0; i < size; i++) { Object item = Array.get(source, i); Class type = entry.getType(); root.write(node, item, type, parent); } node.commit(); }
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); } }
/** * This <code>write</code> method will write the specified object * to the given XML element as as list entries. Each entry within * the given collection must be assignable from the annotated * type specified within the <code>ElementList</code> annotation. * Each entry is serialized as a root element, that is, its * <code>Root</code> annotation is used to extract the name. * * @param list this is the source collection to be serialized * @param node this is the XML element container to be populated */ public void write(OutputNode node, Collection list) throws Exception { for(Object item : list) { if(item != null) { Class expect = entry.getType(); Class actual = item.getClass(); if(!expect.isAssignableFrom(actual)) { throw new PersistenceException("Entry %s does not match %s for %s", actual, expect, type); } root.write(node, item, expect, name); } } }
/** * This <code>write</code> method will write the specified object * to the given XML element as as list entries. Each entry within * the given list must be assignable to the given primitive type. * This will deserialize each entry type as a primitive value. In * order to do this the parent string provided forms the element. * * @param source this is the source object array to be serialized * @param node this is the XML element container to be populated */ public void write(OutputNode node, Object source) throws Exception { Collection list = (Collection) source; for(Object item : list) { if(item != null) { OutputNode child = node.getChild(parent); if(!isOverridden(child, item)) { root.write(child, item); } } } }
/** * This is use to apply for <code>NamespaceList</code> annotations * on the node. If there is no namespace list then this will return * and the node will be left unchanged. If however the namespace * list is not empty the the namespaces are added. * * @param node this is the node to apply the namespace list to */ private void scope(OutputNode node) { NamespaceMap map = node.getNamespaces(); for(Namespace next : scope) { String reference = next.reference(); String prefix = next.prefix(); map.setReference(reference, prefix); } }
public void testSimpleList() throws Exception { Context context = getContext(); Group group = getGroup(); Type type = new ClassType(CompositeListUnionTest.class); List<Shape> list = new ArrayList<Shape>(); Expression expression = new PathParser("some/path", type, new Format()); CompositeListUnion union = new CompositeListUnion( context, group, expression, type); InputNode node = NodeBuilder.read(new StringReader(SOURCE)); InputNode circle = node.getNext(); union.read(circle, list); InputNode square = node.getNext(); union.read(square, list); InputNode triangle = node.getNext(); union.read(triangle, list); assertEquals(list.get(0).getClass(), Circle.class); assertEquals(list.get(1).getClass(), Square.class); assertEquals(list.get(2).getClass(), Triangle.class); OutputNode output = NodeBuilder.write(new PrintWriter(System.out), new Format(3)); OutputNode parent = output.getChild("parent"); union.write(parent, list); }
public void testQualifier() throws Exception { NamespaceDecorator global = new NamespaceDecorator(); NamespaceDecorator qualifier = new NamespaceDecorator(); NamespaceDecorator attribute = new NamespaceDecorator(); global.add(new MockNamespace("global", "http://www.domain.com/global")); qualifier.add(new MockNamespace("a", "http://www.domain.com/a")); qualifier.add(new MockNamespace("b", "http://www.domain.com/b")); qualifier.add(new MockNamespace("c", "http://www.domain.com/c")); attribute.add(new MockNamespace("d", "http://www.domain.com/d")); global.set(new MockNamespace("first", "http://www.domain.com/ignore")); qualifier.set(new MockNamespace("a", "http://www.domain.com/a")); attribute.set(new MockNamespace("b", "http://www.domain.com/b")); StringWriter out = new StringWriter(); OutputNode top = NodeBuilder.write(out); OutputNode root = top.getChild("root"); root.setAttribute("version", "1.0"); qualifier.decorate(root, global); OutputNode child = root.getChild("child"); child.setAttribute("name", "John Doe"); OutputNode name = child.getAttributes().get("name"); attribute.decorate(name); OutputNode grandChild = child.getChild("grandChild"); grandChild.setValue("this is the grand child"); root.commit(); validate(out.toString()); }
public boolean write(Type field, Object value, NodeMap<OutputNode> node, Map map) throws Exception { boolean reference = strategy.write(field, value, node, map); if(!reference) { Class type = value.getClass(); Converter converter = registry.resolve(type); OutputNode source = node.getNode(); if(converter != null) { converter.write(source, value); return true; } return false; } return reference; }