@Override public TextTemplate deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { this.root = value; this.openArg = value.getNode(NODE_OPEN_ARG).getString(TextTemplate.DEFAULT_OPEN_ARG); this.closeArg = value.getNode(NODE_CLOSE_ARG).getString(TextTemplate.DEFAULT_CLOSE_ARG); Text content = value.getNode(NODE_CONTENT).getValue(TypeToken.of(Text.class)); List<Object> elements = new ArrayList<>(); parse(content, elements); return TextTemplate.of(elements.toArray(new Object[elements.size()])); }
@Override public void serialize(TypeToken<?> type, TextTemplate obj, ConfigurationNode value) throws ObjectMappingException { value.getNode(NODE_OPTIONS, NODE_OPEN_ARG).setValue(obj.getOpenArgString()); value.getNode(NODE_OPTIONS, NODE_CLOSE_ARG).setValue(obj.getCloseArgString()); value.getNode(NODE_ARGS).setValue(TOKEN_ARGS, obj.getArguments()); value.getNode(NODE_CONTENT).setValue(TOKEN_TEXT, obj.toText()); }
private Element writeNode(Document document, ConfigurationNode node, String forcedTag) { String tag = defaultTagName; Map<String, String> attributes = ImmutableMap.of(); if (node instanceof AttributedConfigurationNode) { AttributedConfigurationNode attributedNode = ((AttributedConfigurationNode) node); tag = attributedNode.getTagName(); attributes = attributedNode.getAttributes(); } Element element = document.createElement(forcedTag == null ? tag : forcedTag); for (Map.Entry<String, String> attribute : attributes.entrySet()) { element.setAttribute(attribute.getKey(), attribute.getValue()); } if (node.hasMapChildren()) { for (Map.Entry<Object, ? extends ConfigurationNode> child : node.getChildrenMap().entrySet()) { element.appendChild(writeNode(document, child.getValue(), child.getKey().toString())); } } else if (node.hasListChildren()) { if (writeExplicitType) { element.setAttribute("configurate-type", "list"); } for (ConfigurationNode child : node.getChildrenList()) { element.appendChild(writeNode(document, child, null)); } } else { element.appendChild(document.createTextNode(node.getValue().toString())); } return element; }
public static FilePermissionsExConfiguration fromLoader(ConfigurationLoader<?> loader) throws IOException { ConfigurationNode node = loader.load(); ConfigurationNode fallbackConfig; try { fallbackConfig = FilePermissionsExConfiguration.loadDefaultConfiguration(); } catch (IOException e) { throw new Error("PEX's default configuration could not be loaded!", e); } ConfigTransformations.versions().apply(node); node.mergeValuesFrom(fallbackConfig); ConfigurationNode defBackendNode = node.getNode("default-backend"); if (defBackendNode.isVirtual() || defBackendNode.getValue() == null) { // Set based on whether or not the H2 backend is available defBackendNode.setValue("default-file"); /*try { Class.forName("org.h2.Driver"); defBackendNode.setValue("default"); } catch (ClassNotFoundException e) { defBackendNode.setValue("default-file"); }*/ } FilePermissionsExConfiguration config = new FilePermissionsExConfiguration(loader, node); config.load(); return config; }
@Override public Set<?> deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { TypeToken<?> entryType = type.resolveType(Set.class.getTypeParameters()[0]); TypeSerializer<?> entrySerial = value.getOptions().getSerializers().get(entryType); if (entrySerial == null) { throw new ObjectMappingException("No applicable type serializer for type " + entryType); } if (value.hasListChildren()) { List<? extends ConfigurationNode> values = value.getChildrenList(); Set<Object> ret = new HashSet<>(values.size()); for (ConfigurationNode ent : values) { ret.add(entrySerial.deserialize(entryType, ent)); } return ret; } else { Object unwrappedVal = value.getValue(); if (unwrappedVal != null) { return Sets.newHashSet(entrySerial.deserialize(entryType, value)); } } return new HashSet<>(); }
@Test public void testUnattachedNodesTemporary() { ConfigurationNode config = SimpleConfigurationNode.root(); ConfigurationNode node = config.getNode("some", "node"); assertTrue(node.isVirtual()); assertNull(node.getValue()); assertFalse(node.hasListChildren()); assertFalse(node.hasMapChildren()); ConfigurationNode node2 = config.getNode("some", "node"); assertNotSame(node, node2); ConfigurationNode node3 = config.getNode("some").getNode("node"); assertNotSame(node, node3); }
void serialize(ConfigurationNode node) throws ObjectMappingException { if (!node.hasListChildren()) { node.setValue(null); } Map<Set<Entry<String, String>>, ConfigurationNode> existingSections = new HashMap<>(); for (ConfigurationNode child : node.getChildrenList()) { existingSections.put(contextsFrom(child), child); } for (Map.Entry<Set<Entry<String, String>>, DataEntry> ent : contexts.entrySet()) { ConfigurationNode contextSection = existingSections.remove(ent.getKey()); if (contextSection == null) { contextSection = node.getAppendedNode(); ConfigurationNode contextsNode = contextSection.getNode(KEY_CONTEXTS); for (Entry<String, String> context : ent.getKey()) { contextsNode.getNode(context.getKey()).setValue(context.getValue()); } } MAPPER.bind(ent.getValue()).serialize(contextSection); } for (ConfigurationNode unused : existingSections.values()) { unused.setValue(null); } }
@Test public void testSingleListConversion() { ConfigurationNode config = SimpleConfigurationNode.root(); ConfigurationNode node = config.getNode("test", "value"); node.setValue("test"); ConfigurationNode secondChild = node.getAppendedNode(); secondChild.setValue("test2"); assertEquals(Arrays.asList("test", "test2"), node.getValue()); }
private void parseArg(LiteralText source, List<Object> into) throws ObjectMappingException { String name = unwrap(source.getContent()); boolean optional = this.root.getNode(NODE_ARGS, name, NODE_OPT).getBoolean(); Text defaultValue = this.root.getNode(NODE_ARGS, name, NODE_DEF_VAL).getValue(TypeToken.of(Text.class)); TextFormat format = source.getFormat(); into.add(TextTemplate.arg(name).format(format).optional(optional).defaultValue(defaultValue).build()); }
private static Set<Entry<String, String>> contextsFrom(ConfigurationNode node) { Set<Entry<String, String>> contexts = Collections.emptySet(); ConfigurationNode contextsNode = node.getNode(KEY_CONTEXTS); if (contextsNode.hasMapChildren()) { contexts = ImmutableSet.copyOf(Collections2.transform(contextsNode.getChildrenMap().entrySet(), ent -> { return Maps.immutableEntry(ent.getKey().toString(), String.valueOf(ent.getValue().getValue())); })); } return contexts; }
@Test public void testSettingNullRemoves() { ConfigurationNode root = SimpleConfigurationNode.root(); ConfigurationNode child = root.getNode("child").setValue("a"); assertFalse(child.isVirtual()); assertSame(child, root.getNode("child")); child.setValue(null); assertTrue(child.isVirtual()); assertNotSame(child, root.getNode("child")); }
private void generateObject(JsonWriter generator, ConfigurationNode node) throws IOException { if (!node.hasMapChildren()) { throw new IOException("Node passed to generateObject does not have map children!"); } generator.beginObject(); for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { generator.name(ent.getKey().toString()); generateValue(generator, ent.getValue()); } generator.endObject(); }
@Override public Object deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { Class<?> clazz = getInstantiableType(type, value.getNode("__class__").getString()); return value.getOptions().getObjectMapperFactory().getMapper(clazz).bindToNew().populate(value); }
@Override public void serialize(TypeToken<?> type, Set<?> obj, ConfigurationNode value) throws ObjectMappingException { TypeToken<?> entryType = type.resolveType(Set.class.getTypeParameters()[0]); TypeSerializer entrySerial = value.getOptions().getSerializers().get(entryType); if (entrySerial == null) { throw new ObjectMappingException("No applicable type serializer for type " + entryType); } value.setValue(ImmutableSet.of()); for (Object ent : obj) { entrySerial.serialize(entryType, ent, value.getAppendedNode()); } } }
@Test public void testListSerializer() throws ObjectMappingException { final TypeToken<List<String>> stringListType = new TypeToken<List<String>>() {}; final TypeSerializer<List<String>> stringListSerializer = SERIALIZERS.get(stringListType); final ConfigurationNode value = SimpleConfigurationNode.root(); value.getAppendedNode().setValue("hi"); value.getAppendedNode().setValue("there"); value.getAppendedNode().setValue("beautiful"); value.getAppendedNode().setValue("people"); assertEquals(Arrays.asList("hi", "there", "beautiful", "people"), stringListSerializer.deserialize(stringListType, value)); value.setValue(null); stringListSerializer.serialize(stringListType, Arrays.asList("hi", "there", "lame", "people"), value); assertEquals("hi", value.getNode(0).getString()); assertEquals("there", value.getNode(1).getString()); assertEquals("lame", value.getNode(2).getString()); assertEquals("people", value.getNode(3).getString()); }
@Override public BookView deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { BookView.Builder builder = BookView.builder(); builder.author(value.getNode(NODE_AUTHOR).getValue(TOKEN_TEXT)); builder.title(value.getNode(NODE_TITLE).getValue(TOKEN_TEXT)); builder.addPages(value.getNode(NODE_PAGES).getValue(TOKEN_TEXT_LIST)); return builder.build(); }
private void generateArray(JsonGenerator generator, ConfigurationNode node) throws IOException { if (!node.hasListChildren()) { throw new IOException("Node passed to generateArray does not have list children!"); } List<? extends ConfigurationNode> children = node.getChildrenList(); generator.writeStartArray(children.size()); for (ConfigurationNode child : children) { //generateComment(generator, child, true); generateValue(generator, child); } generator.writeEndArray(); } }
private static void generateValue(JsonWriter generator, ConfigurationNode node) throws IOException { if (node.hasMapChildren()) { generateObject(generator, node); } else if (node.hasListChildren()) { generateArray(generator, node); } else if (node.getKey() == null && node.getValue() == null) { generator.beginObject(); generator.endObject(); } else { Object value = node.getValue(); if (value instanceof Double) { generator.value((Double) value); } else if (value instanceof Float) { generator.value((Float) value); } else if (value instanceof Long) { generator.value((Long) value); } else if (value instanceof Integer) { generator.value((Integer) value); } else if (value instanceof Boolean) { generator.value((Boolean) value); } else { generator.value(value.toString()); } } }