private boolean isSupportedType( Object obValue ) { if ( obValue instanceof Scriptable ) { if ( obValue instanceof IdScriptableObject ) { IdScriptableObject jsObject = ( (IdScriptableObject) obValue ); if ( jsObject.getClassName( ).equals( "Date" ) ) { return true; } return false; } else if ( obValue instanceof NativeJavaObject ) { return true; } return false; } return IOUtil.getTypeIndex( obValue ) != -1; }
/** * {@inheritDoc} */ @Override public int getForJavaConversionConfidence(final Class<?> valueInstanceClass, final Class<?> expectedClass) { final int confidence; if (IdScriptableObject.class.isAssignableFrom(valueInstanceClass) && expectedClass.isAssignableFrom(Map.class)) { confidence = LOW_CONFIDENCE; } else { confidence = LOWEST_CONFIDENCE; } return confidence; }
/** * * {@inheritDoc} */ @Override public void afterPropertiesSet() { PropertyCheck.mandatory(this, "registry", this.registry); try { this.consStringClass = Class.forName("org.mozilla.javascript.ConsString"); this.registry.registerValueInstanceConverter(this.consStringClass, this); } catch (final ClassNotFoundException cnfe) { LOGGER.info("Rhino ConsString class is not available - this is normal for an Alfresco release that does not include Rhino 1.7", cnfe); } this.registry.registerValueInstanceConverter(IdScriptableObject.class, this); this.registry.registerValueInstanceConverter(CharSequence.class, this); }
/** * * {@inheritDoc} */ @Override public int getForJavaConversionConfidence(final Class<?> valueInstanceClass, final Class<?> expectedClass) { final int confidence; if ((IdScriptableObject.class.isAssignableFrom(valueInstanceClass) || (this.consStringClass != null && this.consStringClass .isAssignableFrom(valueInstanceClass))) && expectedClass.isAssignableFrom(String.class)) { confidence = HIGHEST_CONFIDENCE; } else { confidence = LOWEST_CONFIDENCE; } return confidence; }
/** * @throws BirtException */ private void preBasicDummy( ) throws BirtException { IQueryResults qr = myPreDataEngine.getQueryResults( queryResultID ); assert ( qr.getResultMetaData( ) != null ); IResultIterator ri = qr.getResultIterator( ); assert ( ri.getResultMetaData( ) != null ); Iterator it = this.expectedValue.iterator( ); while ( ri.next( ) ) { String str = ""; Object ob1 = it.next( ); Object ob2 = ri.getValue( dummyRowExprName ); if ( ob1 instanceof IdScriptableObject ) { ob1 = JavascriptEvalUtil.convertJavascriptValue( ob1 ); ob2 = JavascriptEvalUtil.convertJavascriptValue( ob2 ); } assertEquals( ob1, ob2 ); str += " " + ob2.toString( ); this.testOut.println( "row result set: " + str ); } assertTrue( ri.getResultMetaData( ) != null ); ri.close( ); myPreDataEngine.shutdown( ); }
/** * Test java script object I/O * @throws IOException */ @Test public void testNativeDate( ) throws IOException { final int size = 1000; ByteArrayOutputStream bos = null; DataOutputStream dos = null; byte[] content = null; ByteArrayInputStream bis = null; DataInputStream dis = null; bos = new ByteArrayOutputStream( size ); dos = new DataOutputStream( bos ); Context cx = Context.enter( ); Scriptable sharedScope = new ImporterTopLevel( cx ); Object ob = cx.evaluateString( sharedScope, "new Date", null, -1, null ); IOUtil.writeObject( dos, ob ); content = bos.toByteArray( ); bis = new ByteArrayInputStream( content ); dis = new DataInputStream( bis ); Object ob2 = IOUtil.readObject( dis ); assertTrue( ob instanceof IdScriptableObject ); assertTrue( ob2 instanceof IdScriptableObject ); assertEquals( JavascriptEvalUtil.convertJavascriptValue( ob ), JavascriptEvalUtil.convertJavascriptValue( ob2 ) ); Context.exit( ); }
/** * * {@inheritDoc} */ @Override public void afterPropertiesSet() { PropertyCheck.mandatory(this, "registry", this.registry); this.registry.registerValueInstanceConverter(Date.class, this); this.registry.registerValueInstanceConverter(IdScriptableObject.class, this); }
/** * * {@inheritDoc} */ @Override public int getForJavaConversionConfidence(final Class<?> valueInstanceClass, final Class<?> expectedClass) { final int confidence; if (IdScriptableObject.class.isAssignableFrom(valueInstanceClass) && expectedClass.isAssignableFrom(Date.class)) { confidence = MEDIUM_CONFIDENCE; } else { confidence = LOWEST_CONFIDENCE; } return confidence; }
/** * {@inheritDoc} */ @Override public boolean canConvertValueForJava(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { final boolean canConvert = value instanceof IdScriptableObject && TYPE_DATE.equals(((IdScriptableObject) value).getClassName()) && expectedClass.isAssignableFrom(Date.class); return canConvert; }
/** * * {@inheritDoc} */ @Override public int getForScriptConversionConfidence(final Class<?> valueInstanceClass, final Class<?> expectedClass) { final int confidence; if (Date.class.isAssignableFrom(valueInstanceClass) && expectedClass.isAssignableFrom(IdScriptableObject.class)) { confidence = MEDIUM_CONFIDENCE; } else { confidence = LOWEST_CONFIDENCE; } return confidence; }
/** * {@inheritDoc} */ @Override public boolean canConvertValueForScript(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { final boolean canConvert = value instanceof Date && expectedClass.isAssignableFrom(IdScriptableObject.class); return canConvert; }
/** * * {@inheritDoc} */ @Override public void afterPropertiesSet() { PropertyCheck.mandatory(this, "registry", this.registry); this.registry.registerValueInstanceConverter(IdScriptableObject.class, this); }
/** * {@inheritDoc} */ @Override public boolean canConvertValueForJava(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { boolean canConvert = value instanceof IdScriptableObject && expectedClass.isAssignableFrom(Map.class); if (canConvert) { final IdScriptableObject object = (IdScriptableObject) value; final Object[] propIds = object.getIds(); for (int i = 0; i < propIds.length; i++) { // work on each key in turn final Object propId = propIds[i]; // we are only interested in keys that indicate a list of values if (propId instanceof String) { // get the value out for the specified key final Object val = object.get((String) propId, object); canConvert = canConvert && globalDelegate.canConvertValueForJava(val); } } } return canConvert; }
/** * {@inheritDoc} */ @Override public Object convertValueForJava(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { if (!(value instanceof IdScriptableObject)) { throw new IllegalArgumentException("value must be a IdScriptableObject"); } final IdScriptableObject object = (IdScriptableObject) value; final Object[] propIds = object.getIds(); final Map<String, Object> propValues = new HashMap<String, Object>(propIds.length); for (int i = 0; i < propIds.length; i++) { // work on each key in turn final Object propId = propIds[i]; // we are only interested in keys that indicate a list of values if (propId instanceof String) { // get the value out for the specified key final Object val = object.get((String) propId, object); // recursively call this method to convert the value propValues.put((String) propId, globalDelegate.convertValueForJava(val)); } } return propValues; }
/** * {@inheritDoc} */ @Override public boolean canConvertValueForJava(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { final boolean canConvert = (((value instanceof IdScriptableObject && "String".equals(((IdScriptableObject) value).getClassName())) || (this.consStringClass != null && this.consStringClass .isInstance(value)))) && expectedClass.isAssignableFrom(String.class); return canConvert; }
/** * {@inheritDoc} */ @Override public Object convertValueForJava(final Object value, final ValueConverter globalDelegate, final Class<?> expectedClass) { if (!((value instanceof IdScriptableObject && "String".equals(((IdScriptableObject) value).getClassName())) || (this.consStringClass != null && this.consStringClass .isInstance(value)))) { throw new IllegalArgumentException("value must be a ConsString/NativeString"); } final Object result = Context.jsToJava(value, String.class); return result; }
/** * Handles a Rhino script evaluation result, converting Javascript object * into equivalent Java objects if necessary. * @param inputObj Object returned by rhino engine. * @return If inputObj is a native Javascript object, its equivalent Java object * is returned; otherwise inputObj is returned */ public static Object convertJavascriptValue(Object inputObj) { if ( inputObj instanceof Undefined ) { return null; } if ( inputObj instanceof IdScriptableObject ) { // Return type is possibly a Javascript native object // Convert to Java object with same value String jsClass = ((Scriptable) inputObj).getClassName(); if ( "Date".equals(jsClass) ) { return Context.toType( inputObj, Date.class ); } else if ( "Boolean".equals(jsClass)) { return Boolean.valueOf( Context.toBoolean( inputObj ) ); } else if ( "Number".equals(jsClass)) { return new Double(Context.toNumber(inputObj)); } else if( "String".equals(jsClass) ) { return inputObj.toString(); } else if ( "Array".equals( jsClass ) ) { Object[] obj = new Object[(int) ( (NativeArray) inputObj ).getLength( )]; for ( int i = 0; i < obj.length; i++ ) { obj[i] = convertJavascriptValue( ( (NativeArray) inputObj ).get( i, null ) ); } return obj; } } else if ( inputObj instanceof Wrapper ) { return ( (Wrapper) inputObj ).unwrap( ); } else if ( inputObj instanceof Scriptable ) { return ((Scriptable) inputObj).getDefaultValue( null ); } return inputObj; }
public Object execute(Object[] args) { Context cx = RhinoUtil.enter(); Object ret = null; try { Scriptable scope = RhinoUtil.getScope(); Object jsRet; if (_elStyle && args != null) { Object host = getELStyleHost(cx, scope); jsRet = functionExecute(cx, scope, host, args); } else { jsRet = normalExecute(cx, scope); } ret = RhinoUtil.convertResult(cx, getExpectedClass(), jsRet); } catch (RhinoException e) { if (e instanceof WrappedException) { WrappedException we = (WrappedException) e; Throwable wrapped = we.getWrappedException(); if (wrapped instanceof RenderingBrake) { RhinoUtil.removeWrappedException(we); } } // エラーとなったソース情報が微妙なので微調整。 // 行番号はスクリプト次第でずれてしまう。 int offsetLine; String message; String sourceName; if (e instanceof JavaScriptException && ((JavaScriptException)e).getValue() instanceof IdScriptableObject) { offsetLine = -1; IdScriptableObject scriptable = (IdScriptableObject) ((JavaScriptException) e).getValue(); Object messageProperty = scriptable.get("message", scriptable); if (messageProperty != Scriptable.NOT_FOUND) { message = messageProperty.toString(); } else { message = scriptable.toString(); } } else { offsetLine = e.lineNumber() - _lineNumber + 1; // one "\n" is added message = e.details() + " in script=\n" + getText(); } if (e.lineSource() == null && message != null) { String[] lines = message.split("\n"); offsetLine = (lines.length > offsetLine) ? offsetLine : _offsetLine; if (offsetLine >= 0 && lines.length > offsetLine) { e.initLineSource(lines[offsetLine]); sourceName = _sourceName; } else { sourceName = e.sourceName(); } } else { sourceName = e.sourceName(); } throw new OffsetLineRhinoException( message, sourceName, e.lineNumber(), e.lineSource(), e.columnNumber(), offsetLine, e.getCause()); } finally { Context.exit(); } return ret; }