Java Code Examples for org.mozilla.javascript.Context#getUndefinedValue()

The following examples show how to use org.mozilla.javascript.Context#getUndefinedValue() . 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: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object abs( Context actualContext, Scriptable actualObject, Object[] ArgList,
                          Function FunctionContext ) {

  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[ 0 ] ) ) {
        return new Double( Double.NaN );
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return Context.getUndefinedValue();
      } else {
        return new Double( Math.abs( Context.toNumber( ArgList[ 0 ] ) ) );
      }
    } catch ( Exception e ) {
      return null;
    }
  } else {
    throw Context.reportRuntimeError( "The function call abs requires 1 argument." );
  }
}
 
Example 2
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static Object isRegExp( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {

  if ( ArgList.length >= 2 ) {
    if ( isNull( ArgList, new int[] { 0, 1 } ) ) {
      return null;
    } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {
      return Context.getUndefinedValue();
    }
    String strToMatch = Context.toString( ArgList[0] );
    for ( int i = 1; i < ArgList.length; i++ ) {
      Pattern p = Pattern.compile( Context.toString( ArgList[i] ) );
      Matcher m = p.matcher( strToMatch );
      if ( m.matches() ) {
        return new Double( i );
      }
    }
  }
  return new Double( -1 );
}
 
Example 3
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static Object floor( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[0] ) ) {
        return new Double( Double.NaN );
      } else if ( isUndefined( ArgList[0] ) ) {
        return Context.getUndefinedValue();
      } else {
        return new Double( Math.floor( Context.toNumber( ArgList[0] ) ) );
      }
    } catch ( Exception e ) {
      return null;
      // throw Context.reportRuntimeError(e.toString());
    }
  } else {
    throw Context.reportRuntimeError( "The function call floor requires 1 argument." );
  }
}
 
Example 4
Source File: JsXMLHttpRequest.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void jsFunction_open(String method, String url, Object asyncObj, Object user, Object password) {
    Boolean async;
    if (asyncObj == Context.getUndefinedValue()) {
        async = Boolean.TRUE;
    } else {
        async = (Boolean)Context.jsToJava(asyncObj, Boolean.class);
    }

    if (user == Context.getUndefinedValue()) {
        user = null;
    } else {
        user = Context.jsToJava(user, String.class);
    }
    if (password == Context.getUndefinedValue()) {
        password = null;
    } else {
        password = Context.jsToJava(password, String.class);
    }

    doOpen(method, url, async, (String)user, (String)password);
}
 
Example 5
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object isDate( Context actualContext, Scriptable actualObject, Object[] ArgList,
                             Function FunctionContext ) {

  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[ 0 ] ) ) {
        return null;
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return Context.getUndefinedValue();
      }
      /* java.util.Date d = (java.util.Date) */
      Context.jsToJava( ArgList[ 0 ], Date.class );
      return Boolean.TRUE;
    } catch ( Exception e ) {
      return Boolean.FALSE;
    }
  } else {
    throw Context.reportRuntimeError( "The function call isDate requires 1 argument." );
  }
}
 
Example 6
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static String rtrim( Context actualContext, Scriptable actualObject, Object[] ArgList,
                            Function FunctionContext ) {
  try {
    if ( ArgList.length == 1 ) {
      if ( isNull( ArgList[ 0 ] ) ) {
        return null;
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return (String) Context.getUndefinedValue();
      }
      String strValueToTrim = Context.toString( ArgList[ 0 ] );
      return strValueToTrim.replaceAll( "\\s+$", "" );
    } else {
      throw Context.reportRuntimeError( "The function call rtrim requires 1 argument." );
    }
  } catch ( Exception e ) {
    throw Context.reportRuntimeError( "The function call rtrim is not valid : " + e.getMessage() );
  }
}
 
Example 7
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static Object getNextWorkingDay( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  // (Date dIn){
  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[0] ) ) {
        return null;
      } else if ( isUndefined( ArgList[0] ) ) {
        return Context.getUndefinedValue();
      }
      java.util.Date dIn = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );
      Calendar startDate = Calendar.getInstance();
      startDate.setTime( dIn );
      startDate.add( Calendar.DATE, 1 );
      while ( startDate.get( Calendar.DAY_OF_WEEK ) == Calendar.SATURDAY
        || startDate.get( Calendar.DAY_OF_WEEK ) == Calendar.SUNDAY ) {
        startDate.add( Calendar.DATE, 1 );
      }
      return startDate.getTime();
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( e.toString() );
    }
  } else {
    throw Context.reportRuntimeError( "The function call getNextWorkingDay requires 1 argument." );
  }
}
 
Example 8
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static String rpad( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  try {
    if ( ArgList.length == 3 ) {
      if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {
        return null;
      } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {
        return (String) Context.getUndefinedValue();
      }
      String valueToPad = Context.toString( ArgList[0] );
      String filler = Context.toString( ArgList[1] );
      int size = (int) Context.toNumber( ArgList[2] );

      while ( valueToPad.length() < size ) {
        valueToPad = valueToPad + filler;
      }
      return valueToPad;
    }
  } catch ( Exception e ) {
    throw Context.reportRuntimeError( "The function call rpad requires 3 arguments." );
  }
  return null;
}
 
Example 9
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static Object year( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  try {
    if ( ArgList.length == 1 ) {
      if ( isNull( ArgList[0] ) ) {
        return new Double( Double.NaN );
      } else if ( isUndefined( ArgList[0] ) ) {
        return Context.getUndefinedValue();
      }
      java.util.Date dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );
      Calendar cal = Calendar.getInstance();
      cal.setTime( dArg1 );
      return new Double( cal.get( Calendar.YEAR ) );
    } else {
      throw Context.reportRuntimeError( "The function call year requires 1 argument." );
    }
  } catch ( Exception e ) {
    throw Context.reportRuntimeError( e.toString() );
  }
}
 
Example 10
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static String upper( Context actualContext, Scriptable actualObject, Object[] ArgList,
                            Function FunctionContext ) {
  String sRC = "";
  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[ 0 ] ) ) {
        return null;
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return (String) Context.getUndefinedValue();
      }
      sRC = Context.toString( ArgList[ 0 ] );
      sRC = sRC.toUpperCase();
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( "The function call upper is not valid : " + e.getMessage() );
    }
  } else {
    throw Context.reportRuntimeError( "The function call upper requires 1 argument." );
  }
  return sRC;
}
 
Example 11
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static int getOcuranceString( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  int nr = 0;
  if ( ArgList.length == 2 ) {
    try {
      if ( isNull( ArgList[0] ) ) {
        return 0;
      } else if ( isUndefined( ArgList[0] ) ) {
        return (Integer) Context.getUndefinedValue();
      }
      if ( isNull( ArgList[1] ) ) {
        return 0;
      } else if ( isUndefined( ArgList[1] ) ) {
        return (Integer) Context.getUndefinedValue();
      }
      String string = Context.toString( ArgList[0] );
      String searchFor = Context.toString( ArgList[1] );
      nr = Const.getOcuranceString( string, searchFor );
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( "The function call getOcuranceString is not valid" );
    }
  } else {
    throw Context.reportRuntimeError( "The function call getOcuranceString is not valid" );
  }
  return nr;
}
 
Example 12
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 6 votes vote down vote up
public static Object floor( Context actualContext, Scriptable actualObject, Object[] ArgList,
                            Function FunctionContext ) {
  if ( ArgList.length == 1 ) {
    try {
      if ( isNull( ArgList[ 0 ] ) ) {
        return new Double( Double.NaN );
      } else if ( isUndefined( ArgList[ 0 ] ) ) {
        return Context.getUndefinedValue();
      } else {
        return new Double( Math.floor( Context.toNumber( ArgList[ 0 ] ) ) );
      }
    } catch ( Exception e ) {
      return null;
      // throw Context.reportRuntimeError(e.toString());
    }
  } else {
    throw Context.reportRuntimeError( "The function call floor requires 1 argument." );
  }
}
 
Example 13
Source File: BaseScope.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static Object _loadSvg( final Context cx, final Scriptable thisObj,
                               final Object[] args, final Function funObj ) {

  final String file = args[ 0 ].toString();
  try {
    final BaseScope scope = (BaseScope) thisObj;
    final String parser = "org.apache.xerces.parsers.SAXParser"; //XMLResourceDescriptor.getXMLParserClassName();
    final SAXSVGDocumentFactory f = new SAXSVGDocumentFactory( parser );
    final String uri = "file:" + scope.basePath + "/" + file;
    final SVGOMDocument doc = (SVGOMDocument) f.createDocument( uri );

    // Initialize the CSS Engine for the document
    final SVGDOMImplementation impl = (SVGDOMImplementation) SVGDOMImplementation.getDOMImplementation();
    final UserAgent userAgent = new UserAgentAdapter();
    final BridgeContext ctx = new BridgeContext( userAgent, new DocumentLoader( userAgent ) );
    doc.setCSSEngine( impl.createCSSEngine( doc, ctx ) );

    return Context.javaToJS( doc, scope );
  } catch ( Exception e ) {
    e.printStackTrace();
    logger.error( e );
    return Context.getUndefinedValue();
  }
}
 
Example 14
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public static String lpad( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {

  // (String valueToPad, String filler, int size) {
  try {
    if ( ArgList.length == 3 ) {
      if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {
        return null;
      } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {
        return (String) Context.getUndefinedValue();
      }
      String valueToPad = Context.toString( ArgList[0] );
      String filler = Context.toString( ArgList[1] );
      int size = (int) Context.toNumber( ArgList[2] );

      while ( valueToPad.length() < size ) {
        valueToPad = filler + valueToPad;
      }
      return valueToPad;
    }
  } catch ( Exception e ) {
    throw Context.reportRuntimeError( "The function call lpad requires 3 arguments." );
  }
  return null;
}
 
Example 15
Source File: BaseScope.java    From pentaho-reporting with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Object print( final Context cx, final Scriptable thisObj,
                            final Object[] args, final Function funObj ) {

  for ( final Object arg : args ) {
    final String s = Context.toString( arg );
    logger.info( s );
  }
  return Context.getUndefinedValue();
}
 
Example 16
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static Object str2RegExp( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  String[] strArr = null;
  if ( ArgList.length == 2 ) {
    try {
      if ( isNull( ArgList, new int[] { 0, 1 } ) ) {
        return null;
      } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {
        return Context.getUndefinedValue();
      }
      String strToMatch = Context.toString( ArgList[0] );
      Pattern p = Pattern.compile( Context.toString( ArgList[1] ) );
      Matcher m = p.matcher( strToMatch );
      if ( m.matches() && m.groupCount() > 0 ) {
        strArr = new String[m.groupCount()];
        for ( int i = 1; i <= m.groupCount(); i++ ) {
          strArr[i - 1] = m.group( i );
        }
      }
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( e.toString() );
    }
  } else {
    throw Context.reportRuntimeError( "The function call str2RegExp requires 2 arguments." );
  }
  return strArr;
}
 
Example 17
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings( "fallthrough" )
public static Object truncDate( Context actualContext, Scriptable actualObject, Object[] ArgList, Function FunctionContext ) {
    // 2 arguments: truncation of dates to a certain precision
    //
  if ( ArgList.length == 2 ) {
    if ( isNull( ArgList[0] ) ) {
      return null;
    } else if ( isUndefined( ArgList[0] ) ) {
      return Context.getUndefinedValue();
    }

    // This is the truncation of a date...
    // The second argument specifies the level: ms, s, min, hour, day, month, year
    //
    Date dArg1 = null;
    Integer level = null;
    try {
      dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );
      level = (Integer) Context.jsToJava( ArgList[1], Integer.class );
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( e.toString() );
    }
    return truncDate( dArg1, level );
  } else {
    throw Context
      .reportRuntimeError( "The function call truncDate requires 2 arguments: a date and a level (int)" );
  }
}
 
Example 18
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static Object quarter( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {
  try {
    if ( ArgList.length == 1 ) {
      if ( isNull( ArgList[0] ) ) {
        return new Double( Double.NaN );
      } else if ( isUndefined( ArgList[0] ) ) {
        return Context.getUndefinedValue();
      }
      java.util.Date dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );
      Calendar cal = Calendar.getInstance();
      cal.setTime( dArg1 );

      // Patch by Ingo Klose: calendar months start at 0 in java.
      int iMonth = cal.get( Calendar.MONTH );
      if ( iMonth <= 2 ) {
        return new Double( 1 );
      } else if ( iMonth <= 5 ) {
        return new Double( 2 );
      } else if ( iMonth <= 8 ) {
        return new Double( 3 );
      } else {
        return new Double( 4 );
      }
    } else {
      throw Context.reportRuntimeError( "The function call quarter requires 1 argument." );
    }
  } catch ( Exception e ) {
    throw Context.reportRuntimeError( e.toString() );
  }
}
 
Example 19
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static Object getFiscalDate( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {

  if ( ArgList.length == 2 ) {
    try {
      if ( isNull( ArgList ) ) {
        return null;
      } else if ( isUndefined( ArgList ) ) {
        return Context.getUndefinedValue();
      }
      java.util.Date dIn = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );
      Calendar startDate = Calendar.getInstance();
      Calendar fisStartDate = Calendar.getInstance();
      Calendar fisOffsetDate = Calendar.getInstance();
      startDate.setTime( dIn );
      Format dfFormatter = new SimpleDateFormat( "dd.MM.yyyy" );
      String strOffsetDate = Context.toString( ArgList[1] ) + String.valueOf( startDate.get( Calendar.YEAR ) );
      java.util.Date dOffset = (java.util.Date) dfFormatter.parseObject( strOffsetDate );
      fisOffsetDate.setTime( dOffset );

      String strFisStartDate = "01.01." + String.valueOf( startDate.get( Calendar.YEAR ) + 1 );
      fisStartDate.setTime( (java.util.Date) dfFormatter.parseObject( strFisStartDate ) );
      int iDaysToAdd = (int) ( ( startDate.getTimeInMillis() - fisOffsetDate.getTimeInMillis() ) / 86400000 );
      fisStartDate.add( Calendar.DATE, iDaysToAdd );
      return fisStartDate.getTime();
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( e.toString() );
    }
  } else {
    throw Context.reportRuntimeError( "The function call getFiscalDate requires 2 arguments." );
  }

}
 
Example 20
Source File: ScriptValuesAddedFunctions.java    From hop with Apache License 2.0 4 votes vote down vote up
public static Object dateAdd( Context actualContext, Scriptable actualObject, Object[] ArgList,
                              Function FunctionContext ) {
  if ( ArgList.length == 3 ) {
    try {
      if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {
        return null;
      } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {
        return Context.getUndefinedValue();
      }
      Date dIn = (Date) Context.jsToJava( ArgList[ 0 ], Date.class );
      String strType = Context.toString( ArgList[ 1 ] ).toLowerCase();
      int iValue = (int) Context.toNumber( ArgList[ 2 ] );
      Calendar cal = Calendar.getInstance();
      cal.setTime( dIn );
      if ( strType.equals( "y" ) ) {
        cal.add( Calendar.YEAR, iValue );
      } else if ( strType.equals( "m" ) ) {
        cal.add( Calendar.MONTH, iValue );
      } else if ( strType.equals( "d" ) ) {
        cal.add( Calendar.DATE, iValue );
      } else if ( strType.equals( "w" ) ) {
        cal.add( Calendar.WEEK_OF_YEAR, iValue );
      } else if ( strType.equals( "wd" ) ) {
        int iOffset = 0;
        while ( iOffset < iValue ) {
          cal.add( Calendar.DATE, 1 );
          int day = cal.get( Calendar.DAY_OF_WEEK );
          if ( ( day != Calendar.SATURDAY ) && ( day != Calendar.SUNDAY ) ) {
            iOffset++;
          }
        }
      } else if ( strType.equals( "hh" ) ) {
        cal.add( Calendar.HOUR, iValue );
      } else if ( strType.equals( "mi" ) ) {
        cal.add( Calendar.MINUTE, iValue );
      } else if ( strType.equals( "ss" ) ) {
        cal.add( Calendar.SECOND, iValue );
      }
      return cal.getTime();
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( e.toString() );
    }
  } else {
    throw Context.reportRuntimeError( "The function call dateAdd requires 3 arguments." );
  }
}