private static <T> void writeList(Serializer ser, OutputStream os, String listElement, List<T> list) throws Exception { Format format = new Format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); OutputNode doc = NodeBuilder.write(new OutputStreamWriter(os, UTF_8), format); OutputNode root = doc.getChild("root"); root.setComment(BACKUP_COMMENT); OutputNode listNode = root.getChild(listElement); if (list != null) { for (T t : list) { try { ser.write(t, listNode); } catch (Exception e) { log.debug("Exception While writing node of type: " + t.getClass(), e); } } } root.commit(); }
private static <T> List<T> readList(Serializer ser, File baseDir, String fileName, String listNodeName, Class<T> clazz, boolean notThow) throws Exception { List<T> list = new ArrayList<>(); File xml = new File(baseDir, fileName); if (!xml.exists()) { final String msg = fileName + " missing"; if (notThow) { log.debug(msg); return list; } else { throw new BackupException(msg); } } try (InputStream rootIs = new FileInputStream(xml)) { InputNode root = NodeBuilder.read(rootIs); InputNode listNode = root.getNext(); if (listNodeName.equals(listNode.getName())) { InputNode item = listNode.getNext(); while (item != null) { T o = ser.read(clazz, item, false); list.add(o); item = listNode.getNext(); } } } return list; }
public void testEmptySource() throws Exception { InputNode event = NodeBuilder.read(new StringReader(EMPTY_SOURCE)); assertTrue(event.isRoot()); assertFalse(event.isEmpty()); assertEquals("root", event.getName()); InputNode child = event.getNext(); assertTrue(child.isEmpty()); assertEquals("empty", child.getName()); child = event.getNext(); assertFalse(child.isEmpty()); assertEquals("notEmpty", child.getName()); assertEquals("foo", child.getAttribute("name").getValue()); child = event.getNext(); assertTrue(child.isEmpty()); assertEquals("empty", child.getName()); }
public void testHackJob() throws Exception { Class<?> type = Class.forName("org.simpleframework.xml.stream.DocumentProvider"); Constructor<?> constructor = type.getDeclaredConstructor(); constructor.setAccessible(true); Object value = constructor.newInstance(); Field[] fields = NodeBuilder.class.getDeclaredFields(); for(Field field : fields) { if(field.getName().equalsIgnoreCase("provider")) { field.setAccessible(true); field.set(null, value); } } StringReader reader = new StringReader(SOURCE); InputNode source = NodeBuilder.read(reader); AnnotationStrategy strategy = new AnnotationStrategy(); Persister persister = new Persister(strategy); Something something = persister.read(Something.class, source); assertNotNull(something); assertEquals(something.text, "some inner text"); assertEquals(something.child1, "11"); assertEquals(something.child2, "True"); }
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 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(); }
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); 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(); }
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); PrimitiveValue value = new PrimitiveValue(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); 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 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); PrimitiveValue value = new PrimitiveValue(source, entry, new ClassType(String.class)); OutputNode node = NodeBuilder.write(new PrintWriter(System.out)); value.write(node.getChild("noAttributeString"), "example"); 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 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); 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("inlineString"), exampleMap); 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 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); 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("noAttributeString"), 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); 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("attributeNoKeyString"), exampleMap); node.commit(); }
public void testTwo() throws Exception { Context context = new Source(new TreeStrategy(), new Support(), new Session()); PrimitiveList primitive = new PrimitiveList(context, new ClassType(List.class), new ClassType(String.class), "entry"); InputNode node = NodeBuilder.read(new StringReader(TWO)); Object value = primitive.read(node); assertEquals(value.getClass(), Vector.class); Vector vector = (Vector) value; assertEquals(vector.get(0), "one"); assertEquals(vector.get(1), "two"); InputNode newNode = NodeBuilder.read(new StringReader(TWO)); assertTrue(primitive.validate(newNode)); }
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)); }
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); PrimitiveKey value = new PrimitiveKey(source, entry, new ClassType(String.class)); OutputNode node = NodeBuilder.write(new PrintWriter(System.out)); value.write(node.getChild("notInlineString"), "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); 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 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 testLargeList() 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(LARGE_SOURCE)); for(InputNode next = node.getNext(); next != null; next = node.getNext()) { union.read(next, list); } OutputNode output = NodeBuilder.write(new PrintWriter(System.err), new Format(3)); OutputNode parent = output.getChild("parent"); union.write(parent, list); }