Refine search
private static <T> T parseObject(String rawConfig) { Constructor constructor = new Constructor(ConfiguratorConfig.class); TypeDescription itemDescription = new TypeDescription(ConfiguratorConfig.class); itemDescription.addPropertyParameters("items", ConfigItem.class); constructor.addTypeDescription(itemDescription); Yaml yaml = new Yaml(constructor); return yaml.load(rawConfig); }
public Config loadConfig(URL url) throws ConfigException { try { logger.info("Loading config from {}", url); byte[] configBytes; try (InputStream is = url.openStream()) { configBytes = IOUtils.toByteArray(is); } catch (IOException e) { // getConfigURL should have ruled this out throw new AssertionError(e); } Constructor configConstructor = new Constructor(getConfigClass()); addTypeDescription(configConstructor); MissingPropertiesChecker propertiesChecker = new MissingPropertiesChecker(); configConstructor.setPropertyUtils(propertiesChecker); Yaml yaml = new Yaml(configConstructor); Config result = (Config) yaml.loadAs(new ByteArrayInputStream(configBytes), getConfigClass()); propertiesChecker.check(); return result; } catch (YAMLException e) { throw new ConfigException("Invalid yaml", e); } }
@SuppressWarnings("unchecked") public Object construct(Node node) { SequenceNode snode = (SequenceNode) node; if (Set.class.isAssignableFrom(node.getType())) { if (node.isTwoStepsConstruction()) { throw new YAMLException("Set cannot be recursive."); } else { return constructSet(snode); } else if (Collection.class.isAssignableFrom(node.getType())) { if (node.isTwoStepsConstruction()) { return newList(snode); } else { return constructSequence(snode); } else if (node.getType().isArray()) { if (node.isTwoStepsConstruction()) { return createArray(node.getType(), snode.getValue().size()); } else { return constructArray(snode); argumentNode.setType(type); argumentList[index++] = constructObject(argumentNode); List<Object> argumentList = (List<Object>) constructSequence(snode); Class<?>[] parameterTypes = new Class[argumentList.size()]; int index = 0;
private Object newInstance(TypeDescription memberDescription, String propertyName, Node node) { Object newInstance = memberDescription.newInstance(propertyName, node); if (newInstance != null) { constructedObjects.put(node, newInstance); return constructObjectNoCheck(node); } return constructObject(node); }
private static YamlOrchestrationShardingRuleConfiguration unmarshal(final byte[] yamlByteArray) { return new Yaml(new Constructor(YamlOrchestrationShardingRuleConfiguration.class)).loadAs(new ByteArrayInputStream(yamlByteArray), YamlOrchestrationShardingRuleConfiguration.class); } }
public static ConditionRouterRule parse(String rawRule) { Constructor constructor = new Constructor(ConditionRouterRule.class); Yaml yaml = new Yaml(constructor); ConditionRouterRule rule = yaml.load(rawRule); rule.setRawRule(rawRule); if (CollectionUtils.isEmpty(rule.getConditions())) { rule.setValid(false); } return rule; }
org.yaml.snakeyaml.constructor.Constructor constructor = new org.yaml.snakeyaml.constructor.Constructor(Config.class); TypeDescription seedDesc = new TypeDescription(ParameterizedClass.class); seedDesc.putMapPropertyType("parameters", String.class, String.class); constructor.addTypeDescription(seedDesc); MissingPropertiesChecker propertiesChecker = new MissingPropertiesChecker(); constructor.setPropertyUtils(propertiesChecker); Yaml yaml = new Yaml(constructor); Config result = yaml.loadAs(new ByteArrayInputStream(configBytes), Config.class);
Constructor constructor = new TestNGConstructor(XmlSuite.class); TypeDescription suiteDescription = new TypeDescription(XmlSuite.class); suiteDescription.putListPropertyType("packages", XmlPackage.class); suiteDescription.putListPropertyType("listeners", String.class); suiteDescription.putListPropertyType("tests", XmlTest.class); suiteDescription.putListPropertyType("method-selectors", XmlMethodSelector.class); constructor.addTypeDescription(suiteDescription); testDescription.putMapPropertyType("metaGroups", String.class, List.class); testDescription.putListPropertyType("method-selectors", XmlMethodSelector.class); constructor.addTypeDescription(testDescription); org.yaml.snakeyaml.Yaml y = new org.yaml.snakeyaml.Yaml(constructor); if (is == null) { is = new FileInputStream(new File(filePath)); XmlSuite result = (XmlSuite) y.load(is);
private static Yaml getYaml() { Representer representer = new TapeRepresenter(); representer.addClassTag(YamlTape.class, YamlTape.TAPE_TAG); Constructor constructor = new TapeConstructor(); constructor.addTypeDescription(new TypeDescription(YamlTape.class, YamlTape.TAPE_TAG)); DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(BLOCK); dumperOptions.setWidth(256); Yaml yaml = new Yaml(constructor, representer, dumperOptions); yaml.setBeanAccess(BeanAccess.FIELD); return yaml; } }
throws AraraException { Representer representer = new Representer(); representer.addClassTag(Rule.class, new Tag("!config")); Yaml yaml = new Yaml(new Constructor(Rule.class), representer); Rule rule = null; try { rule = yaml.loadAs(new FileReader(file), Rule.class); } catch (MarkedYAMLException yamlException) { throw new AraraException(
public Config loadConfig(URL url) throws ConfigurationException { try { logger.debug("Loading settings from {}", url); byte[] configBytes; try (InputStream is = url.openStream()) { configBytes = ByteStreams.toByteArray(is); } catch (IOException e) { // getStorageConfigURL should have ruled this out throw new AssertionError(e); } Constructor constructor = new CustomConstructor(Config.class); PropertiesChecker propertiesChecker = new PropertiesChecker(); constructor.setPropertyUtils(propertiesChecker); Yaml yaml = new Yaml(constructor); Config result = loadConfig(yaml, configBytes); propertiesChecker.check(); return result; } catch (YAMLException e) { throw new ConfigurationException("Invalid yaml: " + url + SystemUtils.LINE_SEPARATOR + " Error: " + e.getMessage(), false); } }
public BODODescriptor loadBodoDescriptor() throws IOException { TypeDescription descriptorDescription = new TypeDescription(BODODescriptor.class); descriptorDescription.putListPropertyType("implementations", BODODescriptor.Implementation.class); Yaml yaml = new Yaml(new Constructor(descriptorDescription)); return (BODODescriptor)yaml.load(loadString()); }
@NotNull public static CDepManifestYml convertStringToManifest(@NotNull String url, @NotNull String content) { Invariant.registerYamlFile(url); Yaml yaml = new Yaml(new Constructor(CDepManifestYml.class)); CDepManifestYml manifest; byte[] bytes = content.getBytes(StandardCharsets.UTF_8); try { manifest = (CDepManifestYml) yaml.load(new ByteArrayInputStream(bytes)); // Try to read current version if (manifest != null) { manifest.sourceVersion = CDepManifestYmlVersion.vlatest; } } catch (YAMLException e) { try { manifest = V3Reader.convertStringToManifest(content); } catch (YAMLException e2) { if (!tryCreateSensibleParseError(e, 0)) { // If older readers also couldn't read it then report the original exception. require(false, e.toString()); } return new CDepManifestYml(EMPTY_COORDINATE); } } require(manifest != null, "Manifest was empty"); assert manifest != null; manifest = new ConvertNullToDefaultRewriter().visitCDepManifestYml(manifest); Node nodes = yaml.compose(new InputStreamReader(new ByteArrayInputStream(bytes))); SnakeYmlUtils.mapAndRegisterNodes(url, manifest, nodes); return manifest; }
@SuppressWarnings("unchecked") public PluginManager(ProxyServer proxy) { this.proxy = proxy; // Ignore unknown entries in the plugin descriptions Constructor yamlConstructor = new Constructor(); PropertyUtils propertyUtils = yamlConstructor.getPropertyUtils(); propertyUtils.setSkipMissingProperties( true ); yamlConstructor.setPropertyUtils( propertyUtils ); yaml = new Yaml( yamlConstructor ); eventBus = new EventBus( proxy.getLogger() ); }
@Override protected Yaml initialValue() { Representer representer = new Representer() { { representers.put( Configuration.class, new Represent() { @Override public Node representData(Object data) { return represent( ( (Configuration) data ).self ); } } ); } }; DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle( DumperOptions.FlowStyle.BLOCK ); return new Yaml( new Constructor(), representer, options ); } };
protected Object constructJavaBean2ndStep(MappingNode node, Object object) { flattenMapping(node); Class<? extends Object> beanType = node.getType(); List<NodeTuple> nodeValue = node.getValue(); throw new YAMLException( "Keys must be scalars but found: " + tuple.getKeyNode()); String key = (String) constructObject(keyNode); try { TypeDescription memberDescription = typeDefinitions.get(beanType); Property property = memberDescription == null ? getProperty(beanType, key) : memberDescription.getProperty(key); throw new YAMLException("No writable property '" + key + "' on class: " + beanType.getName()); if (valueNode.getNodeId() == NodeId.sequence) { Class<?> t = arguments[0]; SequenceNode snode = (SequenceNode) valueNode; : constructObject(valueNode); if (property.getType() == String.class && Tag.BINARY.equals(valueNode.getTag()) && value instanceof byte[]) { value = new String((byte[]) value); || !memberDescription.setProperty(object, key, value)) { property.set(object, value);
/** * Create Yaml instance. It is safe to create a few instances and use them * in different Threads. * * @param representer * Representer to emit outgoing objects */ public Yaml(Representer representer) { this(new Constructor(), representer); }
protected Object constructJavaBean2ndStep(MappingNode node, Object object) { flattenMapping(node); Class<? extends Object> beanType = node.getType(); List<NodeTuple> nodeValue = node.getValue(); throw new YAMLException("Keys must be scalars but found: " + tuple.getKeyNode()); String key = (String) constructObject(keyNode); try { Property property = getProperty(beanType, key); valueNode.setType(property.getType()); TypeDescription memberDescription = typeDefinitions.get(beanType); boolean typeDetected = false; if (memberDescription != null) { switch (valueNode.getNodeId()) { case sequence: SequenceNode snode = (SequenceNode) valueNode; Class<? extends Object> memberType = memberDescription .getListPropertyType(key); if (memberType != null) { snode.setListType(memberType); if (!typeDetected && valueNode.getNodeId() != NodeId.scalar) { Object value = constructObject(valueNode); if (property.getType() == String.class && Tag.BINARY.equals(valueNode.getTag()) && value instanceof byte[]) { value = new String((byte[])value);
protected void addTypeDescription(Constructor configConstructor) { TypeDescription mapPropertyTypeDesc = new TypeDescription(MapPropertyTypeDef.class); mapPropertyTypeDesc.putMapPropertyType("parameters", String.class, String.class); configConstructor.addTypeDescription(mapPropertyTypeDesc); }
public Constructor(TypeDescription theRoot, Collection<TypeDescription> moreTDs) { if (theRoot == null) { throw new NullPointerException("Root type must be provided."); } this.yamlConstructors.put(null, new ConstructYamlObject()); if (!Object.class.equals(theRoot.getType())) { rootTag = new Tag(theRoot.getType()); } yamlClassConstructors.put(NodeId.scalar, new ConstructScalar()); yamlClassConstructors.put(NodeId.mapping, new ConstructMapping()); yamlClassConstructors.put(NodeId.sequence, new ConstructSequence()); addTypeDescription(theRoot); if (moreTDs != null) { for (TypeDescription td : moreTDs) { addTypeDescription(td); } } }