public YamlMapping() { super("yaml"); setPomNames("pom.yaml", "pom.yml"); setAcceptLocationExtensions(".yaml", ".yml"); setAcceptOptionKeys("yaml:4.0.0"); setPriority(1); } }
@Override protected void addImplicitResolvers() { addImplicitResolver(Tag.MERGE, MERGE, "<"); addImplicitResolver(Tag.NULL, POM_NULL, "~n\0"); addImplicitResolver(Tag.NULL, EMPTY, null); } }
public Node representData(Object data) { return representMapping(Tag.MAP, toMap((Xpp3Dom) data), null); }
yamlConstructors.put(XPP3DOM_TAG, new ConstructXpp3Dom()); yamlClassConstructors.put(NodeId.mapping, new MavenObjectConstruct()); pomConstructors.put(Dependency.class, new ConstructDependency()); pomConstructors.put(Parent.class, new ConstructParent()); pomConstructors.put(Extension.class, new ConstructExtension()); pomConstructors.put(Plugin.class, new ConstructPlugin()); pomConstructors.put(ReportPlugin.class, new ConstructReportPlugin()); desc.putListPropertyType("developers", Developer.class); desc.putListPropertyType("contributors", Contributor.class); addTypeDescription(desc); addTypeDescription(desc); addTypeDescription(desc); desc.putListPropertyType("filters", String.class); desc.putListPropertyType("plugins", Plugin.class); addTypeDescription(desc); desc.putListPropertyType("filters", String.class); desc.putListPropertyType("plugins", Plugin.class); addTypeDescription(desc); addTypeDescription(desc); addTypeDescription(desc); addTypeDescription(desc);
public YamlModelReader() { ModelConstructor constructor = new ModelConstructor(); yaml = new Yaml(constructor, new Representer(), new DumperOptions(), new ModelResolver()); }
private Xpp3Dom toDom(Xpp3Dom parent, Map<Object, Object> map) { for (Map.Entry<Object, Object> entry : map.entrySet()) { String key = entry.getKey().toString(); Object entryValue = entry.getValue(); Xpp3Dom child = new Xpp3Dom(key); if (key.startsWith(ATTRIBUTE_PREFIX)) { toAttribute(parent, key.replace(ATTRIBUTE_PREFIX, ""), entryValue); continue; } // lists need the insertion of intermediate XML DOM nodes which hold the actual values if (entryValue instanceof List && !((List) entryValue).isEmpty()) { toDom(child, key, (List) entryValue); } else if (entryValue instanceof Map) { //noinspection unchecked child = toDom(child, (Map) entryValue); } else { // if not a list or map then copy the string value child.setValue(entryValue.toString()); } parent.addChild(child); } return parent; }
public void write(Writer output, Map<String, Object> o, Model model) throws IOException { //TODO improve SnakeYAML API (A. Somov) DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setIndent(2); dumperOptions.setWidth(80); Serializer serializer = new Serializer(new Emitter(output, dumperOptions), new ModelResolver(), dumperOptions, Tag.MAP); Representer representer = new ModelRepresenter(); try { serializer.open(); Node node = representer.represent(model); serializer.serialize(node); serializer.close(); } catch (IOException e) { throw new YAMLException(e); } } }
public ModelRepresenter() { this.representers.put(Xpp3Dom.class, new RepresentXpp3Dom()); Represent stringRepresenter = this.representers.get(String.class); this.representers.put(Boolean.class, stringRepresenter); this.multiRepresenters.put(Number.class, stringRepresenter); this.multiRepresenters.put(Date.class, stringRepresenter); this.multiRepresenters.put(Enum.class, stringRepresenter); this.multiRepresenters.put(Calendar.class, stringRepresenter); }
public Object construct(Node node) { Map<Object, Object> mapping = constructMapping((MappingNode) node); Xpp3Dom parent = new Xpp3Dom("configuration"); return toDom(parent, mapping); }
@Override public Object construct(Node node) { ScalarNode scalar = (ScalarNode) node; Coordinate coord = Coordinate.createCoordinate(scalar.getValue()); return coord.mergePlugin(new Plugin()); } }
@Override public Object construct(Node node) { ScalarNode scalar = (ScalarNode) node; Coordinate coord = Coordinate.createCoordinate(scalar.getValue()); return coord.mergeParent(new Parent()); } }
@Override public Object construct(Node node) { ScalarNode scalar = (ScalarNode) node; Coordinate coord = Coordinate.createCoordinate(scalar.getValue()); return coord.mergeReportPlugin(new ReportPlugin()); } }
@Override public Object construct(Node node) { ScalarNode scalar = (ScalarNode) node; Coordinate coord = Coordinate.createCoordinate(scalar.getValue()); return coord.mergeExtension(new Extension()); } }
public int compare(Property o1, Property o2) { // important go first for (String name : names) { int c = compareByName(o1, o2, name); if (c != 0) { return c; } } // all the rest return o1.compareTo(o2); }
@Override protected Set<Property> getProperties(Class<? extends Object> type) throws IntrospectionException { if (type.isAssignableFrom(Model.class)) { return sortTypeWithOrder(type, ORDER_MODEL); } else if (type.isAssignableFrom(Developer.class)) { return sortTypeWithOrder(type, ORDER_DEVELOPER); } else if (type.isAssignableFrom(Contributor.class)) { return sortTypeWithOrder(type, ORDER_CONTRIBUTOR); } else if (type.isAssignableFrom(Dependency.class)) { return sortTypeWithOrder(type, ORDER_DEPENDENCY); } else if (type.isAssignableFrom(Plugin.class)) { return sortTypeWithOrder(type, ORDER_PLUGIN); } else { return super.getProperties(type); } }
private Set<Property> sortTypeWithOrder(Class<? extends Object> type, List<String> order) throws IntrospectionException { Set<Property> standard = super.getProperties(type); Set<Property> sorted = new TreeSet<Property>(new ModelPropertyComparator(order)); sorted.addAll(standard); return sorted; }
public static Coordinate createCoordinate(String scalar) { Matcher matcher = ModelResolver.COORDINATE_PATTERN.matcher(scalar); if (matcher.matches()) { return new Coordinate(matcher.group("groupId"), matcher.group("artifactId"), matcher.group("version")); } else { matcher = ModelResolver.GROUP_NAME_PATTERN.matcher(scalar); if (!matcher.matches()) { throw new IllegalArgumentException("Unexpected node: " + scalar); } return new Coordinate(matcher.group("groupId"), matcher.group("artifactId"), null); } }
@Override public Object construct(Node node) { ScalarNode scalar = (ScalarNode) node; return createDependency(scalar.getValue(), new Dependency()); }
private List<Object> toList(Xpp3Dom node, String childName) { List<Object> list = new ArrayList<>(); int n = node.getChildCount(); for (int i = 0; i < n; i++) { Xpp3Dom child = node.getChild(i); if (!childName.equals(child.getName())) { throw new YAMLException(format("child name: '%s' does not match expected name: '%s' at node %s", child.getName(), childName, node)); } Object childValue = child.getValue(); if (childValue == null) { childValue = toMap(child); } list.add(childValue); } return list; } }