public static Yaml newYaml() { PropertyUtils propertyUtils = new AdvancedPropertyUtils(); propertyUtils.setSkipMissingProperties(true); Constructor constructor = new Constructor(Federations.class); TypeDescription federationDescription = new TypeDescription(Federations.class); federationDescription.putListPropertyType("federatedMetaStores", FederatedMetaStore.class); constructor.addTypeDescription(federationDescription); constructor.setPropertyUtils(propertyUtils); Representer representer = new AdvancedRepresenter(); representer.setPropertyUtils(new FieldOrderPropertyUtils()); representer.addClassTag(Federations.class, Tag.MAP); representer.addClassTag(AbstractMetaStore.class, Tag.MAP); representer.addClassTag(WaggleDanceConfiguration.class, Tag.MAP); representer.addClassTag(YamlStorageConfiguration.class, Tag.MAP); representer.addClassTag(GraphiteConfiguration.class, Tag.MAP); DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setIndent(2); dumperOptions.setDefaultFlowStyle(FlowStyle.BLOCK); return new Yaml(constructor, representer, dumperOptions); }
/** * use wrapper with local tag */ public void testLocalTag() { JavaBeanWithStaticState bean = new JavaBeanWithStaticState(); bean.setName("Bahrack"); bean.setAge(-47); JavaBeanWithStaticState.setType("Type3"); JavaBeanWithStaticState.color = "Violet"; Representer repr = new Representer(); repr.addClassTag(Wrapper.class, new Tag("!mybean")); Yaml yaml = new Yaml(repr); String output = yaml.dump(new Wrapper(bean)); // System.out.println(output); assertEquals("!mybean {age: -47, color: Violet, name: Bahrack, type: Type3}\n", output); // parse back to instance Constructor constr = new Constructor(); TypeDescription description = new TypeDescription(Wrapper.class, new Tag("!mybean")); constr.addTypeDescription(description); yaml = new Yaml(constr); Wrapper wrapper = (Wrapper) yaml.load(output); JavaBeanWithStaticState bean2 = wrapper.createBean(); assertEquals(-47, bean2.getAge()); assertEquals("Bahrack", bean2.getName()); }
/** * explicit TypeDescription is more important then runtime class (which may * be an interface) */ public void testLoadList2() { String output = Util.getLocalResource("examples/list-bean-3.yaml"); // System.out.println(output); TypeDescription descr = new TypeDescription(ListBean.class); descr.putListPropertyType("developers", Developer.class); Yaml beanLoader = new Yaml(new Constructor(descr)); ListBean parsed = beanLoader.loadAs(output, ListBean.class); assertNotNull(parsed); List<Human> developers = parsed.getDevelopers(); assertEquals(2, developers.size()); assertEquals("Committer must be recognised.", Developer.class, developers.get(0).getClass()); Developer fred = (Developer) developers.get(0); assertEquals("Fred", fred.getName()); assertEquals("creator", fred.getRole()); Developer john = (Developer) developers.get(1); assertEquals("John", john.getName()); assertEquals("committer", john.getRole()); }
public void testChildrenArray() { Constructor constructor = new Constructor(Human_WithArrayOfChildren.class); TypeDescription HumanWithChildrenArrayDescription = new TypeDescription( Human_WithArrayOfChildren.class); HumanWithChildrenArrayDescription.putListPropertyType("children", Human_WithArrayOfChildren.class); constructor.addTypeDescription(HumanWithChildrenArrayDescription); Human_WithArrayOfChildren son = createSon(); Yaml yaml = new Yaml(constructor); String output = yaml.dump(son); // System.out.println(output); String etalon = Util.getLocalResource("recursive/with-childrenArray.yaml"); assertEquals(etalon, output); // Human_WithArrayOfChildren son2 = (Human_WithArrayOfChildren) yaml.load(output); checkSon(son2); }
@SuppressWarnings("unchecked") public void testJavaBeanWithTypeDescription() { Constructor c = new CustomBeanConstructor(); TypeDescription descr = new TypeDescription(CodeBean.class, new Tag( "!de.oddb.org,2007/ODDB::Util::Code")); c.addTypeDescription(descr); Yaml yaml = new Yaml(c); String input = Util.getLocalResource("issues/issue56-1.yaml"); int counter = 0; for (Object obj : yaml.loadAll(input)) { // System.out.println(obj); Map<String, Object> map = (Map<String, Object>) obj; Integer oid = (Integer) map.get("oid"); assertTrue(oid > 10000); counter++; } assertEquals(4, counter); assertEquals(55, CodeBean.counter); }
public void testArrayAsMapValueWithTypeDespriptor() { Yaml yaml2dump = new Yaml(); yaml2dump.setBeanAccess(BeanAccess.FIELD); A data = createA(); String dump = yaml2dump.dump(data); // System.out.println(dump); TypeDescription aTypeDescr = new TypeDescription(A.class); aTypeDescr.putMapPropertyType("meta", String.class, String[].class); Constructor c = new Constructor(); c.addTypeDescription(aTypeDescr); Yaml yaml2load = new Yaml(c); yaml2load.setBeanAccess(BeanAccess.FIELD); A loaded = (A) yaml2load.load(dump); assertEquals(data.meta.size(), loaded.meta.size()); Set<Entry<String, String[]>> loadedMeta = loaded.meta.entrySet(); for (Entry<String, String[]> entry : loadedMeta) { assertTrue(data.meta.containsKey(entry.getKey())); Assert.assertArrayEquals(data.meta.get(entry.getKey()), entry.getValue()); } }
public void testArrayAsListValueWithTypeDespriptor() { Yaml yaml2dump = new Yaml(); yaml2dump.setBeanAccess(BeanAccess.FIELD); B data = createB(); String dump = yaml2dump.dump(data); // System.out.println(dump); TypeDescription aTypeDescr = new TypeDescription(B.class); aTypeDescr.putListPropertyType("meta", String[].class); Constructor c = new Constructor(); c.addTypeDescription(aTypeDescr); Yaml yaml2load = new Yaml(c); yaml2load.setBeanAccess(BeanAccess.FIELD); B loaded = (B) yaml2load.load(dump); Assert.assertArrayEquals(data.meta.toArray(), loaded.meta.toArray()); }
public void testDirectives() { String input = Util.getLocalResource("issues/issue149-losing-directives.yaml"); // System.out.println(input); Constructor constr = new Constructor(); TypeDescription description = new TypeDescription(ComponentBean.class, new Tag( "tag:ualberta.ca,2012:" + 29)); constr.addTypeDescription(description); Yaml yaml = new Yaml(constr); Iterator<Object> parsed = yaml.loadAll(input).iterator(); ComponentBean bean1 = (ComponentBean) parsed.next(); assertEquals(0, bean1.getProperty1()); assertEquals("aaa", bean1.getProperty2()); ComponentBean bean2 = (ComponentBean) parsed.next(); assertEquals(3, bean2.getProperty1()); assertEquals("bbb", bean2.getProperty2()); assertFalse(parsed.hasNext()); }
public void testDirectives2() { String input = Util.getLocalResource("issues/issue149-losing-directives-2.yaml"); // System.out.println(input); Constructor constr = new Constructor(); TypeDescription description = new TypeDescription(ComponentBean.class, new Tag( "tag:ualberta.ca,2012:" + 29)); constr.addTypeDescription(description); Yaml yaml = new Yaml(constr); Iterator<Object> parsed = yaml.loadAll(input).iterator(); ComponentBean bean1 = (ComponentBean) parsed.next(); assertEquals(0, bean1.getProperty1()); assertEquals("aaa", bean1.getProperty2()); ComponentBean bean2 = (ComponentBean) parsed.next(); assertEquals(3, bean2.getProperty1()); assertEquals("bbb", bean2.getProperty2()); assertFalse(parsed.hasNext()); }
public void testTypeSafeMap() { Constructor constructor = new Constructor(MyCar.class); TypeDescription carDescription = new TypeDescription(MyCar.class); carDescription.putMapPropertyType("wheels", MyWheel.class, Object.class); constructor.addTypeDescription(carDescription); Yaml yaml = new Yaml(constructor); MyCar car = (MyCar) yaml.load(Util .getLocalResource("constructor/car-no-root-class-map.yaml")); assertEquals("00-FF-Q2", car.getPlate()); Map<MyWheel, Date> wheels = car.getWheels(); assertNotNull(wheels); assertEquals(5, wheels.size()); for (MyWheel wheel : wheels.keySet()) { assertTrue(wheel.getId() > 0); Date date = wheels.get(wheel); long time = date.getTime(); assertTrue("It must be midnight.", time % 10000 == 0); } }
public void testLoadClassTag() { Constructor constructor = new Constructor(); constructor.addTypeDescription(new TypeDescription(Car.class, "!car")); Yaml yaml = new Yaml(constructor); Car car = (Car) yaml.load(Util.getLocalResource("constructor/car-without-tags.yaml")); assertEquals("12-XP-F4", car.getPlate()); List<Wheel> wheels = car.getWheels(); assertNotNull(wheels); assertEquals(5, wheels.size()); Wheel w1 = wheels.get(0); assertEquals(1, w1.getId()); // String carYaml1 = new Yaml().dump(car); assertTrue(carYaml1.startsWith("!!org.yaml.snakeyaml.constructor.Car")); // Representer representer = new Representer(); representer.addClassTag(Car.class, new Tag("!car")); yaml = new Yaml(representer); String carYaml2 = yaml.dump(car); assertEquals(Util.getLocalResource("constructor/car-without-tags.yaml"), carYaml2); }
@Override public Object load(AssetInfo assetInfo) throws IOException { Constructor constructor = new Constructor(); InputStream file = assetInfo.openStream(); Yaml yaml = new Yaml(constructor); constructor.addTypeDescription(new TypeDescription(Theme.class, "!theme")); try { Theme theme = (Theme) yaml.load(file); String path = theme.getPath(); if (path == null || path.equals("")) { theme.setPath(folderName(assetInfo.getKey().getName()) + "/" + theme.getName() + ".css"); } else if (!path.startsWith("/")) { theme.setPath(folderName(assetInfo.getKey().getName()) + "/" + path); } return theme; } finally { file.close(); } }
private void registerTypes(Constructor constructor) { // Standard controls constructor.addTypeDescription(new TypeDescription(PanelLayoutPart.class, "!panel")); constructor.addTypeDescription(new TypeDescription(LabelLayoutPart.class, "!label")); constructor.addTypeDescription(new TypeDescription(CheckBoxLayoutPart.class, "!checkBox")); constructor.addTypeDescription(new TypeDescription(DefaultButtonLayoutPart.class, "!button")); constructor.addTypeDescription(new TypeDescription(SplitPaneLayoutPart.class, "!splitPanel")); constructor.addTypeDescription(new TypeDescription(ScrollPanelLayoutPart.class, "!scrollPanel")); constructor.addTypeDescription(new TypeDescription(ContainerLayoutPart.class, "!container")); constructor.addTypeDescription(new TypeDescription(FrameLayoutPart.class, "!frame")); constructor.addTypeDescription(new TypeDescription(TextFieldLayoutPart.class, "!textField")); constructor.addTypeDescription(new TypeDescription(PasswordLayoutPart.class, "!password")); constructor.addTypeDescription(new TypeDescription(XHTMLLabelLayoutPart.class, "!xhtmlLabel")); // Layouts constructor.addTypeDescription(new TypeDescription(FillLayoutLayoutPart.class, "!fillLayout")); constructor.addTypeDescription(new TypeDescription(BorderLayoutLayoutPart.class, "!borderLayout")); constructor.addTypeDescription(new TypeDescription(MigLayoutLayoutPart.class, "!migLayout")); // Custom ServiceLoader<LayoutPartRegisterable> parts = ServiceLoader.load(LayoutPartRegisterable.class); for (LayoutPartRegisterable part : parts) { part.register(constructor); } }
public ChronixSparkLoader() { Representer representer = new Representer(); representer.getPropertyUtils().setSkipMissingProperties(true); Constructor constructor = new Constructor(YamlConfiguration.class); TypeDescription typeDescription = new TypeDescription(ChronixYAMLConfiguration.class); typeDescription.putMapPropertyType("configurations", Object.class, ChronixYAMLConfiguration.IndividualConfiguration.class); constructor.addTypeDescription(typeDescription); Yaml yaml = new Yaml(constructor, representer); yaml.setBeanAccess(BeanAccess.FIELD); InputStream in = this.getClass().getClassLoader().getResourceAsStream("test_config.yml"); chronixYAMLConfiguration = yaml.loadAs(in, ChronixYAMLConfiguration.class); }
public static NSDTemplate bytesToNSDTemplate(ByteArrayOutputStream b) throws BadFormatException { Constructor constructor = new Constructor(NSDTemplate.class); TypeDescription projectDesc = new TypeDescription(NSDTemplate.class); constructor.addTypeDescription(projectDesc); Yaml yaml = new Yaml(constructor); try { return yaml.loadAs(new ByteArrayInputStream(b.toByteArray()), NSDTemplate.class); } catch (Exception e) { log.error(e.getLocalizedMessage()); throw new BadFormatException( "Problem parsing the descriptor. Check if yaml is formatted correctly."); } }
@Test public void testGetNodesFromVNFDTemplate() throws FileNotFoundException, NotFoundException { InputStream vnfdFile = new FileInputStream(new File("src/main/resources/Testing/testVNFDTemplate.yaml")); Constructor constructor = new Constructor(VNFDTemplate.class); TypeDescription typeDescription = new TypeDescription(VNFDTemplate.class); constructor.addTypeDescription(typeDescription); Yaml yaml = new Yaml(constructor); VNFDTemplate vnfdInput = yaml.loadAs(vnfdFile, VNFDTemplate.class); System.out.println(vnfdInput.getTopology_template().getVDUNodes()); System.out.println(vnfdInput.getTopology_template().getCPNodes()); System.out.println(vnfdInput.getTopology_template().getVNFNodes()); }
@Test public void testGettingVDUNodes() throws FileNotFoundException { InputStream cpFile = new FileInputStream(new File("src/main/resources/Testing/testTopologyTemplate.yaml")); Constructor constructor = new Constructor(TopologyTemplate.class); TypeDescription typeDescription = new TypeDescription(TopologyTemplate.class); constructor.addTypeDescription(typeDescription); Yaml yaml = new Yaml(constructor); TopologyTemplate cpInput = yaml.loadAs(cpFile, TopologyTemplate.class); List<VDUNodeTemplate> vduNodes = cpInput.getVDUNodes(); for (VDUNodeTemplate n : vduNodes) { System.out.println(n.toString()); } }
@Test public void testGettingVLNodes() throws FileNotFoundException { InputStream cpFile = new FileInputStream(new File("src/main/resources/Testing/testVNFDTemplate.yaml")); Constructor constructor = new Constructor(VNFDTemplate.class); TypeDescription typeDescription = new TypeDescription(VNFDTemplate.class); constructor.addTypeDescription(typeDescription); Yaml yaml = new Yaml(constructor); VNFDTemplate cpInput = yaml.loadAs(cpFile, VNFDTemplate.class); List<VLNodeTemplate> vlNodes = cpInput.getTopology_template().getVLNodes(); for (VLNodeTemplate n : vlNodes) { System.out.println(n.toString()); } }
@Test public void testGettingCPNodes() throws FileNotFoundException { InputStream cpFile = new FileInputStream(new File("src/main/resources/Testing/testTopologyTemplate.yaml")); Constructor constructor = new Constructor(TopologyTemplate.class); TypeDescription typeDescription = new TypeDescription(TopologyTemplate.class); constructor.addTypeDescription(typeDescription); Yaml yaml = new Yaml(constructor); TopologyTemplate cpInput = yaml.loadAs(cpFile, TopologyTemplate.class); List<CPNodeTemplate> cpNodes = cpInput.getCPNodes(); for (CPNodeTemplate n : cpNodes) { System.out.println(n.toString()); } }
@Test public void testCreatingVNFDInstance() throws FileNotFoundException, NotFoundException { InputStream vnfdFile = new FileInputStream(new File("src/main/resources/Testing/testVNFDTemplate.yaml")); Constructor constructor = new Constructor(VNFDTemplate.class); TypeDescription typeDescription = new TypeDescription(VNFDTemplate.class); constructor.addTypeDescription(typeDescription); Yaml yaml = new Yaml(constructor); VNFDTemplate vnfdInput = yaml.loadAs(vnfdFile, VNFDTemplate.class); TOSCAParser parser = new TOSCAParser(); VirtualNetworkFunctionDescriptor vnfd = parser.parseVNFDTemplate(vnfdInput); Gson gson = new Gson(); System.out.println(gson.toJson(vnfd)); }
@Test public void testVNFDServerIperf() throws NotFoundException, FileNotFoundException { InputStream vnfdFile = new FileInputStream(new File("src/main/resources/Testing/vnfd_server_iperf.yaml")); Constructor constructor = new Constructor(VNFDTemplate.class); TypeDescription typeDescription = new TypeDescription(VNFDTemplate.class); constructor.addTypeDescription(typeDescription); Yaml yaml = new Yaml(constructor); VNFDTemplate vnfdInput = yaml.loadAs(vnfdFile, VNFDTemplate.class); TOSCAParser parser = new TOSCAParser(); VirtualNetworkFunctionDescriptor vnfd = parser.parseVNFDTemplate(vnfdInput); Gson gson = new Gson(); System.out.println(gson.toJson(vnfd)); }
@Test public void testNSDIperfTemplate() throws FileNotFoundException, NotFoundException { InputStream nsdFile = new FileInputStream(new File("src/main/resources/Testing/testNSDIperf.yaml")); Constructor constructor = new Constructor(NSDTemplate.class); TypeDescription typeDescription = new TypeDescription(NSDTemplate.class); constructor.addTypeDescription(typeDescription); Yaml yaml = new Yaml(constructor); NSDTemplate nsdInput = yaml.loadAs(nsdFile, NSDTemplate.class); TOSCAParser parser = new TOSCAParser(); NetworkServiceDescriptor nsd = parser.parseNSDTemplate(nsdInput); Gson gson = new Gson(); System.out.println(gson.toJson(nsd)); }
@Test public void testMultipleVLs() throws FileNotFoundException, NotFoundException { InputStream nsdFile = new FileInputStream(new File("src/main/resources/Testing/tosca-ns-example.yaml")); Constructor constructor = new Constructor(NSDTemplate.class); TypeDescription typeDescription = new TypeDescription(NSDTemplate.class); constructor.addTypeDescription(typeDescription); Yaml yaml = new Yaml(constructor); NSDTemplate nsdInput = yaml.loadAs(nsdFile, NSDTemplate.class); TOSCAParser parser = new TOSCAParser(); NetworkServiceDescriptor nsd = parser.parseNSDTemplate(nsdInput); Gson gson = new Gson(); System.out.println(gson.toJson(nsd)); }
@Test public void testNSDIperfASTemplate() throws FileNotFoundException, NotFoundException { InputStream nsdFile = new FileInputStream(new File("src/main/resources/Testing/testNSDIperfAutoscaling.yaml")); Constructor constructor = new Constructor(NSDTemplate.class); TypeDescription typeDescription = new TypeDescription(NSDTemplate.class); constructor.addTypeDescription(typeDescription); Yaml yaml = new Yaml(constructor); NSDTemplate nsdInput = yaml.loadAs(nsdFile, NSDTemplate.class); TOSCAParser parser = new TOSCAParser(); NetworkServiceDescriptor nsd = parser.parseNSDTemplate(nsdInput); Gson gson = new Gson(); System.out.println(gson.toJson(nsd)); }
/** * explicit TypeDescription is more important then runtime class (which may be * an interface) */ public void testLoadList2() { String output = Util.getLocalResource("examples/list-bean-3.yaml"); // System.out.println(output); TypeDescription descr = new TypeDescription(ListBean.class); descr.putListPropertyType("developers", Developer.class); Yaml beanLoader = new Yaml(new Constructor(descr)); ListBean parsed = beanLoader.loadAs(output, ListBean.class); assertNotNull(parsed); List<Human> developers = parsed.getDevelopers(); assertEquals(2, developers.size()); assertEquals("Committer must be recognised.", Developer.class, developers .get(0).getClass()); Developer fred = (Developer) developers.get(0); assertEquals("Fred", fred.getName()); assertEquals("creator", fred.getRole()); Developer john = (Developer) developers.get(1); assertEquals("John", john.getName()); assertEquals("committer", john.getRole()); }
@PostConstruct public void init() { try { Constructor constructor = new Constructor(VariableConfig.class); TypeDescription variableConfigTypeDescription = new TypeDescription(VariableConfig.class); variableConfigTypeDescription.putListPropertyType(PROPERTY_VARIABLE_GROUPS, VariableGroup.class); constructor.addTypeDescription(variableConfigTypeDescription); TypeDescription variableGroupTypeDescription = new TypeDescription(VariableGroup.class); variableGroupTypeDescription.putListPropertyType(PROPERTY_VARIABLES, Variable.class); constructor.addTypeDescription(variableGroupTypeDescription); Yaml yaml = new Yaml(constructor); File terraformFolder = resourceService.getClasspathResource(Constants.FOLDER_TERRAFORM); if (!terraformFolder.isFile()) { File[] providerFolderArray = terraformFolder.listFiles(); for (File providerFolder : providerFolderArray) { File variableFile = new File(new URI(providerFolder.toURI() + File.separator + FILE_VARIABLES_YML)); if (variableFile.exists()) { VariableConfig variableConfig = yaml.loadAs(new FileInputStream(variableFile), VariableConfig.class); List<VariableGroup> variableGroupList = variableConfig.getVariableGroups(); providerDefaults.put(providerFolder.getName(), variableGroupList); } } } } catch (Exception e) { LOG.error(e.getMessage(), e); } }
public Constructor(TypeDescription theRoot) { 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()); } typeTags = new HashMap<Tag, Class<? extends Object>>(); typeDefinitions = new HashMap<Class<? extends Object>, TypeDescription>(); yamlClassConstructors.put(NodeId.scalar, new ConstructScalar()); yamlClassConstructors.put(NodeId.mapping, new ConstructMapping()); yamlClassConstructors.put(NodeId.sequence, new ConstructSequence()); addTypeDescription(theRoot); }
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; }