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 <tdelprat@nuxeo.com> 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 ScriptObjectMirrors() { 039 // empty 040 } 041 042 public static Object unwrap(ScriptObjectMirror jso) { 043 if (jso.isArray()) { 044 return unwrapList(jso); 045 } else if (JAVASCRIPT_MAP_CLASS_TYPE.equals(jso.getClassName())) { 046 return unwrapMap(jso); 047 } else if (JAVASCRIPT_DATE_CLASS_TYPE.equals(jso.getClassName())) { 048 return unwrapDate(jso); 049 } else { 050 throw new UnsupportedOperationException(jso.getClassName() + " is not supported!"); 051 } 052 } 053 054 /** 055 * @since 8.4 056 */ 057 public static List<Object> unwrapList(ScriptObjectMirror jso) { 058 if (!jso.isArray()) { 059 throw new IllegalArgumentException("JavaScript input is not an Array!"); 060 } 061 List<Object> l = new ArrayList<>(); 062 for (Object o : jso.values()) { 063 if (o instanceof ScriptObjectMirror) { 064 l.add(unwrap((ScriptObjectMirror) o)); 065 } else { 066 l.add(o); 067 } 068 } 069 return l; 070 } 071 072 public static Map<String, Object> unwrapMap(ScriptObjectMirror jso) { 073 if (!JAVASCRIPT_MAP_CLASS_TYPE.equals(jso.getClassName())) { 074 throw new IllegalArgumentException("JavaScript input is not an Object!"); 075 } 076 Map<String, Object> result = new HashMap<>(); 077 for (String k : jso.keySet()) { 078 Object o = jso.get(k); 079 if (o instanceof ScriptObjectMirror) { 080 result.put(k, unwrap((ScriptObjectMirror) o)); 081 } else { 082 result.put(k, o); 083 } 084 } 085 return result; 086 } 087 088 /** 089 * @since 8.4 090 */ 091 public static Calendar unwrapDate(ScriptObjectMirror jso) { 092 if (!JAVASCRIPT_DATE_CLASS_TYPE.equals(jso.getClassName())) { 093 throw new IllegalArgumentException("JavaScript input is not a Date!"); 094 } 095 Calendar cal = Calendar.getInstance(); 096 cal.setTimeInMillis(((Double) jso.callMember("getTime")).longValue()); 097 return cal; 098 } 099 100 public static Object wrap(Map<String, Object> map) { 101 return ScriptObjectMirror.wrap(map, null); 102 } 103 104}