public static void getConnection(ConfigurationNode config) { Connection myCon = null; // This shows we have gotten the configuration variables we need to make a SQL connection from // the main class. Note that we don't actually do anything to connect to the database here, // and we don't want to return a null Connection, so this method has been made to return a void // instead. If you want to actually attach a mysql DB, change the method to // public static Connection getConnection(ConfigurationNode config) above, and return "myCon" // instead of just return. logger.info("I got the following variables from my parent class: " + config.getChildrenMap()); return; } }
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(); }
private static 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(); }
private void generateObject(JsonGenerator generator, ConfigurationNode node) throws IOException { if (!node.hasMapChildren()) { throw new IOException("Node passed to generateObject does not have map children!"); } generator.writeStartObject(); for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { //generateComment(generator, ent.getValue(), false); generator.writeFieldName(ent.getKey().toString()); generateValue(generator, ent.getValue()); } generator.writeEndObject(); }
private static void generateObject(JsonGenerator generator, ConfigurationNode node) throws IOException { if (!node.hasMapChildren()) { throw new IOException("Node passed to generateObject does not have map children!"); } generator.writeStartObject(); for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { //generateComment(generator, ent.getValue(), false); generator.writeFieldName(ent.getKey().toString()); generateValue(generator, ent.getValue()); } generator.writeEndObject(); }
private static 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(); }
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; }
@Override public Set<String> getRegisteredTypes() { return getSubjectsNode().getChildrenMap().entrySet().stream() .filter(ent -> ent.getValue().hasMapChildren()) .map(Map.Entry::getKey) .map(Object::toString) .distinct() .collect(GuavaCollectors.toImmutableSet()); }
@Override @SuppressWarnings("unchecked") public Set<String> getAllIdentifiers(String type) { return (Set) getSubjectsNode().getNode(type).getChildrenMap().keySet(); }
@Override public Iterable<String> getAllRankLadders() { return Iterables.unmodifiableIterable(Iterables.transform(getRankLaddersNode().getChildrenMap().keySet(), Object::toString)); }
@Override public Iterable<Map.Entry<Map.Entry<String, String>, ImmutableSubjectData>> getAll() { /*return getSubjectsNode().getChildrenMap().keySet().stream() .filter(i -> i != null) .flatMap(type -> { final String typeStr = type.toString(); return getAll(typeStr) .stream() .map(ent -> Maps.immutableEntry(Maps.immutableEntry(typeStr, ent.getKey()), ent.getValue())); }) .collect(GuavaCollectors.toImmutableSet());*/ return Iterables.concat(Iterables.transform(getSubjectsNode().getChildrenMap().keySet(), type -> { if (type == null) { return null; } final String typeStr = type.toString(); return Iterables.transform(getAll(typeStr), input2 -> Maps.immutableEntry(Maps.immutableEntry(type.toString(), input2.getKey()), input2.getValue())); })); }
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; }
private static <T extends ConfigurationNode> Iterator<VisitedNodeImpl<T>> getChildren(VisitedNodeImpl<T> from) { T node = from.getNode(); switch (node.getValueType()) { case LIST: { Object[] path = from.getRawPath(); return Iterators.transform(node.getChildrenList().iterator(), child -> { Objects.requireNonNull(child); //noinspection unchecked T castedChild = ((T) child); Object[] childPath = calculatePath(path, child.getKey()); return new VisitedNodeImpl<>(childPath, castedChild); }); } case MAP: { Object[] path = from.getRawPath(); return Iterators.transform(node.getChildrenMap().entrySet().iterator(), child -> { Objects.requireNonNull(child); //noinspection unchecked T castedChild = ((T) child.getValue()); Object[] childPath = calculatePath(path, child.getKey()); return new VisitedNodeImpl<>(childPath, castedChild); }); } default: return Collections.emptyIterator(); } }
private static ConfigValue fromValue(ConfigurationNode node) { ConfigValue ret; if (node.hasMapChildren()) { Map<String, ConfigValue> children = node.getOptions().getMapFactory().create(); for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { children.put(String.valueOf(ent.getKey()), fromValue(ent.getValue())); } ret = newConfigObject(children); } else if (node.hasListChildren()) { List<ConfigValue> children = new ArrayList<>(); for (ConfigurationNode ent : node.getChildrenList()) { children.add(fromValue(ent)); } ret = newConfigList(children); } else { ret = ConfigValueFactory.fromAnyRef(node.getValue(), "configurate-hocon"); } if (node instanceof CommentedConfigurationNode) { CommentedConfigurationNode commentedNode = ((CommentedConfigurationNode) node); final ConfigValue finalRet = ret; ret = commentedNode.getComment().map(comment -> finalRet.withOrigin(finalRet.origin().withComments(LINE_SPLITTER.splitToList(comment)))).orElse(ret); } return ret; }
private ConfigValue fromValue(ConfigurationNode node) { ConfigValue ret; if (node.hasMapChildren()) { Map<String, ConfigValue> children = node.getOptions().getMapFactory().create(); for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { children.put(String.valueOf(ent.getKey()), fromValue(ent.getValue())); } ret = newConfigObject(children); } else if (node.hasListChildren()) { List<ConfigValue> children = new ArrayList<>(); for (ConfigurationNode ent : node.getChildrenList()) { children.add(fromValue(ent)); } ret = newConfigList(children); } else { ret = ConfigValueFactory.fromAnyRef(node.getValue(), "configurate-hocon"); } if (node instanceof CommentedConfigurationNode) { CommentedConfigurationNode commentedNode = ((CommentedConfigurationNode) node); final ConfigValue finalRet = ret; ret = commentedNode.getComment().map(comment -> finalRet.withOrigin(finalRet.origin().withComments(LINE_SPLITTER.splitToList(comment)))).orElse(ret); } return ret; }
/** * Returns a permissions tree representing the passed configuration node * @param config The configuration node which represents a permission set * @return A tree structure representing the config node */ public static TreeNode permissionTreeFromConfig(ConfigurationNode config) { if (config == null || config.getValue() == null) { return new TreeNode(false); } if (!config.hasMapChildren()) { if (config.getValue().getClass() == Boolean.class) return new TreeNode(config.getKey().toString(), config.getBoolean()); else { TreeNode node = new TreeNode(config.getKey().toString(), true); node.addChild(new TreeNode("*", true)); return node; } } TreeNode root = new TreeNode(config.getKey().toString(), true); for (Map.Entry<Object, ? extends ConfigurationNode> entry : config.getChildrenMap().entrySet()) { if (entry.getKey().toString().equalsIgnoreCase(".")) { root.setValue(entry.getValue().getBoolean()); continue; } root.addChild(permissionTreeFromConfig(entry.getValue())); } return root; }
@Override public Map<?, ?> deserialize(TypeToken<?> type, ConfigurationNode node) throws ObjectMappingException { Map<Object, Object> ret = new LinkedHashMap<>(); if (node.hasMapChildren()) { if (!(type.getType() instanceof ParameterizedType)) { throw new ObjectMappingException("Raw types are not supported for collections"); } TypeToken<?> key = type.resolveType(Map.class.getTypeParameters()[0]); TypeToken<?> value = type.resolveType(Map.class.getTypeParameters()[1]); TypeSerializer keySerial = node.getOptions().getSerializers().get(key); TypeSerializer valueSerial = node.getOptions().getSerializers().get(value); if (keySerial == null) { throw new ObjectMappingException("No type serializer available for type " + key); } if (valueSerial == null) { throw new ObjectMappingException("No type serializer available for type " + value); } for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { Object keyValue = keySerial.deserialize(key, SimpleConfigurationNode.root().setValue(ent.getKey())); Object valueValue = valueSerial.deserialize(value, ent.getValue()); if (keyValue == null || valueValue == null) { continue; } ret.put(keyValue, valueValue); } } return ret; }
@Override public Map<?, ?> deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode node) throws ObjectMappingException { Map<Object, Object> ret = new LinkedHashMap<>(); if (node.hasMapChildren()) { if (!(type.getType() instanceof ParameterizedType)) { throw new ObjectMappingException("Raw types are not supported for collections"); } TypeToken<?> key = type.resolveType(Map.class.getTypeParameters()[0]); TypeToken<?> value = type.resolveType(Map.class.getTypeParameters()[1]); TypeSerializer<?> keySerial = node.getOptions().getSerializers().get(key); TypeSerializer<?> valueSerial = node.getOptions().getSerializers().get(value); if (keySerial == null) { throw new ObjectMappingException("No type serializer available for type " + key); } if (valueSerial == null) { throw new ObjectMappingException("No type serializer available for type " + value); } for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { Object keyValue = keySerial.deserialize(key, SimpleConfigurationNode.root().setValue(ent.getKey())); Object valueValue = valueSerial.deserialize(value, ent.getValue()); if (keyValue == null || valueValue == null) { continue; } ret.put(keyValue, valueValue); } } return ret; }
@Override public Map<?, ?> deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode node) throws ObjectMappingException { Map<Object, Object> ret = new LinkedHashMap<>(); if (node.hasMapChildren()) { if (!(type.getType() instanceof ParameterizedType)) { throw new ObjectMappingException("Raw types are not supported for collections"); } TypeToken<?> key = type.resolveType(Map.class.getTypeParameters()[0]); TypeToken<?> value = type.resolveType(Map.class.getTypeParameters()[1]); TypeSerializer<?> keySerial = node.getOptions().getSerializers().get(key); TypeSerializer<?> valueSerial = node.getOptions().getSerializers().get(value); if (keySerial == null) { throw new ObjectMappingException("No type serializer available for type " + key); } if (valueSerial == null) { throw new ObjectMappingException("No type serializer available for type " + value); } for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { Object keyValue = keySerial.deserialize(key, SimpleConfigurationNode.root().setValue(ent.getKey())); Object valueValue = valueSerial.deserialize(value, ent.getValue()); if (keyValue == null || valueValue == null) { continue; } ret.put(keyValue, valueValue); } } return ret; }
@Test public void testGetChildrenMap() { ConfigurationNode root = SimpleConfigurationNode.root(); ConfigurationNode a = root.getNode("a").setValue("one"); ConfigurationNode b = root.getNode("b").setValue("two"); assertEquals(ImmutableMap.<Object, ConfigurationNode>of("a", a, "b", b), root.getChildrenMap()); }