| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -187,24 +187,35 @@ public Value getVariable(final String varName, final PreprocessorContext context | |||
| 187 | 187 | } | |
| 188 | 188 | } | |
| 189 | 189 | ||
| 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 | + | ||
| 190 | 197 | @Override | |
| 191 | 198 | public void setVariable(final String varName, final Value value, | |
| 192 | 199 | final PreprocessorContext context) { | |
| 193 | 200 | final PreprocessingState state = context.getPreprocessingState(); | |
| 194 | 201 | switch (varName) { | |
| 195 | 202 | case VAR_JCP_BUFFER_ALL: { | |
| 203 | + this.assertNotGlobalPhase(varName, context); | ||
| 196 | 204 | state.setBufferText(value.toString()); | |
| 197 | 205 | } | |
| 198 | 206 | break; | |
| 199 | 207 | case VAR_JCP_BUFFER_POSTFIX: { | |
| 208 | + this.assertNotGlobalPhase(varName, context); | ||
| 200 | 209 | state.setBufferText(value.toString(), PreprocessingState.PrinterType.POSTFIX); | |
| 201 | 210 | } | |
| 202 | 211 | break; | |
| 203 | 212 | case VAR_JCP_BUFFER_MIDDLE: { | |
| 213 | + this.assertNotGlobalPhase(varName, context); | ||
| 204 | 214 | state.setBufferText(value.toString(), PreprocessingState.PrinterType.NORMAL); | |
| 205 | 215 | } | |
| 206 | 216 | break; | |
| 207 | 217 | case VAR_JCP_BUFFER_PREFIX: { | |
| 218 | + this.assertNotGlobalPhase(varName, context); | ||
| 208 | 219 | state.setBufferText(value.toString(), PreprocessingState.PrinterType.PREFIX); | |
| 209 | 220 | } | |
| 210 | 221 | break; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -296,6 +296,7 @@ public Set<FileInfoContainer> findPreprocessedResources() { | |||
| 296 | 296 | ||
| 297 | 297 | /** | |
| 298 | 298 | * Send notification about context start to all registered listeners. | |
| 299 | + * | ||
| 299 | 300 | * @param initedList list accumulating successfully processed listeners, must not be immutable and must not be null | |
| 300 | 301 | * @since 7.2.0 | |
| 301 | 302 | */ | |
@@ -639,14 +640,19 @@ public PreprocessorContext setLocalVariable(final String name, final Value value | |||
| 639 | 640 | throw makeException("Not defined variable name", null); | |
| 640 | 641 | } | |
| 641 | 642 | ||
| 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)) { | ||
| 644 | 650 | 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 [" + | ||
| 646 | 652 | normalized + ']', null); | |
| 653 | + } else { | ||
| 654 | + this.localVarTable.put(normalized, value); | ||
| 647 | 655 | } | |
| 648 | - | ||
| 649 | - localVarTable.put(normalized, value); | ||
| 650 | 656 | return this; | |
| 651 | 657 | } | |
| 652 | 658 | ||
@@ -754,10 +760,22 @@ public boolean containsLocalVariable(final String name) { | |||
| 754 | 760 | * @return this preprocessor context | |
| 755 | 761 | */ | |
| 756 | 762 | public PreprocessorContext clearLocalVariables() { | |
| 757 | - localVarTable.clear(); | ||
| 763 | + this.localVarTable.clear(); | ||
| 758 | 764 | return this; | |
| 759 | 765 | } | |
| 760 | 766 | ||
| 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 | + | ||
| 761 | 779 | /** | |
| 762 | 780 | * Set a global variable value | |
| 763 | 781 | * | |
@@ -779,22 +797,12 @@ public PreprocessorContext setGlobalVariable(final String name, final Value valu | |||
| 779 | 797 | ||
| 780 | 798 | if (this.mapVariableNameToSpecialVarProcessor.containsKey(normalizedName)) { | |
| 781 | 799 | 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); | ||
| 798 | 806 | } else { | |
| 799 | 807 | if (isVerbose()) { | |
| 800 | 808 | final String valueAsStr = value.toString(); | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff 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--- | ||
| 5 | 2 | Some block text which | |
| 6 | 3 | will be placed into a variable as multiline text | |
| 7 | 4 | and prefix buffer is used to accumulate it | |
@@ -15,4 +12,4 @@ and prefix buffer is used to accumulate it | |||
| 15 | 12 | === | |
| 16 | 13 | ||
| 17 | 14 | ... | |
| 18 | - defined postfix | ||
| 15 | + set postfix | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,8 +7,8 @@ will be placed into a variable as multiline text | |||
| 7 | 7 | and prefix buffer is used to accumulate it | |
| 8 | 8 | //#prefix- | |
| 9 | 9 | //#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" | ||
| 12 | 12 | --- | |
| 13 | 13 | //$/*$multiline_prefix$*/ | |
| 14 | 14 | === | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments