Skip to content

Fix Snakeyaml #439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion modello-plugins/modello-plugin-snakeyaml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.33</version>
<version>2.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private void writeClassReaders(ModelClass modelClass, JClass jClass, boolean roo

sc = unmarshall.getSourceCode();

sc.add("Parser parser = new ParserImpl( new StreamReader( reader ) );");
sc.add("Parser parser = new ParserImpl( new StreamReader( reader ), new LoaderOptions() );");

sc.add("return " + readerMethodName + "( parser, strict );");

Expand Down Expand Up @@ -288,6 +288,7 @@ private void generateSnakeYamlReader() throws ModelloException, IOException {
jClass.addImport("org.yaml.snakeyaml.parser.ParserException");
jClass.addImport("org.yaml.snakeyaml.parser.ParserImpl");
jClass.addImport("org.yaml.snakeyaml.reader.StreamReader");
jClass.addImport("org.yaml.snakeyaml.LoaderOptions");
jClass.addImport("java.io.InputStream");
jClass.addImport("java.io.InputStreamReader");
jClass.addImport("java.io.IOException");
Expand Down Expand Up @@ -820,6 +821,8 @@ private void writeHelpers(JClass jClass) {

sc = method.getSourceCode();

sc.add("if (!(event instanceof ScalarEvent))");
sc.addIndented("return false;");
sc.add("String currentName = ( (ScalarEvent) event ).getValue();");

sc.add("");
Expand Down Expand Up @@ -855,9 +858,17 @@ private void writeHelpers(JClass jClass) {

sc.add("if ( strict )");

sc.add("{");
sc.indent();
sc.add("if ( event instanceof ScalarEvent )");
sc.add("{");
sc.addIndented(
"throw new ParserException( \"Unrecognised tag: '\" + ( (ScalarEvent) event ).getValue() + \"'\", event.getStartMark(), \"\", null );");
sc.add("} else {");
sc.addIndented(
"return ; // throw new ParserException( \"Unrecognised : '\" + event.getEventId() + \"'\", event.getStartMark(), \"\", null );");
sc.add("}");
sc.unindent();
sc.add("}");

sc.add("");
Expand Down Expand Up @@ -1041,7 +1052,8 @@ private void writeNewLocation(String trackerVariable, JSourceCode sc) {
return;
}

String constr = "new " + locationTracker.getName() + "( parser.getLineNumber(), parser.getColumnNumber()";
String constr = "new " + locationTracker.getName()
+ "( parser.peekEvent().getStartMark().getLine(), parser.peekEvent().getStartMark().getColumn()";
constr += (sourceTracker != null) ? ", " + SOURCE_PARAM : "";
constr += " )";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ private void generateSnakeYamlWriter() throws ModelloException, IOException {

JClass jClass = new JClass(packageName + '.' + marshallerName);
initHeader(jClass);
suppressAllWarnings(objectModel, jClass);

jClass.addImport("org.yaml.snakeyaml.DumperOptions");
jClass.addImport("org.yaml.snakeyaml.DumperOptions.Version");
Expand Down Expand Up @@ -290,7 +291,8 @@ private void writeClass(ModelClass modelClass, JClass jClass) throws ModelloExce
sc.indent();

writeScalarKey(sc, fieldTagName);
sc.add("generator.emit( new SequenceStartEvent( null, null, true, null, null, false ) );");
sc.add(
"generator.emit( new SequenceStartEvent( null, null, true, null, null, FlowStyle.AUTO ) );");

sc.add("for ( " + toType + " o : " + value + " )");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.codehaus.modello.plugin.snakeyaml;

/*
* Copyright (c) 2004, Codehaus.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import java.util.Map;

import org.codehaus.modello.AbstractModelloJavaGeneratorTest;
import org.codehaus.modello.core.ModelloCore;
import org.codehaus.modello.model.Model;

public class SnakeYamlGeneratorTest extends AbstractModelloJavaGeneratorTest {
public SnakeYamlGeneratorTest() {
super("snakeyaml");
}

public void testYamlGenerator() throws Throwable {
ModelloCore modello = (ModelloCore) lookup(ModelloCore.ROLE);

Model model = modello.loadModel(getXmlResourceReader("/models/maven.mdo"));

Map<String, Object> parameters = getModelloParameters("4.0.0");

modello.generate(model, "java", parameters);
modello.generate(model, "snakeyaml-writer", parameters);
modello.generate(model, "snakeyaml-reader", parameters);

addDependency("org.yaml", "snakeyaml");
compileGeneratedSources();
}
}
Loading