Skip to content

Commit

Permalink
Fragment support between Java / Template file
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Jan 19, 2023
1 parent 906a34f commit 0015610
Show file tree
Hide file tree
Showing 27 changed files with 1,054 additions and 300 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.qute.commons.datamodel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* Base class for data model fragment / template.
*
* @param <T> data model parameter.
*
* @see <a href=
* "https://quarkus.io/guides/qute-reference#fragments">Fragments</a>
* @see <a href=
* "https://quarkus.io/guides/qute-reference#type_safe_fragments">Type-safe
* Fragments</a>
*
* @author Angelo ZERR
*/

public class DataModelBaseTemplate<T extends DataModelParameter> {

private String sourceType;

private String sourceMethod;

private List<T> parameters;

private transient Map<String, T> parametersMap;

/**
* Returns the Java source type where this data model template is defined.
*
* @return the Java source type where this data model template is defined.
*/
public String getSourceType() {
return sourceType;
}

/**
* Set the Java source type where this data model template is defined.
*
* @param sourceType the Java source type where this data model template is
* defined.
*/
public void setSourceType(String sourceType) {
this.sourceType = sourceType;
}

/**
* Returns the Java source method where this data model template is defined and
* null otherwise.
*
* @return the Java source method where this data model template is defined and
* null otherwise.
*/
public String getSourceMethod() {
return sourceMethod;
}

/**
* Set the Java source method where this data model template is defined and null
* otherwise.
*
* @param sourceMethod the Java source method where this data model template is
* defined and null otherwise.
*/
public void setSourceMethod(String sourceMethod) {
this.sourceMethod = sourceMethod;
}

/**
* Returns the list of data model parameters.
*
* @return the list of data model parameters.
*/
public List<T> getParameters() {
return parameters;
}

/**
* Set the list of data model parameters.
*
* @param parameters the list of data model parameters.
*/
public void setParameters(List<T> parameters) {
this.parameters = parameters;
}

/**
* Returns the parameter from the given key and null otherwise.
*
* @param key the parameter key.
*
* @return the parameter from the given key and null otherwise.
*/
public T getParameter(String key) {
List<T> parameters = getParameters();
if (parameters == null) {
return null;
}
return getParametersMap().get(key);
}

/**
* Add the given parameter.
*
* @param parameter the parameter to add.
*/
public void addParameter(T parameter) {
if (parameters == null) {
parameters = new ArrayList<>();
}
parameters.add(parameter);
getParametersMap().put(parameter.getKey(), parameter);
}

private Map<String, T> getParametersMap() {
if (parametersMap == null) {
parametersMap = parameters.stream()
.collect(Collectors.toMap(DataModelParameter::getKey, Function.identity()));
}
return parametersMap;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.qute.commons.datamodel;

import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

/**
* Data model fragment hosts informations about the expected data model
* (parameters) for a given fragment.
*
* @param <T> data model parameter.
*
* @see <a href=
* "https://quarkus.io/guides/qute-reference#fragments">Fragments</a>
* @see <a href=
* "https://quarkus.io/guides/qute-reference#type_safe_fragments">Type-safe
* Fragments</a>
*
* @author Angelo ZERR
*/
public class DataModelFragment<T extends DataModelParameter> extends DataModelBaseTemplate<T> {

private String id;

/**
* Returns the fragment id.
*
* @return the fragment id.
*/
public String getId() {
return id;
}

/**
* Set the fragment id.
*
* @param id the fragment id
*/
public void setId(String id) {
this.id = id;
}

@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("id", this.id);
b.add("parameters", this.getParameters());
return b.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,24 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

/**
* Data model template host informations about the expected data model
* (parameters) for a given template.
*
* @author Angelo ZERR
*
* @param <T> data model parameter.
*
* @author Angelo ZERR
*/
public class DataModelTemplate<T extends DataModelParameter> {
public class DataModelTemplate<T extends DataModelParameter> extends DataModelBaseTemplate<T> {

private String templateUri;

private String sourceType;

private String sourceMethod;

private String sourceField;

private List<T> parameters;

private transient Map<String, T> parametersMap;
private List<DataModelFragment<T>> fragments;

/**
* Returns the template Uri.
Expand All @@ -59,47 +50,6 @@ public void setTemplateUri(String templateUri) {
this.templateUri = templateUri;
}

/**
* Returns the Java source type where this data model template is defined.
*
* @return the Java source type where this data model template is defined.
*/
public String getSourceType() {
return sourceType;
}

/**
* Set the Java source type where this data model template is defined.
*
* @param sourceType the Java source type where this data model template is
* defined.
*/
public void setSourceType(String sourceType) {
this.sourceType = sourceType;
}

/**
* Returns the Java source method where this data model template is defined and
* null otherwise.
*
* @return the Java source method where this data model template is defined and
* null otherwise.
*/
public String getSourceMethod() {
return sourceMethod;
}

/**
* Set the Java source method where this data model template is defined and null
* otherwise.
*
* @param sourceMethod the Java source method where this data model template is
* defined and null otherwise.
*/
public void setSourceMethod(String sourceMethod) {
this.sourceMethod = sourceMethod;
}

/**
* Returns the Java source field where this data model template is defined and
* null otherwise.
Expand All @@ -123,62 +73,50 @@ public void setSourceField(String sourceField) {
}

/**
* Returns the list of data model parameters.
* Add the given fragment.
*
* @return the list of data model parameters.
* @param fragment the fragment to add.
*/
public List<T> getParameters() {
return parameters;
public void addFragment(DataModelFragment<T> fragment) {
if (fragments == null) {
fragments = new ArrayList<>();
}
fragments.add(fragment);
}

/**
* Set the list of data model parameters.
*
* @param parameters the list of data model parameters.
*/
public void setParameters(List<T> parameters) {
this.parameters = parameters;
public List<DataModelFragment<T>> getFragments() {
return fragments;
}

/**
* Returns the parameter from the given key and null otherwise.
*
* @param key the parameter key.
*
* @return the parameter from the given key and null otherwise.
*/
public T getParameter(String key) {
List<T> parameters = getParameters();
if (parameters == null) {
return null;
}
return getParametersMap().get(key);
public void setFragments(List<DataModelFragment<T>> fragments) {
this.fragments = fragments;
}

public void addParameter(T parameter) {
if (parameters == null) {
parameters = new ArrayList<>();
public DataModelFragment<T> getFragment(String fragmentId) {
if (fragmentId == null) {
return null;
}
parameters.add(parameter);
getParametersMap().put(parameter.getKey(), parameter);
}

private Map<String, T> getParametersMap() {
if (parametersMap == null) {
parametersMap = parameters.stream()
.collect(Collectors.toMap(DataModelParameter::getKey, Function.identity()));
List<DataModelFragment<T>> fragments = getFragments();
if (fragments == null || fragments.isEmpty()) {
return null;
}
for (DataModelFragment<T> fragment : fragments) {
if (fragmentId.equals(fragment.getId())) {
return fragment;
}
}
return parametersMap;
return null;
}

@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("templateUri", this.templateUri);
b.add("sourceType", this.sourceType);
b.add("sourceMethod", this.sourceMethod);
b.add("sourceType", this.getSourceType());
b.add("sourceMethod", this.getSourceMethod());
b.add("sourceField", this.sourceField);
b.add("parameters", this.parameters);
b.add("parameters", this.getParameters());
b.add("fragments", this.getFragments());
return b.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class QuteJavaConstants {

public static final String OLD_CHECKED_TEMPLATE_ANNOTATION = "io.quarkus.qute.api.CheckedTemplate";

public static final String CHECKED_TEMPLATE_ANNOTATION_IGNORE_FRAGMENTS = "ignoreFragments";

// @TemplateExtension

public static final String TEMPLATE_EXTENSION_ANNOTATION = "io.quarkus.qute.TemplateExtension";
Expand Down
Loading

0 comments on commit 0015610

Please sign in to comment.