diff options
Diffstat (limited to 'plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/wordupdate/WordMLApplicabilityHandler.java')
-rw-r--r-- | plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/wordupdate/WordMLApplicabilityHandler.java | 469 |
1 files changed, 343 insertions, 126 deletions
diff --git a/plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/wordupdate/WordMLApplicabilityHandler.java b/plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/wordupdate/WordMLApplicabilityHandler.java index d7fb621d38a..ef9c8010b0d 100644 --- a/plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/wordupdate/WordMLApplicabilityHandler.java +++ b/plugins/org.eclipse.osee.define.report/src/org/eclipse/osee/define/report/internal/wordupdate/WordMLApplicabilityHandler.java @@ -10,191 +10,408 @@ *******************************************************************************/ package org.eclipse.osee.define.report.internal.wordupdate; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Stack; +import java.util.TreeMap; import java.util.regex.Matcher; -import java.util.regex.Pattern; +import javax.script.ScriptEngine; +import javax.script.ScriptEngineManager; +import javax.script.ScriptException; import org.antlr.runtime.ANTLRStringStream; import org.antlr.runtime.CommonTokenStream; import org.antlr.runtime.RecognitionException; +import org.codehaus.jackson.map.ObjectMapper; import org.eclipse.osee.framework.core.data.BranchId; +import org.eclipse.osee.framework.core.data.FeatureDefinitionData; import org.eclipse.osee.framework.core.enums.CoreArtifactTypes; +import org.eclipse.osee.framework.core.enums.CoreAttributeTypes; +import org.eclipse.osee.framework.core.grammar.ApplicabilityBlock; +import org.eclipse.osee.framework.core.grammar.ApplicabilityBlock.ApplicabilityType; import org.eclipse.osee.framework.core.grammar.ApplicabilityGrammarLexer; import org.eclipse.osee.framework.core.grammar.ApplicabilityGrammarParser; -import org.eclipse.osee.framework.jdk.core.type.HashCollection; +import org.eclipse.osee.framework.core.util.WordCoreUtil; import org.eclipse.osee.framework.jdk.core.type.OseeCoreException; import org.eclipse.osee.orcs.OrcsApi; import org.eclipse.osee.orcs.data.ArtifactReadable; public class WordMLApplicabilityHandler { - private static String FEATUREAPP = "feature"; - private static String CONFIGAPP = "config"; + public static String previewValidApplicabilityContent(OrcsApi orcsApi, String content, BranchId branch) throws OseeCoreException { + Map<String, List<String>> featureValuesAllowed = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + featureValuesAllowed = + orcsApi.getQueryFactory().applicabilityQuery().getBranchViewFeatureValues(branch, branch.getViewId()); - private static String MAX_TAG_OCCURENCE = "30"; - private static String WORD_ML_TAGS = "(\\<[^>]*?>){0," + MAX_TAG_OCCURENCE + "}"; + String toReturn = content; - private static String TABLE_CELL = "<w:tc>"; - private static String TABLE = "<w:tbl>"; + ArtifactReadable featureDefArt = orcsApi.getQueryFactory().fromBranch(branch).andTypeEquals( + CoreArtifactTypes.FeatureDefinition).getResults().getExactlyOne(); + String featureDefJson = featureDefArt.getSoleAttributeAsString(CoreAttributeTypes.GeneralStringData); + + Collection<String> configurationsAllowed = featureValuesAllowed.get("Config"); + Stack<ApplicabilityBlock> applicabilityBlocks = new Stack<>(); + + Matcher matcher = WordCoreUtil.FULL_PATTERN.matcher(toReturn); + + int searchIndex = 0; + int applicBlockCount = 0; + while (searchIndex < toReturn.length() && matcher.find(searchIndex)) { + String beginFeature = matcher.group(1) != null ? WordCoreUtil.textOnly(matcher.group(1)) : null; + String beginConfiguration = matcher.group(26) != null ? WordCoreUtil.textOnly(matcher.group(26)) : null; + + String endFeature = matcher.group(12) != null ? WordCoreUtil.textOnly(matcher.group(12)) : null; + String endConfiguration = matcher.group(43) != null ? WordCoreUtil.textOnly(matcher.group(43)) : null; + + if (beginFeature != null && beginFeature.toLowerCase().contains(WordCoreUtil.FEATUREAPP)) { + applicBlockCount += 1; + searchIndex = addBeginApplicabilityBlock(ApplicabilityType.Feature, applicabilityBlocks, matcher, + beginFeature, searchIndex); + } else if (beginConfiguration != null && beginConfiguration.toLowerCase().contains(WordCoreUtil.CONFIGAPP)) { + applicBlockCount += 1; + searchIndex = addBeginApplicabilityBlock(ApplicabilityType.Configuration, applicabilityBlocks, matcher, + beginConfiguration, searchIndex); + } else if ((endFeature != null && endFeature.toLowerCase().contains( + WordCoreUtil.FEATUREAPP)) || (endConfiguration != null && endConfiguration.toLowerCase().contains( + WordCoreUtil.CONFIGAPP))) { + applicBlockCount -= 1; + + ApplicabilityBlock applicabilityBlock = getFullApplicabilityBlock(applicabilityBlocks, matcher, toReturn); + + int endBracketGroup = applicabilityBlock.getType().equals(ApplicabilityType.Feature) ? 25 : 60; + String optionalEndBracket = + matcher.group(endBracketGroup) != null ? WordCoreUtil.textOnly(matcher.group(endBracketGroup)) : null; + + String toInsert = evaluateApplicabilityBlock(applicabilityBlock, optionalEndBracket, toReturn, + featureDefJson, featureValuesAllowed, configurationsAllowed); + + String toReplace = + toReturn.substring(applicabilityBlock.getStartInsertIndex(), applicabilityBlock.getEndInsertIndex()); + toReturn = toReturn.replace(toReplace, toInsert); + searchIndex = applicabilityBlock.getStartInsertIndex() + toInsert.length(); + matcher = WordCoreUtil.FULL_PATTERN.matcher(toReturn); + } else { + break; + } + } - public static String END = "E" + WORD_ML_TAGS + "n" + WORD_ML_TAGS + "d ?" + WORD_ML_TAGS + " ?"; - public static String ELSE = "E" + WORD_ML_TAGS + "l" + WORD_ML_TAGS + "s" + WORD_ML_TAGS + "e ?"; - public static String FEATURE = - "F" + WORD_ML_TAGS + "e" + WORD_ML_TAGS + "a" + WORD_ML_TAGS + "t" + WORD_ML_TAGS + "u" + WORD_ML_TAGS + "r" + WORD_ML_TAGS + "e"; - public static String CONFIG = - "C" + WORD_ML_TAGS + "o" + WORD_ML_TAGS + "n" + WORD_ML_TAGS + "f" + WORD_ML_TAGS + "i" + WORD_ML_TAGS + "g" + WORD_ML_TAGS + "u" + WORD_ML_TAGS + "r" + WORD_ML_TAGS + "a" + WORD_ML_TAGS + "t" + WORD_ML_TAGS + "i" + WORD_ML_TAGS + "o" + WORD_ML_TAGS + "n"; + toReturn = removeEmptyLists(toReturn); + if (applicBlockCount != 0) { + throw new OseeCoreException("An applicability block of text is missing an End Feature/Configuration tag"); + } - public static String ENDBRACKETS = WORD_ML_TAGS + "(\\[(.*?)\\]) ?"; - public static String OPTIONAL_ENDBRACKETS = " ?(" + WORD_ML_TAGS + "(\\[.*?\\]))?"; - public static String BEGINFEATURE = FEATURE + ENDBRACKETS; - public static String ENDFEATURE = END + FEATURE + OPTIONAL_ENDBRACKETS; - public static String BEGINCONFIG = CONFIG + ENDBRACKETS; - public static String ENDCONFIG = END + CONFIG + OPTIONAL_ENDBRACKETS; + return toReturn; + } - public static String LOGICAL_STRING = WORD_ML_TAGS + " ?(LM|ID).*?"; + private static String evaluateApplicabilityBlock(ApplicabilityBlock applicabilityBlock, String optionalEndBracket, String fullWordML, String featureDefJson, Map<String, List<String>> featureValuesAllowed, Collection<String> configurationsAllowed) { + //Remove Logical Messages that were mistaken for optional end brackets + if (optionalEndBracket != null && optionalEndBracket.contains(".")) { + int originalEnd = applicabilityBlock.getEndInsertIndex(); + applicabilityBlock.setEndInsertIndex(originalEnd - optionalEndBracket.length()); + } else { + Integer endTextIndex = applicabilityBlock.getEndTextIndex(); + Integer endInsertIndex = applicabilityBlock.getEndInsertIndex(); + String toCheck = fullWordML.substring(endTextIndex, endInsertIndex); - public static Pattern LOGICAL_PATTERN = - Pattern.compile(LOGICAL_STRING, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); + int indexOf = WordCoreUtil.indexOf(toCheck, WordCoreUtil.LOGICAL_STRING); + if (indexOf != -1) { + applicabilityBlock.setEndInsertIndex(endTextIndex + indexOf); + } + } - public static Pattern FEATURE_CONFIG_PATTERN = - Pattern.compile("(" + BEGINFEATURE + "(.*?)" + ENDFEATURE + ")|(" + BEGINCONFIG + "(.*?)" + ENDCONFIG + ")", - Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); + Map<String, String> binDataMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + saveBinData(applicabilityBlock.getFullText(), binDataMap); + String toInsert = evaluateApplicabilityExpression(applicabilityBlock, featureDefJson, configurationsAllowed, + featureValuesAllowed); + toInsert = insertMissingbinData(toInsert, binDataMap); - public static Pattern ELSE_PATTERN = Pattern.compile("(" + FEATURE + "|" + CONFIG + ")" + " " + ELSE, - Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); + return toInsert; + } - public static Pattern TABLE_PATTERN = - Pattern.compile("<w:tbl>(.*?)</w:tbl>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); - public static Pattern TABLE_ROW_PATTERN = - Pattern.compile("<w:tr wsp:rsidR=\".*?\" wsp:rsidRPr=\".*?\" wsp:rsidTr=\".*?\">(.*?)</w:tr>", - Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); - public static Pattern TABLE_CELL_PATTERN = - Pattern.compile("<w:tc>(.*?)</w:tc>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); + private static String removeEmptyLists(String wordML) { + return wordML.replaceAll(WordCoreUtil.EMPTY_LIST_REGEX, ""); + } - private static int startSearchIndex = 0; - private static int startReplaceIndex = 0; - private static int endReplaceIndex = 0; + private static String insertMissingbinData(String toInsert, Map<String, String> binDataMap) { + String temp = toInsert; + Matcher matcher = WordCoreUtil.IMG_SRC_PATTERN.matcher(temp); + while (matcher.find()) { + String srcId = matcher.group(1); + if (binDataMap.containsKey(srcId)) { + String binData = binDataMap.get(srcId); + if (!temp.contains(binData)) { + temp = binData + temp; + } + } + } - public static String previewValidApplicabilityContent(OrcsApi orcsApi, String content, BranchId branch) throws OseeCoreException { - String toReturn = content; - startReplaceIndex = 0; - endReplaceIndex = 0; - startSearchIndex = 0; + return temp; + } - HashCollection<String, String> featureValuesAllowed = - orcsApi.getQueryFactory().applicabilityQuery().getBranchViewFeatureValues(branch, branch.getViewId()); + private static void saveBinData(String fullText, Map<String, String> binDataMap) { + Matcher matcher = WordCoreUtil.BIN_DATA_PATTERN.matcher(fullText); + while (matcher.find()) { + binDataMap.put(matcher.group(1), matcher.group(0)); + } + } - String configuration = featureValuesAllowed.getValues("CONFIG").iterator().next(); + private static int addBeginApplicabilityBlock(ApplicabilityType type, Stack<ApplicabilityBlock> applicabilityBlocks, Matcher matcher, String applicabilityExpression, int searchIndex) { + ApplicabilityBlock beginApplic = new ApplicabilityBlock(); + beginApplic.setType(type); + //Remove extra space + applicabilityExpression = applicabilityExpression.replace(" [", "["); + beginApplic.setApplicabilityExpression(applicabilityExpression); + beginApplic.setStartInsertIndex(matcher.start()); + beginApplic.setStartTextIndex(matcher.end()); + applicabilityBlocks.push(beginApplic); + searchIndex = matcher.end(); + + return searchIndex; + } - ApplicabilityExpression appExp = new ApplicabilityExpression(configuration, featureValuesAllowed); + private static ApplicabilityBlock getFullApplicabilityBlock(Stack<ApplicabilityBlock> applicabilityBlocks, Matcher matcher, String toReturn) { + if (applicabilityBlocks.isEmpty()) { + throw new OseeCoreException("An applicability block of text is missing a start Feature/Configuration tag"); + } + ApplicabilityBlock applic = applicabilityBlocks.pop(); + applic.setEndInsertIndex(matcher.end()); + applic.setEndTextIndex(matcher.start()); + + String insideText = toReturn.substring(applic.getStartTextIndex(), applic.getEndTextIndex()); + applic.setFullText(insideText); + + // Adjust start and end insert indicies if tags are inside a table + if (!applic.getFullText().contains(WordCoreUtil.TABLE) && applic.getFullText().contains( + WordCoreUtil.TABLE_CELL)) { + String findStartOfRow = toReturn.substring(0, applic.getStartInsertIndex()); + int startRowIndex = findStartOfRow.lastIndexOf("<w:tr wsp:rsidR="); + + if (startRowIndex != -1) { + // find end of row after the END configuration/feature tag + String findEndOfRow = toReturn.substring(matcher.end()); + int endRowIndex = findEndOfRow.indexOf("</w:tr>"); + if (endRowIndex != -1) { + endRowIndex = endRowIndex + matcher.end() + 7; + String fullText = toReturn.substring(startRowIndex, endRowIndex); + applic.setIsInTable(true); + applic.setStartInsertIndex(startRowIndex); + applic.setEndInsertIndex(startRowIndex + fullText.length()); + + fullText = fullText.replaceAll(WordCoreUtil.ENDFEATURE + "|" + WordCoreUtil.ENDCONFIG, ""); + fullText = fullText.replaceAll(WordCoreUtil.BEGINFEATURE + "|" + WordCoreUtil.BEGINCONFIG, ""); + applic.setFullText(fullText); + } + } + } - // need to do this to make sure index keeps getting updated for adding and removing content - while (startSearchIndex < toReturn.length()) { - String toSearch = toReturn.substring(startSearchIndex); - String applicabilityContent = findNextApplicability(toSearch); + return applic; + } - if (applicabilityContent == null) { - break; + private static String evaluateApplicabilityExpression(ApplicabilityBlock applic, String featureDefJson, Collection<String> configurationsAllowed, Map<String, List<String>> featureValuesAllowed) { + String applicabilityExpression = applic.getApplicabilityExpression(); + String toInsert = ""; + try { + + String fullText = applic.getFullText(); + + ApplicabilityGrammarLexer lex = + new ApplicabilityGrammarLexer(new ANTLRStringStream(applicabilityExpression.toUpperCase())); + ApplicabilityGrammarParser parser = new ApplicabilityGrammarParser(new CommonTokenStream(lex)); + + parser.start(); + + if (applic.getType().equals(ApplicabilityType.Feature)) { + toInsert = getValidFeatureContent(fullText, applic.isInTable(), parser.getIdValuesMap(), + parser.getOperators(), featureDefJson, featureValuesAllowed); + } else if (applic.getType().equals(ApplicabilityType.Configuration)) { + toInsert = getValidConfigurationContent(fullText, parser.getIdValuesMap(), configurationsAllowed); } - String plainText = WordUtilities.textOnly(applicabilityContent); - String plExpression = plainText.substring(0, plainText.indexOf("]") + 1); + } catch (RecognitionException ex) { + throw new OseeCoreException( + "Failed to parse expression: " + applicabilityExpression + " at start Index: " + applic.getStartInsertIndex()); + } + + return toInsert; + } + + public static String getValidConfigurationContent(String fullText, HashMap<String, List<String>> id_value_map, Collection<String> configurationsAllowed) { + Matcher match = WordCoreUtil.ELSE_PATTERN.matcher(fullText); + String beginningText = fullText; + String elseText = ""; + + if (match.find()) { + beginningText = fullText.substring(0, match.start()); + + elseText = fullText.substring(match.end()); + elseText = elseText.replaceAll(WordCoreUtil.ENDCONFIG, ""); + elseText = elseText.replaceAll(WordCoreUtil.BEGINCONFIG, ""); + } + + String toReturn = ""; - if (plExpression.isEmpty()) { - throw new OseeCoreException("The applicability expression is missing an end bracket\n " + plainText); + // Note: this assumes only OR's are put in between configurations + for (String id : id_value_map.keySet()) { + boolean isIncluded = true; + List<String> values = id_value_map.get(id); + for (String val : values) { + if (val.equalsIgnoreCase("excluded")) { + isIncluded = false; + } + } + + if (containsIgnoreCase(configurationsAllowed, id) == isIncluded) { + toReturn = beginningText; + break; } - toReturn = parseExpression(orcsApi, appExp, plExpression, applicabilityContent, toReturn, branch); } return toReturn; } - private static String parseExpression(OrcsApi orcsApi, ApplicabilityExpression featureAppExp, String plExpression, String contentBlock, String toReturn, BranchId branch) { - String validContent = null; - try { - ApplicabilityGrammarLexer lex = new ApplicabilityGrammarLexer(new ANTLRStringStream(plExpression)); - ApplicabilityGrammarParser parser = new ApplicabilityGrammarParser(new CommonTokenStream(lex)); - parser.start(); + private static String getValidFeatureContent(String fullText, boolean isInTable, HashMap<String, List<String>> featureIdValuesMap, ArrayList<String> featureOperators, String featureDefJson, Map<String, List<String>> featureValuesAllowed) { + ScriptEngineManager sem = new ScriptEngineManager(); + ScriptEngine se = sem.getEngineByName("JavaScript"); - String applicabilityType = parser.getApplicabilityType(); + Matcher match = WordCoreUtil.ELSE_PATTERN.matcher(fullText); + String beginningText = fullText; + String elseText = ""; - if (applicabilityType.equals(FEATUREAPP)) { - ArtifactReadable featureDefArt = orcsApi.getQueryFactory().fromBranch(branch).andTypeEquals( - CoreArtifactTypes.FeatureDefinition).getResults().getExactlyOne(); + if (match.find()) { - validContent = featureAppExp.getValidFeatureContent(contentBlock, parser.getFeatureIdValuesMap(), - parser.getFeatureOperators(), featureDefArt); - } else if (applicabilityType.equals(CONFIGAPP)) { - validContent = featureAppExp.getValidConfigurationContent(contentBlock, parser.getConfigIds()); + if (isInTable) { + String temp = fullText.substring(0, match.end()); + // Find last occurence of table row + int lastIndexOf = temp.lastIndexOf("<w:tr wsp:rsidR="); + if (lastIndexOf != -1) { + elseText = fullText.substring(lastIndexOf); + elseText = elseText.replaceAll(WordCoreUtil.ELSE_EXP, ""); + beginningText = fullText.substring(0, lastIndexOf); + } + } else { + beginningText = fullText.substring(0, match.start()); + elseText = fullText.substring(match.end()); } - } catch (RecognitionException ex) { - throw new OseeCoreException( - "Failed to parse expression: " + plExpression + " at start Index: " + startReplaceIndex); + elseText = elseText.replaceAll(WordCoreUtil.ENDFEATURE, ""); + elseText = elseText.replaceAll(WordCoreUtil.BEGINFEATURE, ""); } - if (validContent != null) { - String toReplace = toReturn.substring(startReplaceIndex, endReplaceIndex); - toReturn = toReturn.replace(toReplace, validContent); - startSearchIndex = startReplaceIndex + validContent.length(); + String toReturn = ""; + String expression = + createFeatureExpression(featureIdValuesMap, featureOperators, featureDefJson, featureValuesAllowed); + + boolean result = false; + try { + result = (boolean) se.eval(expression); + } catch (ScriptException ex) { + throw new OseeCoreException("Failed to parse expression: " + expression); + } + + if (result) { + toReturn = beginningText; } else { - String toReplace = toReturn.substring(startReplaceIndex, endReplaceIndex); - toReturn = toReturn.replace(toReplace, ""); - startSearchIndex = startReplaceIndex; + toReturn = elseText; } return toReturn; } - private static String findNextApplicability(String toSearch) { - String toReturn = null; + private static String createFeatureExpression(HashMap<String, List<String>> featureIdValuesMap, ArrayList<String> featureOperators, String featureDefJson, Map<String, List<String>> featureValuesAllowed) { + ScriptEngineManager sem = new ScriptEngineManager(); + ScriptEngine se = sem.getEngineByName("JavaScript"); - Matcher match = FEATURE_CONFIG_PATTERN.matcher(toSearch); + String myFeatureExpression = ""; + Iterator<String> iterator = featureOperators.iterator(); - if (match.find()) { - // If match contains the table tag, the Feature/Config is around entire table and not just a row so no special parsing needed - if (!match.group(0).contains(TABLE) && match.group(0).contains(TABLE_CELL)) { - String findStartOfRow = toSearch.substring(0, match.start()); - int startRowIndex = findStartOfRow.lastIndexOf("<w:tr wsp:rsidR="); - - if (startRowIndex != -1) { - - String findEndOfRow = toSearch.substring(startRowIndex); - int endRowIndex = findEndOfRow.indexOf("</w:tr>"); - if (endRowIndex != -1) { - endRowIndex = endRowIndex + startRowIndex + 7; - toReturn = toSearch.substring(startRowIndex, endRowIndex); - startReplaceIndex = startRowIndex + startSearchIndex; - endReplaceIndex = startReplaceIndex + toReturn.length(); - } + for (String feature : featureIdValuesMap.keySet()) { + List<String> values = featureIdValuesMap.get(feature); + + String valueExpression = createValueExpression(feature, values, featureDefJson, featureValuesAllowed); + + boolean result = false; + + try { + result = (boolean) se.eval(valueExpression); + } catch (ScriptException ex) { + throw new OseeCoreException("Failed to parse expression: " + valueExpression); + } + + myFeatureExpression += result + " "; + + if (iterator.hasNext()) { + String next = iterator.next(); + if (next.equals("|")) { + myFeatureExpression += "|| "; + } else if (next.equals("&")) { + myFeatureExpression += "&& "; } + } + } + + return myFeatureExpression; + } + + private static String createValueExpression(String feature, List<String> values, String featureDefJson, Map<String, List<String>> featureValuesAllowed) { + String myValueExpression = ""; + for (String value : values) { + if (value.equals("(")) { + myValueExpression += "( "; + } else if (value.equals(")")) { + myValueExpression += ") "; + } else if (value.equals("|")) { + myValueExpression += "|| "; + } else if (value.equals("&")) { + myValueExpression += "&& "; } else { - //this is End Feature optional bracket - int actualEnd = match.end(); - // Group 21 is ending brackets for features, Group 56 is ending brackets for Configuration - String endBracket = null; - int endIndex = -1; - int startIndex = -1; - if (match.group(21) != null) { - endBracket = match.group(21); - endIndex = match.end(21); - startIndex = match.start(21); - } else if (match.group(56) != null) { - endBracket = match.group(56); - endIndex = match.end(56); - startIndex = match.start(56); - } - if (endBracket != null) { - String endBracketText = WordUtilities.textOnly(endBracket); + boolean eval = isFeatureValuePairValid(feature, value, featureDefJson, featureValuesAllowed); + myValueExpression += eval + " "; + } + } + + return myValueExpression; + } + + private static boolean isFeatureValuePairValid(String feature, String value, String featureDefJson, Map<String, List<String>> featureValuesAllowed) { + if (featureValuesAllowed.containsKey(feature)) { + Collection<String> validValues = featureValuesAllowed.get(feature); + + value = value.equalsIgnoreCase("Default") ? getDefaultValue(feature, featureDefJson) : value; + + if (containsIgnoreCase(validValues, value)) { + return true; + } + } + + return false; + } + + private static boolean containsIgnoreCase(Collection<String> validValues, String val) { + for (String validValue : validValues) { + if (validValue.equalsIgnoreCase(val)) { + return true; + } + } + return false; + } + + private static String getDefaultValue(String feature, String featureDefJson) { + String toReturn = null; + try { + ObjectMapper mapper = new ObjectMapper(); + FeatureDefinitionData[] featDataList = mapper.readValue(featureDefJson, FeatureDefinitionData[].class); - // Don't include because it is not a feature/configuration tag - if (endBracketText.contains(".") || toSearch.substring(endIndex).matches(LOGICAL_STRING)) { - actualEnd = startIndex; - } + for (FeatureDefinitionData featData : featDataList) { + if (featData.getName().equalsIgnoreCase(feature)) { + toReturn = featData.getDefaultValue(); + break; } - int e = match.group().length() - (match.end() - actualEnd); - toReturn = match.group(0).substring(0, e); - startReplaceIndex = match.start() + startSearchIndex; - endReplaceIndex = actualEnd + startSearchIndex; } + } catch (Exception e) { + throw new OseeCoreException("Error getting default value for feature: " + feature); } return toReturn; |