Skip to content

Commit e940c5d

Browse files
committed
provided way to set special variables in //#local
1 parent b535553 commit e940c5d

File tree

7 files changed

+116
-29
lines changed

7 files changed

+116
-29
lines changed

jcp/src/main/java/com/igormaznitsa/jcp/context/JCPSpecialVariableProcessor.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,24 +187,35 @@ public Value getVariable(final String varName, final PreprocessorContext context
187187
}
188188
}
189189

190+
private void assertNotGlobalPhase(final String varName, final PreprocessorContext context) {
191+
if (context.getPreprocessingState().isGlobalPhase()) {
192+
throw context.makeException(
193+
"Variable '" + varName + "' is not allowed to set during global phase", null);
194+
}
195+
}
196+
190197
@Override
191198
public void setVariable(final String varName, final Value value,
192199
final PreprocessorContext context) {
193200
final PreprocessingState state = context.getPreprocessingState();
194201
switch (varName) {
195202
case VAR_JCP_BUFFER_ALL: {
203+
this.assertNotGlobalPhase(varName, context);
196204
state.setBufferText(value.toString());
197205
}
198206
break;
199207
case VAR_JCP_BUFFER_POSTFIX: {
208+
this.assertNotGlobalPhase(varName, context);
200209
state.setBufferText(value.toString(), PreprocessingState.PrinterType.POSTFIX);
201210
}
202211
break;
203212
case VAR_JCP_BUFFER_MIDDLE: {
213+
this.assertNotGlobalPhase(varName, context);
204214
state.setBufferText(value.toString(), PreprocessingState.PrinterType.NORMAL);
205215
}
206216
break;
207217
case VAR_JCP_BUFFER_PREFIX: {
218+
this.assertNotGlobalPhase(varName, context);
208219
state.setBufferText(value.toString(), PreprocessingState.PrinterType.PREFIX);
209220
}
210221
break;

jcp/src/main/java/com/igormaznitsa/jcp/context/PreprocessorContext.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ public Set<FileInfoContainer> findPreprocessedResources() {
296296

297297
/**
298298
* Send notification about context start to all registered listeners.
299+
*
299300
* @param initedList list accumulating successfully processed listeners, must not be immutable and must not be null
300301
* @since 7.2.0
301302
*/
@@ -639,14 +640,19 @@ public PreprocessorContext setLocalVariable(final String name, final Value value
639640
throw makeException("Not defined variable name", null);
640641
}
641642

642-
if (mapVariableNameToSpecialVarProcessor.containsKey(normalized) ||
643-
globalVarTable.containsKey(normalized)) {
643+
if (this.mapVariableNameToSpecialVarProcessor.containsKey(normalized)) {
644+
final SpecialVariableProcessor enabledProcessor =
645+
findAllowedSpecialVariableProcessor(normalized)
646+
.orElseThrow(() -> this.makeException("Set of local variable '" + normalized +
647+
"' is not allowed in the point by its processor", null));
648+
enabledProcessor.setVariable(normalized, value, this);
649+
} else if (this.globalVarTable.containsKey(normalized)) {
644650
throw makeException(
645-
"Attempting to set either a global variable or a special variable as a local one [" +
651+
"Cannot override global variable with a local variable of the same name [" +
646652
normalized + ']', null);
653+
} else {
654+
this.localVarTable.put(normalized, value);
647655
}
648-
649-
localVarTable.put(normalized, value);
650656
return this;
651657
}
652658

@@ -754,10 +760,22 @@ public boolean containsLocalVariable(final String name) {
754760
* @return this preprocessor context
755761
*/
756762
public PreprocessorContext clearLocalVariables() {
757-
localVarTable.clear();
763+
this.localVarTable.clear();
758764
return this;
759765
}
760766

767+
private Optional<SpecialVariableProcessor> findAllowedSpecialVariableProcessor(
768+
final String normalizedName) {
769+
return this.mapVariableNameToSpecialVarProcessor.get(normalizedName)
770+
.stream()
771+
.filter(x -> x.isAllowed(
772+
findLastActiveFileContainer(this).orElse(null),
773+
this.getPreprocessingState().findLastPositionInfoInStack().orElse(null),
774+
this,
775+
this.getPreprocessingState()
776+
)).findFirst();
777+
}
778+
761779
/**
762780
* Set a global variable value
763781
*
@@ -779,22 +797,12 @@ public PreprocessorContext setGlobalVariable(final String name, final Value valu
779797

780798
if (this.mapVariableNameToSpecialVarProcessor.containsKey(normalizedName)) {
781799
final SpecialVariableProcessor firstActiveProcessor =
782-
this.mapVariableNameToSpecialVarProcessor.get(normalizedName)
783-
.stream()
784-
.filter(x -> x.isAllowed(
785-
findLastActiveFileContainer(this).orElse(null),
786-
this.getPreprocessingState().findLastPositionInfoInStack().orElse(null),
787-
this,
788-
this.getPreprocessingState()
789-
)).findFirst().orElse(null);
790-
791-
if (firstActiveProcessor == null) {
792-
throw this.makeException(
793-
"Can't set special variable because no any allowed variable processor in the position",
794-
null);
795-
} else {
796-
firstActiveProcessor.setVariable(normalizedName, value, this);
797-
}
800+
this.findAllowedSpecialVariableProcessor(normalizedName)
801+
.orElseThrow(() -> this.makeException(
802+
"Cannot set special variable '" + normalizedName +
803+
"' no valid processor available here, may be it is read only",
804+
null));
805+
firstActiveProcessor.setVariable(normalizedName, value, this);
798806
} else {
799807
if (isVerbose()) {
800808
final String valueAsStr = value.toString();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2002-2019 Igor Maznitsa (http://www.igormaznitsa.com)
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.igormaznitsa.jcp.usecases;
23+
24+
import static org.junit.Assert.assertEquals;
25+
26+
import com.igormaznitsa.jcp.JcpPreprocessor;
27+
import com.igormaznitsa.jcp.context.PreprocessorContext;
28+
29+
public class UsePrefixAsMultilineTest extends AbstractUseCaseTest {
30+
31+
@Override
32+
public void check(PreprocessorContext context, JcpPreprocessor.Statistics stat) throws Exception {
33+
assertEquals(0, stat.getCopied());
34+
assertEquals(1, stat.getPreprocessed());
35+
}
36+
37+
}

jcp/src/test/resources/com/igormaznitsa/jcp/usecases/TextBufferVariablesTest/etl/text.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
Some block text which
2-
will be placed into a variable as multiline text
3-
and prefix buffer is used to accumulate it
4-
---
1+
set prefix---
52
Some block text which
63
will be placed into a variable as multiline text
74
and prefix buffer is used to accumulate it
@@ -15,4 +12,4 @@ and prefix buffer is used to accumulate it
1512
===
1613

1714
...
18-
defined postfix
15+
set postfix

jcp/src/test/resources/com/igormaznitsa/jcp/usecases/TextBufferVariablesTest/src/text.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ will be placed into a variable as multiline text
77
and prefix buffer is used to accumulate it
88
//#prefix-
99
//#local multiline_prefix = jcp.text.buffer.prefix
10-
//#global jcp.text.buffer.prefix="set prefix"
11-
//#global jcp.text.buffer.postfix="set postfix"
10+
//#local jcp.text.buffer.prefix="set prefix"
11+
//#local jcp.text.buffer.postfix="set postfix"
1212
---
1313
//$/*$multiline_prefix$*/
1414
===
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package test;
2+
3+
public class Main {
4+
public static final int mul(int a, b) {
5+
return a * b;
6+
}
7+
8+
9+
public static final int div(int a, b) {
10+
return a / b;
11+
}
12+
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//#prefix+
2+
//$ public static final int mul(int a, b) {
3+
//$ return a * b;
4+
//$ }
5+
//#prefix-
6+
//#postfix+
7+
//$ public static final int div(int a, b) {
8+
//$ return a / b;
9+
//$ }
10+
//#postfix-
11+
//#local mul_func = jcp.text.buffer.prefix
12+
//#local div_func = jcp.text.buffer.postfix
13+
//#local jcp.text.buffer.prefix = ""
14+
//#local jcp.text.buffer.postfix = ""
15+
package test;
16+
17+
public class Main {
18+
/*$mul_func$*/
19+
20+
/*$div_func$*/
21+
}

0 commit comments

Comments
 (0)