org.yaml.snakeyaml.introspector.PropertyUtils Java Examples

The following examples show how to use org.yaml.snakeyaml.introspector.PropertyUtils. 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: ContainerDataYaml.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Given a ContainerType this method returns a Yaml representation of
 * the container properties.
 *
 * @param containerType type of container
 * @return Yamal representation of container properties
 *
 * @throws StorageContainerException if the type is unrecognized
 */
public static Yaml getYamlForContainerType(ContainerType containerType)
    throws StorageContainerException {
  PropertyUtils propertyUtils = new PropertyUtils();
  propertyUtils.setBeanAccess(BeanAccess.FIELD);
  propertyUtils.setAllowReadOnlyProperties(true);

  switch (containerType) {
  case KeyValueContainer:
    Representer representer = new ContainerDataRepresenter();
    representer.setPropertyUtils(propertyUtils);
    representer.addClassTag(
        KeyValueContainerData.class,
        KeyValueContainerData.KEYVALUE_YAML_TAG);

    Constructor keyValueDataConstructor = new ContainerDataConstructor();

    return new Yaml(keyValueDataConstructor, representer);
  default:
    throw new StorageContainerException("Unrecognized container Type " +
        "format " + containerType, ContainerProtos.Result
        .UNKNOWN_CONTAINER_TYPE);
  }
}
 
Example #2
Source File: YAMLConfigManager.java    From siddhi with Apache License 2.0 6 votes vote down vote up
/**
 * Initialises YAML Config Manager by parsing the YAML file
 *
 * @throws YAMLConfigManagerException Exception is thrown if there are issues in processing thr yaml file
 */
private void init(String yamlContent) throws YAMLConfigManagerException {
    try {
        CustomClassLoaderConstructor constructor = new CustomClassLoaderConstructor(
                RootConfiguration.class, RootConfiguration.class.getClassLoader());
        PropertyUtils propertyUtils = new PropertyUtils();
        propertyUtils.setSkipMissingProperties(true);
        constructor.setPropertyUtils(propertyUtils);

        Yaml yaml = new Yaml(constructor);
        yaml.setBeanAccess(BeanAccess.FIELD);
        this.rootConfiguration = yaml.load(yamlContent);
    } catch (Exception e) {
        throw new YAMLConfigManagerException("Unable to parse YAML string, '" + yamlContent + "'.", e);
    }
}
 
Example #3
Source File: AbstractBeanTest.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public void testErrorMessage() throws Exception {

        BeanA1 b = new BeanA1();
        b.setId(2l);
        b.setName("name1");

        Constructor c = new Constructor();
        Representer r = new Representer();

        PropertyUtils pu = new PropertyUtils();
        c.setPropertyUtils(pu);
        r.setPropertyUtils(pu);

        pu.getProperties(BeanA1.class, BeanAccess.FIELD);

        Yaml yaml = new Yaml(c, r);
        // yaml.setBeanAccess(BeanAccess.FIELD);
        String dump = yaml.dump(b);
        BeanA1 b2 = (BeanA1) yaml.load(dump);
        assertEquals(b.getId(), b2.getId());
        assertEquals(b.getName(), b2.getName());
    }
 
Example #4
Source File: YamlFactory.java    From waggle-dance with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #5
Source File: OMapContainer.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
private Object loadYaml(File file) throws FileNotFoundException {
    FileReader reader = new FileReader(file);
    try {
        Constructor constructor = new Constructor();
        PropertyUtils putils = new PropertyUtils();
        putils.setSkipMissingProperties(true);
        constructor.setPropertyUtils(putils);
        Yaml yaml = new Yaml(constructor);
        return yaml.load(reader);
    } catch (Throwable t) {
        throw new RuntimeException("Error loading yaml from: " + file.getAbsolutePath() + "\n" + t.getMessage(), t);
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
Example #6
Source File: ObjectMapItem.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
private Object loadYaml(File omapfile) throws IOException {
    FileReader reader = new FileReader(omapfile);
    try {
        Constructor constructor = new Constructor();
        PropertyUtils putils = new PropertyUtils();
        putils.setSkipMissingProperties(true);
        constructor.setPropertyUtils(putils);
        Yaml yaml = new Yaml(constructor);
        return yaml.load(reader);
    } catch (Throwable t) {
        throw new RuntimeException("Error loading yaml from: " + omapfile.getAbsolutePath() + "\n" + t.getMessage(), t);
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
 
Example #7
Source File: StarterConfigure.java    From ClashForMagisk with GNU General Public License v3.0 6 votes vote down vote up
static StarterConfigure loadFromFile(File file) throws IOException {
    FileInputStream inputStream = new FileInputStream(file);
    Constructor constructor = new Constructor(StarterConfigure.class);

    constructor.setPropertyUtils(new PropertyUtils() {
        {
            setSkipMissingProperties(true);
        }
    });

    StarterConfigure result = new Yaml(constructor).loadAs(inputStream, StarterConfigure.class);

    inputStream.close();

    return result;
}
 
Example #8
Source File: ContainerDataYaml.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Read the yaml content, and return containerData.
 *
 * @throws IOException
 */
public static ContainerData readContainer(InputStream input)
    throws IOException {

  ContainerData containerData;
  PropertyUtils propertyUtils = new PropertyUtils();
  propertyUtils.setBeanAccess(BeanAccess.FIELD);
  propertyUtils.setAllowReadOnlyProperties(true);

  Representer representer = new ContainerDataRepresenter();
  representer.setPropertyUtils(propertyUtils);

  Constructor containerDataConstructor = new ContainerDataConstructor();

  Yaml yaml = new Yaml(containerDataConstructor, representer);
  yaml.setBeanAccess(BeanAccess.FIELD);

  containerData = (ContainerData)
      yaml.load(input);

  return containerData;
}
 
Example #9
Source File: BaseConstructor.java    From onedev with MIT License 5 votes vote down vote up
public void setPropertyUtils(PropertyUtils propertyUtils) {
    this.propertyUtils = propertyUtils;
    explicitPropertyUtils = true;
    Collection<TypeDescription> tds = typeDefinitions.values();
    for (TypeDescription typeDescription : tds) {
        typeDescription.setPropertyUtils(propertyUtils);
    }
}
 
Example #10
Source File: Representer.java    From onedev with MIT License 5 votes vote down vote up
@Override
public void setPropertyUtils(PropertyUtils propertyUtils) {
    super.setPropertyUtils(propertyUtils);
    Collection<TypeDescription> tds = typeDefinitions.values();
    for (TypeDescription typeDescription : tds) {
        typeDescription.setPropertyUtils(propertyUtils);
    }
}
 
Example #11
Source File: MetaInfo.java    From ratel with Apache License 2.0 5 votes vote down vote up
private static Yaml getYaml() {
    DumperOptions options = new DumperOptions();
    options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);

    StringExRepresent representer = new StringExRepresent();
    PropertyUtils propertyUtils = representer.getPropertyUtils();
    propertyUtils.setSkipMissingProperties(true);

    return new Yaml(new StringExConstructor(), representer, options);
}
 
Example #12
Source File: YamlEnvironmentPostProcessor.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static <T> Yaml getYaml(Class<T> tClass) {
  Constructor c = new Constructor(tClass);
  c.setPropertyUtils(new PropertyUtils() {
    @Override
    public Property getProperty(Class<?> type, String name) {
      if (name.indexOf('-') > -1) {
        name = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, name);
      }
      return super.getProperty(type, name);
    }
  });
  return new Yaml(c);
}
 
Example #13
Source File: ClashConfigure.java    From ClashForMagisk with GNU General Public License v3.0 5 votes vote down vote up
static ClashConfigure loadFromFile(File file) throws IOException {
    FileInputStream inputStream = new FileInputStream(file);
    Constructor constructor = new Constructor(ClashConfigure.class);

    constructor.setPropertyUtils(new PropertyUtils() {
        {
            setSkipMissingProperties(true);
        }

        @Override
        public Property getProperty(Class<?> type, String name) {
            switch (name) {
                case "port":
                    name = "portHttp";
                    break;
                case "socks-port":
                    name = "portSocks";
                    break;
                case "redir-port":
                    name = "portRedirect";
                    break;
            }
            return super.getProperty(type, name);
        }
    });

    ClashConfigure result = new Yaml(constructor).loadAs(inputStream, ClashConfigure.class);

    inputStream.close();

    return result;
}
 
Example #14
Source File: PropertyUtilsSharingTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testYamlConstructorWithPropertyUtils() {
    Constructor constructor1 = new Constructor();
    PropertyUtils pu = new PropertyUtils();
    constructor1.setPropertyUtils(pu);
    Yaml yaml = new Yaml(constructor1);
    assertSame(pu, yaml.constructor.getPropertyUtils());
    assertSame(pu, yaml.representer.getPropertyUtils());
}
 
Example #15
Source File: PropertyUtilsSharingTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testYamlRepresenterWithPropertyUtils() {
    Representer representer2 = new Representer();
    PropertyUtils pu = new PropertyUtils();
    representer2.setPropertyUtils(pu);
    Yaml yaml = new Yaml(representer2);
    assertSame(pu, yaml.constructor.getPropertyUtils());
    assertSame(pu, yaml.representer.getPropertyUtils());
}
 
Example #16
Source File: PropertyUtilsSharingTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
@Test
public void testYamlConstructorANDRepresenterWithPropertyUtils() {
    Constructor constructor = new Constructor();
    PropertyUtils pu_c = new PropertyUtils();
    constructor.setPropertyUtils(pu_c);
    Representer representer = new Representer();
    PropertyUtils pu_r = new PropertyUtils();
    representer.setPropertyUtils(pu_r);
    Yaml yaml = new Yaml(constructor, representer);
    assertSame(pu_c, yaml.constructor.getPropertyUtils());
    assertSame(pu_r, yaml.representer.getPropertyUtils());
}
 
Example #17
Source File: VersionedYamlDoc.java    From onedev with MIT License 4 votes vote down vote up
private static Representer newRepresenter() {
	Representer representer = new Representer() {
		
	    @SuppressWarnings("rawtypes")
		@Override
	    protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, 
	    		Object propertyValue,Tag customTag) {
	        if (propertyValue == null 
	        		|| propertyValue instanceof Collection && ((Collection) propertyValue).isEmpty()
	        		|| propertyValue instanceof Map && ((Map) propertyValue).isEmpty()) { 
	        	return null;
	        } else {
	        	return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
	        }
	    }

	};
	representer.setDefaultFlowStyle(FlowStyle.BLOCK);
	representer.setPropertyUtils(new PropertyUtils() {

		@Override
		protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess) {
			List<Property> properties = new ArrayList<>();
			Map<String, Integer> orders = new HashMap<>();
			if (type.getAnnotation(Editable.class) != null) {
				for (Method getter: BeanUtils.findGetters(type)) {
					Editable editable = getter.getAnnotation(Editable.class);
					Method setter = BeanUtils.findSetter(getter);
					if (editable != null && setter != null) {
						String propertyName = BeanUtils.getPropertyName(getter);
						try {
							properties.add(new MethodProperty(new PropertyDescriptor(propertyName, getter, setter)));
						} catch (IntrospectionException e) {
							throw new RuntimeException(e);
						}
						orders.put(propertyName, editable.order());
					}
				}
			}
			Collections.sort(properties, new Comparator<Property>() {

				@Override
				public int compare(Property o1, Property o2) {
					return orders.get(o1.getName()) - orders.get(o2.getName());
				}
				
			});
			return new LinkedHashSet<>(properties);
		}
		
	});
	return representer;
}
 
Example #18
Source File: BaseRepresenter.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
public void setPropertyUtils(PropertyUtils propertyUtils) {
    this.propertyUtils = propertyUtils;
    this.explicitPropertyUtils = true;
}
 
Example #19
Source File: BaseConstructor.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
public void setPropertyUtils(PropertyUtils propertyUtils) {
    this.propertyUtils = propertyUtils;
    explicitPropertyUtils = true;
}
 
Example #20
Source File: BaseRepresenter.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
public void setPropertyUtils(PropertyUtils propertyUtils) {
    this.propertyUtils = propertyUtils;
    this.explicitPropertyUtils = true;
}
 
Example #21
Source File: BaseConstructor.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
public void setPropertyUtils(PropertyUtils propertyUtils) {
    this.propertyUtils = propertyUtils;
    explicitPropertyUtils = true;
}
 
Example #22
Source File: TypeDescription.java    From thorntail with Apache License 2.0 4 votes vote down vote up
public void setPropertyUtils(PropertyUtils propertyUtils) {
    this.propertyUtils = propertyUtils;
}