@Test public void testFormulaContextExtraction() { Object[][] test = { new Object[] { "test:IF()", true, "test", "IF()" }, new Object[] { "=IF()", true, "report", "IF()" }, new Object[] { "report-IF()", false, null, null }, new Object[] { "=IF(:)", true, "report", "IF(:)" }, new Object[] { "test=:IF()", false, null, null }, new Object[] { "test asd:IF()", false, null, null }, }; for ( Object[] objects : test ) { String v = (String) objects[ 0 ]; String[] strings = FormulaUtil.extractFormulaContext( v ); if ( strings[ 0 ] == null ) { Assert.assertFalse( "Failure in " + v, (Boolean) objects[ 1 ] ); } else { assertTrue( "Failure in " + v, (Boolean) objects[ 1 ] ); assertEquals( "Failure in " + v, strings[ 0 ], objects[ 2 ] ); assertEquals( "Failure in " + v, strings[ 1 ], objects[ 3 ] ); } } }
public static String[] extractFormulaContext( String formula ) { String formulaNamespace; String formulaExpression; if ( formula == null ) { formulaNamespace = null; formulaExpression = null; } else { if ( formula.endsWith( ";" ) ) { DebugLog.log( "A formula with a trailing semicolon is not valid. Auto-correcting the formula." ); formula = formula.substring( 0, formula.length() - 1 ); } Pattern pattern = Pattern.compile( "^((\\w+):|=)(.*)" ); Matcher matcher = pattern.matcher( formula ); if ( matcher.matches() ) { formulaNamespace = matcher.group( 2 ); if ( formulaNamespace == null ) { formulaNamespace = "report"; } formulaExpression = matcher.group( 3 ); } else { formulaNamespace = null; formulaExpression = null; } } return new String[] { formulaNamespace, formulaExpression }; }
FormulaUtil { public static String[] extractFormulaContext( String formula ) { String formulaNamespace; String formulaExpression; if ( formula == null ) { formulaNamespace = null; formulaExpression = null; } else { if ( formula.endsWith( ";" ) ) { DebugLog.log( "A formula with a trailing semicolon is not valid. Auto-correcting the formula." ); formula = formula.substring( 0, formula.length() - 1 ); } Pattern pattern = Pattern.compile( "^((\\w+):|=)(.*)" ); Matcher matcher = pattern.matcher( formula ); if ( matcher.matches() ) { formulaNamespace = matcher.group( 2 ); if ( formulaNamespace == null ) { formulaNamespace = "report"; } formulaExpression = matcher.group( 3 ); } else { formulaNamespace = null; formulaExpression = null; } } return new String[] { formulaNamespace, formulaExpression }; } }
FormulaUtil { public static String[] extractFormulaContext( String formula ) { String formulaNamespace; String formulaExpression; if ( formula == null ) { formulaNamespace = null; formulaExpression = null; } else { if ( formula.endsWith( ";" ) ) { DebugLog.log( "A formula with a trailing semicolon is not valid. Auto-correcting the formula." ); formula = formula.substring( 0, formula.length() - 1 ); } Pattern pattern = Pattern.compile( "^((\\w+):|=)(.*)" ); Matcher matcher = pattern.matcher( formula ); if ( matcher.matches() ) { formulaNamespace = matcher.group( 2 ); if ( formulaNamespace == null ) { formulaNamespace = "report"; } formulaExpression = matcher.group( 3 ); } else { formulaNamespace = null; formulaExpression = null; } } return new String[] { formulaNamespace, formulaExpression }; } private  FormulaUtil(); }
FormulaUtil { public static String[] extractFormulaContext( String formula ) { String formulaNamespace; String formulaExpression; if ( formula == null ) { formulaNamespace = null; formulaExpression = null; } else { if ( formula.endsWith( ";" ) ) { DebugLog.log( "A formula with a trailing semicolon is not valid. Auto-correcting the formula." ); formula = formula.substring( 0, formula.length() - 1 ); } Pattern pattern = Pattern.compile( "^((\\w+):|=)(.*)" ); Matcher matcher = pattern.matcher( formula ); if ( matcher.matches() ) { formulaNamespace = matcher.group( 2 ); if ( formulaNamespace == null ) { formulaNamespace = "report"; } formulaExpression = matcher.group( 3 ); } else { formulaNamespace = null; formulaExpression = null; } } return new String[] { formulaNamespace, formulaExpression }; } private  FormulaUtil(); static String quoteReference( final String reference ); static String quoteString( final String text ); static String[] getReferences( final String formula ); static String[] getReferences( final Formula formula ); static String extractFormula( final String formula ); static String extractStaticTextFromFormula( final String formula ); static String extractStaticTextFromFormulaFragment( final String formula ); static boolean isValidFormulaFragment( final String formula ); static String createCellUITextFromFormula( final String formula ); static String createCellUITextFromFormula( final String formula, final FormulaContext context ); static String createEditorTextFromFormula( final String formula,
                                                    final FormulaContext formulaContext ); static String createFormulaFromUIText( final String formula ); static String[] extractFormulaContext( String formula ); }
FormulaUtil { public static String[] extractFormulaContext( String formula ) { String formulaNamespace; String formulaExpression; if ( formula == null ) { formulaNamespace = null; formulaExpression = null; } else { if ( formula.endsWith( ";" ) ) { DebugLog.log( "A formula with a trailing semicolon is not valid. Auto-correcting the formula." ); formula = formula.substring( 0, formula.length() - 1 ); } Pattern pattern = Pattern.compile( "^((\\w+):|=)(.*)" ); Matcher matcher = pattern.matcher( formula ); if ( matcher.matches() ) { formulaNamespace = matcher.group( 2 ); if ( formulaNamespace == null ) { formulaNamespace = "report"; } formulaExpression = matcher.group( 3 ); } else { formulaNamespace = null; formulaExpression = null; } } return new String[] { formulaNamespace, formulaExpression }; } private  FormulaUtil(); static String quoteReference( final String reference ); static String quoteString( final String text ); static String[] getReferences( final String formula ); static String[] getReferences( final Formula formula ); static String extractFormula( final String formula ); static String extractStaticTextFromFormula( final String formula ); static String extractStaticTextFromFormulaFragment( final String formula ); static boolean isValidFormulaFragment( final String formula ); static String createCellUITextFromFormula( final String formula ); static String createCellUITextFromFormula( final String formula, final FormulaContext context ); static String createEditorTextFromFormula( final String formula,
                                                    final FormulaContext formulaContext ); static String createFormulaFromUIText( final String formula ); static String[] extractFormulaContext( String formula );  }