org.pentaho.reporting.libraries.formula.parser.ParseException Java Examples

The following examples show how to use org.pentaho.reporting.libraries.formula.parser.ParseException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DemoApplication.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static void main(final String[] args)
    throws ParseException, EvaluationException
{
  LibFormulaBoot.getInstance().start();

  final String formula = JOptionPane.showInputDialog("Please enter a formula.");

  if (formula == null)
  {
    return;
  }

  // first parse the formula. This checks the general syntax, but does not
  // check whether the used functions or references are actually valid.
  final Formula f = new Formula(formula);

  // connects the parsed formula to the context. The context provides the
  // operator and function implementations and resolves the references.
  f.initialize(new DemoFormulaContext());

  final Object o = f.evaluate();
  JOptionPane.showMessageDialog(null, "The result is " + o,
      "Result", JOptionPane.INFORMATION_MESSAGE);

  System.exit(0);
}
 
Example #2
Source File: AbstractKettleTransformationProducer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void updateTransformationParameter( final FormulaContext formulaContext,
                                            final Trans trans )
  throws UnknownParamException, EvaluationException, ParseException {
  for ( int i = 0; i < this.parameter.length; i++ ) {
    final FormulaParameter mapping = this.parameter[ i ];
    final String sourceName = mapping.getName();
    final Object value = mapping.compute( formulaContext );
    TransMeta transMeta = trans.getTransMeta();
    if ( value != null ) {
      String paramValue = String.valueOf( value );
      trans.setParameterValue( sourceName, paramValue );
      transMeta.setParameterValue( sourceName, paramValue );
    } else {
      trans.setParameterValue( sourceName, null );
      transMeta.setParameterValue( sourceName, null );
    }
    trans.setTransMeta( transMeta );
  }
}
 
Example #3
Source File: FormulaParameter.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static ParameterMapping[] convert( final FormulaParameter[] args ) {
  final ArrayList<ParameterMapping> textList = new ArrayList<ParameterMapping>();
  for ( int i = 0; i < args.length; i++ ) {
    FormulaParameter arg = args[ i ];
    try {
      String[] references = FormulaUtil.getReferences( arg.getFormula() );
      // some functions can have no references at all like '=FALSE()'
      // but it still be correct function.
      textList.add( new ParameterMapping( references.length > 0 ? references[ 0 ] : "",
                                          arg.getName() ) );
    } catch ( ParseException e ) {
      //
    }
  }
  return textList.toArray( new ParameterMapping[ textList.size() ] );
}
 
Example #4
Source File: Formula.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Formula( final String formulaText ) throws ParseException {
  if ( formulaText == null ) {
    throw new NullPointerException();
  }

  try {
    final FormulaParser parser = new FormulaParser();
    this.rootReference = parser.parse( formulaText.trim() );
  } catch ( TokenMgrError tokenMgrError ) {
    // This is ugly.
    throw new FormulaParseException( tokenMgrError );
  }
}
 
Example #5
Source File: FormulaErrorInspection.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Formula compileFormula( final String formula ) throws ParseException {
  // Namespace is not yet used.
  // final String formulaNamespace;
  final String formulaExpression;
  if ( formula == null ) {
    throw new ParseException( "Formula is invalid" );
  }

  if ( formula.length() > 0 && formula.charAt( 0 ) == '=' ) {
    //      formulaNamespace = "report";
    formulaExpression = formula.substring( 1 );
  } else {
    final int separator = formula.indexOf( ':' );
    if ( separator <= 0 || ( ( separator + 1 ) == formula.length() ) ) {
      // error: invalid formula.
      //        formulaNamespace = null;
      formulaExpression = null;
    } else {
      //        formulaNamespace = formula.substring(0, separator);
      formulaExpression = formula.substring( separator + 1 );
    }
  }

  if ( formulaExpression == null ) {
    throw new ParseException( "Formula is invalid" );
  }
  return new Formula( formulaExpression );
}
 
Example #6
Source File: DefaultAttributeCore.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String[] getReferencedFields( final AttributeMetaData metaData, final ReportElement element,
    final Object attributeValue ) {
  if ( element == null ) {
    throw new NullPointerException();
  }

  if ( attributeValue == null ) {
    return EMPTY;
  }
  final String valueRole = metaData.getValueRole();
  if ( "Field".equals( valueRole ) ) {
    if ( attributeValue instanceof String[] ) {
      final String[] vals = (String[]) attributeValue;
      return vals.clone();
    }
    return new String[] { String.valueOf( attributeValue ) };
  } else if ( "Message".equals( valueRole ) ) {
    final String message = String.valueOf( attributeValue );
    final MessageFormatSupport messageFormatSupport = new MessageFormatSupport();
    messageFormatSupport.setFormatString( message );
    return messageFormatSupport.getFields();
  } else if ( "Formula".equals( valueRole ) ) {
    final String formula = String.valueOf( attributeValue );
    try {
      final Object[] objects = FormulaUtil.getReferences( formula );
      final ArrayList<String> list = new ArrayList<String>();
      for ( int i = 0; i < objects.length; i++ ) {
        final Object object = objects[i];
        if ( object instanceof String ) {
          list.add( (String) object );
        }
      }
      return list.toArray( new String[list.size()] );
    } catch ( ParseException e ) {
      return EMPTY;
    }
  }
  return EMPTY;
}
 
Example #7
Source File: DefaultReportPreProcessorPropertyCore.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String[] getReferencedFields( final ReportPreProcessorPropertyMetaData metaData, final Expression expression,
    final Object attributeValue ) {
  if ( expression == null ) {
    throw new NullPointerException();
  }
  if ( attributeValue == null ) {
    return EMPTY;
  }
  final String propertyRole = metaData.getPropertyRole();
  if ( "Field".equals( propertyRole ) ) {
    if ( attributeValue instanceof String[] ) {
      final String[] vals = (String[]) attributeValue;
      return (String[]) vals.clone();
    }
    return new String[] { String.valueOf( attributeValue ) };
  } else if ( "Message".equals( propertyRole ) ) {
    final String message = String.valueOf( attributeValue );
    final MessageFormatSupport messageFormatSupport = new MessageFormatSupport();
    messageFormatSupport.setFormatString( message );
    return messageFormatSupport.getFields();
  } else if ( "Formula".equals( propertyRole ) ) {
    final String formula = String.valueOf( attributeValue );
    try {
      final Object[] objects = FormulaUtil.getReferences( formula );
      final ArrayList list = new ArrayList();
      for ( int i = 0; i < objects.length; i++ ) {
        final Object object = objects[i];
        if ( object instanceof String ) {
          list.add( object );
        }
      }
      return (String[]) list.toArray( new String[list.size()] );
    } catch ( ParseException e ) {
      return EMPTY;
    }
  }
  return EMPTY;
}
 
Example #8
Source File: DefaultExpressionPropertyCore.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String[] getReferencedFields( final ExpressionPropertyMetaData metaData, final Expression expression,
    final Object attributeValue ) {
  if ( expression == null ) {
    throw new NullPointerException();
  }
  final String propertyRole = metaData.getPropertyRole();
  if ( attributeValue == null ) {
    return EMPTY;
  }
  if ( "Field".equals( propertyRole ) ) {
    if ( attributeValue instanceof String[] ) {
      final String[] vals = (String[]) attributeValue;
      return (String[]) vals.clone();
    }
    return new String[] { String.valueOf( attributeValue ) };
  } else if ( "Message".equals( propertyRole ) ) {
    final String message = String.valueOf( attributeValue );
    final MessageFormatSupport messageFormatSupport = new MessageFormatSupport();
    messageFormatSupport.setFormatString( message );
    return messageFormatSupport.getFields();
  } else if ( "Formula".equals( propertyRole ) ) {
    final String formula = String.valueOf( attributeValue );
    try {
      final Object[] objects = FormulaUtil.getReferences( formula );
      final ArrayList list = new ArrayList();
      for ( int i = 0; i < objects.length; i++ ) {
        final Object object = objects[i];
        if ( object instanceof String ) {
          list.add( object );
        }
      }
      return (String[]) list.toArray( new String[list.size()] );
    } catch ( ParseException e ) {
      return EMPTY;
    }
  }
  return EMPTY;
}
 
Example #9
Source File: VariableReadHandlerTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void getObject() throws SAXException, ParseException {
  when( testAttributes.getValue( anyString(), eq( "datarow-name" ) ) ).thenReturn( "TEST_DATAROW_NAME" );
  variableReadHandler.startParsing( testAttributes );
  FormulaParameter actualResult = variableReadHandler.getObject();
  assertNotNull( actualResult );
  assertEquals( "=[TEST_DATAROW_NAME]", actualResult.getFormula() );
  assertEquals( "TEST_DATAROW_NAME", actualResult.getName() );
}
 
Example #10
Source File: FormulaArgument.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static String[] convert( final FormulaArgument[] args ) {
  final ArrayList<String> textList = new ArrayList<String>();
  for ( int i = 0; i < args.length; i++ ) {
    FormulaArgument arg = args[ i ];
    try {
      String[] references = FormulaUtil.getReferences( arg.getFormula() );
      if ( references.length > 0 ) {
        textList.add( references[ 0 ] );
      }
    } catch ( ParseException e ) {
      //
    }
  }
  return textList.toArray( new String[ textList.size() ] );
}
 
Example #11
Source File: AbstractKettleTransformationProducer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public String[] getReferencedFields() throws ParseException {
  final LinkedHashSet<String> retval = new LinkedHashSet<>();
  HashSet<String> args = new HashSet<>();
  for ( final FormulaArgument argument : arguments ) {
    args.addAll( Arrays.asList( argument.getReferencedFields() ) );
  }
  for ( final FormulaParameter formulaParameter : parameter ) {
    args.addAll( Arrays.asList( formulaParameter.getReferencedFields() ) );
  }
  retval.addAll( args );
  retval.add( DataFactory.QUERY_LIMIT );
  return retval.toArray( new String[ retval.size() ] );
}
 
Example #12
Source File: AbstractKettleTransformationProducer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private String[] fillArguments( final FormulaContext context ) throws EvaluationException, ParseException {
  final String[] params = new String[ arguments.length ];
  for ( int i = 0; i < arguments.length; i++ ) {
    final FormulaArgument arg = arguments[ i ];
    Object compute = arg.compute( context );
    if ( compute == null ) {
      params[ i ] = null;
    } else {
      params[ i ] = String.valueOf( compute );
    }
  }
  return params;
}
 
Example #13
Source File: AbstractKettleTransformationProducer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Trans prepareTransformation( final DataRow parameters,
                                     final DataFactoryContext context,
                                     final TransMeta transMeta )
  throws EvaluationException, ParseException, KettleException {
  final FormulaContext formulaContext = new WrappingFormulaContext( context.getFormulaContext(), parameters );
  final String[] params = fillArguments( formulaContext );

  final Trans trans = new Trans( transMeta );
  trans.setArguments( params );
  updateTransformationParameter( formulaContext, trans );
  transMeta.setInternalKettleVariables();
  trans.prepareExecution( params );
  return trans;
}
 
Example #14
Source File: AbstractKettleTransformationProducer.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected TableModel performQueryOnTransformation( final DataRow parameters,
                                                 final int queryLimit,
                                                 final DataFactoryContext context,
                                                 final TransMeta transMeta )
  throws EvaluationException, ParseException, KettleException, ReportDataFactoryException {
  TableProducer tableProducer = null;
  Trans trans = null;

  try {
    trans = prepareTransformation( parameters, context, transMeta );

    final RowMetaInterface row = transMeta.getStepFields( getStepName() );
    tableProducer = new TableProducer( row, queryLimit, isStopOnError() );
    StepInterface targetStep = findTargetStep( trans );
    targetStep.addRowListener( tableProducer );

    currentlyRunningTransformation = trans;

    trans.startThreads();
    trans.waitUntilFinished();
  } finally {
    if ( null != trans ) {
      trans.cleanup();
    }
    currentlyRunningTransformation = null;
  }

  if ( trans.getErrors() != 0 && isStopOnError() ) {
    throw new ReportDataFactoryException( String
      .format( "Transformation reported %d records with errors and stop-on-error is true. Aborting.",
        trans.getErrors() ) );
  }

  return tableProducer.getTableModel();
}
 
Example #15
Source File: FormulaParsingTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testParseFailure() throws Exception {
  try {
    final Formula formula = new Formula( "T([year4))" );
    fail();
  } catch ( ParseException pe ) {
  }
}
 
Example #16
Source File: FormulaParsingTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testParse() throws ParseException, EvaluationException {
  final Formula formula = new Formula( "MID(UPPER([name] & \n\r" +
    "   \" \" \n\r" +
    "   & [firstname]);5;10)" );
  final DefaultFormulaContext context = new DefaultFormulaContext();
  context.defineReference( "name", "name" );
  context.defineReference( "firstname", "firstname" );
  formula.initialize( context );
  final Object o = formula.evaluate();
  assertEquals( "Formula value", " FIRSTNAME", o );
}
 
Example #17
Source File: FormulaUtil.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static String[] getReferences( final String formula ) throws ParseException {
  if ( formula == null ) {
    throw new NullPointerException();
  }
  final String formulaExpression = extractFormula( formula );
  if ( formulaExpression == null ) {
    throw new ParseException( "Formula is invalid" );
  }
  return getReferences( new Formula( formulaExpression ) );
}
 
Example #18
Source File: FormulaParsePosition.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void main(String[] args) throws EvaluationException, ParseException
{
  final Formula f = new Formula("IF(NOT(TRUE()));\"Testing\"; \"Other\"");

  // connects the parsed formula to the context. The context provides the
  // operator and function implementations and resolves the references.
  f.initialize(new DefaultFormulaContext());

  final LValue rootReference = f.getRootReference();
  print(rootReference, 0);
}
 
Example #19
Source File: ArrayTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testInvalidInlineArrays2() throws EvaluationException, ParseException {
  final Formula formula = new Formula( "{3;1|2;6;5;6}" );
  formula.initialize( context );
  final Object evaluate = formula.evaluate();
  assertEquals( evaluate, LibFormulaErrorValue.ERROR_ILLEGAL_ARRAY_VALUE );

}
 
Example #20
Source File: FormulaUtilTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * See PRD-5511 for details.
 * @throws ParseException
 */
@Test
public void getReferencesTest() throws ParseException {
  String[] references = FormulaUtil.getReferences( "=CSVTEXT([parmTerritory];FALSE();\",\";\"'\")" );
  assertTrue("References list is not empty", references.length > 0);
  assertEquals("Fromula has one reference to paramTerritory", "parmTerritory", references[0]);
}
 
Example #21
Source File: ArrayTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testEmptyArray() throws EvaluationException, ParseException {
  final Formula formula = new Formula( "{}" );
  formula.initialize( context );
  final Object evaluate = formula.evaluate();
  assertTrue( evaluate instanceof StaticArrayCallback );
  StaticArrayCallback sc = (StaticArrayCallback) evaluate;
  assertEquals( 0, sc.getColumnCount() );
  assertEquals( 0, sc.getRowCount() );

}
 
Example #22
Source File: FormulaArgument.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public String[] getReferencedFields() throws ParseException {
  return FormulaUtil.getReferences( getFormula() );
}
 
Example #23
Source File: FormulaData.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public Formula createFormula( String formulaText ) throws EvaluationException, ParseException {
  Formula result = new Formula( formulaText );
  result.initialize( context );
  return result;
}
 
Example #24
Source File: FormulaErrorInspection.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void inspectExpression( final ReportDesignerContext designerContext,
                                  final ReportDocumentContext reportRenderContext,
                                  final InspectionResultListener resultHandler,
                                  final String[] columnNames,
                                  final Expression expression,
                                  final ExpressionMetaData expressionMetaData ) {
  if ( expressionMetaData == null ) {
    return;
  }


  try {
    final BeanUtility utility = new BeanUtility( expression );
    final ExpressionPropertyMetaData[] datas = expressionMetaData.getPropertyDescriptions();
    for ( int i = 0; i < datas.length; i++ ) {
      final ExpressionPropertyMetaData metaData = datas[ i ];
      if ( metaData.isHidden() ) {
        continue;
      }
      if ( !WorkspaceSettings.getInstance().isVisible( metaData ) ) {
        continue;
      }

      if ( MetaData.VALUEROLE_FORMULA.equals( metaData.getPropertyRole() ) == false )//NON-NLS
      {
        continue;
      }

      final Object o = utility.getProperty( metaData.getName() );
      if ( o instanceof String == false ) {
        continue;
      }

      try {
        compileFormula( (String) o );
      } catch ( ParseException fpe ) {
        resultHandler.notifyInspectionResult( new InspectionResult( this, InspectionResult.Severity.WARNING,
          Messages.getString( "FormulaErrorInspection.ExpressionInvalidFormula", expression.getName(),
            metaData.getDisplayName( Locale.getDefault() ) ),
          new PropertyLocationInfo( expression, metaData.getName() ) ) );
      }
    }
  } catch ( Exception e ) {
    resultHandler.notifyInspectionResult( new InspectionResult( this, InspectionResult.Severity.WARNING,
      e.getMessage(),
      new LocationInfo( expression ) ) );
  }
}
 
Example #25
Source File: FormulaEditorPanel.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void revalidateFormulaSyntax() {
  try {
    final String rawFormula = editorModel.getFormulaText();
    if ( StringUtils.isEmpty( rawFormula ) ) {
      errorTextHolder.setText( "" );
      errorTextHolder.setToolTipText( null );
      errorIconHolder.setIcon( null );
      return;
    }
    final String formulaText = FormulaUtil.extractFormula( rawFormula );
    if ( StringUtils.isEmpty( formulaText ) ) {
      errorTextHolder.setText( Messages.getInstance().getString( "FormulaEditorDialog.ShortErrorNoFormulaContext" ) );
      errorTextHolder
        .setToolTipText( Messages.getInstance().getString( "FormulaEditorDialog.ErrorNoFormulaContext" ) );
      return;
    }
    final Formula formula = new Formula( formulaText );
    formula.initialize( formulaContext );
    final TypeValuePair pair = formula.evaluateTyped();

    if ( pair.getValue() instanceof LibFormulaErrorValue ) {
      errorTextHolder.setText( Messages.getInstance().getString( "FormulaEditorDialog.ShortEvaluationError" ) );
      errorTextHolder.setToolTipText( Messages.getInstance().getString( "FormulaEditorDialog.EvaluationError" ) );
    } else {
      errorTextHolder.setToolTipText( null );
      errorTextHolder.setText( Messages.getInstance()
        .getString( "FormulaEditorDialog.EvaluationResult", String.valueOf( pair.getValue() ) ) );
    }
    errorIconHolder.setIcon( null );
  } catch ( ParseException pe ) {
    errorIconHolder.setIcon( errorIcon );
    if ( pe.currentToken == null ) {
      errorTextHolder.setText( Messages.getInstance().getString( "FormulaEditorDialog.ShortParseError" ) );
      errorTextHolder.setToolTipText(
        Messages.getInstance().getString( "FormulaEditorDialog.GenericParseError", pe.getLocalizedMessage() ) );
    } else {
      final String token = pe.currentToken.toString();
      final int line = pe.currentToken.beginLine;
      final int column = pe.currentToken.beginColumn;
      errorTextHolder.setText( Messages.getInstance().getString( "FormulaEditorDialog.ShortParseError" ) );
      errorTextHolder.setToolTipText( Messages.getInstance().getString( "FormulaEditorDialog.ParseError",
        new Object[] { token, line, column } ) );
    }
  } catch ( Exception e ) {
    errorIconHolder.setIcon( errorIcon );
    errorTextHolder.setText( Messages.getInstance().getString( "FormulaEditorDialog.ShortParseError" ) );
    errorTextHolder.setToolTipText(
      Messages.getInstance().getString( "FormulaEditorDialog.GenericParseError", e.getLocalizedMessage() ) );
  }
}
 
Example #26
Source File: FormulaArgumentTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Test
public void getReferencedFields() throws ParseException {
  FormulaArgument argument = new FormulaArgument( "=[TEST_FIELD]" );
  assertArrayEquals( new String[] { "TEST_FIELD" }, argument.getReferencedFields() );
}
 
Example #27
Source File: FormulaArgument.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Object compute( final FormulaContext formulaContext ) throws EvaluationException, ParseException {
  Formula f = new Formula( FormulaUtil.extractFormula( formula ) );
  f.initialize( formulaContext );
  return f.evaluate();
}
 
Example #28
Source File: FormulaParsingTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testEmptyArray() throws ParseException, EvaluationException {
  final Formula formula = new Formula( "{}" );
  formula.initialize( new DefaultFormulaContext() );
  formula.getRootReference();

}
 
Example #29
Source File: FormulaParsingTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testEmptyArray2() throws ParseException, EvaluationException {
  final Formula formula = new Formula( "COUNT({})" );
  formula.initialize( new DefaultFormulaContext() );
  formula.getRootReference();

}
 
Example #30
Source File: FormulaParsingTest.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testTermOperands() throws ParseException, EvaluationException {
  final Formula formula = new Formula( "\"a\" & \"b\" & \"c\"" );
  formula.initialize( new DefaultFormulaContext() );
  Term term = (Term) formula.getRootReference();
  term.getOperands();
}