Skip to content

Commit 6d247da

Browse files
committed
Update code with checking matcher
1 parent 5d7be68 commit 6d247da

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

core/esmf-aspect-model-java-generator/src/main/java/org/eclipse/esmf/aspectmodel/java/AspectModelJavaUtil.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.stream.IntStream;
2929
import java.util.stream.Stream;
3030

31+
import javax.xml.datatype.DatatypeConstants;
32+
3133
import org.eclipse.esmf.aspectmodel.VersionInfo;
3234
import org.eclipse.esmf.aspectmodel.java.exception.CodeGenerationException;
3335
import org.eclipse.esmf.aspectmodel.visitor.AspectStreamTraversalVisitor;
@@ -474,9 +476,7 @@ public static String generateInitializer( final Property property, final String
474476
final Class<?> result = SammXsdType.getJavaTypeForMetaModelType( typeResource );
475477
codeGenerationConfig.importTracker().importExplicit( result );
476478
final String initializer = valueInitializer.apply( typeResource, value );
477-
return property.isOptional()
478-
? optionalExpression( initializer, codeGenerationConfig )
479-
: initializer;
479+
return optionalExpression( initializer, codeGenerationConfig );
480480
} ).orElseThrow( () -> new CodeGenerationException(
481481
"The Either Characteristic is not allowed for Properties used as elements in a StructuredValue" ) );
482482
}
@@ -683,6 +683,21 @@ public static String optionalExpression(
683683
final JavaCodeGenerationConfig codeGenerationConfig
684684
) {
685685
codeGenerationConfig.importTracker().importExplicit( Optional.class );
686-
return "Optional.of(" + expression + ")";
686+
codeGenerationConfig.importTracker().importExplicit( DatatypeConstants.class );
687+
688+
// Check if this is for XMLGregorianCalendar date handling
689+
if ( expression.contains( "matcher.group(1)" ) && expression.contains( "XMLGregorianCalendarDate" ) ) {
690+
return "Optional.ofNullable(matcher.group(1))" +
691+
".filter(v -> v != null)" +
692+
".map(v -> Integer.valueOf(v))" +
693+
".map(v -> _datatypeFactory.newXMLGregorianCalendarDate(v, " +
694+
"DatatypeConstants.FIELD_UNDEFINED, " +
695+
"DatatypeConstants.FIELD_UNDEFINED, " +
696+
"DatatypeConstants.FIELD_UNDEFINED))";
697+
}
698+
699+
// Default case - use ofNullable with empty check
700+
return "Optional.ofNullable(" + expression + ")" +
701+
".filter(v -> v != null)";
687702
}
688703
}

core/esmf-aspect-model-java-generator/src/main/java/org/eclipse/esmf/aspectmodel/java/StructuredValuePropertiesDeconstructor.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package org.eclipse.esmf.aspectmodel.java;
1515

1616
import java.util.List;
17+
import java.util.Optional;
1718
import java.util.stream.Collectors;
1819
import java.util.stream.Stream;
1920

@@ -22,6 +23,7 @@
2223
import org.eclipse.esmf.metamodel.HasProperties;
2324
import org.eclipse.esmf.metamodel.Property;
2425
import org.eclipse.esmf.metamodel.characteristic.StructuredValue;
26+
import org.eclipse.esmf.metamodel.impl.DefaultProperty;
2527

2628
import org.apache.commons.lang3.StringUtils;
2729

@@ -116,6 +118,18 @@ private List<Property> getAllProperties( final List<DeconstructionSet> deconstru
116118
return deconstructionSets.stream()
117119
.map( DeconstructionSet::elementProperties )
118120
.flatMap( List::stream )
121+
.map( property -> (Property) new DefaultProperty(
122+
MetaModelBaseAttributes.builder()
123+
.fromModelElement( property )
124+
.build(),
125+
property.getCharacteristic(),
126+
property.getExampleValue(),
127+
true, // making it optional
128+
property.isNotInPayload(),
129+
Optional.of( property.getPayloadName() ),
130+
property.isAbstract(),
131+
property.getExtends()
132+
) )
119133
.toList();
120134
}
121135

core/esmf-aspect-model-java-generator/src/test/java/org/eclipse/esmf/aspectmodel/java/AspectModelJavaGeneratorTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,9 @@ void testGenerateAspectWithOptionalPropertyAndEntityWithSeparateFiles() throws I
258258
ConstructorDeclaration constructor = unit.findFirst( ConstructorDeclaration.class )
259259
.orElseThrow( () -> new AssertionError( "Constructor not found in ProductionPeriodEntity" ) );
260260
String constructorBody = constructor.getBody().toString();
261-
assertThat( constructorBody ).contains( "Optional.of(_datatypeFactory.newXMLGregorianCalendarDate(" );
261+
assertThat( constructorBody ).contains( "Optional.ofNullable(" );
262+
assertThat( constructorBody ).contains( ".filter(v -> v != null)" );
263+
assertThat( constructorBody ).contains( "_datatypeFactory.newXMLGregorianCalendarDate(" );
262264
}
263265

264266
@Test

core/esmf-test-aspect-models/src/main/resources/valid/org.eclipse.esmf.test/1.0.0/AspectWithOptionalPropertiesAndEntityWithSeparateFiles.ttl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
samm:preferredName "Year with Optional Month"@en ;
5656
samm:dataType xsd:string ;
5757
samm-c:deconstructionRule "^([0-9]{4})(?:-(0[1-9]|1[0-2]))?" ;
58-
samm-c:elements ( "^" :year "-" [ samm:property :month; samm:optional true ] "$" ) .
58+
samm-c:elements ( "^" :year "-" :month "$" ) .
5959

6060
:YearAndOptionalMonthRegex a samm-c:RegularExpressionConstraint ;
6161
samm:value "^([0-9]{4})(?:-(0[1-9]|1[0-2]))?" .
@@ -64,6 +64,7 @@
6464
samm:characteristic :YearCharacteristic .
6565

6666
:month a samm:Property ;
67+
samm:optional true ;
6768
samm:characteristic :MonthCharacteristic .
6869

6970
:YearCharacteristic a samm:Characteristic ;

0 commit comments

Comments
 (0)