Skip to content
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
56 changes: 17 additions & 39 deletions htmlflow-core/src/main/java/htmlflow/HtmlFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package htmlflow;

import htmlflow.continuations.HtmlResolver;
import htmlflow.visitor.HtmlDocVisitor;
import htmlflow.visitor.HtmlViewVisitor;
import htmlflow.visitor.HtmlViewVisitorAsync;
Expand All @@ -49,8 +50,9 @@ private HtmlFlow() {}
* @param template An HtmlTemplate function, which depends on an HtmlView used to create HTMl
* elements.
* @param isIndented Set indentation on or off.
* @return The compiled chain, or the linked chain when it does not compile.
*/
private static PreprocessingVisitor preprocessing(
private static HtmlResolver preprocessing(
HtmlTemplate template,
boolean isIndented
) {
Expand All @@ -69,19 +71,17 @@ private static PreprocessingVisitor preprocessing(
* accumulated in the internal StringBuffer out.
*/
preView.getVisitor().resolve(null);
return pre;
return pre.compile();
}

/**
* Performs preprocessing for Micro Frontend (MFE) views. Creates a specialized visitor that collects
* MFE configurations and processes them to enable MFE support in the view.
*
* @param template An HtmlTemplate function that defines the HTML structure with MFE components
* @return A PreprocessingVisitorMfe containing the processed MFE components
* @return The compiled chain, or the linked chain when it does not compile.
*/
private static PreprocessingVisitorMfe preprocessingMfe(
HtmlTemplate template
) {
private static HtmlResolver preprocessingMfe(HtmlTemplate template) {
PreprocessingVisitorMfe processView = new PreprocessingVisitorMfe(
false
);
Expand All @@ -94,7 +94,7 @@ private static PreprocessingVisitorMfe preprocessingMfe(
template.resolve(preView);
// second process
preView.getVisitor().resolve(null);
return processView;
return processView.compile();
}

/**
Expand Down Expand Up @@ -186,22 +186,12 @@ static <M> HtmlView<M> view(
boolean preEncoding,
boolean mfeEnabled
) {
if (preEncoding && mfeEnabled) {
PreprocessingVisitorMfe processView = preprocessingMfe(template);
return new HtmlView<>(
() ->
new HtmlViewVisitor(
out,
isIndented,
processView.getFirst()
),
template,
threadSafe
);
} else if (preEncoding) {
PreprocessingVisitor pre = preprocessing(template, isIndented);
if (preEncoding) {
HtmlResolver resolver = mfeEnabled
? preprocessingMfe(template)
: preprocessing(template, isIndented);
return new HtmlView<>(
(() -> new HtmlViewVisitor(out, isIndented, pre.getFirst())),
() -> new HtmlViewVisitor(out, isIndented, resolver),
template,
threadSafe
);
Expand Down Expand Up @@ -260,32 +250,20 @@ static <M> HtmlView<M> view(
boolean preEncoding,
boolean mfeEnabled
) {
if (preEncoding && mfeEnabled) {
PreprocessingVisitorMfe processView = preprocessingMfe(template);
if (preEncoding) {
HtmlResolver resolver = mfeEnabled
? preprocessingMfe(template)
: preprocessing(template, isIndented);
return new HtmlView<>(
() ->
new HtmlViewVisitor(
new StringBuilder(),
isIndented,
processView.getFirst()
resolver
),
template,
threadSafe
);
} else if (preEncoding) {
PreprocessingVisitor pre = preprocessing(template, isIndented);
return new HtmlView<>(
(
() ->
new HtmlViewVisitor(
new StringBuilder(),
isIndented,
pre.getFirst()
)
),
template,
threadSafe
);
} else {
return new HtmlViewHot<>(
() -> new HtmlViewVisitorHot(new StringBuilder(), isIndented),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* Base class for a linked list of nodes, corresponding to HtmlContinuation objects.
* HtmlContinuation is responsible for emitting an HTML block and call the next node.
*/
public abstract class HtmlContinuation {
public abstract class HtmlContinuation implements HtmlResolver {

/** A negative number means that should be ignored. */
final int currentDepth;
Expand Down Expand Up @@ -74,17 +74,19 @@ public HtmlContinuation getNext() {
}

/**
* Executes this continuation and calls the next one if exist.
* Emits this block and calls the next continuation if it exists.
*
* @param model
*/
public abstract void execute(Object model);
@Override
public abstract void resolve(Object model);

/**
* Creates a copy of this HtmlContinuation with a new visitor
*
* @param visitor
*/
@Override
public abstract HtmlContinuation copy(HtmlVisitor visitor);

@SuppressWarnings("squid:S3011")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public HtmlContinuationAsync(
}

@Override
public final void execute(Object model) {
public final void resolve(Object model) {
if (currentDepth >= 0) {
this.visitor.setIsClosed(isClosed);
this.visitor.setDepth(currentDepth);
Expand All @@ -41,7 +41,7 @@ public final void execute(Object model) {
(T) model,
() -> {
if (next != null) {
next.execute(model);
next.resolve(model);
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public HtmlContinuationAsyncTerminationNode(CompletableFuture<Void> cf) {
this.cf = cf;
}

public void execute(Object model) {
public void resolve(Object model) {
cf.complete(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ protected HtmlContinuationSync(
* @param model
*/
@Override
public final void execute(Object model) {
public final void resolve(Object model) {
if (currentDepth >= 0) {
this.visitor.setIsClosed(isClosed);
this.visitor.setDepth(currentDepth);
}
emitHtml(model);
if (next != null) {
next.execute(model);
next.resolve(model);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* MIT License
*
* Copyright (c) 2025, xmlet HtmlFlow
*
* 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.
*/

package htmlflow.continuations;

import htmlflow.visitor.HtmlVisitor;
import java.util.function.Function;

/**
* HtmlContinuation for a loop body that runs once per item. The body carries its own leading
* indentation.
*
* @author Bernardo Pereira
*/
public class HtmlContinuationSyncForEach<M, E> extends HtmlContinuationSync {

private final Function<M, ? extends Iterable<E>> items;

private final HtmlResolver item;

public HtmlContinuationSyncForEach(
Function<M, ? extends Iterable<E>> items,
HtmlResolver item,
HtmlVisitor visitor,
HtmlContinuation next
) {
super(-1, false, visitor, next);
this.items = items;
this.item = item;
}

@SuppressWarnings("unchecked")
@Override
protected final void emitHtml(Object model) {
for (E e : items.apply((M) model)) item.resolve(e);
}

@Override
public HtmlContinuation copy(HtmlVisitor v) {
return new HtmlContinuationSyncForEach<>(
items,
item.copy(v),
v,
next != null ? next.copy(v) : null
);
}
}
Loading
Loading