Java Code Examples for org.yaml.snakeyaml.constructor.Constructor#addTypeDescription()

The following examples show how to use org.yaml.snakeyaml.constructor.Constructor#addTypeDescription() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: YAMLToJavaDeserialisationUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenLoadYAMLDocumentWithTypeDescription_thenLoadCorrectJavaObjectWithCorrectGenericType() {
    Constructor constructor = new Constructor(Customer.class);
    TypeDescription customTypeDescription = new TypeDescription(Customer.class);
    customTypeDescription.addPropertyParameters("contactDetails", Contact.class);
    constructor.addTypeDescription(customTypeDescription);
    Yaml yaml = new Yaml(constructor);
    InputStream inputStream = this.getClass()
        .getClassLoader()
        .getResourceAsStream("yaml/customer_with_contact_details.yaml");
    Customer customer = yaml.load(inputStream);
    assertNotNull(customer);
    assertEquals("John", customer.getFirstName());
    assertEquals("Doe", customer.getLastName());
    assertEquals(31, customer.getAge());
    assertNotNull(customer.getContactDetails());
    assertEquals(2, customer.getContactDetails().size());
    assertEquals("mobile", customer.getContactDetails()
        .get(0)
        .getType());
    assertEquals("landline", customer.getContactDetails()
        .get(1)
        .getType());
}
 
Example 2
Source File: PerlTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 3
Source File: Human_WithArrayOfChildrenTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: ToscaTest.java    From NFVO with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
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));
}
 
Example 5
Source File: Utils.java    From NFVO with Apache License 2.0 6 votes vote down vote up
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.");
    }
  }
 
Example 6
Source File: ToscaTest.java    From NFVO with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
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());
  }
}
 
Example 7
Source File: ToscaTest.java    From NFVO with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
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());
  }
}
 
Example 8
Source File: GlobalDirectivesTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example 9
Source File: Utils.java    From NFVO with Apache License 2.0 5 votes vote down vote up
public static VNFDTemplate fileToVNFDTemplate(String fileName) throws FileNotFoundException {

    InputStream tosca = new FileInputStream(new File(fileName));
    Constructor constructor = new Constructor(VNFDTemplate.class);
    TypeDescription projectDesc = new TypeDescription(VNFDTemplate.class);

    constructor.addTypeDescription(projectDesc);

    Yaml yaml = new Yaml(constructor);
    return yaml.loadAs(tosca, VNFDTemplate.class);
  }
 
Example 10
Source File: HumanGenericsTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void testChildrenSetAsRoot() throws IOException, IntrospectionException {
    if (!GenericsBugDetector.isProperIntrospection()) {
        return;
    }
    String etalon = Util.getLocalResource("recursive/generics/with-children-as-set.yaml");

    Constructor constructor = new Constructor();
    TypeDescription humanDescription = new TypeDescription(HumanGen.class);
    humanDescription.putMapPropertyType("children", HumanGen.class, Object.class);
    constructor.addTypeDescription(humanDescription);

    Yaml yaml = new Yaml(constructor);
    Set<HumanGen> children2 = (Set<HumanGen>) yaml.load(etalon);
    assertNotNull(children2);
    assertEquals(2, children2.size());

    HumanGen firstChild = children2.iterator().next();

    HumanGen father2 = firstChild.getFather();
    assertEquals("Father", father2.getName());
    assertEquals("Mother", firstChild.getMother().getName());
    assertSame(father2, father2.getBankAccountOwner());
    assertSame(father2.getPartner(), firstChild.getMother());
    assertSame(father2, firstChild.getMother().getPartner());

    assertSame(father2.getPartner().getChildren(), children2);

    for (Object child : children2) {
        assertSame(HumanGen.class, child.getClass()); // check if type
        // descriptor was correct
    }
}
 
Example 11
Source File: EcoParser.java    From incubator-heron with Apache License 2.0 5 votes vote down vote up
private static Yaml topologyYaml() {
  Constructor topologyConstructor = new Constructor(EcoTopologyDefinition.class);

  TypeDescription topologyDescription = new TypeDescription(EcoTopologyDefinition.class);

  topologyDescription.putListPropertyType("spouts", SpoutDefinition.class);
  topologyDescription.putListPropertyType("bolts", BoltDefinition.class);
  topologyConstructor.addTypeDescription(topologyDescription);

  return new Yaml(topologyConstructor);
}
 
Example 12
Source File: Workflow.java    From arbiter with Apache License 2.0 5 votes vote down vote up
/**
 * Defines how to construct a Workflow objects when reading from YAML
 *
 * @return A snakeyaml Constructor instance that will be used to create Workflow objects
 */
public static Constructor getYamlConstructor() {
    Constructor workflowConstructor = new Constructor(Workflow.class);
    TypeDescription workflowDescription = new TypeDescription(Workflow.class);
    workflowDescription.putListPropertyType("actions", Action.class);
    workflowConstructor.addTypeDescription(workflowDescription);

    return workflowConstructor;
}
 
Example 13
Source File: PropOrderInfluenceWhenAliasedInGenericCollectionTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testABWithCustomTag() {
    SuperSaverAccount supersaver = new SuperSaverAccount();
    GeneralAccount generalAccount = new GeneralAccount();

    CustomerAB customerAB = new CustomerAB();
    ArrayList<Account> all = new ArrayList<Account>();
    all.add(supersaver);
    all.add(generalAccount);
    ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
    general.add(generalAccount);
    general.add(supersaver);

    customerAB.aAll = all;
    customerAB.bGeneral = general;

    Constructor constructor = new Constructor();
    Representer representer = new Representer();
    Tag generalAccountTag = new Tag("!GA");
    constructor
            .addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag));
    representer.addClassTag(GeneralAccount.class, generalAccountTag);

    Yaml yaml = new Yaml(constructor, representer);
    String dump = yaml.dump(customerAB);
    // System.out.println(dump);
    CustomerAB parsed = (CustomerAB) yaml.load(dump);
    assertNotNull(parsed);
}
 
Example 14
Source File: PropOrderInfluenceWhenAliasedInGenericCollectionTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testABPropertyWithCustomTag() {
    SuperSaverAccount supersaver = new SuperSaverAccount();
    GeneralAccount generalAccount = new GeneralAccount();

    CustomerAB_Property customerAB_property = new CustomerAB_Property();
    ArrayList<Account> all = new ArrayList<Account>();
    all.add(supersaver);
    all.add(generalAccount);
    ArrayList<GeneralAccount> general = new ArrayList<GeneralAccount>();
    general.add(generalAccount);
    general.add(supersaver);

    customerAB_property.acc = generalAccount;
    customerAB_property.bGeneral = general;

    Constructor constructor = new Constructor();
    Representer representer = new Representer();

    Tag generalAccountTag = new Tag("!GA");
    constructor
            .addTypeDescription(new TypeDescription(GeneralAccount.class, generalAccountTag));
    representer.addClassTag(GeneralAccount.class, generalAccountTag);

    Yaml yaml = new Yaml(constructor, representer);
    String dump = yaml.dump(customerAB_property);
    // System.out.println(dump);
    CustomerAB_Property parsed = (CustomerAB_Property) yaml.load(dump);
    assertNotNull(parsed);
}
 
Example 15
Source File: EntityFactory.java    From xibalba with MIT License 4 votes vote down vote up
/**
 * Create an enemy.
 *
 * @param name     Name of enemy to create
 * @param position Vector2 of their position
 * @return The enemy
 */
public Entity createEnemy(String name, Vector2 position) {
  Constructor constructor = new Constructor(EnemyData.class);
  constructor.addTypeDescription(new TypeDescription(Bleed.class, "!Bleed"));
  constructor.addTypeDescription(new TypeDescription(Charm.class, "!Charm"));
  constructor.addTypeDescription(new TypeDescription(DealDamage.class, "!DealDamage"));
  constructor.addTypeDescription(new TypeDescription(Poison.class, "!Poison"));
  constructor.addTypeDescription(new TypeDescription(RaiseHealth.class, "!RaiseHealth"));
  constructor.addTypeDescription(new TypeDescription(RaiseSpeed.class, "!RaiseSpeed"));
  Yaml yaml = new Yaml(constructor);

  EnemyData data = (EnemyData) yaml.load(Main.enemiesData.get(name));
  Entity entity = new Entity();

  entity.add(new PositionComponent(position));
  entity.add(new EnemyComponent());
  entity.add(new SkillsComponent());
  entity.add(new BodyComponent(data.bodyParts, data.wearableBodyParts));

  entity.add(new VisualComponent(
          Main.asciiAtlas.createSprite(
              data.visual.get("character")), position, Main.parseColor(data.visual.get("color"))
      )
  );

  entity.add(new AttributesComponent(
      i18n.get("entities.enemies." + name + ".name"),
      i18n.get("entities.enemies." + name + ".description"),
      data.type,
      data.attributes.get("speed"),
      data.attributes.get("vision"),
      data.attributes.get("hearing"),
      data.attributes.get("toughness"),
      data.attributes.get("strength"),
      data.attributes.get("agility")
  ));

  BrainComponent brain = new BrainComponent(entity);
  entity.add(brain);

  brain.aggression = data.brain.aggression;
  brain.fearThreshold = data.brain.fearThreshold;

  brain.dna = new Array<>();
  data.brain.dna.forEach((str) -> brain.dna.add(BrainComponent.Dna.valueOf(str)));

  if (data.effects != null) {
    entity.add(new EffectsComponent(data));
  }

  return entity;
}
 
Example 16
Source File: UseCaseService.java    From cloud-portal with MIT License 4 votes vote down vote up
private void getUseCasesFromFileSystem() {

		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 useCaseRootFolder = resourceService.getClasspathResource(Constants.FOLDER_USE_CASE);
			
			for (File useCaseFolder : useCaseRootFolder.listFiles()) {
				for (File providerFolder : useCaseFolder.listFiles()) {
					for (File provisionerFolder : providerFolder.listFiles()) {
						
						String useCaseName = useCaseFolder.getName();
						
						StringBuilder useCaseIdBuilder = new StringBuilder();
						useCaseIdBuilder.append(useCaseName);
						useCaseIdBuilder.append(Constants.CHAR_DASH);
						useCaseIdBuilder.append(providerFolder.getName());
						useCaseIdBuilder.append(Constants.CHAR_DASH);
						useCaseIdBuilder.append(provisionerFolder.getName());
						
						String id = useCaseIdBuilder.toString();
						String provider = providerFolder.getName();
						String provisioner = provisionerFolder.getName();

						List<VariableGroup> variableGroups = new ArrayList<>();
						File variableFile = new File(new URI(provisionerFolder.toURI() + File.separator + FILE_VARIABLES_YML));
						if (variableFile.exists()) {
							VariableConfig variableConfig = yaml.loadAs(new FileInputStream(variableFile), VariableConfig.class);
							variableGroups = variableConfig.getVariableGroups();
						}
						
						StringBuilder resourcePathBuilder = new StringBuilder();
						resourcePathBuilder.append(useCaseRootFolder.getName());
						resourcePathBuilder.append(File.separator);
						resourcePathBuilder.append(useCaseName);
						resourcePathBuilder.append(File.separator);
						resourcePathBuilder.append(providerFolder.getName());
						resourcePathBuilder.append(File.separator);
						resourcePathBuilder.append(provisionerFolder.getName());
						
						UseCase useCase = new UseCase();
						useCase.setId(id);
						useCase.setName(useCaseName);
						useCase.setProvider(provider);
						useCase.setProvisioner(provisioner);
						useCase.setVariableGroups(variableGroups);
						useCase.setResourceFolderPath(resourcePathBuilder.toString());
						useCaseMap.put(id, useCase);
						
						providers.add(provider);
					}
				}
			}
		}
		catch (Exception e) {
			LOG.error(e.getMessage(), e);
		}
	}
 
Example 17
Source File: HumanTest.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
public void testChildren2() {
    Human2 father = new Human2();
    father.setName("Father");
    father.setBirthday(new Date(1000000000));
    father.setBirthPlace("Leningrad");
    father.setBankAccountOwner(father);
    //
    Human2 mother = new Human2();
    mother.setName("Mother");
    mother.setBirthday(new Date(100000000000L));
    mother.setBirthPlace("Saint-Petersburg");
    father.setPartner(mother);
    mother.setPartner(father);
    mother.setBankAccountOwner(father);
    //
    Human2 son = new Human2();
    son.setName("Son");
    son.setBirthday(new Date(310000000000L));
    son.setBirthPlace("Munich");
    son.setBankAccountOwner(father);
    son.setFather(father);
    son.setMother(mother);
    //
    Human2 daughter = new Human2();
    daughter.setName("Daughter");
    daughter.setBirthday(new Date(420000000000L));
    daughter.setBirthPlace("New York");
    daughter.setBankAccountOwner(father);
    daughter.setFather(father);
    daughter.setMother(mother);
    //
    HashMap<Human2, String> children = new LinkedHashMap<Human2, String>(2);
    children.put(son, "son");
    children.put(daughter, "daughter");
    father.setChildren(children);
    mother.setChildren(children);
    //

    Constructor constructor = new Constructor(Human2.class);
    TypeDescription humanDescription = new TypeDescription(Human2.class);
    humanDescription.putMapPropertyType("children", Human2.class, String.class);
    constructor.addTypeDescription(humanDescription);

    Yaml yaml = new Yaml(constructor);
    String output = yaml.dump(son);
    // System.out.println(output);
    String etalon = Util.getLocalResource("recursive/with-children-2.yaml");
    assertEquals(etalon, output);
    //
    Human2 son2 = (Human2) yaml.load(output);
    assertNotNull(son2);
    assertEquals("Son", son.getName());

    Human2 father2 = son2.getFather();
    assertEquals("Father", father2.getName());
    assertEquals("Mother", son2.getMother().getName());
    assertSame(father2, father2.getBankAccountOwner());
    assertSame(father2.getPartner(), son2.getMother());
    assertSame(father2, son2.getMother().getPartner());

    Map<Human2, String> children2 = father2.getChildren();
    assertEquals(2, children2.size());
    assertSame(father2.getPartner().getChildren(), children2);

    validateMapKeys(children2);
}
 
Example 18
Source File: HumanTest.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void testChildrenListRoot() {
    Human3 father = new Human3();
    father.setName("Father");
    father.setBirthday(new Date(1000000000));
    father.setBirthPlace("Leningrad");
    father.setBankAccountOwner(father);
    //
    Human3 mother = new Human3();
    mother.setName("Mother");
    mother.setBirthday(new Date(100000000000L));
    mother.setBirthPlace("Saint-Petersburg");
    father.setPartner(mother);
    mother.setPartner(father);
    mother.setBankAccountOwner(father);
    //
    Human3 son = new Human3();
    son.setName("Son");
    son.setBirthday(new Date(310000000000L));
    son.setBirthPlace("Munich");
    son.setBankAccountOwner(father);
    son.setFather(father);
    son.setMother(mother);
    //
    Human3 daughter = new Human3();
    daughter.setName("Daughter");
    daughter.setBirthday(new Date(420000000000L));
    daughter.setBirthPlace("New York");
    daughter.setBankAccountOwner(father);
    daughter.setFather(father);
    daughter.setMother(mother);
    //
    ArrayList<Human3> children = new ArrayList<Human3>();
    children.add(son);
    children.add(daughter);
    father.setChildren(children);
    mother.setChildren(children);
    //

    Constructor constructor = new Constructor();
    TypeDescription Human3Description = new TypeDescription(Human3.class);
    Human3Description.putListPropertyType("children", Human3.class);
    constructor.addTypeDescription(Human3Description);

    Yaml yaml = new Yaml(constructor);
    String output = yaml.dump(father.getChildren());
    // System.out.println(output);
    String etalon = Util.getLocalResource("recursive/with-children-as-list.yaml");
    assertEquals(etalon, output);
    //
    List<Human3> children2 = (List<Human3>) yaml.load(output);
    assertNotNull(children2);
    Human3 son2 = children2.iterator().next();
    assertEquals(2, children2.size());

    Human3 father2 = son2.getFather();
    assertEquals("Father", father2.getName());
    assertEquals("Mother", son2.getMother().getName());
    assertSame(father2, father2.getBankAccountOwner());
    assertSame(father2.getPartner(), son2.getMother());
    assertSame(father2, son2.getMother().getPartner());

    assertSame(father2.getPartner().getChildren(), children2);

    for (Object child : children2) {
        // check if type descriptor was correct
        assertSame(Human3.class, child.getClass());
    }
}
 
Example 19
Source File: EntityFactory.java    From xibalba with MIT License 4 votes vote down vote up
/**
 * Create an item.
 *
 * @param key      Key of item to create
 * @param position Vector2 of their position
 * @return The item
 */
public Entity createItem(String key, Vector2 position) {
  Constructor constructor = new Constructor(ItemData.class);
  constructor.addTypeDescription(new TypeDescription(Bleed.class, "!Bleed"));
  constructor.addTypeDescription(new TypeDescription(Charm.class, "!Charm"));
  constructor.addTypeDescription(new TypeDescription(DealDamage.class, "!DealDamage"));
  constructor.addTypeDescription(new TypeDescription(Poison.class, "!Poison"));
  constructor.addTypeDescription(new TypeDescription(RaiseHealth.class, "!RaiseHealth"));
  constructor.addTypeDescription(new TypeDescription(StartFire.class, "!StartFire"));
  TypeDescription itemDescription = new TypeDescription(ItemData.class);
  itemDescription.putListPropertyType("requiredComponent", ItemRequiredComponentData.class);
  constructor.addTypeDescription(itemDescription);
  Yaml yaml = new Yaml(constructor);
  ItemData data = (ItemData) yaml.load(Main.itemsData.get(key));
  Entity entity = new Entity();

  entity.add(new PositionComponent(position));

  entity.add(
      new ItemComponent(
          key,
          i18n.get("entities.items." + key + ".name"),
          i18n.get("entities.items." + key + ".description"),
          data
      )
  );

  switch (data.type) {
    case "armor":
      entity.add(new ArmorComponent());
      break;
    case "weapon":
      entity.add(new WeaponComponent(data.weaponType, data.ammunition));
      break;
    case "light":
      ArrayList<Color> colors = new ArrayList<>();
      for (String hex : data.lightColors) {
        colors.add(Main.parseColor(hex));
      }
      entity.add(new LightComponent(data.lightRadius, data.lightFlickers, colors));
      break;
    default:
  }

  if (data.ammunitionType != null) {
    entity.add(new AmmunitionComponent(data.ammunitionType));
  }

  if (data.effects != null) {
    entity.add(new EffectsComponent(data));
  }

  entity.add(new VisualComponent(
      Main.asciiAtlas.createSprite(data.visual.get("character")),
      position, Main.parseColor(data.visual.get("color"))
  ));

  return entity;
}
 
Example 20
Source File: Utils.java    From NFVO with Apache License 2.0 3 votes vote down vote up
public static NSDTemplate stringToNSDTemplate(String someYaml) {

    Constructor constructor = new Constructor(NSDTemplate.class);
    TypeDescription projectDesc = new TypeDescription(NSDTemplate.class);

    constructor.addTypeDescription(projectDesc);

    Yaml yaml = new Yaml(constructor);
    return yaml.loadAs(someYaml, NSDTemplate.class);
  }