Skip to content

Commit 50f30ba

Browse files
Load Content Types from YML #11288 (#11289)
1 parent 0d3b089 commit 50f30ba

File tree

23 files changed

+316
-83
lines changed

23 files changed

+316
-83
lines changed

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ jackson-dataformat-smile = { module = "com.fasterxml.jackson.dataformat:jackson-
9999
jackson-dataformat-cbor = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-cbor", version.ref = "jackson" }
100100
jackson-dataformat-yaml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml", version.ref = "jackson" }
101101

102+
snakeyaml = { module = "org.yaml:snakeyaml", version = "2.4" }
103+
102104
tika-core = { module = "org.apache.tika:tika-core", version.ref = "tika" }
103105
tika-bundlestandard = { module = "org.apache.tika:tika-bundle-standard", version.ref = "tika" }
104106
tika-parser-apple = { module = "org.apache.tika:tika-parser-apple-module", version.ref = "tika" }

modules/core/core-api/src/main/java/com/enonic/xp/descriptor/DescriptorKey.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ public final class DescriptorKey
1919

2020
private final String name;
2121

22-
private DescriptorKey( final ApplicationKey applicationKey, final String name )
22+
private final String extension;
23+
24+
private DescriptorKey( final ApplicationKey applicationKey, final String name, final String extension )
2325
{
2426
this.applicationKey = Objects.requireNonNull( applicationKey );
2527
this.name = CharacterChecker.check( name, "Not a valid name for DescriptorKey [" + name + "]" );
28+
this.extension = extension;
2629
}
2730

2831
public ApplicationKey getApplicationKey()
@@ -35,6 +38,11 @@ public String getName()
3538
return name;
3639
}
3740

41+
public String getExtension()
42+
{
43+
return extension;
44+
}
45+
3846
@Override
3947
public boolean equals( final Object o )
4048
{
@@ -47,13 +55,13 @@ public boolean equals( final Object o )
4755
return false;
4856
}
4957
final DescriptorKey that = (DescriptorKey) o;
50-
return applicationKey.equals( that.applicationKey ) && name.equals( that.name );
58+
return applicationKey.equals( that.applicationKey ) && name.equals( that.name ) && extension.equals( that.extension );
5159
}
5260

5361
@Override
5462
public int hashCode()
5563
{
56-
return Objects.hash( applicationKey, name );
64+
return Objects.hash( applicationKey, name, extension );
5765
}
5866

5967
@Override
@@ -68,11 +76,16 @@ public static DescriptorKey from( final String key )
6876
final int index = key.indexOf( SEPARATOR );
6977
final String applicationKey = index == -1 ? key : key.substring( 0, index );
7078
final String descriptorName = index == -1 ? "" : key.substring( index + 1 );
71-
return new DescriptorKey( ApplicationKey.from( applicationKey ), descriptorName );
79+
return DescriptorKey.from( ApplicationKey.from( applicationKey ), descriptorName );
7280
}
7381

7482
public static DescriptorKey from( final ApplicationKey applicationKey, final String descriptorName )
7583
{
76-
return new DescriptorKey( applicationKey, descriptorName );
84+
return new DescriptorKey( applicationKey, descriptorName, "xml" );
85+
}
86+
87+
public static DescriptorKey from( final ApplicationKey applicationKey, final String descriptorName, final String extension )
88+
{
89+
return new DescriptorKey( applicationKey, descriptorName, extension );
7790
}
7891
}

modules/core/core-api/src/main/java/com/enonic/xp/descriptor/DescriptorKeyLocator.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,26 @@ public final class DescriptorKeyLocator
1212
public DescriptorKeyLocator( final ResourceService service, final String path, final boolean optional )
1313
{
1414
this.service = service;
15-
this.pattern = "^" + path + "/(?<name>[^/]+)/\\k<name>\\.(?:xml" + ( optional ? "|js" : "" ) + ")$";
15+
this.pattern = "^" + path + "/(?<name>[^/]+)/\\k<name>\\.(?:xml|yml" + ( optional ? "|js" : "" ) + ")$";
1616
}
1717

1818
public DescriptorKeys findKeys( final ApplicationKey key )
1919
{
2020
return this.service.findFiles( key, this.pattern )
2121
.stream()
22-
.map( resource -> DescriptorKey.from( key, getNameWithoutExtension( resource.getName() ) ) )
22+
.map( resource -> {
23+
String nameWithoutExtension = getNameWithoutExtension( resource.getName() );
24+
String extension = resource.getName().length() - nameWithoutExtension.length() > 1 ? resource.getName()
25+
.substring( nameWithoutExtension.length() + 1 ) : "";
26+
if ( "yml".equals( extension ) )
27+
{
28+
return DescriptorKey.from( key, nameWithoutExtension, extension );
29+
}
30+
else
31+
{
32+
return DescriptorKey.from( key, nameWithoutExtension );
33+
}
34+
} )
2335
.collect( DescriptorKeys.collector() );
2436
}
2537

modules/core/core-api/src/main/java/com/enonic/xp/schema/content/ContentTypeName.java

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.enonic.xp.schema.content;
22

3+
import java.util.Objects;
4+
35
import com.enonic.xp.annotation.PublicApi;
46
import com.enonic.xp.app.ApplicationKey;
57
import com.enonic.xp.schema.BaseSchemaName;
@@ -56,14 +58,28 @@ public final class ContentTypeName
5658

5759
private static final ContentTypeName MEDIA_UNKNOWN = new ContentTypeName( ApplicationKey.MEDIA_MOD, "unknown" );
5860

61+
private final String extension;
62+
5963
private ContentTypeName( final String name )
64+
{
65+
this( name, "xml" );
66+
}
67+
68+
private ContentTypeName( final String name, final String extension )
6069
{
6170
super( name );
71+
this.extension = extension;
6272
}
6373

6474
private ContentTypeName( final ApplicationKey applicationKey, final String localName )
75+
{
76+
this( applicationKey, localName, "xml" );
77+
}
78+
79+
private ContentTypeName( final ApplicationKey applicationKey, final String localName, final String extension )
6580
{
6681
super( applicationKey, localName );
82+
this.extension = extension;
6783
}
6884

6985
public static ContentTypeName structured()
@@ -313,6 +329,11 @@ public boolean isUnknownMedia()
313329
return MEDIA_UNKNOWN.equals( this );
314330
}
315331

332+
public String getExtension()
333+
{
334+
return extension;
335+
}
336+
316337
@Override
317338
public int compareTo( final ContentTypeName that )
318339
{
@@ -321,11 +342,46 @@ public int compareTo( final ContentTypeName that )
321342

322343
public static ContentTypeName from( final ApplicationKey applicationKey, final String localName )
323344
{
324-
return new ContentTypeName( applicationKey, localName );
345+
return new ContentTypeName( applicationKey, localName, "xml" );
346+
}
347+
348+
public static ContentTypeName from( final ApplicationKey applicationKey, final String localName, final String extension )
349+
{
350+
return new ContentTypeName( applicationKey, localName, extension );
325351
}
326352

327353
public static ContentTypeName from( final String contentTypeName )
328354
{
329-
return new ContentTypeName( contentTypeName );
355+
return ContentTypeName.from( contentTypeName, "xml" );
356+
}
357+
358+
public static ContentTypeName from( final String contentTypeName, final String extension )
359+
{
360+
return new ContentTypeName( contentTypeName, extension );
361+
}
362+
363+
@Override
364+
public boolean equals( Object o )
365+
{
366+
if ( this == o )
367+
{
368+
return true;
369+
}
370+
if ( o == null || getClass() != o.getClass() )
371+
{
372+
return false;
373+
}
374+
if ( !super.equals( o ) )
375+
{
376+
return false;
377+
}
378+
ContentTypeName that = (ContentTypeName) o;
379+
return Objects.equals( extension, that.extension );
380+
}
381+
382+
@Override
383+
public int hashCode()
384+
{
385+
return Objects.hash( super.hashCode(), extension );
330386
}
331387
}

modules/core/core-api/src/main/java/com/enonic/xp/schema/content/ContentTypeService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ public interface ContentTypeService
1818
Set<String> getMimeTypes( ContentTypeNames napes );
1919

2020
ContentTypeValidationResult validate( ContentType type );
21+
22+
ContentType.Builder createContentTypeFromYml( String contentTypeYml, ApplicationKey applicationKey );
2123
}

modules/core/core-api/src/test/java/com/enonic/xp/descriptor/DescriptorKeysTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ public void testConcat()
6666
@Test
6767
void equalsContract()
6868
{
69-
EqualsVerifier.forClass( DescriptorKey.class ).withNonnullFields( "applicationKey", "name" ).verify();
69+
EqualsVerifier.forClass( DescriptorKey.class ).withNonnullFields( "applicationKey", "name", "extension" ).verify();
7070
}
7171
}

modules/core/core-api/src/test/java/com/enonic/xp/schema/content/ContentTypeNameTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class ContentTypeNameTest
1313
@Test
1414
public void equalsContract()
1515
{
16-
EqualsVerifier.forClass( ContentTypeName.class ).usingGetClass().withNonnullFields( "applicationKey", "localName" ).verify();
16+
EqualsVerifier.forClass( ContentTypeName.class ).usingGetClass().withNonnullFields( "applicationKey", "localName", "extension" ).verify();
1717
}
1818

1919
@Test

modules/core/core-app/src/main/java/com/enonic/xp/core/impl/app/DynamicResourceManager.java

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ final class DynamicResourceManager
3131
}
3232

3333
Resource createResource( final NodePath folderPath, final String name, final String resource )
34+
{
35+
return createResource( folderPath, name, resource, "xml" );
36+
}
37+
38+
Resource createResource( final NodePath folderPath, final String name, final String resource, final String extension )
3439
{
3540
return VirtualAppContext.createContext().callWith( () -> {
3641

@@ -49,34 +54,54 @@ Resource createResource( final NodePath folderPath, final String name, final Str
4954

5055
if ( resource != null )
5156
{
52-
resourceData.setXml( SchemaNodePropertyNames.RESOURCE, resource );
57+
if ( "xml".equals( extension ) )
58+
{
59+
resourceData.setXml( SchemaNodePropertyNames.RESOURCE, resource );
60+
}
61+
else
62+
{
63+
resourceData.setString( SchemaNodePropertyNames.RESOURCE, resource );
64+
}
5365
}
5466

5567
final Node schemaNode = nodeService.create( CreateNodeParams.create()
5668
.parent( resourceFolder.path() )
57-
.name( name + ".xml" )
69+
.name( name + "." + extension )
5870
.data( resourceData )
5971
.inheritPermissions( true )
6072
.refresh( RefreshMode.ALL )
6173
.build() );
6274

63-
return new NodeValueResource( ResourceKey.from( appKeyFromNodePath( folderPath), resourcePathFromNodePath( schemaNode.path() ) ), schemaNode );
75+
return new NodeValueResource(
76+
ResourceKey.from( appKeyFromNodePath( folderPath ), resourcePathFromNodePath( schemaNode.path() ) ), schemaNode );
6477
} );
6578
}
6679

6780
Resource updateResource( final NodePath folderPath, final String name, final String resource )
81+
{
82+
return updateResource( folderPath, name, resource, "xml" );
83+
}
84+
85+
Resource updateResource( final NodePath folderPath, final String name, final String resource, final String extension )
6886
{
6987
return VirtualAppContext.createContext().callWith( () -> {
7088

7189
final PropertyTree resourceData = new PropertyTree();
7290

7391
if ( resource != null )
7492
{
75-
resourceData.setXml( SchemaNodePropertyNames.RESOURCE, resource );
93+
if ( "xml".equals( extension ) )
94+
{
95+
resourceData.setXml( SchemaNodePropertyNames.RESOURCE, resource );
96+
}
97+
else
98+
{
99+
resourceData.setString( SchemaNodePropertyNames.RESOURCE, resource );
100+
}
76101
}
77102

78103
final Node schemaNode = nodeService.update( UpdateNodeParams.create()
79-
.path( new NodePath( folderPath, NodeName.from( name + ".xml" ) ) )
104+
.path( new NodePath( folderPath, NodeName.from( name + "." + extension ) ) )
80105
.editor( toBeEdited -> toBeEdited.data = resourceData )
81106
.refresh( RefreshMode.ALL )
82107
.build() );
@@ -87,35 +112,51 @@ Resource updateResource( final NodePath folderPath, final String name, final Str
87112
}
88113

89114
boolean resourceNodeExists( final NodePath folderPath, final String name )
115+
{
116+
return resourceNodeExists( folderPath, name, "xml" );
117+
}
118+
119+
boolean resourceNodeExists( final NodePath folderPath, final String name, final String extension )
90120
{
91121
return VirtualAppContext.createContext()
92-
.callWith( () -> nodeService.nodeExists( new NodePath( folderPath, NodeName.from( name + ".xml" ) ) ) );
122+
.callWith( () -> nodeService.nodeExists( new NodePath( folderPath, NodeName.from( name + "." + extension ) ) ) );
93123
}
94124

95125
Resource getResource( final NodePath folderPath, final String name )
126+
{
127+
return getResource( folderPath, name, "xml" );
128+
}
129+
130+
Resource getResource( final NodePath folderPath, final String name, final String extension )
96131
{
97132
return VirtualAppContext.createContext()
98-
.callWith( () -> resourceService.getResource(
99-
ResourceKey.from( appKeyFromNodePath( folderPath ), resourcePathFromNodePath( folderPath ) + "/" + name + ".xml" ) ) );
133+
.callWith( () -> resourceService.getResource( ResourceKey.from( appKeyFromNodePath( folderPath ),
134+
resourcePathFromNodePath( folderPath ) + "/" + name + "." +
135+
extension ) ) );
100136
}
101137

102138
List<Resource> listResources( final NodePath folderPath )
103139
{
104140
return VirtualAppContext.createContext()
105141
.callWith( () -> resourceService.findFiles( appKeyFromNodePath( folderPath ),
106-
resourcePathFromNodePath( folderPath ) + "/" + ".+/.+\\.xml" )
142+
resourcePathFromNodePath( folderPath ) + "/" + ".+/.+\\.(?:xml|yml)" )
107143
.stream()
108144
.map( resourceService::getResource )
109145
.collect( Collectors.toList() ) );
110146
}
111147

112148
boolean deleteResource( final NodePath folderPath, final String name, final boolean deleteFolder )
149+
{
150+
return deleteResource( folderPath, name, deleteFolder, "xml" );
151+
}
152+
153+
boolean deleteResource( final NodePath folderPath, final String name, final boolean deleteFolder, final String extension )
113154
{
114155
return VirtualAppContext.createContext()
115156
.callWith( () -> nodeService.delete( DeleteNodeParams.create()
116157
.nodePath( deleteFolder
117158
? folderPath
118-
: new NodePath( folderPath, NodeName.from( name + ".xml" ) ) )
159+
: new NodePath( folderPath, NodeName.from( name + "." + extension ) ) )
119160
.refresh( RefreshMode.ALL )
120161
.build() ) )
121162
.getNodeBranchEntries()

0 commit comments

Comments
 (0)