Skip to main content
summaryrefslogtreecommitdiffstats
blob: 525b44a5d0badbf43c3a6ea93dd1d0ff2f118cad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/

package org.eclipse.internal.xpand2.ast;

import java.util.Set;

import org.eclipse.internal.xpand2.pr.ProtectedRegion;
import org.eclipse.internal.xpand2.pr.ProtectedRegionSyntaxException;
import org.eclipse.internal.xtend.expression.ast.Expression;
import org.eclipse.xpand2.XpandExecutionContext;
import org.eclipse.xtend.expression.AnalysationIssue;
import org.eclipse.xtend.expression.EvaluationException;

/**
 * *
 * 
 * @author Sven Efftinge (http://www.efftinge.de) *
 */
public class ProtectStatement extends StatementWithBody {

    private Expression commentStart;

    private Expression commentEnd;

    private Expression id;

    private boolean disable;

    public ProtectStatement( final Expression commentStart,
            final Expression commentEnd, final Statement[] body, final Expression id, final boolean disable) {
        super(body);
        this.commentStart = commentStart;
        this.commentEnd = commentEnd;
        this.id = id;
        this.disable = disable;
    }

    public Expression getCommentEnd() {
        return commentEnd;
    }

    public Expression getCommentStart() {
        return commentStart;
    }

    public Expression getId() {
        return id;
    }
    
    public boolean getDisable () {
        return disable;
    }

    public void analyzeInternal(final XpandExecutionContext ctx, final Set<AnalysationIssue> issues) {
        getCommentStart().analyze(ctx, issues);
        getCommentEnd().analyze(ctx, issues);
        getId().analyze(ctx, issues);

        for (int i = 0; i < body.length; i++) {
            body[i].analyze(ctx, issues);
        }
    }

    @Override
    public void evaluateInternal(final XpandExecutionContext ctx) {
        final String cStart = nullSafe(getCommentStart().evaluate(ctx));
        if (cStart == null)
            throw new EvaluationException("NullEvaluation!", getCommentStart(), ctx);
        final String cEnd = nullSafe(getCommentEnd().evaluate(ctx));
        if (cEnd == null)
            throw new EvaluationException("NullEvaluation!", getCommentEnd(), ctx);
        final String id = nullSafe(getId().evaluate(ctx));
        if (id == null)
            throw new EvaluationException("NullEvaluation!", getId(), ctx);

        ProtectedRegion region = null;
        if (ctx.getProtectedRegionResolver() != null) {
            region = ctx.getProtectedRegionResolver().getProtectedRegion(id.toString());
        } else
            throw new EvaluationException("No protected region resolver configured!",this,ctx);

        if (region == null) {
            region = ctx.getProtectedRegionResolver().createProtectedRegion(id, disable);
            ctx.getOutput().write(region.getStartString(cStart, cEnd));
            for (int i = 0; i < body.length; i++) {
                body[i].evaluate(ctx);
            }
            ctx.getOutput().write(region.getEndString(cStart, cEnd));
        } else {
            ctx.getOutput().write(region.getStartString(cStart, cEnd));
            try {
                ctx.getOutput().write(region.getBody(cStart, cEnd));
            } catch (final ProtectedRegionSyntaxException e) {
                throw new EvaluationException(e.getMessage(), getId(), ctx);
            }
            ctx.getOutput().write(region.getEndString(cStart, cEnd));
        }

    }

    private String nullSafe(final Object string) {
        return string != null ? string.toString() : "";
    }

}

Back to the top