CustomConstructor(Class<?> theRoot) { super(theRoot); TypeDescription seedDesc = new TypeDescription(ParameterizedClass.class); seedDesc.putMapPropertyType("parameters", String.class, String.class); addTypeDescription(seedDesc); }
/** * Make YAML aware how to parse a custom Class. If there is no root Class assigned in constructor * then the 'root' property of this definition is respected. * * @param definition to be added to the Constructor * @return the previous value associated with <tt>definition</tt>, or <tt>null</tt> if there was * no mapping for <tt>definition</tt>. */ public TypeDescription addTypeDescription(TypeDescription definition) { if (definition == null) { throw new NullPointerException("TypeDescription is required."); } Tag tag = definition.getTag(); typeTags.put(tag, definition.getType()); return typeDefinitions.put(definition.getType(), definition); }
private Yaml prepareLoader(ClassLoader classLoader) { final Constructor constructor = new MyConstructor(Holder.class, classLoader); final TypeDescription typeDescription = new TypeDescription(Holder.class); typeDescription.putListPropertyType("prototypes", DefaultEntityPrototype.class); constructor.addTypeDescription(typeDescription); final Loader loader = new Loader(constructor); final Yaml yaml = new Yaml(loader); return yaml; }
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); }
public static XmlSuite parse(String filePath, InputStream is) throws FileNotFoundException { Constructor constructor = new TestNGConstructor(XmlSuite.class); { TypeDescription suiteDescription = new TypeDescription(XmlSuite.class); suiteDescription.putListPropertyType("packages", XmlPackage.class); suiteDescription.putListPropertyType("listeners", String.class); suiteDescription.putListPropertyType("tests", XmlTest.class); suiteDescription.putListPropertyType("method-selectors", XmlMethodSelector.class); constructor.addTypeDescription(suiteDescription); } { TypeDescription testDescription = new TypeDescription(XmlTest.class); testDescription.putListPropertyType("classes", XmlClass.class); testDescription.putMapPropertyType("metaGroups", String.class, List.class); testDescription.putListPropertyType("method-selectors", XmlMethodSelector.class); constructor.addTypeDescription(testDescription); } org.yaml.snakeyaml.Yaml y = new org.yaml.snakeyaml.Yaml(constructor); if (is == null) is = new FileInputStream(new File(filePath)); XmlSuite result = (XmlSuite) y.load(is); result.setFileName(filePath); // DEBUG // System.out.println("[Yaml] " + result.toXml()); // Adjust XmlTest parents and indices for (XmlTest t : result.getTests()) { t.setSuite(result); int index = 0; for (XmlClass c : t.getClasses()) { c.setIndex(index++); } } return result; }
protected Object constructJavaBean2ndStep(MappingNode node, Object object) { flattenMapping(node); Class<? extends Object> beanType = node.getType(); List<NodeTuple> nodeValue = node.getValue(); for (NodeTuple tuple : nodeValue) { ScalarNode keyNode; if (tuple.getKeyNode() instanceof ScalarNode) { // key must be scalar keyNode = (ScalarNode) tuple.getKeyNode(); } else { throw new YAMLException("Keys must be scalars but found: " + tuple.getKeyNode()); } Node valueNode = tuple.getValueNode(); // keys can only be Strings keyNode.setType(String.class); String key = (String) constructObject(keyNode); try { Property property = getProperty(beanType, key); valueNode.setType(property.getType()); TypeDescription memberDescription = typeDefinitions.get(beanType); boolean typeDetected = false; if (memberDescription != null) { switch (valueNode.getNodeId()) { case sequence: SequenceNode snode = (SequenceNode) valueNode; Class<? extends Object> memberType = memberDescription.getListPropertyType(key); if (memberType != null) { snode.setListType(memberType); typeDetected = true; } else if (property.getType().isArray()) { snode.setListType(property.getType().getComponentType()); typeDetected = true; } break; case mapping: MappingNode mnode = (MappingNode) valueNode; Class<? extends Object> keyType = memberDescription.getMapKeyType(key); if (keyType != null) { mnode.setTypes(keyType, memberDescription.getMapValueType(key)); typeDetected = true; } break; default: // scalar } } if (!typeDetected && valueNode.getNodeId() != NodeId.scalar) { // only if there is no explicit TypeDescription Class<?>[] arguments = property.getActualTypeArguments(); if (arguments != null) { // type safe (generic) collection may contain the // proper class if (valueNode.getNodeId() == NodeId.sequence) { Class<?> t = arguments[0]; SequenceNode snode = (SequenceNode) valueNode; snode.setListType(t); } else if (valueNode.getTag().equals(Tag.SET)) { Class<?> t = arguments[0]; MappingNode mnode = (MappingNode) valueNode; mnode.setOnlyKeyType(t); mnode.setUseClassConstructor(true); } else if (property.getType().isAssignableFrom(Map.class)) { Class<?> ketType = arguments[0]; Class<?> valueType = arguments[1]; MappingNode mnode = (MappingNode) valueNode; mnode.setTypes(ketType, valueType); mnode.setUseClassConstructor(true); } else { // the type for collection entries cannot be // detected } } } Object value = constructObject(valueNode); property.set(object, value); } catch (Exception e) { throw new YAMLException( "Cannot create property=" + key + " for JavaBean=" + object + "; " + e.getMessage(), e); } } return object; }
public ModelConstructor() { super(Model.class); yamlConstructors.put(Tags.MAP, new ConstructXpp3Dom()); TypeDescription desc; desc = new TypeDescription(Model.class); desc.putListPropertyType("licenses", License.class); desc.putListPropertyType("mailingLists", MailingList.class); desc.putListPropertyType("dependencies", Dependency.class); desc.putListPropertyType("modules", String.class); desc.putListPropertyType("profiles", Profile.class); desc.putListPropertyType("repositories", Repository.class); desc.putListPropertyType("pluginRepositories", Repository.class); desc.putListPropertyType("developers", Developer.class); desc.putListPropertyType("contributors", Contributor.class); addTypeDescription(desc); desc = new TypeDescription(Dependency.class); desc.putListPropertyType("exclusions", Exclusion.class); addTypeDescription(desc); desc = new TypeDescription(DependencyManagement.class); desc.putListPropertyType("dependencies", Dependency.class); addTypeDescription(desc); desc = new TypeDescription(Build.class); desc.putListPropertyType("extensions", Extension.class); desc.putListPropertyType("resources", Resource.class); desc.putListPropertyType("testResources", Resource.class); desc.putListPropertyType("filters", String.class); desc.putListPropertyType("plugins", Plugin.class); addTypeDescription(desc); desc = new TypeDescription(BuildBase.class); desc.putListPropertyType("resources", Resource.class); desc.putListPropertyType("testResources", Resource.class); desc.putListPropertyType("filters", String.class); desc.putListPropertyType("plugins", Plugin.class); addTypeDescription(desc); desc = new TypeDescription(PluginManagement.class); desc.putListPropertyType("plugins", Plugin.class); addTypeDescription(desc); desc = new TypeDescription(Plugin.class); desc.putListPropertyType("executions", PluginExecution.class); addTypeDescription(desc); desc = new TypeDescription(PluginExecution.class); desc.putListPropertyType("goals", String.class); addTypeDescription(desc); desc = new TypeDescription(Reporting.class); desc.putListPropertyType("plugins", ReportPlugin.class); addTypeDescription(desc); desc = new TypeDescription(ReportPlugin.class); desc.putListPropertyType("reportSets", ReportSet.class); addTypeDescription(desc); desc = new TypeDescription(ReportSet.class); desc.putListPropertyType("reports", String.class); addTypeDescription(desc); desc = new TypeDescription(CiManagement.class); desc.putListPropertyType("notifiers", Notifier.class); addTypeDescription(desc); desc = new TypeDescription(Developer.class); desc.putListPropertyType("roles", String.class); addTypeDescription(desc); desc = new TypeDescription(Contributor.class); desc.putListPropertyType("roles", String.class); addTypeDescription(desc); desc = new TypeDescription(MailingList.class); desc.putListPropertyType("otherArchives", String.class); addTypeDescription(desc); // Simple types addTypeDescription(new TypeDescription(DistributionManagement.class)); addTypeDescription(new TypeDescription(Scm.class)); addTypeDescription(new TypeDescription(IssueManagement.class)); addTypeDescription(new TypeDescription(Parent.class)); addTypeDescription(new TypeDescription(Organization.class)); }