@Override public StructuredOutput array(@Nonnull String name, @Nonnull String elementName, @Nonnull Collection<?> array) { beginArray(name); for (Object o : array) { property(elementName, o); } endArray(); return this; }
/** * Finally creates the tag or object with the given name and attributes. */ public void build() { beginObject(name, attributes.toArray(new Attribute[attributes.size()])); } }
@Override public StructuredOutput property(String name, Object data) { if (getCurrentType() != ElementType.OBJECT && getCurrentType() != ElementType.ARRAY) { throw new IllegalArgumentException("Invalid result structure. Cannot place a property here."); } writeProperty(name, data); nesting.get(0).setEmpty(false); return this; }
@Override public <E> StructuredOutput array(@Nonnull String name, @Nonnull Collection<E> array, BiConsumer<StructuredOutput, E> arrayConsumer) { beginArray(name); for (E e : array) { arrayConsumer.accept(this, e); } endArray(); return this; }
@Override public StructuredOutput beginObject(String name, Attribute... attributes) { startObject(name, attributes); if (!nesting.isEmpty()) { nesting.get(0).setEmpty(false); } nesting.add(0, new Element(ElementType.OBJECT, name)); return this; }
@Override public StructuredOutput nullsafeProperty(@Nonnull String name, @Nullable Object data) { property(name, data != null ? data : ""); return this; } }
@Override public StructuredOutput endArray() { if (nesting.isEmpty()) { throw new IllegalArgumentException("Invalid result structure. No array to close"); } Element e = nesting.get(0); nesting.remove(0); if (e.getType() != ElementType.ARRAY) { throw new IllegalArgumentException("Invalid result structure. No array to close"); } endArray(e.getName()); return this; }
@Override public StructuredOutput beginArray(String name) { startArray(name); if (!nesting.isEmpty()) { nesting.get(0).setEmpty(false); } nesting.add(0, new Element(ElementType.ARRAY, name)); return this; }
@Override public StructuredOutput endObject() { if (nesting.isEmpty()) { throw new IllegalArgumentException("Invalid result structure. No object to close"); } Element e = nesting.get(0); nesting.remove(0); if (e.getType() != ElementType.OBJECT) { throw new IllegalArgumentException("Invalid result structure. No object to close"); } endObject(e.getName()); return this; }
/** * Closes the output and this XML document. */ public void endOutput() { endObject(); if (opensCalled-- == 1) { super.endResult(); try { hd.endDocument(); out.close(); } catch (SAXException | IOException e) { throw Exceptions.handle(e); } } }
@Override public StructuredOutput beginObject(String name) { startObject(name, (Attribute[]) null); if (!nesting.isEmpty()) { nesting.get(0).setEmpty(false); } nesting.add(0, new Element(ElementType.OBJECT, name)); return this; }