001/* 002 * (C) Copyright 2015-2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thierry Delprat <[email protected]> 018 */ 019package org.nuxeo.automation.scripting.internals; 020 021import java.util.ArrayList; 022import java.util.Calendar; 023import java.util.HashMap; 024import java.util.List; 025import java.util.Map; 026 027import jdk.nashorn.api.scripting.ScriptObjectMirror; 028 029/** 030 * @since 7.2 031 */ 032public class ScriptObjectMirrors { 033 034 private static final String JAVASCRIPT_MAP_CLASS_TYPE = "Object"; 035 036 private static final String JAVASCRIPT_DATE_CLASS_TYPE = "Date"; 037 038 private static final String JAVASCRIPT_GLOBAL_CLASS_TYPE = "global"; 039 040 private static final String JAVASCRIPT_FUNCTION_CLASS_TYPE = "Function"; 041 042 043 private ScriptObjectMirrors() { 044 // empty 045 } 046 047 public static Object unwrap(ScriptObjectMirror jso) { 048 if (jso.isArray()) { 049 return unwrapList(jso); 050 } else if (JAVASCRIPT_MAP_CLASS_TYPE.equals(jso.getClassName())) { 051 return unwrapMap(jso); 052 } else if (JAVASCRIPT_DATE_CLASS_TYPE.equals(jso.getClassName())) { 053 return unwrapDate(jso); 054 } else if (JAVASCRIPT_GLOBAL_CLASS_TYPE.equals(jso.getClassName())) { 055 return null; 056 } else if (JAVASCRIPT_FUNCTION_CLASS_TYPE.equals(jso.getClassName())) { 057 return null; 058 } else { 059 throw new UnsupportedOperationException(jso.getClassName() + " is not supported!"); 060 } 061 } 062 063 /** 064 * @since 8.4 065 */ 066 public static List<Object> unwrapList(ScriptObjectMirror jso) { 067 if (!jso.isArray()) { 068 throw new IllegalArgumentException("JavaScript input is not an Array!"); 069 } 070 List<Object> l = new ArrayList<>(); 071 for (Object o : jso.values()) { 072 if (o instanceof ScriptObjectMirror) { 073 l.add(unwrap((ScriptObjectMirror) o)); 074 } else { 075 l.add(o); 076 } 077 } 078 return l; 079 } 080 081 public static Map<String, Object> unwrapMap(ScriptObjectMirror jso) { 082 if (!JAVASCRIPT_MAP_CLASS_TYPE.equals(jso.getClassName())) { 083 throw new IllegalArgumentException("JavaScript input is not an Object!"); 084 } 085 Map<String, Object> result = new HashMap<>(); 086 for (String k : jso.keySet()) { 087 Object o = jso.get(k); 088 if (o instanceof ScriptObjectMirror) { 089 result.put(k, unwrap((ScriptObjectMirror) o)); 090 } else { 091 result.put(k, DocumentScriptingWrapper.unwrap(o)); 092 } 093 } 094 return result; 095 } 096 097 /** 098 * @since 8.4 099 */ 100 public static Calendar unwrapDate(ScriptObjectMirror jso) { 101 if (!JAVASCRIPT_DATE_CLASS_TYPE.equals(jso.getClassName())) { 102 throw new IllegalArgumentException("JavaScript input is not a Date!"); 103 } 104 Calendar cal = Calendar.getInstance(); 105 cal.setTimeInMillis(((Double) jso.callMember("getTime")).longValue()); 106 return cal; 107 } 108 109 public static Object wrap(Map<String, Object> map) { 110 return ScriptObjectMirror.wrap(map, null); 111 } 112 113}