Java Code Examples for org.yaml.snakeyaml.representer.Representer#setPropertyUtils()

The following examples show how to use org.yaml.snakeyaml.representer.Representer#setPropertyUtils() . 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
/**
 * 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 2
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 3
Source File: Yaml.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create Yaml instance. It is safe to create a few instances and use them
 * in different Threads.
 * 
 * @param constructor
 *            BaseConstructor to construct incoming documents
 * @param representer
 *            Representer to emit outgoing objects
 * @param dumperOptions
 *            DumperOptions to configure outgoing objects
 * @param resolver
 *            Resolver to detect implicit type
 */
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
        Resolver resolver) {
    if (!constructor.isExplicitPropertyUtils()) {
        constructor.setPropertyUtils(representer.getPropertyUtils());
    } else if (!representer.isExplicitPropertyUtils()) {
        representer.setPropertyUtils(constructor.getPropertyUtils());
    }
    this.constructor = constructor;
    representer.setDefaultFlowStyle(dumperOptions.getDefaultFlowStyle());
    representer.setDefaultScalarStyle(dumperOptions.getDefaultScalarStyle());
    representer.getPropertyUtils().setAllowReadOnlyProperties(
            dumperOptions.isAllowReadOnlyProperties());
    representer.setTimeZone(dumperOptions.getTimeZone());
    this.representer = representer;
    this.dumperOptions = dumperOptions;
    this.resolver = resolver;
    this.name = "Yaml:" + System.identityHashCode(this);
}
 
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: Yaml.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
/**
 * Create Yaml instance. It is safe to create a few instances and use them
 * in different Threads.
 * 
 * @param constructor
 *            BaseConstructor to construct incoming documents
 * @param representer
 *            Representer to emit outgoing objects
 * @param dumperOptions
 *            DumperOptions to configure outgoing objects
 * @param resolver
 *            Resolver to detect implicit type
 */
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
        Resolver resolver) {
    if (!constructor.isExplicitPropertyUtils()) {
        constructor.setPropertyUtils(representer.getPropertyUtils());
    } else if (!representer.isExplicitPropertyUtils()) {
        representer.setPropertyUtils(constructor.getPropertyUtils());
    }
    this.constructor = constructor;
    representer.setDefaultFlowStyle(dumperOptions.getDefaultFlowStyle());
    representer.setDefaultScalarStyle(dumperOptions.getDefaultScalarStyle());
    representer.getPropertyUtils().setAllowReadOnlyProperties(
            dumperOptions.isAllowReadOnlyProperties());
    representer.setTimeZone(dumperOptions.getTimeZone());
    this.representer = representer;
    this.dumperOptions = dumperOptions;
    this.resolver = resolver;
    this.name = "Yaml:" + System.identityHashCode(this);
}
 
Example 6
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 7
Source File: Yaml.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create Yaml instance. It is safe to create a few instances and use them
 * in different Threads.
 *
 * @param constructor
 *            BaseConstructor to construct incoming documents
 * @param representer
 *            Representer to emit outgoing objects
 * @param dumperOptions
 *            DumperOptions to configure outgoing objects
 * @param resolver
 *            Resolver to detect implicit type
 */
public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
            Resolver resolver) {
    if (!constructor.isExplicitPropertyUtils()) {
        constructor.setPropertyUtils(representer.getPropertyUtils());
    } else if (!representer.isExplicitPropertyUtils()) {
        representer.setPropertyUtils(constructor.getPropertyUtils());
    }
    this.constructor = constructor;
    representer.setDefaultFlowStyle(dumperOptions.getDefaultFlowStyle());
    representer.setDefaultScalarStyle(dumperOptions.getDefaultScalarStyle());
    representer.getPropertyUtils().setAllowReadOnlyProperties(
            dumperOptions.isAllowReadOnlyProperties());
    representer.setTimeZone(dumperOptions.getTimeZone());
    this.representer = representer;
    this.dumperOptions = dumperOptions;
    this.resolver = resolver;
    this.name = "Yaml:" + System.identityHashCode(this);
}
 
Example 8
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 9
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 10
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 11
Source File: AbstractChartWriter.java    From microbean-helm with Apache License 2.0 3 votes vote down vote up
/**
 * Creates and returns a new {@link Yaml} instance for (optional)
 * use in writing {@link ConfigOrBuilder} and {@link
 * MetadataOrBuilder} objects.
 *
 * <p>This method never returns {@code null}.</p>
 *
 * <p>Overrides of this method must not return {@code null}.</p>
 *
 * <p>Behavior is undefined if overrides of this method interact
 * with other methods defined by this class.</p>
 *
 * @return a non-{@code null} {@link Yaml} instance
 */
protected Yaml createYaml() {
  final Representer representer = new TerseRepresenter();
  representer.setPropertyUtils(new CustomPropertyUtils());
  final DumperOptions options = new DumperOptions();
  options.setAllowReadOnlyProperties(true);
  return new Yaml(new SafeConstructor(), representer, options);
}